formatting work
This commit is contained in:
parent
1eb53d544c
commit
44bf48068f
@ -8,7 +8,6 @@ import java.time.Duration;
|
|||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
|
||||||
public class ProcessUtils {
|
public class ProcessUtils {
|
||||||
|
|
||||||
public static String getClassPath() {
|
public static String getClassPath() {
|
||||||
|
@ -8,7 +8,6 @@ public class ServiceMain {
|
|||||||
ProcessHandle thisProcess = ProcessHandle.current();
|
ProcessHandle thisProcess = ProcessHandle.current();
|
||||||
long pid = thisProcess.getPid();
|
long pid = thisProcess.getPid();
|
||||||
|
|
||||||
|
|
||||||
Optional<String[]> opArgs = Optional.ofNullable(args);
|
Optional<String[]> opArgs = Optional.ofNullable(args);
|
||||||
String procName = opArgs.map(str -> str.length > 0 ? str[0] : null).orElse(System.getProperty("sun.java.command"));
|
String procName = opArgs.map(str -> str.length > 0 ? str[0] : null).orElse(System.getProperty("sun.java.command"));
|
||||||
|
|
||||||
|
@ -19,10 +19,7 @@ public class Java9OptionalsStreamTest {
|
|||||||
public void filterOutPresentOptionalsWithFilter() {
|
public void filterOutPresentOptionalsWithFilter() {
|
||||||
assertEquals(4, listOfOptionals.size());
|
assertEquals(4, listOfOptionals.size());
|
||||||
|
|
||||||
List<String> filteredList = listOfOptionals.stream()
|
List<String> filteredList = listOfOptionals.stream().filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList());
|
||||||
.filter(Optional::isPresent)
|
|
||||||
.map(Optional::get)
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
|
|
||||||
assertEquals(2, filteredList.size());
|
assertEquals(2, filteredList.size());
|
||||||
assertEquals("foo", filteredList.get(0));
|
assertEquals("foo", filteredList.get(0));
|
||||||
@ -33,9 +30,7 @@ public class Java9OptionalsStreamTest {
|
|||||||
public void filterOutPresentOptionalsWithFlatMap() {
|
public void filterOutPresentOptionalsWithFlatMap() {
|
||||||
assertEquals(4, listOfOptionals.size());
|
assertEquals(4, listOfOptionals.size());
|
||||||
|
|
||||||
List<String> filteredList = listOfOptionals.stream()
|
List<String> filteredList = listOfOptionals.stream().flatMap(o -> o.isPresent() ? Stream.of(o.get()) : Stream.empty()).collect(Collectors.toList());
|
||||||
.flatMap(o -> o.isPresent() ? Stream.of(o.get()) : Stream.empty())
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
assertEquals(2, filteredList.size());
|
assertEquals(2, filteredList.size());
|
||||||
|
|
||||||
assertEquals("foo", filteredList.get(0));
|
assertEquals("foo", filteredList.get(0));
|
||||||
@ -46,9 +41,7 @@ public class Java9OptionalsStreamTest {
|
|||||||
public void filterOutPresentOptionalsWithFlatMap2() {
|
public void filterOutPresentOptionalsWithFlatMap2() {
|
||||||
assertEquals(4, listOfOptionals.size());
|
assertEquals(4, listOfOptionals.size());
|
||||||
|
|
||||||
List<String> filteredList = listOfOptionals.stream()
|
List<String> filteredList = listOfOptionals.stream().flatMap(o -> o.map(Stream::of).orElseGet(Stream::empty)).collect(Collectors.toList());
|
||||||
.flatMap(o -> o.map(Stream::of).orElseGet(Stream::empty))
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
assertEquals(2, filteredList.size());
|
assertEquals(2, filteredList.size());
|
||||||
|
|
||||||
assertEquals("foo", filteredList.get(0));
|
assertEquals("foo", filteredList.get(0));
|
||||||
@ -59,9 +52,7 @@ public class Java9OptionalsStreamTest {
|
|||||||
public void filterOutPresentOptionalsWithJava9() {
|
public void filterOutPresentOptionalsWithJava9() {
|
||||||
assertEquals(4, listOfOptionals.size());
|
assertEquals(4, listOfOptionals.size());
|
||||||
|
|
||||||
List<String> filteredList = listOfOptionals.stream()
|
List<String> filteredList = listOfOptionals.stream().flatMap(Optional::stream).collect(Collectors.toList());
|
||||||
.flatMap(Optional::stream)
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
|
|
||||||
assertEquals(2, filteredList.size());
|
assertEquals(2, filteredList.size());
|
||||||
assertEquals("foo", filteredList.get(0));
|
assertEquals("foo", filteredList.get(0));
|
||||||
|
@ -13,7 +13,6 @@ import org.junit.Test;
|
|||||||
|
|
||||||
public class MultiResultionImageTest {
|
public class MultiResultionImageTest {
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void baseMultiResImageTest() {
|
public void baseMultiResImageTest() {
|
||||||
int baseIndex = 1;
|
int baseIndex = 1;
|
||||||
@ -38,10 +37,8 @@ public class MultiResultionImageTest {
|
|||||||
return 8 * (i + 1);
|
return 8 * (i + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static BufferedImage createImage(int i) {
|
private static BufferedImage createImage(int i) {
|
||||||
return new BufferedImage(getSize(i), getSize(i),
|
return new BufferedImage(getSize(i), getSize(i), BufferedImage.TYPE_INT_RGB);
|
||||||
BufferedImage.TYPE_INT_RGB);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
package com.baeldung.java9.httpclient;
|
package com.baeldung.java9.httpclient;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
import static java.net.HttpURLConnection.HTTP_OK;
|
import static java.net.HttpURLConnection.HTTP_OK;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
@ -117,7 +115,9 @@ public class SimpleHttpRequestsTest {
|
|||||||
sb.append(k).append(":");
|
sb.append(k).append(":");
|
||||||
List<String> l = hMap.get(k);
|
List<String> l = hMap.get(k);
|
||||||
if (l != null) {
|
if (l != null) {
|
||||||
l.forEach( s -> { sb.append(s).append(","); } );
|
l.forEach(s -> {
|
||||||
|
sb.append(s).append(",");
|
||||||
|
});
|
||||||
}
|
}
|
||||||
sb.append("\n");
|
sb.append("\n");
|
||||||
}
|
}
|
||||||
|
@ -57,7 +57,6 @@ public class TryWithResourcesTest {
|
|||||||
assertEquals("Expected and Actual does not match", 5, closeCount);
|
assertEquals("Expected and Actual does not match", 5, closeCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static class CloseableException extends Exception implements AutoCloseable {
|
static class CloseableException extends Exception implements AutoCloseable {
|
||||||
@Override
|
@Override
|
||||||
public void close() {
|
public void close() {
|
||||||
@ -66,5 +65,3 @@ public class TryWithResourcesTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -13,15 +13,11 @@ public class CollectorImprovementTest {
|
|||||||
public void givenList_whenSatifyPredicate_thenMapValueWithOccurences() {
|
public void givenList_whenSatifyPredicate_thenMapValueWithOccurences() {
|
||||||
List<Integer> numbers = List.of(1, 2, 3, 5, 5);
|
List<Integer> numbers = List.of(1, 2, 3, 5, 5);
|
||||||
|
|
||||||
Map<Integer, Long> result = numbers.stream()
|
Map<Integer, Long> result = numbers.stream().filter(val -> val > 3).collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
|
||||||
.filter(val -> val > 3).collect(Collectors.groupingBy(
|
|
||||||
Function.identity(), Collectors.counting()));
|
|
||||||
|
|
||||||
assertEquals(1, result.size());
|
assertEquals(1, result.size());
|
||||||
|
|
||||||
result = numbers.stream().collect(Collectors.groupingBy(
|
result = numbers.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.filtering(val -> val > 3, Collectors.counting())));
|
||||||
Function.identity(), Collectors.filtering(val -> val > 3,
|
|
||||||
Collectors.counting())));
|
|
||||||
|
|
||||||
assertEquals(4, result.size());
|
assertEquals(4, result.size());
|
||||||
}
|
}
|
||||||
@ -32,20 +28,13 @@ public class CollectorImprovementTest {
|
|||||||
Blog blog2 = new Blog("2", "Disappointing", "Ok", "Could be better");
|
Blog blog2 = new Blog("2", "Disappointing", "Ok", "Could be better");
|
||||||
List<Blog> blogs = List.of(blog1, blog2);
|
List<Blog> blogs = List.of(blog1, blog2);
|
||||||
|
|
||||||
Map<String, List<List<String>>> authorComments1 =
|
Map<String, List<List<String>>> authorComments1 = blogs.stream().collect(Collectors.groupingBy(Blog::getAuthorName, Collectors.mapping(Blog::getComments, Collectors.toList())));
|
||||||
blogs.stream().collect(Collectors.groupingBy(
|
|
||||||
Blog::getAuthorName, Collectors.mapping(
|
|
||||||
Blog::getComments, Collectors.toList())));
|
|
||||||
|
|
||||||
assertEquals(2, authorComments1.size());
|
assertEquals(2, authorComments1.size());
|
||||||
assertEquals(2, authorComments1.get("1").get(0).size());
|
assertEquals(2, authorComments1.get("1").get(0).size());
|
||||||
assertEquals(3, authorComments1.get("2").get(0).size());
|
assertEquals(3, authorComments1.get("2").get(0).size());
|
||||||
|
|
||||||
Map<String, List<String>> authorComments2 =
|
Map<String, List<String>> authorComments2 = blogs.stream().collect(Collectors.groupingBy(Blog::getAuthorName, Collectors.flatMapping(blog -> blog.getComments().stream(), Collectors.toList())));
|
||||||
blogs.stream().collect(Collectors.groupingBy(
|
|
||||||
Blog::getAuthorName, Collectors.flatMapping(
|
|
||||||
blog -> blog.getComments().stream(),
|
|
||||||
Collectors.toList())));
|
|
||||||
|
|
||||||
assertEquals(2, authorComments2.size());
|
assertEquals(2, authorComments2.size());
|
||||||
assertEquals(2, authorComments2.get("1").size());
|
assertEquals(2, authorComments2.get("1").size());
|
||||||
|
@ -17,16 +17,11 @@ public class StreamFeaturesTest {
|
|||||||
public static class TakeAndDropWhileTest {
|
public static class TakeAndDropWhileTest {
|
||||||
|
|
||||||
public Stream<String> getStreamAfterTakeWhileOperation() {
|
public Stream<String> getStreamAfterTakeWhileOperation() {
|
||||||
return Stream
|
return Stream.iterate("", s -> s + "s").takeWhile(s -> s.length() < 10);
|
||||||
.iterate("", s -> s + "s")
|
|
||||||
.takeWhile(s -> s.length() < 10);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Stream<String> getStreamAfterDropWhileOperation() {
|
public Stream<String> getStreamAfterDropWhileOperation() {
|
||||||
return Stream
|
return Stream.iterate("", s -> s + "s").takeWhile(s -> s.length() < 10).dropWhile(s -> !s.contains("sssss"));
|
||||||
.iterate("", s -> s + "s")
|
|
||||||
.takeWhile(s -> s.length() < 10)
|
|
||||||
.dropWhile(s -> !s.contains("sssss"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -75,19 +70,19 @@ public class StreamFeaturesTest {
|
|||||||
public static class OfNullableTest {
|
public static class OfNullableTest {
|
||||||
|
|
||||||
private List<String> collection = Arrays.asList("A", "B", "C");
|
private List<String> collection = Arrays.asList("A", "B", "C");
|
||||||
private Map<String, Integer> map = new HashMap<>() {{
|
private Map<String, Integer> map = new HashMap<>() {
|
||||||
|
{
|
||||||
put("A", 10);
|
put("A", 10);
|
||||||
put("C", 30);
|
put("C", 30);
|
||||||
}};
|
}
|
||||||
|
};
|
||||||
|
|
||||||
private Stream<Integer> getStreamWithOfNullable() {
|
private Stream<Integer> getStreamWithOfNullable() {
|
||||||
return collection.stream()
|
return collection.stream().flatMap(s -> Stream.ofNullable(map.get(s)));
|
||||||
.flatMap(s -> Stream.ofNullable(map.get(s)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Stream<Integer> getStream() {
|
private Stream<Integer> getStream() {
|
||||||
return collection.stream()
|
return collection.stream().flatMap(s -> {
|
||||||
.flatMap(s -> {
|
|
||||||
Integer temp = map.get(s);
|
Integer temp = map.get(s);
|
||||||
return temp != null ? Stream.of(temp) : Stream.empty();
|
return temp != null ? Stream.of(temp) : Stream.empty();
|
||||||
});
|
});
|
||||||
@ -107,10 +102,7 @@ public class StreamFeaturesTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testOfNullable() {
|
public void testOfNullable() {
|
||||||
|
|
||||||
assertEquals(
|
assertEquals(testOfNullableFrom(getStream()), testOfNullableFrom(getStreamWithOfNullable()));
|
||||||
testOfNullableFrom(getStream()),
|
|
||||||
testOfNullableFrom(getStreamWithOfNullable())
|
|
||||||
);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,7 +22,6 @@ import static org.junit.Assert.assertTrue;
|
|||||||
|
|
||||||
public class ProcessApi {
|
public class ProcessApi {
|
||||||
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void init() {
|
public void init() {
|
||||||
|
|
||||||
|
@ -9,7 +9,8 @@ public class Person implements CouchbaseEntity {
|
|||||||
private String name;
|
private String name;
|
||||||
private String homeTown;
|
private String homeTown;
|
||||||
|
|
||||||
Person() {}
|
Person() {
|
||||||
|
}
|
||||||
|
|
||||||
public Person(Builder b) {
|
public Person(Builder b) {
|
||||||
this.id = b.id;
|
this.id = b.id;
|
||||||
|
@ -13,9 +13,7 @@ import com.baeldung.couchbase.async.service.BucketService;
|
|||||||
public class PersonCrudService extends AbstractCrudService<Person> {
|
public class PersonCrudService extends AbstractCrudService<Person> {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public PersonCrudService(
|
public PersonCrudService(@Qualifier("TutorialBucketService") BucketService bucketService, PersonDocumentConverter converter) {
|
||||||
@Qualifier("TutorialBucketService") BucketService bucketService,
|
|
||||||
PersonDocumentConverter converter) {
|
|
||||||
super(bucketService, converter);
|
super(bucketService, converter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,10 +11,7 @@ public class PersonDocumentConverter implements JsonDocumentConverter<Person> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public JsonDocument toDocument(Person p) {
|
public JsonDocument toDocument(Person p) {
|
||||||
JsonObject content = JsonObject.empty()
|
JsonObject content = JsonObject.empty().put("type", "Person").put("name", p.getName()).put("homeTown", p.getHomeTown());
|
||||||
.put("type", "Person")
|
|
||||||
.put("name", p.getName())
|
|
||||||
.put("homeTown", p.getHomeTown());
|
|
||||||
return JsonDocument.create(p.getId(), content);
|
return JsonDocument.create(p.getId(), content);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,8 +21,7 @@ public class RegistrationService {
|
|||||||
public Person findRegistrant(String id) {
|
public Person findRegistrant(String id) {
|
||||||
try {
|
try {
|
||||||
return crud.read(id);
|
return crud.read(id);
|
||||||
}
|
} catch (CouchbaseException e) {
|
||||||
catch(CouchbaseException e) {
|
|
||||||
return crud.readFromReplica(id);
|
return crud.readFromReplica(id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -73,9 +73,7 @@ public abstract class AbstractCrudService<T extends CouchbaseEntity> implements
|
|||||||
@Override
|
@Override
|
||||||
public List<T> readBulk(Iterable<String> ids) {
|
public List<T> readBulk(Iterable<String> ids) {
|
||||||
final AsyncBucket asyncBucket = bucket.async();
|
final AsyncBucket asyncBucket = bucket.async();
|
||||||
Observable<JsonDocument> asyncOperation = Observable
|
Observable<JsonDocument> asyncOperation = Observable.from(ids).flatMap(new Func1<String, Observable<JsonDocument>>() {
|
||||||
.from(ids)
|
|
||||||
.flatMap(new Func1<String, Observable<JsonDocument>>() {
|
|
||||||
public Observable<JsonDocument> call(String key) {
|
public Observable<JsonDocument> call(String key) {
|
||||||
return asyncBucket.get(key);
|
return asyncBucket.get(key);
|
||||||
}
|
}
|
||||||
@ -83,8 +81,7 @@ public abstract class AbstractCrudService<T extends CouchbaseEntity> implements
|
|||||||
|
|
||||||
final List<T> items = new ArrayList<T>();
|
final List<T> items = new ArrayList<T>();
|
||||||
try {
|
try {
|
||||||
asyncOperation.toBlocking()
|
asyncOperation.toBlocking().forEach(new Action1<JsonDocument>() {
|
||||||
.forEach(new Action1<JsonDocument>() {
|
|
||||||
public void call(JsonDocument doc) {
|
public void call(JsonDocument doc) {
|
||||||
T item = converter.fromDocument(doc);
|
T item = converter.fromDocument(doc);
|
||||||
items.add(item);
|
items.add(item);
|
||||||
@ -100,9 +97,7 @@ public abstract class AbstractCrudService<T extends CouchbaseEntity> implements
|
|||||||
@Override
|
@Override
|
||||||
public void createBulk(Iterable<T> items) {
|
public void createBulk(Iterable<T> items) {
|
||||||
final AsyncBucket asyncBucket = bucket.async();
|
final AsyncBucket asyncBucket = bucket.async();
|
||||||
Observable
|
Observable.from(items).flatMap(new Func1<T, Observable<JsonDocument>>() {
|
||||||
.from(items)
|
|
||||||
.flatMap(new Func1<T, Observable<JsonDocument>>() {
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@Override
|
@Override
|
||||||
public Observable<JsonDocument> call(final T t) {
|
public Observable<JsonDocument> call(final T t) {
|
||||||
@ -110,62 +105,34 @@ public abstract class AbstractCrudService<T extends CouchbaseEntity> implements
|
|||||||
t.setId(UUID.randomUUID().toString());
|
t.setId(UUID.randomUUID().toString());
|
||||||
}
|
}
|
||||||
JsonDocument doc = converter.toDocument(t);
|
JsonDocument doc = converter.toDocument(t);
|
||||||
return asyncBucket.insert(doc)
|
return asyncBucket.insert(doc).retryWhen(RetryBuilder.anyOf(BackpressureException.class).delay(Delay.exponential(TimeUnit.MILLISECONDS, 100)).max(10).build());
|
||||||
.retryWhen(RetryBuilder
|
|
||||||
.anyOf(BackpressureException.class)
|
|
||||||
.delay(Delay.exponential(TimeUnit.MILLISECONDS, 100))
|
|
||||||
.max(10)
|
|
||||||
.build());
|
|
||||||
}
|
}
|
||||||
})
|
}).last().toBlocking().single();
|
||||||
.last()
|
|
||||||
.toBlocking()
|
|
||||||
.single();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void updateBulk(Iterable<T> items) {
|
public void updateBulk(Iterable<T> items) {
|
||||||
final AsyncBucket asyncBucket = bucket.async();
|
final AsyncBucket asyncBucket = bucket.async();
|
||||||
Observable
|
Observable.from(items).flatMap(new Func1<T, Observable<JsonDocument>>() {
|
||||||
.from(items)
|
|
||||||
.flatMap(new Func1<T, Observable<JsonDocument>>() {
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@Override
|
@Override
|
||||||
public Observable<JsonDocument> call(final T t) {
|
public Observable<JsonDocument> call(final T t) {
|
||||||
JsonDocument doc = converter.toDocument(t);
|
JsonDocument doc = converter.toDocument(t);
|
||||||
return asyncBucket.upsert(doc)
|
return asyncBucket.upsert(doc).retryWhen(RetryBuilder.anyOf(BackpressureException.class).delay(Delay.exponential(TimeUnit.MILLISECONDS, 100)).max(10).build());
|
||||||
.retryWhen(RetryBuilder
|
|
||||||
.anyOf(BackpressureException.class)
|
|
||||||
.delay(Delay.exponential(TimeUnit.MILLISECONDS, 100))
|
|
||||||
.max(10)
|
|
||||||
.build());
|
|
||||||
}
|
}
|
||||||
})
|
}).last().toBlocking().single();
|
||||||
.last()
|
|
||||||
.toBlocking()
|
|
||||||
.single();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void deleteBulk(Iterable<String> ids) {
|
public void deleteBulk(Iterable<String> ids) {
|
||||||
final AsyncBucket asyncBucket = bucket.async();
|
final AsyncBucket asyncBucket = bucket.async();
|
||||||
Observable
|
Observable.from(ids).flatMap(new Func1<String, Observable<JsonDocument>>() {
|
||||||
.from(ids)
|
|
||||||
.flatMap(new Func1<String, Observable<JsonDocument>>() {
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@Override
|
@Override
|
||||||
public Observable<JsonDocument> call(String key) {
|
public Observable<JsonDocument> call(String key) {
|
||||||
return asyncBucket.remove(key)
|
return asyncBucket.remove(key).retryWhen(RetryBuilder.anyOf(BackpressureException.class).delay(Delay.exponential(TimeUnit.MILLISECONDS, 100)).max(10).build());
|
||||||
.retryWhen(RetryBuilder
|
|
||||||
.anyOf(BackpressureException.class)
|
|
||||||
.delay(Delay.exponential(TimeUnit.MILLISECONDS, 100))
|
|
||||||
.max(10)
|
|
||||||
.build());
|
|
||||||
}
|
}
|
||||||
})
|
}).last().toBlocking().single();
|
||||||
.last()
|
|
||||||
.toBlocking()
|
|
||||||
.single();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -20,10 +20,7 @@ public class CodeSnippets {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static Cluster loadClusterWithCustomEnvironment() {
|
static Cluster loadClusterWithCustomEnvironment() {
|
||||||
CouchbaseEnvironment env = DefaultCouchbaseEnvironment.builder()
|
CouchbaseEnvironment env = DefaultCouchbaseEnvironment.builder().connectTimeout(10000).kvTimeout(3000).build();
|
||||||
.connectTimeout(10000)
|
|
||||||
.kvTimeout(3000)
|
|
||||||
.build();
|
|
||||||
return CouchbaseCluster.create(env, "localhost");
|
return CouchbaseCluster.create(env, "localhost");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -36,12 +33,7 @@ public class CodeSnippets {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static JsonDocument insertExample(Bucket bucket) {
|
static JsonDocument insertExample(Bucket bucket) {
|
||||||
JsonObject content = JsonObject.empty()
|
JsonObject content = JsonObject.empty().put("name", "John Doe").put("type", "Person").put("email", "john.doe@mydomain.com").put("homeTown", "Chicago");
|
||||||
.put("name", "John Doe")
|
|
||||||
.put("type", "Person")
|
|
||||||
.put("email", "john.doe@mydomain.com")
|
|
||||||
.put("homeTown", "Chicago")
|
|
||||||
;
|
|
||||||
String id = UUID.randomUUID().toString();
|
String id = UUID.randomUUID().toString();
|
||||||
JsonDocument document = JsonDocument.create(id, content);
|
JsonDocument document = JsonDocument.create(id, content);
|
||||||
JsonDocument inserted = bucket.insert(document);
|
JsonDocument inserted = bucket.insert(document);
|
||||||
@ -72,8 +64,7 @@ public class CodeSnippets {
|
|||||||
static JsonDocument getFirstFromReplicaExample(Bucket bucket, String id) {
|
static JsonDocument getFirstFromReplicaExample(Bucket bucket, String id) {
|
||||||
try {
|
try {
|
||||||
return bucket.get(id);
|
return bucket.get(id);
|
||||||
}
|
} catch (CouchbaseException e) {
|
||||||
catch(CouchbaseException e) {
|
|
||||||
List<JsonDocument> list = bucket.getFromReplica(id, ReplicaMode.FIRST);
|
List<JsonDocument> list = bucket.getFromReplica(id, ReplicaMode.FIRST);
|
||||||
if (!list.isEmpty()) {
|
if (!list.isEmpty()) {
|
||||||
return list.get(0);
|
return list.get(0);
|
||||||
|
@ -7,7 +7,8 @@ public class Person {
|
|||||||
private String name;
|
private String name;
|
||||||
private String homeTown;
|
private String homeTown;
|
||||||
|
|
||||||
Person() {}
|
Person() {
|
||||||
|
}
|
||||||
|
|
||||||
public Person(Builder b) {
|
public Person(Builder b) {
|
||||||
this.id = b.id;
|
this.id = b.id;
|
||||||
|
@ -11,10 +11,7 @@ public class PersonDocumentConverter implements JsonDocumentConverter<Person> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public JsonDocument toDocument(Person p) {
|
public JsonDocument toDocument(Person p) {
|
||||||
JsonObject content = JsonObject.empty()
|
JsonObject content = JsonObject.empty().put("type", "Person").put("name", p.getName()).put("homeTown", p.getHomeTown());
|
||||||
.put("type", "Person")
|
|
||||||
.put("name", p.getName())
|
|
||||||
.put("homeTown", p.getHomeTown());
|
|
||||||
return JsonDocument.create(p.getId(), content);
|
return JsonDocument.create(p.getId(), content);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,8 +21,7 @@ public class RegistrationService {
|
|||||||
public Person findRegistrant(String id) {
|
public Person findRegistrant(String id) {
|
||||||
try {
|
try {
|
||||||
return crud.read(id);
|
return crud.read(id);
|
||||||
}
|
} catch (CouchbaseException e) {
|
||||||
catch(CouchbaseException e) {
|
|
||||||
return crud.readFromReplica(id);
|
return crud.readFromReplica(id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -59,9 +59,7 @@ public class ClusterServiceImpl implements ClusterService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<JsonDocument> getDocumentsAsync(final AsyncBucket asyncBucket, Iterable<String> keys) {
|
public List<JsonDocument> getDocumentsAsync(final AsyncBucket asyncBucket, Iterable<String> keys) {
|
||||||
Observable<JsonDocument> asyncBulkGet = Observable
|
Observable<JsonDocument> asyncBulkGet = Observable.from(keys).flatMap(new Func1<String, Observable<JsonDocument>>() {
|
||||||
.from(keys)
|
|
||||||
.flatMap(new Func1<String, Observable<JsonDocument>>() {
|
|
||||||
public Observable<JsonDocument> call(String key) {
|
public Observable<JsonDocument> call(String key) {
|
||||||
return asyncBucket.get(key);
|
return asyncBucket.get(key);
|
||||||
}
|
}
|
||||||
@ -69,8 +67,7 @@ public class ClusterServiceImpl implements ClusterService {
|
|||||||
|
|
||||||
final List<JsonDocument> docs = new ArrayList<>();
|
final List<JsonDocument> docs = new ArrayList<>();
|
||||||
try {
|
try {
|
||||||
asyncBulkGet.toBlocking()
|
asyncBulkGet.toBlocking().forEach(new Action1<JsonDocument>() {
|
||||||
.forEach(new Action1<JsonDocument>() {
|
|
||||||
public void call(JsonDocument doc) {
|
public void call(JsonDocument doc) {
|
||||||
docs.add(doc);
|
docs.add(doc);
|
||||||
}
|
}
|
||||||
|
@ -207,17 +207,10 @@ public class PersonCrudServiceIntegrationTest extends AsyncIntegrationTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Person randomPerson() {
|
private Person randomPerson() {
|
||||||
return Person.Builder.newInstance()
|
return Person.Builder.newInstance().name(RandomStringUtils.randomAlphabetic(10)).homeTown(RandomStringUtils.randomAlphabetic(10)).build();
|
||||||
.name(RandomStringUtils.randomAlphabetic(10))
|
|
||||||
.homeTown(RandomStringUtils.randomAlphabetic(10))
|
|
||||||
.build();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Person randomPersonWithId() {
|
private Person randomPersonWithId() {
|
||||||
return Person.Builder.newInstance()
|
return Person.Builder.newInstance().id(UUID.randomUUID().toString()).name(RandomStringUtils.randomAlphabetic(10)).homeTown(RandomStringUtils.randomAlphabetic(10)).build();
|
||||||
.id(UUID.randomUUID().toString())
|
|
||||||
.name(RandomStringUtils.randomAlphabetic(10))
|
|
||||||
.homeTown(RandomStringUtils.randomAlphabetic(10))
|
|
||||||
.build();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -66,17 +66,10 @@ public class PersonCrudServiceIntegrationTest extends IntegrationTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Person buildClarkKent() {
|
private Person buildClarkKent() {
|
||||||
return Person.Builder.newInstance()
|
return Person.Builder.newInstance().id(CLARK_KENT_ID).name(CLARK_KENT).homeTown(SMALLVILLE).build();
|
||||||
.id(CLARK_KENT_ID)
|
|
||||||
.name(CLARK_KENT)
|
|
||||||
.homeTown(SMALLVILLE)
|
|
||||||
.build();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Person randomPerson() {
|
private Person randomPerson() {
|
||||||
return Person.Builder.newInstance()
|
return Person.Builder.newInstance().name(RandomStringUtils.randomAlphabetic(10)).homeTown(RandomStringUtils.randomAlphabetic(10)).build();
|
||||||
.name(RandomStringUtils.randomAlphabetic(10))
|
|
||||||
.homeTown(RandomStringUtils.randomAlphabetic(10))
|
|
||||||
.build();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,9 +35,6 @@ public abstract class MemberRepository extends AbstractEntityRepository<Member,
|
|||||||
|
|
||||||
public List<Member> findAllOrderedByNameWithQueryDSL() {
|
public List<Member> findAllOrderedByNameWithQueryDSL() {
|
||||||
final QMember member = QMember.member;
|
final QMember member = QMember.member;
|
||||||
return jpaQuery()
|
return jpaQuery().from(member).orderBy(member.email.asc()).list(member);
|
||||||
.from(member)
|
|
||||||
.orderBy(member.email.asc())
|
|
||||||
.list(member);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -64,7 +64,6 @@ public class MemberRegistration {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void register(Member member) throws Exception {
|
public void register(Member member) throws Exception {
|
||||||
log.info("Registering " + member.getName());
|
log.info("Registering " + member.getName());
|
||||||
validateMember(member);
|
validateMember(member);
|
||||||
|
@ -41,27 +41,13 @@ import org.junit.runner.RunWith;
|
|||||||
public class MemberRegistrationTest {
|
public class MemberRegistrationTest {
|
||||||
@Deployment
|
@Deployment
|
||||||
public static Archive<?> createTestArchive() {
|
public static Archive<?> createTestArchive() {
|
||||||
File[] files = Maven.resolver().loadPomFromFile("pom.xml")
|
File[] files = Maven.resolver().loadPomFromFile("pom.xml").importRuntimeDependencies().resolve().withTransitivity().asFile();
|
||||||
.importRuntimeDependencies().resolve().withTransitivity().asFile();
|
|
||||||
|
|
||||||
return ShrinkWrap.create(WebArchive.class, "test.war")
|
return ShrinkWrap.create(WebArchive.class, "test.war")
|
||||||
.addClasses(
|
.addClasses(EntityManagerProducer.class, Member.class, MemberRegistration.class, MemberRepository.class, Resources.class, QueryDslRepositoryExtension.class, QueryDslSupport.class, SecondaryPersistenceUnit.class,
|
||||||
EntityManagerProducer.class,
|
SecondaryEntityManagerProducer.class, SecondaryEntityManagerResolver.class)
|
||||||
Member.class,
|
.addAsResource("META-INF/test-persistence.xml", "META-INF/persistence.xml").addAsResource("META-INF/apache-deltaspike.properties").addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml").addAsWebInfResource("test-ds.xml")
|
||||||
MemberRegistration.class,
|
.addAsWebInfResource("test-secondary-ds.xml").addAsLibraries(files);
|
||||||
MemberRepository.class,
|
|
||||||
Resources.class,
|
|
||||||
QueryDslRepositoryExtension.class,
|
|
||||||
QueryDslSupport.class,
|
|
||||||
SecondaryPersistenceUnit.class,
|
|
||||||
SecondaryEntityManagerProducer.class,
|
|
||||||
SecondaryEntityManagerResolver.class)
|
|
||||||
.addAsResource("META-INF/test-persistence.xml", "META-INF/persistence.xml")
|
|
||||||
.addAsResource("META-INF/apache-deltaspike.properties")
|
|
||||||
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
|
|
||||||
.addAsWebInfResource("test-ds.xml")
|
|
||||||
.addAsWebInfResource("test-secondary-ds.xml")
|
|
||||||
.addAsLibraries(files);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
|
@ -22,19 +22,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void configure(HttpSecurity http) throws Exception {
|
protected void configure(HttpSecurity http) throws Exception {
|
||||||
http.sessionManagement()
|
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.ALWAYS).and().requestMatchers().antMatchers("/eureka/**").and().authorizeRequests().antMatchers("/eureka/**").hasRole("SYSTEM").anyRequest().denyAll().and().httpBasic().and().csrf()
|
||||||
.sessionCreationPolicy(SessionCreationPolicy.ALWAYS)
|
|
||||||
.and()
|
|
||||||
.requestMatchers()
|
|
||||||
.antMatchers("/eureka/**")
|
|
||||||
.and()
|
|
||||||
.authorizeRequests()
|
|
||||||
.antMatchers("/eureka/**").hasRole("SYSTEM")
|
|
||||||
.anyRequest().denyAll()
|
|
||||||
.and()
|
|
||||||
.httpBasic()
|
|
||||||
.and()
|
|
||||||
.csrf()
|
|
||||||
.disable();
|
.disable();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,24 +30,15 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
|||||||
// no order tag means this is the last security filter to be evaluated
|
// no order tag means this is the last security filter to be evaluated
|
||||||
public static class AdminSecurityConfig extends WebSecurityConfigurerAdapter {
|
public static class AdminSecurityConfig extends WebSecurityConfigurerAdapter {
|
||||||
|
|
||||||
@Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
|
@Autowired
|
||||||
|
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
|
||||||
auth.inMemoryAuthentication();
|
auth.inMemoryAuthentication();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override protected void configure(HttpSecurity http) throws Exception {
|
@Override
|
||||||
http
|
protected void configure(HttpSecurity http) throws Exception {
|
||||||
.sessionManagement()
|
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER).and().httpBasic().disable().authorizeRequests().antMatchers(HttpMethod.GET, "/").hasRole("ADMIN").antMatchers("/info", "/health").authenticated().anyRequest().denyAll()
|
||||||
.sessionCreationPolicy(SessionCreationPolicy.NEVER)
|
.and().csrf().disable();
|
||||||
.and()
|
|
||||||
.httpBasic()
|
|
||||||
.disable()
|
|
||||||
.authorizeRequests()
|
|
||||||
.antMatchers(HttpMethod.GET, "/").hasRole("ADMIN")
|
|
||||||
.antMatchers("/info","/health").authenticated()
|
|
||||||
.anyRequest().denyAll()
|
|
||||||
.and()
|
|
||||||
.csrf()
|
|
||||||
.disable();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user