updated N1QL example code

This commit is contained in:
Seun Matt 2017-09-17 18:44:01 +01:00
commit 18b830d695
18 changed files with 542 additions and 26 deletions

View File

@ -0,0 +1,35 @@
package com.baeldung.stream;
import static org.junit.Assert.fail;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.Stream;
import org.junit.Test;
public class SupplierStreamTest {
@Test(expected = IllegalStateException.class)
public void givenStream_whenStreamUsedTwice_thenThrowException() {
Stream<String> stringStream = Stream.of("A", "B", "C", "D");
Optional<String> result1 = stringStream.findAny();
System.out.println(result1.get());
Optional<String> result2 = stringStream.findFirst();
System.out.println(result2.get());
}
@Test
public void givenStream_whenUsingSupplier_thenNoExceptionIsThrown() {
try {
Supplier<Stream<String>> streamSupplier = () -> Stream.of("A", "B", "C", "D");
Optional<String> result1 = streamSupplier.get().findAny();
System.out.println(result1.get());
Optional<String> result2 = streamSupplier.get().findFirst();
System.out.println(result2.get());
} catch (IllegalStateException e) {
fail();
}
}
}

View File

@ -28,12 +28,6 @@
<artifactId>jackson-databind</artifactId>
<version>${jackson-version}</version>
</dependency>
<dependency>
<groupId>io.vavr</groupId>
<artifactId>vavr</artifactId>
<version>${vavr-version}</version>
</dependency>
<!-- Spring Context for Dependency Injection -->
<dependency>
@ -82,7 +76,6 @@
<couchbase.client.version>2.5.0</couchbase.client.version>
<spring-framework.version>4.3.5.RELEASE</spring-framework.version>
<commons-lang3.version>3.5</commons-lang3.version>
<vavr-version>0.9.0</vavr-version>
<jackson-version>2.9.1</jackson-version>
</properties>

View File

@ -3,8 +3,6 @@ package com.baeldung.couchbase.n1ql;
import com.couchbase.client.java.query.N1qlQueryResult;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.vavr.control.Try;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

View File

@ -1,9 +1,7 @@
package com.baeldung.couchbase.n1ql;
import com.baeldung.couchbase.n1ql.IntegrationTestConfig;
import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.Cluster;
import com.couchbase.client.java.CouchbaseCluster;
import com.couchbase.client.java.document.JsonDocument;
import com.couchbase.client.java.document.json.JsonArray;
import com.couchbase.client.java.document.json.JsonObject;
@ -12,32 +10,24 @@ import com.couchbase.client.java.query.N1qlQueryResult;
import com.couchbase.client.java.query.N1qlQueryRow;
import com.couchbase.client.java.query.Statement;
import com.fasterxml.jackson.databind.JsonNode;
import io.vavr.collection.Stream;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import rx.Observable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import static com.baeldung.couchbase.n1ql.CodeSnippets.extractJsonResult;
import static com.couchbase.client.java.query.Select.select;
import static com.couchbase.client.java.query.dsl.Expression.i;
import static com.couchbase.client.java.query.dsl.Expression.s;
import static com.couchbase.client.java.query.dsl.Expression.x;
import static org.junit.Assert.*;
import static com.couchbase.client.java.query.dsl.Expression.*;
import static org.junit.Assert.assertNotNull;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { IntegrationTestConfig.class })
@ -193,8 +183,8 @@ public class N1QLIntegrationTest {
public void givenDocuments_whenBatchInsert_thenResult() {
Bucket bucket = bucketFactory.getTravelSampleBucket();
List<JsonDocument> documents = Stream.rangeClosed(0,10)
.map( i -> {
List<JsonDocument> documents = IntStream.rangeClosed(0,10)
.mapToObj( i -> {
JsonObject content = JsonObject.create()
.put("id", i)
.put("type", "airline")

6
ethereumj/.gitgnore Normal file
View File

@ -0,0 +1,6 @@
.idea
target
database
logs
target
*.iml

4
ethereumj/README.md Normal file
View File

@ -0,0 +1,4 @@
## EthereumJ
### Relevant Articles:
- [Introduction to EthereumJ](http://www.baeldung.com/intro-to-ethereumj)

110
ethereumj/pom.xml Normal file
View File

@ -0,0 +1,110 @@
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung.ethereumj</groupId>
<artifactId>ethereumj</artifactId>
<packaging>war</packaging>
<version>1.0.0</version>
<name>ethereumj</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<tomcat.version>8.5.4</tomcat.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>
<repositories>
<repository>
<id>Ethereum</id>
<name>Ethereum</name>
<url>https://dl.bintray.com/ethereum/maven/</url>
</repository>
</repositories>
<dependencies>
<!-- Spring Boot Dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<!--<scope>provided</scope>-->
</dependency>
<!-- Unit Testing -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>1.5.6.RELEASE</version>
<scope>test</scope>
</dependency>
<!-- Ethereum -->
<dependency>
<groupId>org.ethereum</groupId>
<artifactId>ethereumj-core</artifactId>
<version>1.5.0-RELEASE</version>
</dependency>
<!-- JSTL/JSP -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<finalName>ethereumj</finalName>
</build>
<profiles>
<profile>
<id>integration</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<excludes>
<exclude>none</exclude>
</excludes>
<includes>
<include>*/EthControllerTestOne.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>

View File

@ -0,0 +1,16 @@
package com.baeldung.ethereumj;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.web.support.SpringBootServletInitializer;
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@SpringBootApplication
public class ApplicationMain extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(ApplicationMain.class, args);
}
}

View File

@ -0,0 +1,9 @@
package com.baeldung.ethereumj;
public class Constants {
public static final String ENDPOINT_ONE = "/api/get/bestblock/";
public static final String ENDPOINT_TWO = "/api/get/difficulty/";
public static final String RESPONSE_TYPE ="application/json/";
}

View File

@ -0,0 +1,25 @@
package com.baeldung.ethereumj.beans;
import com.baeldung.ethereumj.listeners.EthListener;
import org.ethereum.core.Block;
import org.ethereum.facade.Ethereum;
import org.ethereum.facade.EthereumFactory;
import java.math.BigInteger;
public class EthBean {
private Ethereum ethereum;
public void start() {
this.ethereum = EthereumFactory.createEthereum();
this.ethereum.addListener(new EthListener(ethereum));
}
public Block getBestBlock() {
return this.ethereum.getBlockchain().getBestBlock();
}
public BigInteger getTotalDifficulty() {
return this.ethereum.getBlockchain().getTotalDifficulty();
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.ethereumj.config;
import com.baeldung.ethereumj.beans.EthBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.Executors;
@Configuration
public class EthConfig {
@Bean
EthBean ethBeanConfig() throws Exception {
EthBean eBean = new EthBean();
Executors.newSingleThreadExecutor().submit(eBean::start);
return eBean;
}
}

View File

@ -0,0 +1,44 @@
package com.baeldung.ethereumj.controllers;
import com.baeldung.ethereumj.Constants;
import com.baeldung.ethereumj.beans.EthBean;
import com.baeldung.ethereumj.transfer.EthResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.PostConstruct;
import javax.servlet.ServletContext;
@RestController
public class EthController {
@Autowired
EthBean ethBean;
@Autowired
private ServletContext servletContext;
private Logger l = LoggerFactory.getLogger(EthController.class);
@RequestMapping(Constants.ENDPOINT_ONE)
public EthResponse getBestBlock() {
l.debug("Request received - fetching best block.");
EthResponse r = new EthResponse();
r.setResponse(ethBean.getBestBlock().toString());
return r;
}
@RequestMapping(Constants.ENDPOINT_TWO)
public EthResponse getTotalDifficulty() {
l.debug("Request received - calculating total difficulty.");
EthResponse r = new EthResponse();
r.setResponse(ethBean.getTotalDifficulty().toString());
return r;
}
@PostConstruct
public void showIt() {
l.debug(servletContext.getContextPath());
}
}

View File

@ -0,0 +1,71 @@
package com.baeldung.ethereumj.listeners;
import org.ethereum.core.Block;
import org.ethereum.core.TransactionReceipt;
import org.ethereum.facade.Ethereum;
import org.ethereum.listener.EthereumListenerAdapter;
import org.ethereum.util.BIUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.math.BigInteger;
import java.util.List;
public class EthListener extends EthereumListenerAdapter {
private Logger l = LoggerFactory.getLogger(EthListener.class);
private Ethereum ethereum;
private boolean syncDone = false;
private static final int thou = 1000;
private void out(String t) {
l.info(t);
}
private String calcNetHashRate(Block block) {
String response = "Net hash rate not available";
if (block.getNumber() > thou) {
long timeDelta = 0;
for (int i = 0; i < thou; ++i) {
Block parent = ethereum
.getBlockchain()
.getBlockByHash(block.getParentHash());
timeDelta += Math.abs(block.getTimestamp() - parent.getTimestamp());
}
response = String.valueOf(block
.getDifficultyBI()
.divide(BIUtil.toBI(timeDelta / thou))
.divide(new BigInteger("1000000000"))
.doubleValue()) + " GH/s";
}
return response;
}
public EthListener(Ethereum ethereum) {
this.ethereum = ethereum;
}
@Override
public void onBlock(Block block, List<TransactionReceipt> receipts) {
if (syncDone) {
out("Net hash rate: " + calcNetHashRate(block));
out("Block difficulty: " + block.getDifficultyBI().toString());
out("Block transactions: " + block.getTransactionsList().toString());
out("Best block (last block): " + ethereum
.getBlockchain()
.getBestBlock().toString());
out("Total difficulty: " + ethereum
.getBlockchain()
.getTotalDifficulty().toString());
}
}
@Override
public void onSyncDone(SyncState state) {
out("onSyncDone " + state);
if (!syncDone) {
out(" ** SYNC DONE ** ");
syncDone = true;
}
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.ethereumj.transfer;
public class EthResponse {
private String response;
public String getResponse() {
return response;
}
public void setResponse(String response) {
this.response = response;
}
}

View File

@ -0,0 +1,2 @@
server.servlet-path=/
server.port=8080

View File

@ -0,0 +1,72 @@
package com.baeldung.ethereumj.controllers;
import com.baeldung.ethereumj.ApplicationMain;
import com.baeldung.ethereumj.Constants;
import com.baeldung.ethereumj.transfer.EthResponse;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.*;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate;
import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertNotNull;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ApplicationMain.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@TestPropertySource(properties = "server.port=8080")
public class EthControllerTestOne {
@LocalServerPort
int port;
private RestTemplate restTemplate = new RestTemplate();
private String url(String uri) {
String s = "http://localhost:" + port + uri;
System.out.println(s);
return s;
}
@Before
public void setup() {
restTemplate = new RestTemplate();
}
@Test()
public void bestBlockTest() throws Exception {
Thread.sleep(20000);
EthResponse a = restTemplate.getForObject(url(Constants.ENDPOINT_ONE), EthResponse.class);
assertNotNull(a);
ResponseEntity<EthResponse> b = restTemplate.exchange(
url(Constants.ENDPOINT_ONE),
HttpMethod.GET, new HttpEntity<EthResponse>(null, new HttpHeaders()), EthResponse.class);
assertTrue("Status 200?", b.getStatusCode().equals(HttpStatus.OK));
System.out.println("Status 200?: " + b.getStatusCode().equals(HttpStatus.OK));
assertTrue("Dynamic data returned?", b.hasBody());
System.out.println("Dynamic data returned?: " + b.hasBody());
}
@Test()
public void difficultyTest() throws Exception {
Thread.sleep(20000);
ResponseEntity<EthResponse> a = restTemplate.exchange(
url(Constants.ENDPOINT_TWO),
HttpMethod.GET, new HttpEntity<EthResponse>(null, new HttpHeaders()), EthResponse.class);
assertTrue("Status 200?", a.getStatusCode().equals(HttpStatus.OK));
System.out.println("Status 200?: " + a.getStatusCode().equals(HttpStatus.OK));
assertTrue("Dynamic data returned?", a.hasBody());
System.out.println("Dynamic data returned?: " + a.hasBody());
}
}

View File

@ -50,6 +50,8 @@
<module>deltaspike</module>
<module>dozer</module>
<module>ethereumj</module>
<!--<module>ejb</module>-->
<module>feign</module>

View File

@ -0,0 +1,107 @@
package com.baeldung.vavr.collections;
import static io.vavr.API.Array;
import static io.vavr.API.Failure;
import static io.vavr.API.List;
import static io.vavr.API.None;
import static io.vavr.API.Some;
import static io.vavr.API.Stream;
import static io.vavr.API.Success;
import static io.vavr.API.Tuple;
import static io.vavr.API.Vector;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import io.vavr.Tuple3;
import io.vavr.collection.Array;
import io.vavr.collection.List;
import io.vavr.collection.Stream;
import io.vavr.collection.Vector;
import io.vavr.control.Option;
import io.vavr.control.Try;
public class CollectionFactoryMethodsUnitTest {
@Test
public void givenANoneOptionElement_whenCreated_thenCorrect() {
Option<Integer> none = None();
assertFalse(none == null);
assertEquals(none, Option.none());
}
@Test
public void givenASomeOptionElement_whenCreated_thenCorrect() {
Option<Integer> some = Some(1);
assertFalse(some == null);
assertTrue(some.contains(1));
}
@Test
public void givenATupleElement_whenCreated_thenCorrect() {
Tuple3<Character, String, Integer> tuple = Tuple('a', "chain", 2);
assertTrue(tuple!=null);
assertEquals(tuple._1(), new Character('a'));
assertEquals(tuple._2(), "chain");
assertEquals(tuple._3().intValue(), 2);
}
@Test
public void givenASuccessObject_whenEvaluated_thenSuccess() {
Try<Integer> integer = Success(55);
assertEquals(integer.get().intValue(), 55);
}
@Test
public void givenAFailureObject_whenEvaluated_thenExceptionThrown() {
Try<Integer> failure = Failure(new Exception("Exception X encapsulated here"));
try {
Integer i = failure.get();// evaluate a failure raise the exception
System.out.println(i);// not executed
} catch (Exception e) {
assertEquals(e.getMessage(), "Exception X encapsulated here");
}
}
@Test
public void givenAList_whenCreated_thenCorrect() {
List<Integer> list = List(1, 2, 3, 4, 5);
assertEquals(list.size(), 5);
assertEquals(list.get(0).intValue(), 1);
}
@Test
public void givenAnEmptyList_whenCreated_thenCorrect() {
List<Integer> empty = List();
assertEquals(empty.size(), 0);
assertEquals(empty, List.empty());
}
@Test
public void givenAnArray_whenCreated_thenCorrect() {
Array<Integer> array = Array(1, 2, 3, 4, 5);
assertEquals(array.size(), 5);
assertEquals(array.get(0).intValue(), 1);
}
@Test
public void givenAStream_whenCreated_thenCorrect() {
Stream<Integer> stream = Stream(1, 2, 3, 4, 5);
assertEquals(stream.size(), 5);
assertEquals(stream.get(0).intValue(), 1);
}
@Test
public void givenAVector_whenCreated_thenCorrect() {
Vector<Integer> vector = Vector(1, 2, 3, 4, 5);
assertEquals(vector.size(), 5);
assertEquals(vector.get(0).intValue(), 1);
}
}