From a0acc40f556bffc7878de99d85bc815f4219971f Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Mon, 26 Jun 2017 20:04:13 +0200 Subject: [PATCH 1/3] BAEL-981 pact article code --- libraries/pom.xml | 12 +++ .../PactConsumerDrivenContractUnitTest.java | 82 +++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 libraries/src/test/java/com/baeldung/pact/PactConsumerDrivenContractUnitTest.java diff --git a/libraries/pom.xml b/libraries/pom.xml index b7a33abbfe..de001ef622 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -381,6 +381,17 @@ java-lsh ${java-lsh.version} + + au.com.dius + pact-jvm-consumer-junit_2.11 + ${pact.version} + test + + + org.codehaus.groovy + groovy-all + 2.4.10 + @@ -410,6 +421,7 @@ 1.1.0 4.1.10.Final 0.10 + 3.5.0 diff --git a/libraries/src/test/java/com/baeldung/pact/PactConsumerDrivenContractUnitTest.java b/libraries/src/test/java/com/baeldung/pact/PactConsumerDrivenContractUnitTest.java new file mode 100644 index 0000000000..2952938cd9 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/pact/PactConsumerDrivenContractUnitTest.java @@ -0,0 +1,82 @@ +package com.baeldung.pact; + + +import au.com.dius.pact.consumer.Pact; +import au.com.dius.pact.consumer.PactProviderRuleMk2; +import au.com.dius.pact.consumer.PactVerification; +import au.com.dius.pact.consumer.dsl.PactDslWithProvider; +import au.com.dius.pact.model.RequestResponsePact; +import org.junit.Rule; +import org.junit.Test; +import org.springframework.http.*; +import org.springframework.web.client.RestTemplate; + +import java.util.HashMap; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; + +public class PactConsumerDrivenContractUnitTest { + + @Rule + public PactProviderRuleMk2 mockProvider + = new PactProviderRuleMk2("test_provider", "localhost", 8080, this); + + @Pact(consumer = "test_consumer") + public RequestResponsePact createPact(PactDslWithProvider builder) { + Map headers = new HashMap(); + headers.put("Content-Type", "application/json"); + + return builder + .given("test GET ") + .uponReceiving("GET REQUEST") + .path("/") + .method("GET") + .willRespondWith() + .status(200) + .headers(headers) + .body("{\"condition\": true, \"name\": \"tom\"}") + .given("test POST") + .uponReceiving("POST REQUEST") + .method("POST") + .headers(headers) + .body("{\"name\": \"Michael\"}") + .path("/create") + .willRespondWith() + .status(201) + .headers(headers) + .body("") + .toPact(); + } + + + @Test + @PactVerification() + public void givenGet_whenSendRequest_shouldReturn200WithProperHeaderAndBody() { + //when + ResponseEntity response + = new RestTemplate().getForEntity(mockProvider.getUrl(), String.class); + + //then + assertThat(response.getStatusCode().value()).isEqualTo(200); + assertThat(response.getHeaders().get("Content-Type").contains("application/json")).isTrue(); + assertThat(response.getBody()).contains("condition", "true", "name", "tom"); + + //and + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.setContentType(MediaType.APPLICATION_JSON); + String jsonBody = "{\"name\": \"Michael\"}"; + + //when + ResponseEntity postResponse = new RestTemplate().exchange( + mockProvider.getUrl() + "/create", + HttpMethod.POST, + new HttpEntity<>(jsonBody, httpHeaders), + String.class + ); + + //then + assertThat(postResponse.getStatusCode().value()).isEqualTo(201); + } + +} From df782e425b4857af670fd17b9dd73b48d6ecd2a3 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Fri, 30 Jun 2017 06:35:55 +0200 Subject: [PATCH 2/3] Reformat (#2177) --- .../baeldung/spring/drools/model/Fare.java | 7 --- .../drools/service/TaxiFareConfiguration.java | 9 ++-- .../TaxiFareCalculatorServiceTest.java | 52 ++++++++++--------- 3 files changed, 31 insertions(+), 37 deletions(-) diff --git a/spring-drools/src/main/java/com/baeldung/spring/drools/model/Fare.java b/spring-drools/src/main/java/com/baeldung/spring/drools/model/Fare.java index a67156f167..86044b281a 100644 --- a/spring-drools/src/main/java/com/baeldung/spring/drools/model/Fare.java +++ b/spring-drools/src/main/java/com/baeldung/spring/drools/model/Fare.java @@ -4,12 +4,10 @@ public class Fare { private Long nightSurcharge; private Long rideFare; - private Long totalFare; public Fare() { nightSurcharge = 0L; rideFare = 0L; - totalFare = 0L; } public Long getNightSurcharge() { @@ -31,9 +29,4 @@ public class Fare { public Long getTotalFare() { return nightSurcharge + rideFare; } - - public void setTotalFare(Long totalFare) { - this.totalFare = totalFare; - } - } diff --git a/spring-drools/src/main/java/com/baeldung/spring/drools/service/TaxiFareConfiguration.java b/spring-drools/src/main/java/com/baeldung/spring/drools/service/TaxiFareConfiguration.java index 8fed574639..8da1d4b992 100644 --- a/spring-drools/src/main/java/com/baeldung/spring/drools/service/TaxiFareConfiguration.java +++ b/spring-drools/src/main/java/com/baeldung/spring/drools/service/TaxiFareConfiguration.java @@ -13,7 +13,7 @@ import org.springframework.context.annotation.Configuration; @Configuration public class TaxiFareConfiguration { - public static final String drlFile = "TAXI_FARE_RULE.drl"; + private static final String drlFile = "TAXI_FARE_RULE.drl"; @Bean public KieContainer kieContainer() { @@ -25,14 +25,11 @@ public class TaxiFareConfiguration { kieBuilder.buildAll(); KieModule kieModule = kieBuilder.getKieModule(); - KieContainer kContainer = kieServices.newKieContainer(kieModule.getReleaseId()); - - return kContainer; + return kieServices.newKieContainer(kieModule.getReleaseId()); } @Bean public TaxiFareCalculatorService taxiFareCalculatorService() { - TaxiFareCalculatorService taxiFareCalculatorService = new TaxiFareCalculatorService(); - return taxiFareCalculatorService; + return new TaxiFareCalculatorService(); } } diff --git a/spring-drools/src/test/java/com/baeldung/spring/drools/service/TaxiFareCalculatorServiceTest.java b/spring-drools/src/test/java/com/baeldung/spring/drools/service/TaxiFareCalculatorServiceTest.java index 5569772c48..d329300a2b 100644 --- a/spring-drools/src/test/java/com/baeldung/spring/drools/service/TaxiFareCalculatorServiceTest.java +++ b/spring-drools/src/test/java/com/baeldung/spring/drools/service/TaxiFareCalculatorServiceTest.java @@ -1,25 +1,23 @@ package com.baeldung.spring.drools.service; +import com.baeldung.spring.drools.model.Fare; +import com.baeldung.spring.drools.model.TaxiRide; import org.junit.Assert; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.support.AnnotationConfigContextLoader; -import com.baeldung.spring.drools.model.TaxiRide; -import com.baeldung.spring.drools.model.Fare; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = TaxiFareConfiguration.class) public class TaxiFareCalculatorServiceTest { @Autowired - TaxiFareCalculatorService taxiFareCalculatorService; + private TaxiFareCalculatorService taxiFareCalculatorService; @Test public void testCalculateFareScenario1() { @@ -28,10 +26,11 @@ public class TaxiFareCalculatorServiceTest { taxiRide.setDistanceInMile(9L); Fare rideFare = new Fare(); Long totalCharge = taxiFareCalculatorService.calculateFare(taxiRide, rideFare); - Assert.assertNotNull(totalCharge); - Assert.assertEquals(Long.valueOf(70), totalCharge); + + assertNotNull(totalCharge); + assertEquals(Long.valueOf(70), totalCharge); } - + @Test public void testCalculateFareScenario2() { TaxiRide taxiRide = new TaxiRide(); @@ -39,8 +38,9 @@ public class TaxiFareCalculatorServiceTest { taxiRide.setDistanceInMile(5L); Fare rideFare = new Fare(); Long totalCharge = taxiFareCalculatorService.calculateFare(taxiRide, rideFare); - Assert.assertNotNull(totalCharge); - Assert.assertEquals(Long.valueOf(100), totalCharge); + + assertNotNull(totalCharge); + assertEquals(Long.valueOf(100), totalCharge); } @Test @@ -50,10 +50,11 @@ public class TaxiFareCalculatorServiceTest { taxiRide.setDistanceInMile(50L); Fare rideFare = new Fare(); Long totalCharge = taxiFareCalculatorService.calculateFare(taxiRide, rideFare); - Assert.assertNotNull(totalCharge); - Assert.assertEquals(Long.valueOf(170), totalCharge); + + assertNotNull(totalCharge); + assertEquals(Long.valueOf(170), totalCharge); } - + @Test public void testCalculateFareScenario4() { TaxiRide taxiRide = new TaxiRide(); @@ -61,10 +62,11 @@ public class TaxiFareCalculatorServiceTest { taxiRide.setDistanceInMile(50L); Fare rideFare = new Fare(); Long totalCharge = taxiFareCalculatorService.calculateFare(taxiRide, rideFare); - Assert.assertNotNull(totalCharge); - Assert.assertEquals(Long.valueOf(250), totalCharge); + + assertNotNull(totalCharge); + assertEquals(Long.valueOf(250), totalCharge); } - + @Test public void testCalculateFareScenario5() { TaxiRide taxiRide = new TaxiRide(); @@ -72,10 +74,11 @@ public class TaxiFareCalculatorServiceTest { taxiRide.setDistanceInMile(100L); Fare rideFare = new Fare(); Long totalCharge = taxiFareCalculatorService.calculateFare(taxiRide, rideFare); - Assert.assertNotNull(totalCharge); - Assert.assertEquals(Long.valueOf(220), totalCharge); + + assertNotNull(totalCharge); + assertEquals(Long.valueOf(220), totalCharge); } - + @Test public void testCalculateFareScenario6() { TaxiRide taxiRide = new TaxiRide(); @@ -83,8 +86,9 @@ public class TaxiFareCalculatorServiceTest { taxiRide.setDistanceInMile(100L); Fare rideFare = new Fare(); Long totalCharge = taxiFareCalculatorService.calculateFare(taxiRide, rideFare); - Assert.assertNotNull(totalCharge); - Assert.assertEquals(Long.valueOf(350), totalCharge); + + assertNotNull(totalCharge); + assertEquals(Long.valueOf(350), totalCharge); } - + } From 69c330eb6c770713660075dff0138d079463046c Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Fri, 30 Jun 2017 11:42:22 +0200 Subject: [PATCH 3/3] BAEL-867 coroutines tests (#2011) * BAEL-867 coroutines tests * BAEL-867 add two channels examples * BAEL-867 fix example * BAEL-867 remove channels * BAEL-867 optimize imports * BAEL-867 remove kotlin module --- kotlin/pom.xml | 25 ++- .../com/baeldung/kotlin/CoroutinesTest.kt | 179 ++++++++++++++++++ pom.xml | 2 - 3 files changed, 199 insertions(+), 7 deletions(-) create mode 100644 kotlin/src/test/kotlin/com/baeldung/kotlin/CoroutinesTest.kt diff --git a/kotlin/pom.xml b/kotlin/pom.xml index 7b45e9809b..bcf5b36385 100644 --- a/kotlin/pom.xml +++ b/kotlin/pom.xml @@ -1,6 +1,6 @@ + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 com.baeldung @@ -13,6 +13,15 @@ 1.0.0-SNAPSHOT + + + + central + http://jcenter.bintray.com + + + + org.jetbrains.kotlin @@ -31,6 +40,11 @@ ${kotlin-reflect.version} test + + org.jetbrains.kotlinx + kotlinx-coroutines-core + ${kotlinx.version} + @@ -104,10 +118,11 @@ - 1.1.1 - 1.1.1 - 1.1.1 - 1.1.1 + 1.1.2 + 1.1.2 + 1.1.2 + 1.1.2 + 0.15 \ No newline at end of file diff --git a/kotlin/src/test/kotlin/com/baeldung/kotlin/CoroutinesTest.kt b/kotlin/src/test/kotlin/com/baeldung/kotlin/CoroutinesTest.kt new file mode 100644 index 0000000000..54fafdb3e1 --- /dev/null +++ b/kotlin/src/test/kotlin/com/baeldung/kotlin/CoroutinesTest.kt @@ -0,0 +1,179 @@ +package com.baeldung.kotlin + +import kotlinx.coroutines.experimental.* +import org.junit.Test +import java.util.concurrent.atomic.AtomicInteger +import kotlin.coroutines.experimental.buildSequence +import kotlin.system.measureTimeMillis +import kotlin.test.assertEquals +import kotlin.test.assertTrue + + +class CoroutinesTest { + + @Test + fun givenBuildSequence_whenTakeNElements_thenShouldReturnItInALazyWay() { + //given + val fibonacciSeq = buildSequence { + var a = 0 + var b = 1 + + yield(1) + + while (true) { + yield(a + b) + + val tmp = a + b + a = b + b = tmp + } + } + + //when + val res = fibonacciSeq.take(5).toList() + + //then + assertEquals(res, listOf(1, 1, 2, 3, 5)) + } + + @Test + fun givenLazySeq_whenTakeNElements_thenShouldReturnAllElements() { + //given + val lazySeq = buildSequence { + print("START ") + for (i in 1..5) { + yield(i) + print("STEP ") + } + print("END") + } + //when + val res = lazySeq.take(10).toList() + + //then + assertEquals(res, listOf(1, 2, 3, 4, 5)) + } + + @Test + fun givenAsyncCoroutine_whenStartIt_thenShouldExecuteItInTheAsyncWay() { + //given + val res = mutableListOf() + + //when + runBlocking { + val promise = launch(CommonPool) { expensiveComputation(res) } + res.add("Hello,") + promise.join() + } + + //then + assertEquals(res, listOf("Hello,", "word!")) + } + + + suspend fun expensiveComputation(res: MutableList) { + delay(1000L) + res.add("word!") + } + + @Test + fun givenHugeAmountOfCoroutines_whenStartIt_thenShouldExecuteItWithoutOutOfMemory() { + runBlocking { + //given + val counter = AtomicInteger(0) + val numberOfCoroutines = 100_000 + + //when + val jobs = List(numberOfCoroutines) { + launch(CommonPool) { + delay(1L) + counter.incrementAndGet() + } + } + jobs.forEach { it.join() } + + //then + assertEquals(counter.get(), numberOfCoroutines) + } + } + + @Test + fun givenCancellableJob_whenRequestForCancel_thenShouldQuit() { + runBlocking { + //given + val job = launch(CommonPool) { + while (isActive) { + println("is working") + } + } + + delay(1300L) + + //when + job.cancel() + + //then cancel successfully + + } + } + + @Test(expected = CancellationException::class) + fun givenAsyncAction_whenDeclareTimeout_thenShouldFinishWhenTimedOut() { + runBlocking { + withTimeout(1300L) { + repeat(1000) { i -> + println("Some expensive computation $i ...") + delay(500L) + } + } + } + } + + @Test + fun givenHaveTwoExpensiveAction_whenExecuteThemAsync_thenTheyShouldRunConcurrently() { + runBlocking { + val delay = 1000L + val time = measureTimeMillis { + //given + val one = async(CommonPool) { someExpensiveComputation(delay) } + val two = async(CommonPool) { someExpensiveComputation(delay) } + + //when + runBlocking { + one.await() + two.await() + } + } + + //then + assertTrue(time < delay * 2) + } + } + + @Test + fun givenTwoExpensiveAction_whenExecuteThemLazy_thenTheyShouldNotConcurrently() { + runBlocking { + val delay = 1000L + val time = measureTimeMillis { + //given + val one = async(CommonPool, CoroutineStart.LAZY) { someExpensiveComputation(delay) } + val two = async(CommonPool, CoroutineStart.LAZY) { someExpensiveComputation(delay) } + + //when + runBlocking { + one.await() + two.await() + } + } + + //then + assertTrue(time > delay * 2) + } + } + + suspend fun someExpensiveComputation(delayInMilliseconds: Long) { + delay(delayInMilliseconds) + } + + +} diff --git a/pom.xml b/pom.xml index 91d7abd5ca..43f31d5c98 100644 --- a/pom.xml +++ b/pom.xml @@ -88,8 +88,6 @@ junit5 jws - kotlin - libraries log-mdc log4j