Merge branch 'eugenp:master' into master

This commit is contained in:
jsgrah-spring
2022-10-04 12:23:02 +02:00
committed by GitHub
59 changed files with 994 additions and 205 deletions
+1 -1
View File
@@ -71,7 +71,7 @@
</dependencies>
<properties>
<axon-bom.version>4.5.17</axon-bom.version>
<axon-bom.version>4.6.0</axon-bom.version>
</properties>
</project>
@@ -92,6 +92,11 @@ public class OrderRestEndpoint {
return orderQueryService.findAllOrders();
}
@GetMapping(path = "/all-orders-streaming", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<OrderResponse> allOrdersStreaming() {
return orderQueryService.allOrdersStreaming();
}
@GetMapping("/total-shipped/{product-id}")
public Integer totalShipped(@PathVariable("product-id") String productId) {
return orderQueryService.totalShipped(productId);
@@ -17,7 +17,10 @@ import org.axonframework.config.ProcessingGroup;
import org.axonframework.eventhandling.EventHandler;
import org.axonframework.queryhandling.QueryHandler;
import org.axonframework.queryhandling.QueryUpdateEmitter;
import org.reactivestreams.Publisher;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.ArrayList;
import java.util.HashMap;
@@ -101,6 +104,11 @@ public class InMemoryOrdersEventHandler implements OrdersEventHandler {
return new ArrayList<>(orders.values());
}
@QueryHandler
public Publisher<Order> handleStreaming(FindAllOrderedProductsQuery query) {
return Mono.fromCallable(orders::values).flatMapMany(Flux::fromIterable);
}
@QueryHandler
public Integer handle(TotalProductsShippedQuery query) {
return orders.values()
@@ -9,6 +9,7 @@ import org.axonframework.messaging.responsetypes.ResponseType;
import org.axonframework.messaging.responsetypes.ResponseTypes;
import org.axonframework.queryhandling.QueryGateway;
import org.axonframework.queryhandling.SubscriptionQueryResult;
import org.reactivestreams.Publisher;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
@@ -34,6 +35,11 @@ public class OrderQueryService {
.collect(Collectors.toList()));
}
public Flux<OrderResponse> allOrdersStreaming() {
Publisher<Order> publisher = queryGateway.streamingQuery(new FindAllOrderedProductsQuery(), Order.class);
return Flux.from(publisher).map(OrderResponse::new);
}
public Integer totalShipped(String productId) {
return queryGateway.scatterGather(new TotalProductsShippedQuery(productId),
ResponseTypes.instanceOf(Integer.class), 10L, TimeUnit.SECONDS)
@@ -11,6 +11,7 @@ import com.baeldung.axon.coreapi.queries.FindAllOrderedProductsQuery;
import com.baeldung.axon.coreapi.queries.Order;
import com.baeldung.axon.coreapi.queries.OrderUpdatesQuery;
import com.baeldung.axon.coreapi.queries.TotalProductsShippedQuery;
import org.reactivestreams.Publisher;
import java.util.List;
@@ -32,6 +33,8 @@ public interface OrdersEventHandler {
List<Order> handle(FindAllOrderedProductsQuery query);
Publisher<Order> handleStreaming(FindAllOrderedProductsQuery query);
Integer handle(TotalProductsShippedQuery query);
Order handle(OrderUpdatesQuery query);
+4
View File
@@ -10,6 +10,10 @@ POST http://localhost:8080/ship-unconfirmed-order
GET http://localhost:8080/all-orders
### Receive all existing orders using a stream
GET http://localhost:8080/all-orders-streaming
### Create Order with id 666a1661-474d-4046-8b12-8b5896312768
POST http://localhost:8080/order/666a1661-474d-4046-8b12-8b5896312768
@@ -14,10 +14,13 @@ import com.baeldung.axon.coreapi.queries.OrderUpdatesQuery;
import com.baeldung.axon.coreapi.queries.TotalProductsShippedQuery;
import org.axonframework.queryhandling.QueryUpdateEmitter;
import org.junit.jupiter.api.*;
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.function.Consumer;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
@@ -68,6 +71,26 @@ public abstract class AbstractOrdersEventHandlerUnitTest {
assertEquals(orderTwo, order_2);
}
@Test
void givenTwoOrdersPlacedOfWhichOneNotShipped_whenFindAllOrderedProductsQueryStreaming_thenCorrectOrdersAreReturned() {
resetWithTwoOrders();
final Consumer<Order> orderVerifier = order -> {
if (order.getOrderId().equals(orderOne.getOrderId())) {
assertEquals(orderOne, order);
} else if (order.getOrderId().equals(orderTwo.getOrderId())) {
assertEquals(orderTwo, order);
} else {
throw new RuntimeException("Would expect either order one or order two");
}
};
StepVerifier.create(Flux.from(handler.handleStreaming(new FindAllOrderedProductsQuery())))
.assertNext(orderVerifier)
.assertNext(orderVerifier)
.expectComplete()
.verify();
}
@Test
void givenNoOrdersPlaced_whenTotalProductsShippedQuery_thenZeroReturned() {
assertEquals(0, handler.handle(new TotalProductsShippedQuery(PRODUCT_ID_1)));
@@ -6,6 +6,7 @@ import com.baeldung.axon.coreapi.events.OrderShippedEvent;
import com.baeldung.axon.coreapi.events.ProductAddedEvent;
import com.baeldung.axon.coreapi.events.ProductCountDecrementedEvent;
import com.baeldung.axon.coreapi.events.ProductCountIncrementedEvent;
import com.baeldung.axon.coreapi.queries.FindAllOrderedProductsQuery;
import com.baeldung.axon.coreapi.queries.Order;
import org.axonframework.eventhandling.gateway.EventGateway;
@@ -13,6 +14,7 @@ import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;
import java.util.Collections;
@@ -60,6 +62,15 @@ class OrderQueryServiceIntegrationTest {
.isEmpty());
}
@Test
void givenOrderCreatedEventSend_whenCallingAllOrdersStreaming_thenOneOrderIsReturned() {
Flux<OrderResponse> result = queryService.allOrdersStreaming();
StepVerifier.create(result)
.assertNext(order -> assertEquals(orderId, order.getOrderId()))
.expectComplete()
.verify();
}
@Test
void givenThreeDeluxeChairsShipped_whenCallingAllShippedChairs_then234PlusTreeIsReturned() {
Order order = new Order(orderId);
@@ -0,0 +1,43 @@
package com.baeldung.listandset;
import org.junit.Test;
import org.junit.Assert;
import java.util.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class ListAndSetUnitTest {
@Test
public void givenList_whenDuplicates_thenAllowed(){
List<Integer> integerList = new ArrayList<>();
integerList.add(2);
integerList.add(3);
integerList.add(4);
integerList.add(4);
assertEquals(integerList.size(), 4);
}
@Test
public void givenSet_whenDuplicates_thenNotAllowed(){
Set<Integer> integerSet = new HashSet<>();
integerSet.add(2);
integerSet.add(3);
integerSet.add(4);
integerSet.add(4);
assertEquals(integerSet.size(), 3);
}
@Test
public void givenSet_whenOrdering_thenMayBeAllowed(){
Set<Integer> set1 = new LinkedHashSet<>();
set1.add(2);
set1.add(3);
set1.add(4);
Set<Integer> set2 = new LinkedHashSet<>();
set2.add(2);
set2.add(3);
set2.add(4);
Assert.assertArrayEquals(set1.toArray(), set2.toArray());
}
}
@@ -56,6 +56,12 @@
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
@@ -80,6 +86,7 @@
<assertj.version>3.22.0</assertj.version>
<maven.compiler.source.version>17</maven.compiler.source.version>
<maven.compiler.target.version>17</maven.compiler.target.version>
<spring.version>5.3.22</spring.version>
</properties>
</project>
@@ -0,0 +1,132 @@
package com.baeldung.list.tostring;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Arrays;
import java.util.List;
import java.util.StringJoiner;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
class ListToStringUnitTest {
@Test
void givenAListOfString_whenUsingJava8_thenConvertToStringByUsingString() {
List<String> arraysAsList = Arrays.asList("ONE", "TWO", "THREE");
String commaSeparatedString = String.join(",", arraysAsList);
assertThat(commaSeparatedString).isEqualTo("ONE,TWO,THREE");
}
@Test
void givenAListOfString_whenUsingJava8_thenConvertToStringByUsingStringJoiner() {
List<String> arraysAsList = Arrays.asList("ONE", "TWO", "THREE");
StringJoiner stringJoiner = new StringJoiner(",");
arraysAsList.stream()
.forEach(v -> stringJoiner.add(v));
String commaSeparatedString = stringJoiner.toString();
assertThat(commaSeparatedString).isEqualTo("ONE,TWO,THREE");
StringJoiner stringJoinerWithDelimiterPrefixSuffix = new StringJoiner(",", "[", "]");
arraysAsList.stream()
.forEach(v -> stringJoinerWithDelimiterPrefixSuffix.add(v));
String commaSeparatedStringWithDelimiterPrefixSuffix = stringJoinerWithDelimiterPrefixSuffix.toString();
assertThat(commaSeparatedStringWithDelimiterPrefixSuffix).isEqualTo("[ONE,TWO,THREE]");
}
@Test
void givenAListOfString_whenUsingJava8_thenConvertToStringByUsingCollectors() {
List<String> arraysAsList = Arrays.asList("ONE", "TWO", "THREE");
String commaSeparatedUsingCollect = arraysAsList.stream()
.collect(Collectors.joining(","));
assertThat(commaSeparatedUsingCollect).isEqualTo("ONE,TWO,THREE");
String commaSeparatedObjectToString = arraysAsList.stream()
.map(Object::toString)
.collect(Collectors.joining(","));
assertThat(commaSeparatedObjectToString).isEqualTo("ONE,TWO,THREE");
String commaSeparatedStringValueOf = arraysAsList.stream()
.map(String::valueOf)
.collect(Collectors.joining(","));
assertThat(commaSeparatedStringValueOf).isEqualTo("ONE,TWO,THREE");
String commaSeparatedStringValueOfWithDelimiterPrefixSuffix = arraysAsList.stream()
.map(String::valueOf)
.collect(Collectors.joining(",", "[", "]"));
assertThat(commaSeparatedStringValueOfWithDelimiterPrefixSuffix).isEqualTo("[ONE,TWO,THREE]");
String commaSeparatedUsingReduce = arraysAsList.stream()
.reduce((x, y) -> x + "," + y)
.get();
assertThat(commaSeparatedUsingReduce).isEqualTo("ONE,TWO,THREE");
}
@Test
void givenAListOfString_whenUsingApacheCommonsLang_thenConvertToStringByUsingStringUtils() {
List<String> arraysAsList = Arrays.asList("ONE", "TWO", "THREE");
String commaSeparatedString = org.apache.commons.lang3.StringUtils.join(arraysAsList, ",");
assertThat(commaSeparatedString).isEqualTo("ONE,TWO,THREE");
String commaSeparatedStringIndex = org.apache.commons.lang3.StringUtils.join(arraysAsList.toArray(), ",", 0, 3);
assertThat(commaSeparatedStringIndex).isEqualTo("ONE,TWO,THREE");
}
@Test
void givenAListOfString_whenUsingSpringCore_thenConvertToStringByUsingStringUtils() {
List<String> arraysAsList = Arrays.asList("ONE", "TWO", "THREE");
String collectionToCommaDelimitedString = org.springframework.util.StringUtils.collectionToCommaDelimitedString(arraysAsList);
assertThat(collectionToCommaDelimitedString).isEqualTo("ONE,TWO,THREE");
String collectionToDelimitedString = org.springframework.util.StringUtils.collectionToDelimitedString(arraysAsList, ",");
assertThat(collectionToDelimitedString).isEqualTo("ONE,TWO,THREE");
}
@Test
void givenAListOfString_whenUsingGoogleGuava_thenConvertToStringByUsingJoiner() {
List<String> arraysAsList = Arrays.asList("ONE", "TWO", "THREE");
String commaSeparatedString = com.google.common.base.Joiner.on(",")
.join(arraysAsList);
assertThat(commaSeparatedString).isEqualTo("ONE,TWO,THREE");
List<String> arraysAsListWithNull = Arrays.asList("ONE", null, "TWO", null, "THREE");
String commaSeparatedStringSkipNulls = com.google.common.base.Joiner.on(",")
.skipNulls()
.join(arraysAsListWithNull);
assertThat(commaSeparatedStringSkipNulls).isEqualTo("ONE,TWO,THREE");
String commaSeparatedStringUseForNull = com.google.common.base.Joiner.on(",")
.useForNull(" ")
.join(arraysAsListWithNull);
assertThat(commaSeparatedStringUseForNull).isEqualTo("ONE, ,TWO, ,THREE");
}
}
@@ -3,4 +3,5 @@ This module contains articles about date operations in Java.
### Relevant Articles:
- [Create Date From Unix Timestamp in Java](https://www.baeldung.com/java-date-unix-timestamp)
- [[<-- Prev]](/core-java-modules/core-java-date-operations-2)
@@ -5,3 +5,4 @@
### Relevant Articles:
- [Java 8 Predicate Chain](https://www.baeldung.com/java-predicate-chain)
- [Use Cases for Static Methods in Java](https://www.baeldung.com/java-static-methods-use-cases)
- [TriFunction Interface in Java](https://www.baeldung.com/java-trifunction)
@@ -11,4 +11,5 @@ This module contains articles about core Java input and output (IO)
- [Read User Input Until a Condition is Met](https://www.baeldung.com/java-read-input-until-condition)
- [Java Scanner.skip method with examples](https://www.baeldung.com/java-scanner-skip)
- [Generate the MD5 Checksum for a File in Java](https://www.baeldung.com/java-md5-checksum-file)
- [Getting the Filename From a String Containing an Absolute File Path](https://www.baeldung.com/java-filename-full-path)
- [[<-- Prev]](/core-java-modules/core-java-io-3)
@@ -4,3 +4,4 @@ This module contains articles about core Java input/output(IO) APIs.
### Relevant Articles:
- [Constructing a Relative Path From Two Absolute Paths in Java](https://www.baeldung.com/java-relative-path-absolute)
- [Java Scanner Taking a Character Input](https://www.baeldung.com/java-scanner-character-input)
@@ -7,4 +7,5 @@
- [Evaluating a Math Expression in Java](https://www.baeldung.com/java-evaluate-math-expression-string)
- [Swap Two Variables in Java](https://www.baeldung.com/java-swap-two-variables)
- [Java Program to Find the Roots of a Quadratic Equation](https://www.baeldung.com/roots-quadratic-equation/)
- [Create a BMI Calculator in Java](https://www.baeldung.com/java-body-mass-index-calculator)
- More articles: [[<-- Prev]](/core-java-modules/core-java-lang-math-2)
@@ -0,0 +1,51 @@
package com.baeldung.portscanner;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
public class PortScanner {
private static final int poolSize = 10;
private static final int timeOut = 200;
public void runPortScan(String ip, int nbrPortMaxToScan) throws IOException, RuntimeException {
ConcurrentLinkedQueue openPorts = new ConcurrentLinkedQueue<>();
ExecutorService executorService = Executors.newFixedThreadPool(poolSize);
AtomicInteger port = new AtomicInteger(0);
while (port.get() < nbrPortMaxToScan) {
final int currentPort = port.getAndIncrement();
executorService.submit(() -> {
try {
Socket socket = new Socket();
socket.connect(new InetSocketAddress(ip, currentPort), timeOut);
socket.close();
openPorts.add(currentPort);
System.out.println(ip + " ,port open: " + currentPort);
} catch (IOException e) {
System.err.println(e);
}
});
}
executorService.shutdown();
try {
executorService.awaitTermination(10, TimeUnit.MINUTES);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
List openPortList = new ArrayList<>();
System.out.println("openPortsQueue: " + openPorts.size());
while (!openPorts.isEmpty()) {
openPortList.add(openPorts.poll());
}
openPortList.forEach(p -> System.out.println("port " + p + " is open"));
}
}
@@ -0,0 +1,17 @@
package com.baeldung.portScanner;
import com.baeldung.portscanner.PortScanner;
import org.junit.jupiter.api.Test;
import java.io.IOException;
class PortScannerUnitTest {
private static final int nbrPortMax = 1000; // Max is 65535, number of available ports
private static final String ip = "127.0.0.1";
PortScanner portScanner = new PortScanner();
@Test public void when_Run_then_lunchPortScan() throws IOException, RuntimeException {
portScanner.runPortScan(ip, nbrPortMax);
}
}
@@ -5,3 +5,4 @@
- [Armstrong Numbers in Java](https://www.baeldung.com/java-armstrong-numbers)
- [List All Factors of a Number in Java](https://www.baeldung.com/java-list-factors-integer)
- [Make Division of Two Integers Result in a Float](https://www.baeldung.com/java-integer-division-float-result)
- [Creating Random Numbers With No Duplicates in Java](https://www.baeldung.com/java-unique-random-numbers)
@@ -2,3 +2,4 @@
### Relevant Articles:
- [Compare Characters in Java](https://www.baeldung.com/java-compare-characters)
- [String Concatenation in Java](https://www.baeldung.com/java-string-concatenation)
@@ -3,3 +3,4 @@
### Relevant Articles:
- [Generating Alphanumeric UUID String in Java](https://www.baeldung.com/java-generate-alphanumeric-uuid)
- [Guide to UUID in Java](http://www.baeldung.com/java-uuid)
- [Validate UUID String in Java](https://www.baeldung.com/java-validate-uuid-string)
+1
View File
@@ -6,4 +6,5 @@
- [Difference Between links and depends_on in Docker Compose](https://www.baeldung.com/ops/docker-compose-links-depends-on)
- [Mounting Multiple Volumes on a Docker Container](https://www.baeldung.com/ops/docker-mounting-multiple-volumes)
- [Rebuild Docker Container in Docker Compose](https://www.baeldung.com/rebuild-docker-container-compose/)
- [Assign Static IP to Docker Container and Docker-Compose](https://www.baeldung.com/ops/docker-assign-static-ip-container)
+2
View File
@@ -3,3 +3,5 @@
- [Pushing a Docker Image to a Private Repository](https://www.baeldung.com/ops/docker-push-image-to-private-repository)
- [How to Include Files Outside of Docker’s Build Context](https://www.baeldung.com/ops/docker-include-files-outside-build-context)
- [Adding a Comment in a Dockerfile](https://www.baeldung.com/ops/docker-dockerfile-comments/)
- [Updating PATH Environment Variable in Dockerfile](https://www.baeldung.com/ops/dockerfile-path-environment-variable)
- [Keep Subdirectory Structure in Dockerfile Copy](https://www.baeldung.com/ops/dockerfile-copy-same-subdirectory-structure)
+1
View File
@@ -2,3 +2,4 @@
### Relevant Articles:
- [Dockerizing a Java Application](https://www.baeldung.com/java-dockerize-app)
- [Debugging an Application Running in Docker With IntelliJ IDEA](https://www.baeldung.com/docker-debug-app-with-intellij)
@@ -0,0 +1,88 @@
<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2" properties="5.0" jmeter="5.5">
<hashTree>
<TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="LatencyVsRespomseTime" enabled="true">
<stringProp name="TestPlan.comments"></stringProp>
<boolProp name="TestPlan.functional_mode">false</boolProp>
<boolProp name="TestPlan.tearDown_on_shutdown">true</boolProp>
<boolProp name="TestPlan.serialize_threadgroups">false</boolProp>
<elementProp name="TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
<collectionProp name="Arguments.arguments"/>
</elementProp>
<stringProp name="TestPlan.user_define_classpath"></stringProp>
</TestPlan>
<hashTree>
<ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Thread Group" enabled="true">
<stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
<elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="Loop Controller" enabled="true">
<boolProp name="LoopController.continue_forever">false</boolProp>
<stringProp name="LoopController.loops">1</stringProp>
</elementProp>
<stringProp name="ThreadGroup.num_threads">1</stringProp>
<stringProp name="ThreadGroup.ramp_time">1</stringProp>
<boolProp name="ThreadGroup.scheduler">false</boolProp>
<stringProp name="ThreadGroup.duration"></stringProp>
<stringProp name="ThreadGroup.delay"></stringProp>
<boolProp name="ThreadGroup.same_user_on_next_iteration">true</boolProp>
</ThreadGroup>
<hashTree>
<HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="HTTP Request" enabled="true">
<elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
<collectionProp name="Arguments.arguments"/>
</elementProp>
<stringProp name="HTTPSampler.domain"></stringProp>
<stringProp name="HTTPSampler.port"></stringProp>
<stringProp name="HTTPSampler.protocol"></stringProp>
<stringProp name="HTTPSampler.contentEncoding"></stringProp>
<stringProp name="HTTPSampler.path">http://localhost:3000</stringProp>
<stringProp name="HTTPSampler.method">GET</stringProp>
<boolProp name="HTTPSampler.follow_redirects">true</boolProp>
<boolProp name="HTTPSampler.auto_redirects">false</boolProp>
<boolProp name="HTTPSampler.use_keepalive">true</boolProp>
<boolProp name="HTTPSampler.DO_MULTIPART_POST">false</boolProp>
<stringProp name="HTTPSampler.embedded_url_re"></stringProp>
<stringProp name="HTTPSampler.connect_timeout"></stringProp>
<stringProp name="HTTPSampler.response_timeout"></stringProp>
</HTTPSamplerProxy>
<hashTree/>
<ResultCollector guiclass="ViewResultsFullVisualizer" testclass="ResultCollector" testname="View Results Tree" enabled="true">
<boolProp name="ResultCollector.error_logging">false</boolProp>
<objProp>
<name>saveConfig</name>
<value class="SampleSaveConfiguration">
<time>true</time>
<latency>true</latency>
<timestamp>true</timestamp>
<success>true</success>
<label>true</label>
<code>true</code>
<message>true</message>
<threadName>true</threadName>
<dataType>true</dataType>
<encoding>false</encoding>
<assertions>true</assertions>
<subresults>true</subresults>
<responseData>false</responseData>
<samplerData>false</samplerData>
<xml>false</xml>
<fieldNames>true</fieldNames>
<responseHeaders>false</responseHeaders>
<requestHeaders>false</requestHeaders>
<responseDataOnError>false</responseDataOnError>
<saveAssertionResultsFailureMessage>true</saveAssertionResultsFailureMessage>
<assertionsResultsToSave>0</assertionsResultsToSave>
<bytes>true</bytes>
<sentBytes>true</sentBytes>
<url>true</url>
<threadCounts>true</threadCounts>
<idleTime>true</idleTime>
<connectTime>true</connectTime>
</value>
</objProp>
<stringProp name="filename"></stringProp>
</ResultCollector>
<hashTree/>
</hashTree>
</hashTree>
</hashTree>
</jmeterTestPlan>
+2 -2
View File
@@ -24,8 +24,8 @@
</dependencies>
<properties>
<spring.version>5.3.22</spring.version>
<spring-security.version>5.6.0</spring-security.version>
<spring.version>5.3.23</spring.version>
<spring-security.version>5.7.3</spring-security.version>
<spring-boot-starter-test.version>1.5.10.RELEASE</spring-boot-starter-test.version>
</properties>
@@ -4,3 +4,4 @@
- [DAO vs Repository Patterns](https://www.baeldung.com/java-dao-vs-repository)
- [Difference Between MVC and MVP Patterns](https://www.baeldung.com/mvc-vs-mvp-pattern)
- [The DTO Pattern (Data Transfer Object)](https://www.baeldung.com/java-dto-pattern)
- [SEDA With Spring Integration and Apache Camel](https://www.baeldung.com/spring-apache-camel-seda-integration)
@@ -8,3 +8,4 @@
- [JDBC Connection Status](https://www.baeldung.com/jdbc-connection-status)
- [Get the Number of Rows in a ResultSet](https://www.baeldung.com/java-resultset-number-of-rows)
- [Converting a JDBC ResultSet to JSON in Java](https://www.baeldung.com/java-jdbc-convert-resultset-to-json)
- [Guide to MicroStream](https://www.baeldung.com/microstream-intro)
@@ -0,0 +1,157 @@
package com.baeldung.mongo.insert;
import com.mongodb.*;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import org.bson.conversions.Bson;
import java.util.ArrayList;
import java.util.List;
import static com.mongodb.client.model.Filters.eq;
import static com.mongodb.client.model.Filters.in;
public class InsertArrayOperation {
private static MongoClient mongoClient;
private static MongoDatabase database;
private static MongoCollection<Document> collection;
private static DB db;
private static DBCollection dbCollection;
private static String collectionName;
private static String databaseName;
public static void setUp() {
if (mongoClient == null) {
mongoClient = new MongoClient("localhost", 27017);
databaseName = "baeldung";
collectionName = "student";
database = mongoClient.getDatabase(databaseName);
collection = database.getCollection(collectionName);
db = mongoClient.getDB(databaseName);
dbCollection = db.getCollection(collectionName);
}
}
public static void insertSingleBsonDocumentWithStringArray() {
BasicDBList coursesList = new BasicDBList();
coursesList.add("Chemistry");
coursesList.add("Science");
DBObject student = new BasicDBObject().append("studentId", "STUD1")
.append("name", "Jim")
.append("age", 13)
.append("courses", coursesList);
dbCollection.insert(student);
Bson filter = eq("studentId", "STUD1");
FindIterable<Document> documents = collection.find(filter);
MongoCursor<Document> cursor = documents.iterator();
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
}
public static void insertSingleDocumentWithStringArray() {
List<String> coursesList = new ArrayList<>();
coursesList.add("Science");
coursesList.add("Geography");
Document student = new Document().append("studentId", "STUD2")
.append("name", "Sam")
.append("age", 13)
.append("courses", coursesList);
collection.insertOne(student);
Bson filter = eq("studentId", "STUD2");
FindIterable<Document> documents = collection.find(filter);
MongoCursor<Document> cursor = documents.iterator();
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
}
public static void insertMultipleDocumentsWithStringArray() {
List<String> coursesList1 = new ArrayList<>();
coursesList1.add("Chemistry");
coursesList1.add("Geography");
Document student1 = new Document().append("studentId", "STUD3")
.append("name", "Sarah")
.append("age", 12)
.append("courses", coursesList1);
List<String> coursesList2 = new ArrayList<>();
coursesList2.add("Maths");
coursesList2.add("History");
Document student2 = new Document().append("studentId", "STUD4")
.append("name", "Tom")
.append("age", 13)
.append("courses", coursesList2);
List<Document> students = new ArrayList<>();
students.add(student1);
students.add(student2);
collection.insertMany(students);
Bson filter = in("studentId", "STUD3", "STUD4");
FindIterable<Document> documents = collection.find(filter);
MongoCursor<Document> cursor = documents.iterator();
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
}
public static void insertSingleDocumentWithObjectArray() {
Document course1 = new Document().append("name", "C1")
.append("points", 5);
Document course2 = new Document().append("name", "C2")
.append("points", 7);
List<Document> coursesList = new ArrayList<>();
coursesList.add(course1);
coursesList.add(course2);
Document student = new Document().append("studentId", "STUD5")
.append("name", "Avin")
.append("age", 13)
.append("courses", coursesList);
collection.insertOne(student);
Bson filter = eq("studentId", "STUD5");
FindIterable<Document> documents = collection.find(filter);
MongoCursor<Document> cursor = documents.iterator();
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
}
public static void main(String args[]) {
setUp();
insertSingleBsonDocumentWithStringArray();
insertSingleDocumentWithStringArray();
insertMultipleDocumentsWithStringArray();
insertSingleDocumentWithObjectArray();
}
}
@@ -0,0 +1,150 @@
package com.baeldung.mongo.insert;
import com.mongodb.*;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.junit.BeforeClass;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import static com.mongodb.client.model.Filters.eq;
import static com.mongodb.client.model.Filters.in;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
public class InsertArrayOperationLiveTest {
private static MongoClient mongoClient;
private static MongoDatabase database;
private static MongoCollection<Document> collection;
private static DB db;
private static DBCollection dbCollection;
@BeforeClass
public static void setUp() {
if (mongoClient == null) {
mongoClient = new MongoClient("localhost", 27017);
database = mongoClient.getDatabase("baeldung");
collection = database.getCollection("student");
db = mongoClient.getDB("baeldung");
dbCollection = db.getCollection("student");
collection.drop();
}
}
@Test
public void givenSingleStudentObjectWithStringArray_whenInsertingDBObject_thenCheckingForDocument() {
BasicDBList coursesList = new BasicDBList();
coursesList.add("Chemistry");
coursesList.add("Science");
DBObject student = new BasicDBObject().append("studentId", "STUD1")
.append("name", "Jim")
.append("age", 13)
.append("courses", coursesList);
dbCollection.insert(student);
Bson filter = eq("studentId", "STUD2");
FindIterable<Document> documents = collection.find(filter);
MongoCursor<Document> cursor = documents.iterator();
assertNotNull(cursor);
assertTrue(cursor.hasNext());
}
@Test
public void givenSingleStudentObjectWithStringArray_whenInsertingDocument_thenCheckingForDocument() {
List<String> coursesList = new ArrayList<>();
coursesList.add("Science");
coursesList.add("Geography");
Document student = new Document().append("studentId", "STUD2")
.append("name", "Sam")
.append("age", 13)
.append("courses", coursesList);
collection.insertOne(student);
Bson filter = eq("studentId", "STUD2");
FindIterable<Document> documents = collection.find(filter);
MongoCursor<Document> cursor = documents.iterator();
assertNotNull(cursor);
assertTrue(cursor.hasNext());
}
@Test
public void givenMultipleStudentObjectsWithStringArray_whenInsertingDocuments_thenCheckingForDocuments() {
List<String> coursesList1 = new ArrayList<>();
coursesList1.add("Chemistry");
coursesList1.add("Geography");
Document student1 = new Document().append("studentId", "STUD3")
.append("name", "Sarah")
.append("age", 12)
.append("courses", coursesList1);
List<String> coursesList2 = new ArrayList<>();
coursesList2.add("Maths");
coursesList2.add("History");
Document student2 = new Document().append("studentId", "STUD4")
.append("name", "Tom")
.append("age", 13)
.append("courses", coursesList2);
List<Document> students = new ArrayList<>();
students.add(student1);
students.add(student2);
collection.insertMany(students);
Bson filter = in("studentId", "STUD3", "STUD4");
FindIterable<Document> documents = collection.find(filter);
MongoCursor<Document> cursor = documents.iterator();
assertNotNull(cursor);
assertTrue(cursor.hasNext());
}
@Test
public void givenSingleStudentObjectWithObjectArray_whenInsertingDocument_thenCheckingForDocument() {
Document course1 = new Document().append("name", "C1")
.append("points", 5);
Document course2 = new Document().append("name", "C2")
.append("points", 7);
List<Document> coursesList = new ArrayList<>();
coursesList.add(course1);
coursesList.add(course2);
Document student = new Document().append("studentId", "STUD5")
.append("name", "Sam")
.append("age", 13)
.append("courses", coursesList);
collection.insertOne(student);
Bson filter = eq("studentId", "STUD5");
FindIterable<Document> documents = collection.find(filter);
MongoCursor<Document> cursor = documents.iterator();
assertNotNull(cursor);
assertTrue(cursor.hasNext());
}
}

Some files were not shown because too many files have changed in this diff Show More