diff --git a/core-java-collections/src/main/java/com/baeldung/java/list/WaysToIterate.java b/core-java-collections/src/main/java/com/baeldung/java/list/WaysToIterate.java new file mode 100644 index 0000000000..3cce08eabb --- /dev/null +++ b/core-java-collections/src/main/java/com/baeldung/java/list/WaysToIterate.java @@ -0,0 +1,67 @@ +package com.baeldung.java.list; + +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; + +/** + * Demonstrates the different ways to loop over + * the elements of a list. + */ +public class WaysToIterate { + + List countries = Arrays.asList("Germany", "Panama", "Australia"); + + /** + * Iterate over a list using a basic for loop + */ + public void iterateWithForLoop() { + for (int i = 0; i < countries.size(); i++) { + System.out.println(countries.get(i)); + } + } + + /** + * Iterate over a list using the enhanced for loop + */ + public void iterateWithEnhancedForLoop() { + for (String country : countries) { + System.out.println(country); + } + } + + /** + * Iterate over a list using an Iterator + */ + public void iterateWithIterator() { + Iterator countriesIterator = countries.iterator(); + while(countriesIterator.hasNext()) { + System.out.println(countriesIterator.next()); + } + } + + /** + * Iterate over a list using a ListIterator + */ + public void iterateWithListIterator() { + ListIterator listIterator = countries.listIterator(); + while(listIterator.hasNext()) { + System.out.println(listIterator.next()); + } + } + + /** + * Iterate over a list using the Iterable.forEach() method + */ + public void iterateWithForEach() { + countries.forEach(System.out::println); + } + + /** + * Iterate over a list using the Stream.forEach() method + */ + public void iterateWithStreamForEach() { + countries.stream().forEach((c) -> System.out.println(c)); + } +} \ No newline at end of file diff --git a/core-java-collections/src/test/java/com/baeldung/java/list/WaysToIterateUnitTest.java b/core-java-collections/src/test/java/com/baeldung/java/list/WaysToIterateUnitTest.java new file mode 100644 index 0000000000..973c60b233 --- /dev/null +++ b/core-java-collections/src/test/java/com/baeldung/java/list/WaysToIterateUnitTest.java @@ -0,0 +1,71 @@ +package com.baeldung.java.list; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; + +import org.junit.Test; + +public class WaysToIterateUnitTest { + + List globalCountries = new ArrayList(); + List europeanCountries = Arrays.asList("Germany", "Panama", "Australia"); + + @Test + public void whenIteratingUsingForLoop_thenReturnThreeAsSizeOfList() { + for (int i = 0; i < europeanCountries.size(); i++) { + globalCountries.add(europeanCountries.get(i)); + } + assertEquals(globalCountries.size(), 3); + globalCountries.clear(); + } + + @Test + public void whenIteratingUsingEnhancedForLoop_thenReturnThreeAsSizeOfList() { + for (String country : europeanCountries) { + globalCountries.add(country); + } + assertEquals(globalCountries.size(), 3); + globalCountries.clear(); + } + + @Test + public void whenIteratingUsingIterator_thenReturnThreeAsSizeOfList() { + Iterator countriesIterator = europeanCountries.iterator(); + while (countriesIterator.hasNext()) { + globalCountries.add(countriesIterator.next()); + } + + assertEquals(globalCountries.size(), 3); + globalCountries.clear(); + } + + @Test + public void whenIteratingUsingListIterator_thenReturnThreeAsSizeOfList() { + ListIterator countriesIterator = europeanCountries.listIterator(); + while (countriesIterator.hasNext()) { + globalCountries.add(countriesIterator.next()); + } + + assertEquals(globalCountries.size(), 3); + globalCountries.clear(); + } + + @Test + public void whenIteratingUsingForEach_thenReturnThreeAsSizeOfList() { + europeanCountries.forEach(country -> globalCountries.add(country)); + assertEquals(globalCountries.size(), 3); + globalCountries.clear(); + } + + @Test + public void whenIteratingUsingStreamForEach_thenReturnThreeAsSizeOfList() { + europeanCountries.stream().forEach((country) -> globalCountries.add(country)); + assertEquals(globalCountries.size(), 3); + globalCountries.clear(); + } +} \ No newline at end of file diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/countdownlatch/CountdownLatchCountExample.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/countdownlatch/CountdownLatchCountExample.java new file mode 100644 index 0000000000..08c7eeec03 --- /dev/null +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/countdownlatch/CountdownLatchCountExample.java @@ -0,0 +1,33 @@ +package com.baeldung.concurrent.countdownlatch; + +import java.util.concurrent.CountDownLatch; + +public class CountdownLatchCountExample { + + private int count; + + public CountdownLatchCountExample(int count) { + this.count = count; + } + + public boolean callTwiceInSameThread() { + CountDownLatch countDownLatch = new CountDownLatch(count); + Thread t = new Thread(() -> { + countDownLatch.countDown(); + countDownLatch.countDown(); + }); + t.start(); + + try { + countDownLatch.await(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + return countDownLatch.getCount() == 0; + } + + public static void main(String[] args) { + CountdownLatchCountExample ex = new CountdownLatchCountExample(2); + System.out.println("Is CountDown Completed : " + ex.callTwiceInSameThread()); + } +} diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/countdownlatch/CountdownLatchResetExample.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/countdownlatch/CountdownLatchResetExample.java new file mode 100644 index 0000000000..1828b7f91e --- /dev/null +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/countdownlatch/CountdownLatchResetExample.java @@ -0,0 +1,41 @@ +package com.baeldung.concurrent.countdownlatch; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.atomic.AtomicInteger; + +public class CountdownLatchResetExample { + + private int count; + private int threadCount; + private final AtomicInteger updateCount; + + CountdownLatchResetExample(int count, int threadCount) { + updateCount = new AtomicInteger(0); + this.count = count; + this.threadCount = threadCount; + } + + public int countWaits() { + CountDownLatch countDownLatch = new CountDownLatch(count); + ExecutorService es = Executors.newFixedThreadPool(threadCount); + for (int i = 0; i < threadCount; i++) { + es.execute(() -> { + long prevValue = countDownLatch.getCount(); + countDownLatch.countDown(); + if (countDownLatch.getCount() != prevValue) { + updateCount.incrementAndGet(); + } + }); + } + + es.shutdown(); + return updateCount.get(); + } + + public static void main(String[] args) { + CountdownLatchResetExample ex = new CountdownLatchResetExample(5, 20); + System.out.println("Count : " + ex.countWaits()); + } +} diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierCompletionMethodExample.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierCompletionMethodExample.java new file mode 100644 index 0000000000..7c1299da62 --- /dev/null +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierCompletionMethodExample.java @@ -0,0 +1,45 @@ +package com.baeldung.concurrent.cyclicbarrier; + +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.CyclicBarrier; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.atomic.AtomicInteger; + +public class CyclicBarrierCompletionMethodExample { + + private int count; + private int threadCount; + private final AtomicInteger updateCount; + + CyclicBarrierCompletionMethodExample(int count, int threadCount) { + updateCount = new AtomicInteger(0); + this.count = count; + this.threadCount = threadCount; + } + + public int countTrips() { + + CyclicBarrier cyclicBarrier = new CyclicBarrier(count, () -> { + updateCount.incrementAndGet(); + }); + + ExecutorService es = Executors.newFixedThreadPool(threadCount); + for (int i = 0; i < threadCount; i++) { + es.execute(() -> { + try { + cyclicBarrier.await(); + } catch (InterruptedException | BrokenBarrierException e) { + e.printStackTrace(); + } + }); + } + es.shutdown(); + return updateCount.get(); + } + + public static void main(String[] args) { + CyclicBarrierCompletionMethodExample ex = new CyclicBarrierCompletionMethodExample(5, 20); + System.out.println("Count : " + ex.countTrips()); + } +} diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierCountExample.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierCountExample.java new file mode 100644 index 0000000000..9d637b428b --- /dev/null +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierCountExample.java @@ -0,0 +1,32 @@ +package com.baeldung.concurrent.cyclicbarrier; + +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.CyclicBarrier; + +public class CyclicBarrierCountExample { + + private int count; + + public CyclicBarrierCountExample(int count) { + this.count = count; + } + + public boolean callTwiceInSameThread() { + CyclicBarrier cyclicBarrier = new CyclicBarrier(count); + Thread t = new Thread(() -> { + try { + cyclicBarrier.await(); + cyclicBarrier.await(); + } catch (InterruptedException | BrokenBarrierException e) { + e.printStackTrace(); + } + }); + t.start(); + return cyclicBarrier.isBroken(); + } + + public static void main(String[] args) { + CyclicBarrierCountExample ex = new CyclicBarrierCountExample(7); + System.out.println("Count : " + ex.callTwiceInSameThread()); + } +} diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierResetExample.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierResetExample.java new file mode 100644 index 0000000000..76b6198bc4 --- /dev/null +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierResetExample.java @@ -0,0 +1,46 @@ +package com.baeldung.concurrent.cyclicbarrier; + +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.CyclicBarrier; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.atomic.AtomicInteger; + +public class CyclicBarrierResetExample { + + private int count; + private int threadCount; + private final AtomicInteger updateCount; + + CyclicBarrierResetExample(int count, int threadCount) { + updateCount = new AtomicInteger(0); + this.count = count; + this.threadCount = threadCount; + } + + public int countWaits() { + + CyclicBarrier cyclicBarrier = new CyclicBarrier(count); + + ExecutorService es = Executors.newFixedThreadPool(threadCount); + for (int i = 0; i < threadCount; i++) { + es.execute(() -> { + try { + if (cyclicBarrier.getNumberWaiting() > 0) { + updateCount.incrementAndGet(); + } + cyclicBarrier.await(); + } catch (InterruptedException | BrokenBarrierException e) { + e.printStackTrace(); + } + }); + } + es.shutdown(); + return updateCount.get(); + } + + public static void main(String[] args) { + CyclicBarrierResetExample ex = new CyclicBarrierResetExample(7, 20); + System.out.println("Count : " + ex.countWaits()); + } +} diff --git a/core-java-concurrency/src/test/java/com/baeldung/concurrent/countdownlatch/CountdownLatchCountExampleUnitTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/countdownlatch/CountdownLatchCountExampleUnitTest.java new file mode 100644 index 0000000000..835efa53f2 --- /dev/null +++ b/core-java-concurrency/src/test/java/com/baeldung/concurrent/countdownlatch/CountdownLatchCountExampleUnitTest.java @@ -0,0 +1,15 @@ +package com.baeldung.concurrent.countdownlatch; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +public class CountdownLatchCountExampleUnitTest { + + @Test + public void whenCountDownLatch_completed() { + CountdownLatchCountExample ex = new CountdownLatchCountExample(2); + boolean isCompleted = ex.callTwiceInSameThread(); + assertTrue(isCompleted); + } +} diff --git a/core-java-concurrency/src/test/java/com/baeldung/concurrent/countdownlatch/CountdownLatchResetExampleUnitTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/countdownlatch/CountdownLatchResetExampleUnitTest.java new file mode 100644 index 0000000000..d2d43f6312 --- /dev/null +++ b/core-java-concurrency/src/test/java/com/baeldung/concurrent/countdownlatch/CountdownLatchResetExampleUnitTest.java @@ -0,0 +1,15 @@ +package com.baeldung.concurrent.countdownlatch; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +public class CountdownLatchResetExampleUnitTest { + + @Test + public void whenCountDownLatch_noReset() { + CountdownLatchResetExample ex = new CountdownLatchResetExample(7,20); + int lineCount = ex.countWaits(); + assertTrue(lineCount <= 7); + } +} diff --git a/core-java-concurrency/src/test/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierCompletionMethodExampleUnitTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierCompletionMethodExampleUnitTest.java new file mode 100644 index 0000000000..310063c86c --- /dev/null +++ b/core-java-concurrency/src/test/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierCompletionMethodExampleUnitTest.java @@ -0,0 +1,15 @@ +package com.baeldung.concurrent.cyclicbarrier; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class CyclicBarrierCompletionMethodExampleUnitTest { + + @Test + public void whenCyclicBarrier_countTrips() { + CyclicBarrierCompletionMethodExample ex = new CyclicBarrierCompletionMethodExample(7,20); + int lineCount = ex.countTrips(); + assertEquals(2, lineCount); + } +} diff --git a/core-java-concurrency/src/test/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierCountExampleUnitTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierCountExampleUnitTest.java new file mode 100644 index 0000000000..9b7f3d9945 --- /dev/null +++ b/core-java-concurrency/src/test/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierCountExampleUnitTest.java @@ -0,0 +1,15 @@ +package com.baeldung.concurrent.cyclicbarrier; + +import static org.junit.Assert.assertFalse; + +import org.junit.Test; + +public class CyclicBarrierCountExampleUnitTest { + + @Test + public void whenCyclicBarrier_notCompleted() { + CyclicBarrierCountExample ex = new CyclicBarrierCountExample(2); + boolean isCompleted = ex.callTwiceInSameThread(); + assertFalse(isCompleted); + } +} diff --git a/core-java-concurrency/src/test/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierResetExampleUnitTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierResetExampleUnitTest.java new file mode 100644 index 0000000000..8d2b148f06 --- /dev/null +++ b/core-java-concurrency/src/test/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierResetExampleUnitTest.java @@ -0,0 +1,15 @@ +package com.baeldung.concurrent.cyclicbarrier; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +public class CyclicBarrierResetExampleUnitTest { + + @Test + public void whenCyclicBarrier_reset() { + CyclicBarrierResetExample ex = new CyclicBarrierResetExample(7,20); + int lineCount = ex.countWaits(); + assertTrue(lineCount > 7); + } +} diff --git a/core-java-lang/pom.xml b/core-java-lang/pom.xml index ace39de274..2f307859f1 100644 --- a/core-java-lang/pom.xml +++ b/core-java-lang/pom.xml @@ -66,6 +66,12 @@ mail ${javax.mail.version} + + nl.jqno.equalsverifier + equalsverifier + ${equalsverifier.version} + test + @@ -424,6 +430,7 @@ 3.1.1 2.0.3.RELEASE 1.6.0 + 3.0.3 diff --git a/core-java/src/main/java/com/baeldung/className/RetrievingClassName.java b/core-java-lang/src/main/java/com/baeldung/className/RetrievingClassName.java similarity index 100% rename from core-java/src/main/java/com/baeldung/className/RetrievingClassName.java rename to core-java-lang/src/main/java/com/baeldung/className/RetrievingClassName.java diff --git a/core-java-lang/src/main/java/com/baeldung/equalshashcode/Money.java b/core-java-lang/src/main/java/com/baeldung/equalshashcode/Money.java new file mode 100644 index 0000000000..60c043545d --- /dev/null +++ b/core-java-lang/src/main/java/com/baeldung/equalshashcode/Money.java @@ -0,0 +1,36 @@ +package com.baeldung.equalshashcode; + +class Money { + + int amount; + String currencyCode; + + Money(int amount, String currencyCode) { + this.amount = amount; + this.currencyCode = currencyCode; + } + + @Override + public boolean equals(Object o) { + if (o == this) + return true; + if (!(o instanceof Money)) + return false; + Money other = (Money)o; + boolean currencyCodeEquals = (this.currencyCode == null && other.currencyCode == null) + || (this.currencyCode != null && this.currencyCode.equals(other.currencyCode)); + return this.amount == other.amount + && currencyCodeEquals; + } + + @Override + public int hashCode() { + int result = 17; + result = 31 * result + amount; + if (currencyCode != null) { + result = 31 * result + currencyCode.hashCode(); + } + return result; + } + +} diff --git a/core-java-lang/src/main/java/com/baeldung/equalshashcode/Team.java b/core-java-lang/src/main/java/com/baeldung/equalshashcode/Team.java new file mode 100644 index 0000000000..c2dee1de6b --- /dev/null +++ b/core-java-lang/src/main/java/com/baeldung/equalshashcode/Team.java @@ -0,0 +1,39 @@ +package com.baeldung.equalshashcode; + +class Team { + + final String city; + final String department; + + Team(String city, String department) { + this.city = city; + this.department = department; + } + + @Override + public final boolean equals(Object o) { + if (o == this) + return true; + if (!(o instanceof Team)) + return false; + Team otherTeam = (Team)o; + boolean cityEquals = (this.city == null && otherTeam.city == null) + || this.city != null && this.city.equals(otherTeam.city); + boolean departmentEquals = (this.department == null && otherTeam.department == null) + || this.department != null && this.department.equals(otherTeam.department); + return cityEquals && departmentEquals; + } + + @Override + public final int hashCode() { + int result = 17; + if (city != null) { + result = 31 * result + city.hashCode(); + } + if (department != null) { + result = 31 * result + department.hashCode(); + } + return result; + } + +} diff --git a/core-java-lang/src/main/java/com/baeldung/equalshashcode/Voucher.java b/core-java-lang/src/main/java/com/baeldung/equalshashcode/Voucher.java new file mode 100644 index 0000000000..19f46e0358 --- /dev/null +++ b/core-java-lang/src/main/java/com/baeldung/equalshashcode/Voucher.java @@ -0,0 +1,38 @@ +package com.baeldung.equalshashcode; + +class Voucher { + + private Money value; + private String store; + + Voucher(int amount, String currencyCode, String store) { + this.value = new Money(amount, currencyCode); + this.store = store; + } + + @Override + public boolean equals(Object o) { + if (o == this) + return true; + if (!(o instanceof Voucher)) + return false; + Voucher other = (Voucher)o; + boolean valueEquals = (this.value == null && other.value == null) + || (this.value != null && this.value.equals(other.value)); + boolean storeEquals = (this.store == null && other.store == null) + || (this.store != null && this.store.equals(other.store)); + return valueEquals && storeEquals; + } + + @Override + public int hashCode() { + int result = 17; + if (this.value != null) { + result = 31 * result + value.hashCode(); + } + if (this.store != null) { + result = 31 * result + store.hashCode(); + } + return result; + } +} \ No newline at end of file diff --git a/core-java-lang/src/main/java/com/baeldung/equalshashcode/WrongTeam.java b/core-java-lang/src/main/java/com/baeldung/equalshashcode/WrongTeam.java new file mode 100644 index 0000000000..c4477aa790 --- /dev/null +++ b/core-java-lang/src/main/java/com/baeldung/equalshashcode/WrongTeam.java @@ -0,0 +1,30 @@ +package com.baeldung.equalshashcode; + +/* (non-Javadoc) +* This class overrides equals, but it doesn't override hashCode. +* +* To see which problems this leads to: +* TeamUnitTest.givenMapKeyWithoutHashCode_whenSearched_thenReturnsWrongValue +*/ +class WrongTeam { + + String city; + String department; + + WrongTeam(String city, String department) { + this.city = city; + this.department = department; + } + + @Override + public boolean equals(Object o) { + if (o == this) + return true; + if (!(o instanceof WrongTeam)) + return false; + WrongTeam otherTeam = (WrongTeam)o; + return this.city == otherTeam.city + && this.department == otherTeam.department; + } + +} diff --git a/core-java-lang/src/main/java/com/baeldung/equalshashcode/WrongVoucher.java b/core-java-lang/src/main/java/com/baeldung/equalshashcode/WrongVoucher.java new file mode 100644 index 0000000000..97935bf8de --- /dev/null +++ b/core-java-lang/src/main/java/com/baeldung/equalshashcode/WrongVoucher.java @@ -0,0 +1,47 @@ +package com.baeldung.equalshashcode; + +/* (non-Javadoc) +* This class extends the Money class that has overridden the equals method and once again overrides the equals method. +* +* To see which problems this leads to: +* MoneyUnitTest.givenMoneyAndVoucherInstances_whenEquals_thenReturnValuesArentSymmetric +*/ +class WrongVoucher extends Money { + + private String store; + + WrongVoucher(int amount, String currencyCode, String store) { + super(amount, currencyCode); + + this.store = store; + } + + @Override + public boolean equals(Object o) { + if (o == this) + return true; + if (!(o instanceof WrongVoucher)) + return false; + WrongVoucher other = (WrongVoucher)o; + boolean currencyCodeEquals = (this.currencyCode == null && other.currencyCode == null) + || (this.currencyCode != null && this.currencyCode.equals(other.currencyCode)); + boolean storeEquals = (this.store == null && other.store == null) + || (this.store != null && this.store.equals(other.store)); + return this.amount == other.amount + && currencyCodeEquals + && storeEquals; + } + + @Override + public int hashCode() { + int result = 17; + result = 31 * result + amount; + if (this.currencyCode != null) { + result = 31 * result + currencyCode.hashCode(); + } + if (this.store != null) { + result = 31 * result + store.hashCode(); + } + return result; + } +} \ No newline at end of file diff --git a/core-java/src/test/java/com/baeldung/className/RetrievingClassNameUnitTest.java b/core-java-lang/src/test/java/com/baeldung/className/RetrievingClassNameUnitTest.java similarity index 100% rename from core-java/src/test/java/com/baeldung/className/RetrievingClassNameUnitTest.java rename to core-java-lang/src/test/java/com/baeldung/className/RetrievingClassNameUnitTest.java diff --git a/core-java-lang/src/test/java/com/baeldung/equalshashcode/MoneyUnitTest.java b/core-java-lang/src/test/java/com/baeldung/equalshashcode/MoneyUnitTest.java new file mode 100644 index 0000000000..60584fdb53 --- /dev/null +++ b/core-java-lang/src/test/java/com/baeldung/equalshashcode/MoneyUnitTest.java @@ -0,0 +1,27 @@ +package com.baeldung.equalshashcode; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; + +import org.junit.Test; + +public class MoneyUnitTest { + + @Test + public void givenMoneyInstancesWithSameAmountAndCurrency_whenEquals_thenReturnsTrue() { + Money income = new Money(55, "USD"); + Money expenses = new Money(55, "USD"); + + assertTrue(income.equals(expenses)); + } + + @Test + public void givenMoneyAndVoucherInstances_whenEquals_thenReturnValuesArentSymmetric() { + Money cash = new Money(42, "USD"); + WrongVoucher voucher = new WrongVoucher(42, "USD", "Amazon"); + + assertFalse(voucher.equals(cash)); + assertTrue(cash.equals(voucher)); + } + +} diff --git a/core-java-lang/src/test/java/com/baeldung/equalshashcode/TeamUnitTest.java b/core-java-lang/src/test/java/com/baeldung/equalshashcode/TeamUnitTest.java new file mode 100644 index 0000000000..a2de408796 --- /dev/null +++ b/core-java-lang/src/test/java/com/baeldung/equalshashcode/TeamUnitTest.java @@ -0,0 +1,46 @@ +package com.baeldung.equalshashcode; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Test; + +import nl.jqno.equalsverifier.EqualsVerifier; + +public class TeamUnitTest { + + @Test + public void givenMapKeyWithHashCode_whenSearched_thenReturnsCorrectValue() { + Map leaders = new HashMap<>(); + leaders.put(new Team("New York", "development"), "Anne"); + leaders.put(new Team("Boston", "development"), "Brian"); + leaders.put(new Team("Boston", "marketing"), "Charlie"); + + Team myTeam = new Team("New York", "development"); + String myTeamleader = leaders.get(myTeam); + + assertEquals("Anne", myTeamleader); + } + + @Test + public void givenMapKeyWithoutHashCode_whenSearched_thenReturnsWrongValue() { + Map leaders = new HashMap<>(); + leaders.put(new WrongTeam("New York", "development"), "Anne"); + leaders.put(new WrongTeam("Boston", "development"), "Brian"); + leaders.put(new WrongTeam("Boston", "marketing"), "Charlie"); + + WrongTeam myTeam = new WrongTeam("New York", "development"); + String myTeamleader = leaders.get(myTeam); + + assertFalse("Anne".equals(myTeamleader)); + } + + @Test + public void equalsContract() { + EqualsVerifier.forClass(Team.class).verify(); + } + +} diff --git a/core-java/src/main/java/com/baeldung/graph/Graph.java b/core-java/src/main/java/com/baeldung/graph/Graph.java new file mode 100644 index 0000000000..43b5c0aa08 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/graph/Graph.java @@ -0,0 +1,76 @@ +package com.baeldung.graph; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public class Graph { + private Map> adjVertices; + + Graph() { + this.adjVertices = new HashMap>(); + } + + void addVertex(String label) { + adjVertices.putIfAbsent(new Vertex(label), new ArrayList<>()); + } + + void removeVertex(String label) { + Vertex v = new Vertex(label); + adjVertices.values().stream().map(e -> e.remove(v)).collect(Collectors.toList()); + adjVertices.remove(new Vertex(label)); + } + + void addEdge(String label1, String label2) { + Vertex v1 = new Vertex(label1); + Vertex v2 = new Vertex(label2); + adjVertices.get(v1).add(v2); + adjVertices.get(v2).add(v1); + } + + void removeEdge(String label1, String label2) { + Vertex v1 = new Vertex(label1); + Vertex v2 = new Vertex(label2); + List eV1 = adjVertices.get(v1); + List eV2 = adjVertices.get(v2); + if (eV1 != null) + eV1.remove(v2); + if (eV2 != null) + eV2.remove(v1); + } + + List getAdjVertices(String label) { + return adjVertices.get(new Vertex(label)); + } + + String printGraph() { + StringBuffer sb = new StringBuffer(); + for(Vertex v : adjVertices.keySet()) { + sb.append(v); + sb.append(adjVertices.get(v)); + } + return sb.toString(); + } + + class Vertex { + String label; + Vertex(String label) { + this.label = label; + } + @Override + public boolean equals(Object obj) { + Vertex vertex = (Vertex) obj; + return vertex.label == label; + } + @Override + public int hashCode() { + return label.hashCode(); + } + @Override + public String toString() { + return label; + } + } +} \ No newline at end of file diff --git a/core-java/src/main/java/com/baeldung/graph/GraphTraversal.java b/core-java/src/main/java/com/baeldung/graph/GraphTraversal.java new file mode 100644 index 0000000000..479e653a5c --- /dev/null +++ b/core-java/src/main/java/com/baeldung/graph/GraphTraversal.java @@ -0,0 +1,44 @@ +package com.baeldung.graph; + +import java.util.LinkedHashSet; +import java.util.LinkedList; +import java.util.Queue; +import java.util.Set; +import java.util.Stack; + +import com.baeldung.graph.Graph.Vertex; + +public class GraphTraversal { + static Set depthFirstTraversal(Graph graph, String root) { + Set visited = new LinkedHashSet(); + Stack stack = new Stack(); + stack.push(root); + while (!stack.isEmpty()) { + String vertex = stack.pop(); + if (!visited.contains(vertex)) { + visited.add(vertex); + for (Vertex v : graph.getAdjVertices(vertex)) { + stack.push(v.label); + } + } + } + return visited; + } + + static Set breadthFirstTraversal(Graph graph, String root) { + Set visited = new LinkedHashSet(); + Queue queue = new LinkedList(); + queue.add(root); + visited.add(root); + while (!queue.isEmpty()) { + String vertex = queue.poll(); + for (Vertex v : graph.getAdjVertices(vertex)) { + if (!visited.contains(v.label)) { + visited.add(v.label); + queue.add(v.label); + } + } + } + return visited; + } +} \ No newline at end of file diff --git a/core-java/src/test/java/com/baeldung/graph/GraphTraversalUnitTest.java b/core-java/src/test/java/com/baeldung/graph/GraphTraversalUnitTest.java new file mode 100644 index 0000000000..d955d56d95 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/graph/GraphTraversalUnitTest.java @@ -0,0 +1,36 @@ +package com.baeldung.graph; + +import org.junit.Assert; +import org.junit.Test; + +public class GraphTraversalUnitTest { + @Test + public void givenAGraph_whenTraversingDepthFirst_thenExpectedResult() { + Graph graph = createGraph(); + Assert.assertEquals("[Bob, Rob, Maria, Alice, Mark]", + GraphTraversal.depthFirstTraversal(graph, "Bob").toString()); + } + + @Test + public void givenAGraph_whenTraversingBreadthFirst_thenExpectedResult() { + Graph graph = createGraph(); + Assert.assertEquals("[Bob, Alice, Rob, Mark, Maria]", + GraphTraversal.breadthFirstTraversal(graph, "Bob").toString()); + } + + Graph createGraph() { + Graph graph = new Graph(); + graph.addVertex("Bob"); + graph.addVertex("Alice"); + graph.addVertex("Mark"); + graph.addVertex("Rob"); + graph.addVertex("Maria"); + graph.addEdge("Bob", "Alice"); + graph.addEdge("Bob", "Rob"); + graph.addEdge("Alice", "Mark"); + graph.addEdge("Rob", "Mark"); + graph.addEdge("Alice", "Maria"); + graph.addEdge("Rob", "Maria"); + return graph; + } +} \ No newline at end of file diff --git a/core-java/src/test/java/com/baeldung/regexp/optmization/OptimizedMatcherUnitTest.java b/core-java/src/test/java/com/baeldung/regexp/optmization/OptimizedMatcherManualTest.java similarity index 98% rename from core-java/src/test/java/com/baeldung/regexp/optmization/OptimizedMatcherUnitTest.java rename to core-java/src/test/java/com/baeldung/regexp/optmization/OptimizedMatcherManualTest.java index 2be6b6ad4b..f44968a5a7 100644 --- a/core-java/src/test/java/com/baeldung/regexp/optmization/OptimizedMatcherUnitTest.java +++ b/core-java/src/test/java/com/baeldung/regexp/optmization/OptimizedMatcherManualTest.java @@ -11,7 +11,7 @@ import java.util.regex.Pattern; import static org.junit.Assert.assertTrue; -public class OptimizedMatcherUnitTest { +public class OptimizedMatcherManualTest { private String action; diff --git a/java-collections-conversions/src/test/java/org/baeldung/convertarraytostring/ArrayToStringUnitTest.java b/java-collections-conversions/src/test/java/org/baeldung/convertarraytostring/ArrayToStringUnitTest.java new file mode 100644 index 0000000000..b563475997 --- /dev/null +++ b/java-collections-conversions/src/test/java/org/baeldung/convertarraytostring/ArrayToStringUnitTest.java @@ -0,0 +1,136 @@ +package org.baeldung.convertarraytostring; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +import org.apache.commons.lang3.StringUtils; +import org.junit.Test; + +import com.google.common.base.Joiner; +import com.google.common.base.Splitter; + +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertEquals; + +public class ArrayToStringUnitTest { + + // convert with Java + + @Test + public void givenAStringArray_whenConvertBeforeJava8_thenReturnString() { + + String[] strArray = { "Convert", "Array", "With", "Java" }; + StringBuilder stringBuilder = new StringBuilder(); + + for (int i = 0; i < strArray.length; i++) { + stringBuilder.append(strArray[i]); + } + String joinedString = stringBuilder.toString(); + + assertThat(joinedString, instanceOf(String.class)); + assertEquals("ConvertArrayWithJava", joinedString); + } + + @Test + public void givenAString_whenConvertBeforeJava8_thenReturnStringArray() { + + String input = "lorem ipsum dolor sit amet"; + String[] strArray = input.split(" "); + + assertThat(strArray, instanceOf(String[].class)); + assertEquals(5, strArray.length); + + input = "loremipsum"; + strArray = input.split(""); + assertThat(strArray, instanceOf(String[].class)); + assertEquals(10, strArray.length); + } + + @Test + public void givenAnIntArray_whenConvertBeforeJava8_thenReturnString() { + + int[] strArray = { 1, 2, 3, 4, 5 }; + StringBuilder stringBuilder = new StringBuilder(); + + for (int i = 0; i < strArray.length; i++) { + stringBuilder.append(Integer.valueOf(strArray[i])); + } + String joinedString = stringBuilder.toString(); + + assertThat(joinedString, instanceOf(String.class)); + assertEquals("12345", joinedString); + } + + // convert with Java Stream API + + @Test + public void givenAStringArray_whenConvertWithJavaStream_thenReturnString() { + + String[] strArray = { "Convert", "With", "Java", "Streams" }; + String joinedString = Arrays.stream(strArray) + .collect(Collectors.joining()); + assertThat(joinedString, instanceOf(String.class)); + assertEquals("ConvertWithJavaStreams", joinedString); + + joinedString = Arrays.stream(strArray) + .collect(Collectors.joining(",")); + assertThat(joinedString, instanceOf(String.class)); + assertEquals("Convert,With,Java,Streams", joinedString); + } + + + // convert with Apache Commons + + @Test + public void givenAStringArray_whenConvertWithApacheCommons_thenReturnString() { + + String[] strArray = { "Convert", "With", "Apache", "Commons" }; + String joinedString = StringUtils.join(strArray); + + assertThat(joinedString, instanceOf(String.class)); + assertEquals("ConvertWithApacheCommons", joinedString); + } + + @Test + public void givenAString_whenConvertWithApacheCommons_thenReturnStringArray() { + + String input = "lorem ipsum dolor sit amet"; + String[] strArray = StringUtils.split(input, " "); + + assertThat(strArray, instanceOf(String[].class)); + assertEquals(5, strArray.length); + } + + + // convert with Guava + + @Test + public void givenAStringArray_whenConvertWithGuava_thenReturnString() { + + String[] strArray = { "Convert", "With", "Guava", null }; + String joinedString = Joiner.on("") + .skipNulls() + .join(strArray); + + assertThat(joinedString, instanceOf(String.class)); + assertEquals("ConvertWithGuava", joinedString); + } + + + @Test + public void givenAString_whenConvertWithGuava_thenReturnStringArray() { + + String input = "lorem ipsum dolor sit amet"; + + List resultList = Splitter.on(' ') + .trimResults() + .omitEmptyStrings() + .splitToList(input); + String[] strArray = resultList.toArray(new String[0]); + + assertThat(strArray, instanceOf(String[].class)); + assertEquals(5, strArray.length); + } +} diff --git a/libraries-data/src/main/kafka-connect/01_Quick_Start/connect-file-sink.properties b/libraries-data/src/main/kafka-connect/01_Quick_Start/connect-file-sink.properties new file mode 100644 index 0000000000..594ccc6e95 --- /dev/null +++ b/libraries-data/src/main/kafka-connect/01_Quick_Start/connect-file-sink.properties @@ -0,0 +1,20 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +name=local-file-sink +connector.class=FileStreamSink +tasks.max=1 +file=test.sink.txt +topics=connect-test \ No newline at end of file diff --git a/libraries-data/src/main/kafka-connect/01_Quick_Start/connect-file-source.properties b/libraries-data/src/main/kafka-connect/01_Quick_Start/connect-file-source.properties new file mode 100644 index 0000000000..599cf4cb2a --- /dev/null +++ b/libraries-data/src/main/kafka-connect/01_Quick_Start/connect-file-source.properties @@ -0,0 +1,20 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +name=local-file-source +connector.class=FileStreamSource +tasks.max=1 +file=test.txt +topic=connect-test \ No newline at end of file diff --git a/libraries-data/src/main/kafka-connect/01_Quick_Start/connect-standalone.properties b/libraries-data/src/main/kafka-connect/01_Quick_Start/connect-standalone.properties new file mode 100644 index 0000000000..a2369fa144 --- /dev/null +++ b/libraries-data/src/main/kafka-connect/01_Quick_Start/connect-standalone.properties @@ -0,0 +1,44 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# These are defaults. This file just demonstrates how to override some settings. +bootstrap.servers=localhost:9092 + +# The converters specify the format of data in Kafka and how to translate it into Connect data. Every Connect user will +# need to configure these based on the format they want their data in when loaded from or stored into Kafka +key.converter=org.apache.kafka.connect.json.JsonConverter +value.converter=org.apache.kafka.connect.json.JsonConverter +# Converter-specific settings can be passed in by prefixing the Converter's setting with the converter we want to apply +# it to +key.converter.schemas.enable=false +value.converter.schemas.enable=false + +offset.storage.file.filename=/tmp/connect.offsets +# Flush much faster than normal, which is useful for testing/debugging +offset.flush.interval.ms=10000 + +# Set to a list of filesystem paths separated by commas (,) to enable class loading isolation for plugins +# (connectors, converters, transformations). The list should consist of top level directories that include +# any combination of: +# a) directories immediately containing jars with plugins and their dependencies +# b) uber-jars with plugins and their dependencies +# c) directories immediately containing the package directory structure of classes of plugins and their dependencies +# Note: symlinks will be followed to discover dependencies or plugins. +# Examples: +# plugin.path=/usr/local/share/java,/usr/local/share/kafka/plugins,/opt/connectors, +# Replace the relative path below with an absolute path if you are planning to start Kafka Connect from within a +# directory other than the home directory of Confluent Platform. +plugin.path=C:\Software\confluent-5.0.0\share\java +#plugin.path=./share/java diff --git a/libraries-data/src/main/kafka-connect/02_Distributed/connect-distributed.properties b/libraries-data/src/main/kafka-connect/02_Distributed/connect-distributed.properties new file mode 100644 index 0000000000..5b91baddbd --- /dev/null +++ b/libraries-data/src/main/kafka-connect/02_Distributed/connect-distributed.properties @@ -0,0 +1,88 @@ +## +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +## + +# This file contains some of the configurations for the Kafka Connect distributed worker. This file is intended +# to be used with the examples, and some settings may differ from those used in a production system, especially +# the `bootstrap.servers` and those specifying replication factors. + +# A list of host/port pairs to use for establishing the initial connection to the Kafka cluster. +bootstrap.servers=localhost:9092 + +# unique name for the cluster, used in forming the Connect cluster group. Note that this must not conflict with consumer group IDs +group.id=connect-cluster + +# The converters specify the format of data in Kafka and how to translate it into Connect data. Every Connect user will +# need to configure these based on the format they want their data in when loaded from or stored into Kafka +key.converter=org.apache.kafka.connect.json.JsonConverter +value.converter=org.apache.kafka.connect.json.JsonConverter +# Converter-specific settings can be passed in by prefixing the Converter's setting with the converter we want to apply +# it to +key.converter.schemas.enable=true +value.converter.schemas.enable=true + +# Topic to use for storing offsets. This topic should have many partitions and be replicated and compacted. +# Kafka Connect will attempt to create the topic automatically when needed, but you can always manually create +# the topic before starting Kafka Connect if a specific topic configuration is needed. +# Most users will want to use the built-in default replication factor of 3 or in some cases even specify a larger value. +# Since this means there must be at least as many brokers as the maximum replication factor used, we'd like to be able +# to run this example on a single-broker cluster and so here we instead set the replication factor to 1. +offset.storage.topic=connect-offsets +offset.storage.replication.factor=1 +#offset.storage.partitions=25 + +# Topic to use for storing connector and task configurations; note that this should be a single partition, highly replicated, +# and compacted topic. Kafka Connect will attempt to create the topic automatically when needed, but you can always manually create +# the topic before starting Kafka Connect if a specific topic configuration is needed. +# Most users will want to use the built-in default replication factor of 3 or in some cases even specify a larger value. +# Since this means there must be at least as many brokers as the maximum replication factor used, we'd like to be able +# to run this example on a single-broker cluster and so here we instead set the replication factor to 1. +config.storage.topic=connect-configs +config.storage.replication.factor=1 + +# Topic to use for storing statuses. This topic can have multiple partitions and should be replicated and compacted. +# Kafka Connect will attempt to create the topic automatically when needed, but you can always manually create +# the topic before starting Kafka Connect if a specific topic configuration is needed. +# Most users will want to use the built-in default replication factor of 3 or in some cases even specify a larger value. +# Since this means there must be at least as many brokers as the maximum replication factor used, we'd like to be able +# to run this example on a single-broker cluster and so here we instead set the replication factor to 1. +status.storage.topic=connect-status +status.storage.replication.factor=1 +#status.storage.partitions=5 + +# Flush much faster than normal, which is useful for testing/debugging +offset.flush.interval.ms=10000 + +# These are provided to inform the user about the presence of the REST host and port configs +# Hostname & Port for the REST API to listen on. If this is set, it will bind to the interface used to listen to requests. +#rest.host.name= +#rest.port=8083 + +# The Hostname & Port that will be given out to other workers to connect to i.e. URLs that are routable from other servers. +#rest.advertised.host.name= +#rest.advertised.port= + +# Set to a list of filesystem paths separated by commas (,) to enable class loading isolation for plugins +# (connectors, converters, transformations). The list should consist of top level directories that include +# any combination of: +# a) directories immediately containing jars with plugins and their dependencies +# b) uber-jars with plugins and their dependencies +# c) directories immediately containing the package directory structure of classes of plugins and their dependencies +# Examples: +# plugin.path=/usr/local/share/java,/usr/local/share/kafka/plugins,/opt/connectors, +# Replace the relative path below with an absolute path if you are planning to start Kafka Connect from within a +# directory other than the home directory of Confluent Platform. +plugin.path=./share/java diff --git a/libraries-data/src/main/kafka-connect/02_Distributed/connect-file-sink.json b/libraries-data/src/main/kafka-connect/02_Distributed/connect-file-sink.json new file mode 100644 index 0000000000..8902ecce52 --- /dev/null +++ b/libraries-data/src/main/kafka-connect/02_Distributed/connect-file-sink.json @@ -0,0 +1,9 @@ +{ + "name": "local-file-sink", + "config": { + "connector.class": "FileStreamSink", + "tasks.max": 1, + "file": "test-distributed.sink.txt", + "topics": "connect-distributed" + } +} diff --git a/libraries-data/src/main/kafka-connect/02_Distributed/connect-file-source.json b/libraries-data/src/main/kafka-connect/02_Distributed/connect-file-source.json new file mode 100644 index 0000000000..77e949c91b --- /dev/null +++ b/libraries-data/src/main/kafka-connect/02_Distributed/connect-file-source.json @@ -0,0 +1,9 @@ +{ + "name": "local-file-source", + "config": { + "connector.class": "FileStreamSource", + "tasks.max": 1, + "file": "test-distributed.txt", + "topic": "connect-distributed" + } +} diff --git a/libraries-data/src/main/kafka-connect/03_Transform/connect-distributed.properties b/libraries-data/src/main/kafka-connect/03_Transform/connect-distributed.properties new file mode 100644 index 0000000000..fa63be24b8 --- /dev/null +++ b/libraries-data/src/main/kafka-connect/03_Transform/connect-distributed.properties @@ -0,0 +1,88 @@ +## +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +## + +# This file contains some of the configurations for the Kafka Connect distributed worker. This file is intended +# to be used with the examples, and some settings may differ from those used in a production system, especially +# the `bootstrap.servers` and those specifying replication factors. + +# A list of host/port pairs to use for establishing the initial connection to the Kafka cluster. +bootstrap.servers=localhost:9092 + +# unique name for the cluster, used in forming the Connect cluster group. Note that this must not conflict with consumer group IDs +group.id=connect-cluster + +# The converters specify the format of data in Kafka and how to translate it into Connect data. Every Connect user will +# need to configure these based on the format they want their data in when loaded from or stored into Kafka +key.converter=org.apache.kafka.connect.json.JsonConverter +value.converter=org.apache.kafka.connect.json.JsonConverter +# Converter-specific settings can be passed in by prefixing the Converter's setting with the converter we want to apply +# it to +key.converter.schemas.enable=false +value.converter.schemas.enable=false + +# Topic to use for storing offsets. This topic should have many partitions and be replicated and compacted. +# Kafka Connect will attempt to create the topic automatically when needed, but you can always manually create +# the topic before starting Kafka Connect if a specific topic configuration is needed. +# Most users will want to use the built-in default replication factor of 3 or in some cases even specify a larger value. +# Since this means there must be at least as many brokers as the maximum replication factor used, we'd like to be able +# to run this example on a single-broker cluster and so here we instead set the replication factor to 1. +offset.storage.topic=connect-offsets +offset.storage.replication.factor=1 +#offset.storage.partitions=25 + +# Topic to use for storing connector and task configurations; note that this should be a single partition, highly replicated, +# and compacted topic. Kafka Connect will attempt to create the topic automatically when needed, but you can always manually create +# the topic before starting Kafka Connect if a specific topic configuration is needed. +# Most users will want to use the built-in default replication factor of 3 or in some cases even specify a larger value. +# Since this means there must be at least as many brokers as the maximum replication factor used, we'd like to be able +# to run this example on a single-broker cluster and so here we instead set the replication factor to 1. +config.storage.topic=connect-configs +config.storage.replication.factor=1 + +# Topic to use for storing statuses. This topic can have multiple partitions and should be replicated and compacted. +# Kafka Connect will attempt to create the topic automatically when needed, but you can always manually create +# the topic before starting Kafka Connect if a specific topic configuration is needed. +# Most users will want to use the built-in default replication factor of 3 or in some cases even specify a larger value. +# Since this means there must be at least as many brokers as the maximum replication factor used, we'd like to be able +# to run this example on a single-broker cluster and so here we instead set the replication factor to 1. +status.storage.topic=connect-status +status.storage.replication.factor=1 +#status.storage.partitions=5 + +# Flush much faster than normal, which is useful for testing/debugging +offset.flush.interval.ms=10000 + +# These are provided to inform the user about the presence of the REST host and port configs +# Hostname & Port for the REST API to listen on. If this is set, it will bind to the interface used to listen to requests. +#rest.host.name= +#rest.port=8083 + +# The Hostname & Port that will be given out to other workers to connect to i.e. URLs that are routable from other servers. +#rest.advertised.host.name= +#rest.advertised.port= + +# Set to a list of filesystem paths separated by commas (,) to enable class loading isolation for plugins +# (connectors, converters, transformations). The list should consist of top level directories that include +# any combination of: +# a) directories immediately containing jars with plugins and their dependencies +# b) uber-jars with plugins and their dependencies +# c) directories immediately containing the package directory structure of classes of plugins and their dependencies +# Examples: +# plugin.path=/usr/local/share/java,/usr/local/share/kafka/plugins,/opt/connectors, +# Replace the relative path below with an absolute path if you are planning to start Kafka Connect from within a +# directory other than the home directory of Confluent Platform. +plugin.path=./share/java diff --git a/libraries-data/src/main/kafka-connect/03_Transform/connect-file-source-transform.json b/libraries-data/src/main/kafka-connect/03_Transform/connect-file-source-transform.json new file mode 100644 index 0000000000..e5e21a0608 --- /dev/null +++ b/libraries-data/src/main/kafka-connect/03_Transform/connect-file-source-transform.json @@ -0,0 +1,15 @@ +{ + "name": "local-file-source", + "config": { + "connector.class": "FileStreamSource", + "tasks.max": 1, + "file": "transformation.txt", + "topic": "connect-transformation", + "transforms": "MakeMap,InsertSource", + "transforms.MakeMap.type": "org.apache.kafka.connect.transforms.HoistField$Value", + "transforms.MakeMap.field": "line", + "transforms.InsertSource.type": "org.apache.kafka.connect.transforms.InsertField$Value", + "transforms.InsertSource.static.field": "data_source", + "transforms.InsertSource.static.value": "test-file-source" + } +} diff --git a/libraries-data/src/main/kafka-connect/04_Custom/connect-distributed.properties b/libraries-data/src/main/kafka-connect/04_Custom/connect-distributed.properties new file mode 100644 index 0000000000..5b91baddbd --- /dev/null +++ b/libraries-data/src/main/kafka-connect/04_Custom/connect-distributed.properties @@ -0,0 +1,88 @@ +## +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +## + +# This file contains some of the configurations for the Kafka Connect distributed worker. This file is intended +# to be used with the examples, and some settings may differ from those used in a production system, especially +# the `bootstrap.servers` and those specifying replication factors. + +# A list of host/port pairs to use for establishing the initial connection to the Kafka cluster. +bootstrap.servers=localhost:9092 + +# unique name for the cluster, used in forming the Connect cluster group. Note that this must not conflict with consumer group IDs +group.id=connect-cluster + +# The converters specify the format of data in Kafka and how to translate it into Connect data. Every Connect user will +# need to configure these based on the format they want their data in when loaded from or stored into Kafka +key.converter=org.apache.kafka.connect.json.JsonConverter +value.converter=org.apache.kafka.connect.json.JsonConverter +# Converter-specific settings can be passed in by prefixing the Converter's setting with the converter we want to apply +# it to +key.converter.schemas.enable=true +value.converter.schemas.enable=true + +# Topic to use for storing offsets. This topic should have many partitions and be replicated and compacted. +# Kafka Connect will attempt to create the topic automatically when needed, but you can always manually create +# the topic before starting Kafka Connect if a specific topic configuration is needed. +# Most users will want to use the built-in default replication factor of 3 or in some cases even specify a larger value. +# Since this means there must be at least as many brokers as the maximum replication factor used, we'd like to be able +# to run this example on a single-broker cluster and so here we instead set the replication factor to 1. +offset.storage.topic=connect-offsets +offset.storage.replication.factor=1 +#offset.storage.partitions=25 + +# Topic to use for storing connector and task configurations; note that this should be a single partition, highly replicated, +# and compacted topic. Kafka Connect will attempt to create the topic automatically when needed, but you can always manually create +# the topic before starting Kafka Connect if a specific topic configuration is needed. +# Most users will want to use the built-in default replication factor of 3 or in some cases even specify a larger value. +# Since this means there must be at least as many brokers as the maximum replication factor used, we'd like to be able +# to run this example on a single-broker cluster and so here we instead set the replication factor to 1. +config.storage.topic=connect-configs +config.storage.replication.factor=1 + +# Topic to use for storing statuses. This topic can have multiple partitions and should be replicated and compacted. +# Kafka Connect will attempt to create the topic automatically when needed, but you can always manually create +# the topic before starting Kafka Connect if a specific topic configuration is needed. +# Most users will want to use the built-in default replication factor of 3 or in some cases even specify a larger value. +# Since this means there must be at least as many brokers as the maximum replication factor used, we'd like to be able +# to run this example on a single-broker cluster and so here we instead set the replication factor to 1. +status.storage.topic=connect-status +status.storage.replication.factor=1 +#status.storage.partitions=5 + +# Flush much faster than normal, which is useful for testing/debugging +offset.flush.interval.ms=10000 + +# These are provided to inform the user about the presence of the REST host and port configs +# Hostname & Port for the REST API to listen on. If this is set, it will bind to the interface used to listen to requests. +#rest.host.name= +#rest.port=8083 + +# The Hostname & Port that will be given out to other workers to connect to i.e. URLs that are routable from other servers. +#rest.advertised.host.name= +#rest.advertised.port= + +# Set to a list of filesystem paths separated by commas (,) to enable class loading isolation for plugins +# (connectors, converters, transformations). The list should consist of top level directories that include +# any combination of: +# a) directories immediately containing jars with plugins and their dependencies +# b) uber-jars with plugins and their dependencies +# c) directories immediately containing the package directory structure of classes of plugins and their dependencies +# Examples: +# plugin.path=/usr/local/share/java,/usr/local/share/kafka/plugins,/opt/connectors, +# Replace the relative path below with an absolute path if you are planning to start Kafka Connect from within a +# directory other than the home directory of Confluent Platform. +plugin.path=./share/java diff --git a/libraries-data/src/main/kafka-connect/04_Custom/connect-mongodb-sink.json b/libraries-data/src/main/kafka-connect/04_Custom/connect-mongodb-sink.json new file mode 100644 index 0000000000..333768e4b7 --- /dev/null +++ b/libraries-data/src/main/kafka-connect/04_Custom/connect-mongodb-sink.json @@ -0,0 +1,22 @@ +{ + "firstName": "John", + "lastName": "Smith", + "age": 25, + "address": { + "streetAddress": "21 2nd Street", + "city": "New York", + "state": "NY", + "postalCode": "10021" + }, + "phoneNumber": [{ + "type": "home", + "number": "212 555-1234" + }, { + "type": "fax", + "number": "646 555-4567" + } + ], + "gender": { + "type": "male" + } +} diff --git a/libraries-data/src/main/kafka-connect/04_Custom/connect-mqtt-source.json b/libraries-data/src/main/kafka-connect/04_Custom/connect-mqtt-source.json new file mode 100644 index 0000000000..02d87c5ad7 --- /dev/null +++ b/libraries-data/src/main/kafka-connect/04_Custom/connect-mqtt-source.json @@ -0,0 +1,11 @@ +{ + "name": "mqtt-source", + "config": { + "connector.class": "io.confluent.connect.mqtt.MqttSourceConnector", + "tasks.max": 1, + "mqtt.server.uri": "ws://broker.hivemq.com:8000/mqtt", + "mqtt.topics": "baeldung", + "kafka.topic": "connect-custom", + "value.converter": "org.apache.kafka.connect.converters.ByteArrayConverter" + } +} diff --git a/logging-modules/log4j/pom.xml b/logging-modules/log4j/pom.xml index 1b27e03445..c5ce1d06ad 100644 --- a/logging-modules/log4j/pom.xml +++ b/logging-modules/log4j/pom.xml @@ -44,12 +44,6 @@ disruptor ${disruptor.version} - - - org.springframework.boot - spring-boot-starter-webflux - ${spring-boot.version} - @@ -57,7 +51,6 @@ 2.7 2.7 3.3.6 - 2.1.0.RELEASE \ No newline at end of file diff --git a/persistence-modules/hibernate5/src/main/java/com/baeldung/hibernate/findall/FindAll.java b/persistence-modules/hibernate5/src/main/java/com/baeldung/hibernate/findall/FindAll.java new file mode 100644 index 0000000000..cc0c234df0 --- /dev/null +++ b/persistence-modules/hibernate5/src/main/java/com/baeldung/hibernate/findall/FindAll.java @@ -0,0 +1,35 @@ +package com.baeldung.hibernate.findall; + +import java.util.List; + +import javax.persistence.TypedQuery; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Root; + +import org.hibernate.Session; + +import com.baeldung.hibernate.pojo.Student; + +public class FindAll { + + private Session session; + + public FindAll(Session session) { + super(); + this.session = session; + } + + public List findAllWithJpql() { + return session.createQuery("SELECT a FROM Student a", Student.class).getResultList(); + } + + public List findAllWithCriteriaQuery() { + CriteriaBuilder cb = session.getCriteriaBuilder(); + CriteriaQuery cq = cb.createQuery(Student.class); + Root rootEntry = cq.from(Student.class); + CriteriaQuery all = cq.select(rootEntry); + TypedQuery allQuery = session.createQuery(all); + return allQuery.getResultList(); + } +} diff --git a/persistence-modules/hibernate5/src/test/java/com/baeldung/hibernate/findall/FindAllUnitTest.java b/persistence-modules/hibernate5/src/test/java/com/baeldung/hibernate/findall/FindAllUnitTest.java new file mode 100644 index 0000000000..8a1b9e9791 --- /dev/null +++ b/persistence-modules/hibernate5/src/test/java/com/baeldung/hibernate/findall/FindAllUnitTest.java @@ -0,0 +1,63 @@ +package com.baeldung.hibernate.findall; + +import static org.junit.Assert.assertEquals; + +import java.io.IOException; +import java.util.List; + +import org.hibernate.Session; +import org.hibernate.Transaction; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.baeldung.hibernate.HibernateUtil; +import com.baeldung.hibernate.pojo.Student; + +public class FindAllUnitTest { + + private Session session; + private Transaction transaction; + + private FindAll findAll; + + @Before + public void setUp() throws IOException { + + session = HibernateUtil.getSessionFactory().openSession(); + transaction = session.beginTransaction(); + findAll = new FindAll(session); + + session.createNativeQuery("delete from Student").executeUpdate(); + + Student student1 = new Student(); + session.persist(student1); + + Student student2 = new Student(); + session.persist(student2); + + Student student3 = new Student(); + session.persist(student3); + + transaction.commit(); + transaction = session.beginTransaction(); + } + + @After + public void tearDown() { + transaction.rollback(); + session.close(); + } + + @Test + public void givenCriteriaQuery_WhenFindAll_ThenGetAllPersons() { + List list = findAll.findAllWithCriteriaQuery(); + assertEquals(3, list.size()); + } + + @Test + public void givenJpql_WhenFindAll_ThenGetAllPersons() { + List list = findAll.findAllWithJpql(); + assertEquals(3, list.size()); + } +} diff --git a/persistence-modules/spring-data-cassandra-reactive/pom.xml b/persistence-modules/spring-data-cassandra-reactive/pom.xml new file mode 100644 index 0000000000..037b1fd3c1 --- /dev/null +++ b/persistence-modules/spring-data-cassandra-reactive/pom.xml @@ -0,0 +1,70 @@ + + + 4.0.0 + + com.baeldung + spring-data-cassandra-reactive + 0.0.1-SNAPSHOT + jar + + spring-data-cassandra-reactive + Spring Data Cassandra reactive + + + org.springframework.boot + spring-boot-starter-parent + 2.1.0.RELEASE + + + + + UTF-8 + UTF-8 + + 1.8 + + + + + org.springframework.data + spring-data-cassandra + 2.1.2.RELEASE + + + io.projectreactor + reactor-core + + + org.springframework.boot + spring-boot-starter-web + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + io.projectreactor + reactor-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + diff --git a/persistence-modules/spring-data-cassandra-reactive/src/main/java/com/baeldung/cassandra/reactive/SpringDataCassandraReactiveApplication.java b/persistence-modules/spring-data-cassandra-reactive/src/main/java/com/baeldung/cassandra/reactive/SpringDataCassandraReactiveApplication.java new file mode 100644 index 0000000000..5f467042a3 --- /dev/null +++ b/persistence-modules/spring-data-cassandra-reactive/src/main/java/com/baeldung/cassandra/reactive/SpringDataCassandraReactiveApplication.java @@ -0,0 +1,12 @@ +package com.baeldung.cassandra.reactive; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringDataCassandraReactiveApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringDataCassandraReactiveApplication.class, args); + } +} diff --git a/persistence-modules/spring-data-cassandra-reactive/src/main/java/com/baeldung/cassandra/reactive/controller/EmployeeController.java b/persistence-modules/spring-data-cassandra-reactive/src/main/java/com/baeldung/cassandra/reactive/controller/EmployeeController.java new file mode 100644 index 0000000000..e9de213e61 --- /dev/null +++ b/persistence-modules/spring-data-cassandra-reactive/src/main/java/com/baeldung/cassandra/reactive/controller/EmployeeController.java @@ -0,0 +1,49 @@ +package com.baeldung.cassandra.reactive.controller; + +import com.baeldung.cassandra.reactive.model.Employee; +import com.baeldung.cassandra.reactive.service.EmployeeService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +import javax.annotation.PostConstruct; +import java.util.ArrayList; +import java.util.List; + +@RestController +@RequestMapping("employee") +public class EmployeeController { + + @Autowired + EmployeeService employeeService; + + @PostConstruct + public void saveEmployees() { + List employees = new ArrayList<>(); + employees.add(new Employee(123, "John Doe", "Delaware", "jdoe@xyz.com", 31)); + employees.add(new Employee(324, "Adam Smith", "North Carolina", "asmith@xyz.com", 43)); + employees.add(new Employee(355, "Kevin Dunner", "Virginia", "kdunner@xyz.com", 24)); + employees.add(new Employee(643, "Mike Lauren", "New York", "mlauren@xyz.com", 41)); + employeeService.initializeEmployees(employees); + } + + @GetMapping("/list") + public Flux getAllEmployees() { + Flux employees = employeeService.getAllEmployees(); + return employees; + } + + @GetMapping("/{id}") + public Mono getEmployeeById(@PathVariable int id) { + return employeeService.getEmployeeById(id); + } + + @GetMapping("/filterByAge/{age}") + public Flux getEmployeesFilterByAge(@PathVariable int age) { + return employeeService.getEmployeesFilterByAge(age); + } +} diff --git a/persistence-modules/spring-data-cassandra-reactive/src/main/java/com/baeldung/cassandra/reactive/model/Employee.java b/persistence-modules/spring-data-cassandra-reactive/src/main/java/com/baeldung/cassandra/reactive/model/Employee.java new file mode 100644 index 0000000000..a78f884778 --- /dev/null +++ b/persistence-modules/spring-data-cassandra-reactive/src/main/java/com/baeldung/cassandra/reactive/model/Employee.java @@ -0,0 +1,20 @@ +package com.baeldung.cassandra.reactive.model; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.springframework.data.cassandra.core.mapping.PrimaryKey; +import org.springframework.data.cassandra.core.mapping.Table; + +@Table +@Data +@AllArgsConstructor +@NoArgsConstructor +public class Employee { + @PrimaryKey + private int id; + private String name; + private String address; + private String email; + private int age; +} diff --git a/persistence-modules/spring-data-cassandra-reactive/src/main/java/com/baeldung/cassandra/reactive/repository/EmployeeRepository.java b/persistence-modules/spring-data-cassandra-reactive/src/main/java/com/baeldung/cassandra/reactive/repository/EmployeeRepository.java new file mode 100644 index 0000000000..22dc2b4565 --- /dev/null +++ b/persistence-modules/spring-data-cassandra-reactive/src/main/java/com/baeldung/cassandra/reactive/repository/EmployeeRepository.java @@ -0,0 +1,11 @@ +package com.baeldung.cassandra.reactive.repository; + +import com.baeldung.cassandra.reactive.model.Employee; +import org.springframework.data.cassandra.repository.AllowFiltering; +import org.springframework.data.cassandra.repository.ReactiveCassandraRepository; +import reactor.core.publisher.Flux; + +public interface EmployeeRepository extends ReactiveCassandraRepository { + @AllowFiltering + Flux findByAgeGreaterThan(int age); +} diff --git a/persistence-modules/spring-data-cassandra-reactive/src/main/java/com/baeldung/cassandra/reactive/service/EmployeeService.java b/persistence-modules/spring-data-cassandra-reactive/src/main/java/com/baeldung/cassandra/reactive/service/EmployeeService.java new file mode 100644 index 0000000000..40c330937a --- /dev/null +++ b/persistence-modules/spring-data-cassandra-reactive/src/main/java/com/baeldung/cassandra/reactive/service/EmployeeService.java @@ -0,0 +1,35 @@ +package com.baeldung.cassandra.reactive.service; + +import com.baeldung.cassandra.reactive.model.Employee; +import com.baeldung.cassandra.reactive.repository.EmployeeRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +import java.util.List; + +@Service +public class EmployeeService { + + @Autowired + EmployeeRepository employeeRepository; + + public void initializeEmployees(List employees) { + Flux savedEmployees = employeeRepository.saveAll(employees); + savedEmployees.subscribe(); + } + + public Flux getAllEmployees() { + Flux employees = employeeRepository.findAll(); + return employees; + } + + public Flux getEmployeesFilterByAge(int age) { + return employeeRepository.findByAgeGreaterThan(age); + } + + public Mono getEmployeeById(int id) { + return employeeRepository.findById(id); + } +} diff --git a/persistence-modules/spring-data-cassandra-reactive/src/main/resources/application.properties b/persistence-modules/spring-data-cassandra-reactive/src/main/resources/application.properties new file mode 100644 index 0000000000..7ed2f10131 --- /dev/null +++ b/persistence-modules/spring-data-cassandra-reactive/src/main/resources/application.properties @@ -0,0 +1,2 @@ +spring.data.cassandra.keyspace-name=practice +spring.data.cassandra.port=9042 \ No newline at end of file diff --git a/persistence-modules/spring-data-cassandra-reactive/src/test/java/com/baeldung/cassandra/reactive/ReactiveEmployeeRepositoryIntegrationTest.java b/persistence-modules/spring-data-cassandra-reactive/src/test/java/com/baeldung/cassandra/reactive/ReactiveEmployeeRepositoryIntegrationTest.java new file mode 100644 index 0000000000..ad726fe969 --- /dev/null +++ b/persistence-modules/spring-data-cassandra-reactive/src/test/java/com/baeldung/cassandra/reactive/ReactiveEmployeeRepositoryIntegrationTest.java @@ -0,0 +1,55 @@ +package com.baeldung.cassandra.reactive; + +import com.baeldung.cassandra.reactive.model.Employee; +import com.baeldung.cassandra.reactive.repository.EmployeeRepository; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; +import reactor.test.StepVerifier; + + +@RunWith(SpringRunner.class) +@SpringBootTest +public class ReactiveEmployeeRepositoryIntegrationTest { + + @Autowired + EmployeeRepository repository; + + @Before + public void setUp() { + + Flux deleteAndInsert = repository.deleteAll() // + .thenMany(repository.saveAll(Flux.just( + new Employee(111, "John Doe", "Delaware", "jdoe@xyz.com", 31), + new Employee(222, "Adam Smith", "North Carolina", "asmith@xyz.com", 43), + new Employee(333, "Kevin Dunner", "Virginia", "kdunner@xyz.com", 24), + new Employee(444, "Mike Lauren", "New York", "mlauren@xyz.com", 41)))); + + StepVerifier.create(deleteAndInsert).expectNextCount(4).verifyComplete(); + } + + @Test + public void givenRecordsAreInserted_whenDbIsQueried_thenShouldIncludeNewRecords() { + + Mono saveAndCount = repository.count() + .doOnNext(System.out::println) + .thenMany(repository.saveAll(Flux.just(new Employee(325, "Kim Jones", "Florida", "kjones@xyz.com", 42), + new Employee(654, "Tom Moody", "New Hampshire", "tmoody@xyz.com", 44)))) + .last() + .flatMap(v -> repository.count()) + .doOnNext(System.out::println); + + StepVerifier.create(saveAndCount).expectNext(6L).verifyComplete(); + } + + @Test + public void givenAgeForFilter_whenDbIsQueried_thenShouldReturnFilteredRecords() { + StepVerifier.create(repository.findByAgeGreaterThan(35)).expectNextCount(2).verifyComplete(); + } + +} diff --git a/pom.xml b/pom.xml index 70248b9993..8fa93373fc 100644 --- a/pom.xml +++ b/pom.xml @@ -377,6 +377,7 @@ feign flips testing-modules/groovy-spock + testing-modules/load-testing-comparison google-cloud google-web-toolkit gson @@ -636,7 +637,7 @@ dubbo persistence-modules/flyway - + JGit jni jooby @@ -661,24 +662,13 @@ sse-jaxrs static-analysis stripe - + structurizr Twitter4J wicket xstream cas/cas-secured-app cas/cas-server - - - - - - - - - - - - + graphql/graphql-java @@ -686,9 +676,6 @@ spring-boot-custom-starter/greeter persistence-modules/spring-boot-h2/spring-boot-h2-database - - - flyway-cdi-extension spring-security-acl @@ -710,7 +697,7 @@ spring-security-mvc-session spring-security-mvc-socket spring-security-openid - + spring-security-react spring-security-rest-basic-auth spring-security-rest-custom spring-security-rest @@ -733,8 +720,8 @@ spring-rest-angular spring-rest-full spring-rest-query-language - - + spring-rest + spring-rest-simple spring-resttemplate helidon @@ -1029,7 +1016,7 @@ jackson java-strings - + java-collections-conversions java-collections-maps java-streams @@ -1113,9 +1100,10 @@ rabbitmq - persistence-modules/spring-data-mongodb - --> @@ -1713,6 +1701,7 @@ persistence-modules/spring-data-elasticsearch core-java-concurrency core-java-concurrency-collections + restx diff --git a/restx/data/credentials.json b/restx/data/credentials.json new file mode 100644 index 0000000000..c1a4fcf531 --- /dev/null +++ b/restx/data/credentials.json @@ -0,0 +1,12 @@ +{ + "//": "lines with // keys are just comments (we don't have real comments in json)", + "//": "this file stores password passed through md5+bcrypt hash", + "//": "you can use `restx hash md5+bcrypt {password}` shell command to get hashed passwords to put here", + + "//": "to help startup with restx, there are comments with clear text passwords,", + "//": "which should obviously not be stored here.", + "user1": "$2a$10$iZluFUJShbjb1ue68bLrDuGCeJL9EMLHelVIf8u0SUbCseDOvKnoe", + "//": "user 1 password is 'user1-pwd'", + "user2": "$2a$10$oym3SYMFXf/9gGfDKKHO4eM1vWNqAZMsRZCL.BORCaP4yp5cdiCXu", + "//": "user 2 password is 'user2-pwd'" +} \ No newline at end of file diff --git a/restx/data/users.json b/restx/data/users.json new file mode 100644 index 0000000000..834e03c4b4 --- /dev/null +++ b/restx/data/users.json @@ -0,0 +1,4 @@ +[ + {"name":"user1", "roles": ["hello"]}, + {"name":"user2", "roles": []} +] \ No newline at end of file diff --git a/restx/md.restx.json b/restx/md.restx.json new file mode 100644 index 0000000000..c87244001c --- /dev/null +++ b/restx/md.restx.json @@ -0,0 +1,38 @@ +{ + "module": "restx-demo:restx-demo:0.1-SNAPSHOT", + "packaging": "war", + + "properties": { + "java.version": "1.8", + "restx.version": "0.35-rc4" + }, + "fragments": { + "maven": [ + "classpath:///restx/build/fragments/maven/javadoc-apidoclet.xml" ] + }, + "dependencies": { + "compile": [ + "io.restx:restx-core:${restx.version}", + "io.restx:restx-security-basic:${restx.version}", + "io.restx:restx-core-annotation-processor:${restx.version}", + "io.restx:restx-factory:${restx.version}", + "io.restx:restx-factory-admin:${restx.version}", + "io.restx:restx-validation:${restx.version}", + "io.restx:restx-monitor-codahale:${restx.version}", + "io.restx:restx-monitor-admin:${restx.version}", + "io.restx:restx-log-admin:${restx.version}", + "io.restx:restx-i18n-admin:${restx.version}", + "io.restx:restx-stats-admin:${restx.version}", + "io.restx:restx-servlet:${restx.version}", + "io.restx:restx-server-jetty8:${restx.version}!optional", + "io.restx:restx-apidocs:${restx.version}", + "io.restx:restx-specs-admin:${restx.version}", + "io.restx:restx-admin:${restx.version}", + "ch.qos.logback:logback-classic:1.0.13" + ], + "test": [ + "io.restx:restx-specs-tests:${restx.version}", + "junit:junit:4.11" + ] + } +} diff --git a/restx/pom.xml b/restx/pom.xml new file mode 100644 index 0000000000..c6233b968c --- /dev/null +++ b/restx/pom.xml @@ -0,0 +1,155 @@ + + + 4.0.0 + + restx + 0.1-SNAPSHOT + war + restx-demo + + + 1.8 + 1.8 + 0.35-rc4 + + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + + io.restx + restx-core + ${restx.version} + + + io.restx + restx-security-basic + ${restx.version} + + + io.restx + restx-core-annotation-processor + ${restx.version} + + + io.restx + restx-factory + ${restx.version} + + + io.restx + restx-factory-admin + ${restx.version} + + + io.restx + restx-validation + ${restx.version} + + + io.restx + restx-monitor-codahale + ${restx.version} + + + io.restx + restx-monitor-admin + ${restx.version} + + + io.restx + restx-log-admin + ${restx.version} + + + io.restx + restx-i18n-admin + ${restx.version} + + + io.restx + restx-stats-admin + ${restx.version} + + + io.restx + restx-servlet + ${restx.version} + + + io.restx + restx-server-jetty8 + ${restx.version} + true + + + io.restx + restx-apidocs + ${restx.version} + + + io.restx + restx-specs-admin + ${restx.version} + + + io.restx + restx-admin + ${restx.version} + + + ch.qos.logback + logback-classic + 1.0.13 + + + io.restx + restx-specs-tests + ${restx.version} + test + + + junit + junit + 4.11 + test + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + + + attach-docs + + prepare-package + + jar + + + + + ${maven.compiler.source} + restx.apidocs.doclet.ApidocsDoclet + + io.restx + restx-apidocs-doclet + ${restx.version} + + -restx-target-dir ${project.basedir}/target/classes + + + + + diff --git a/restx/src/main/java/restx/demo/AppModule.java b/restx/src/main/java/restx/demo/AppModule.java new file mode 100644 index 0000000000..26bc681481 --- /dev/null +++ b/restx/src/main/java/restx/demo/AppModule.java @@ -0,0 +1,74 @@ +package restx.demo; + +import restx.config.ConfigLoader; +import restx.config.ConfigSupplier; +import restx.factory.Provides; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.base.Charsets; +import com.google.common.collect.ImmutableSet; +import restx.security.*; +import restx.factory.Module; +import restx.factory.Provides; +import javax.inject.Named; + +import java.nio.file.Paths; + +@Module +public class AppModule { + @Provides + public SignatureKey signatureKey() { + return new SignatureKey("restx-demo -447494532235718370 restx-demo 801c9eaf-4116-48f2-906b-e979fba72757".getBytes(Charsets.UTF_8)); + } + + @Provides + @Named("restx.admin.password") + public String restxAdminPassword() { + return "4780"; + } + + @Provides + public ConfigSupplier appConfigSupplier(ConfigLoader configLoader) { + // Load settings.properties in restx.demo package as a set of config entries + return configLoader.fromResource("restx/demo/settings"); + } + + @Provides + public CredentialsStrategy credentialsStrategy() { + return new BCryptCredentialsStrategy(); + } + + @Provides + public BasicPrincipalAuthenticator basicPrincipalAuthenticator( + SecuritySettings securitySettings, CredentialsStrategy credentialsStrategy, + @Named("restx.admin.passwordHash") String defaultAdminPasswordHash, ObjectMapper mapper) { + return new StdBasicPrincipalAuthenticator(new StdUserService<>( + // use file based users repository. + // Developer's note: prefer another storage mechanism for your users if you need real user management + // and better perf + new FileBasedUserRepository<>( + StdUser.class, // this is the class for the User objects, that you can get in your app code + // with RestxSession.current().getPrincipal().get() + // it can be a custom user class, it just need to be json deserializable + mapper, + + // this is the default restx admin, useful to access the restx admin console. + // if one user with restx-admin role is defined in the repository, this default user won't be + // available anymore + new StdUser("admin", ImmutableSet.of("*")), + + // the path where users are stored + Paths.get("data/users.json"), + + // the path where credentials are stored. isolating both is a good practice in terms of security + // it is strongly recommended to follow this approach even if you use your own repository + Paths.get("data/credentials.json"), + + // tells that we want to reload the files dynamically if they are touched. + // this has a performance impact, if you know your users / credentials never change without a + // restart you can disable this to get better perfs + true), + credentialsStrategy, defaultAdminPasswordHash), + securitySettings); + } +} diff --git a/restx/src/main/java/restx/demo/AppServer.java b/restx/src/main/java/restx/demo/AppServer.java new file mode 100644 index 0000000000..d66aadac68 --- /dev/null +++ b/restx/src/main/java/restx/demo/AppServer.java @@ -0,0 +1,32 @@ +package restx.demo; + +import com.google.common.base.Optional; +import restx.server.WebServer; +import restx.server.Jetty8WebServer; + +/** + * This class can be used to run the app. + * + * Alternatively, you can deploy the app as a war in a regular container like tomcat or jetty. + * + * Reading the port from system env PORT makes it compatible with heroku. + */ +public class AppServer { + public static final String WEB_INF_LOCATION = "src/main/webapp/WEB-INF/web.xml"; + public static final String WEB_APP_LOCATION = "src/main/webapp"; + + public static void main(String[] args) throws Exception { + int port = Integer.valueOf(Optional.fromNullable(System.getenv("PORT")).or("8080")); + WebServer server = new Jetty8WebServer(WEB_INF_LOCATION, WEB_APP_LOCATION, port, "0.0.0.0"); + + /* + * load mode from system property if defined, or default to dev + * be careful with that setting, if you use this class to launch your server in production, make sure to launch + * it with -Drestx.mode=prod or change the default here + */ + System.setProperty("restx.mode", System.getProperty("restx.mode", "dev")); + System.setProperty("restx.app.package", "restx.demo"); + + server.startAndAwait(); + } +} diff --git a/restx/src/main/java/restx/demo/Roles.java b/restx/src/main/java/restx/demo/Roles.java new file mode 100644 index 0000000000..1240da70d1 --- /dev/null +++ b/restx/src/main/java/restx/demo/Roles.java @@ -0,0 +1,10 @@ +package restx.demo; + +/** + * A list of roles for the application. + * + * We don't use an enum here because it must be used inside an annotation. + */ +public final class Roles { + public static final String HELLO_ROLE = "hello"; +} diff --git a/restx/src/main/java/restx/demo/domain/Message.java b/restx/src/main/java/restx/demo/domain/Message.java new file mode 100644 index 0000000000..733c00deff --- /dev/null +++ b/restx/src/main/java/restx/demo/domain/Message.java @@ -0,0 +1,21 @@ +package restx.demo.domain; + +public class Message { + private String message; + + public String getMessage() { + return message; + } + + public Message setMessage(final String message) { + this.message = message; + return this; + } + + @Override + public String toString() { + return "Message{" + + "message='" + message + '\'' + + '}'; + } +} diff --git a/restx/src/main/java/restx/demo/rest/HelloResource.java b/restx/src/main/java/restx/demo/rest/HelloResource.java new file mode 100644 index 0000000000..5cb2c2a5e6 --- /dev/null +++ b/restx/src/main/java/restx/demo/rest/HelloResource.java @@ -0,0 +1,62 @@ +package restx.demo.rest; + +import restx.demo.domain.Message; +import restx.demo.Roles; +import org.joda.time.DateTime; +import restx.annotations.GET; +import restx.annotations.POST; +import restx.annotations.RestxResource; +import restx.factory.Component; +import restx.security.PermitAll; +import restx.security.RolesAllowed; +import restx.security.RestxSession; + +import javax.validation.constraints.NotNull; + +@Component @RestxResource +public class HelloResource { + + /** + * Say hello to currently logged in user. + * + * Authorized only for principals with Roles.HELLO_ROLE role. + * + * @return a Message to say hello + */ + @GET("/message") + @RolesAllowed(Roles.HELLO_ROLE) + public Message sayHello() { + return new Message().setMessage(String.format( + "hello %s, it's %s", + RestxSession.current().getPrincipal().get().getName(), + DateTime.now().toString("HH:mm:ss"))); + } + + /** + * Say hello to anybody. + * + * Does not require authentication. + * + * @return a Message to say hello + */ + @GET("/hello") + @PermitAll + public Message helloPublic(String who) { + return new Message().setMessage(String.format( + "hello %s, it's %s", + who, DateTime.now().toString("HH:mm:ss"))); + } + + public static class MyPOJO { + @NotNull + String value; + public String getValue(){ return value; } + public void setValue(String value){ this.value = value; } + } + @POST("/mypojo") + @PermitAll + public MyPOJO helloPojo(MyPOJO pojo){ + pojo.setValue("hello "+pojo.getValue()); + return pojo; + } +} diff --git a/restx/src/main/resources/logback.xml b/restx/src/main/resources/logback.xml new file mode 100644 index 0000000000..524bca6b1f --- /dev/null +++ b/restx/src/main/resources/logback.xml @@ -0,0 +1,94 @@ + + + true + + + + + ${LOGS_FOLDER}/errors.log + + ERROR + + + %d [%-16thread] [%-10X{principal}] %-5level %logger{36} - %msg%n + + + ${LOGS_FOLDER}/errors.%d.log + 30 + + + + + + + + ${LOGS_FOLDER}/app.log + + INFO + + + %d [%-16thread] [%-10X{principal}] %-5level %logger{36} - %msg%n + + + ${LOGS_FOLDER}/app.%d.log + 10 + + + + ${LOGS_FOLDER}/debug.log + + %d [%-16thread] [%-10X{principal}] %-5level %logger{36} - %msg%n + + + ${LOGS_FOLDER}/debug.%i.log.zip + 1 + 3 + + + + 50MB + + + + + + + + + + + + %d [%-16thread] [%-10X{principal}] %-5level %logger{36} - %msg%n + + + + ${LOGS_FOLDER}/app.log + + %d [%-16thread] [%-10X{principal}] %-5level %logger{36} - %msg%n + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/restx/src/main/resources/restx/demo/settings.properties b/restx/src/main/resources/restx/demo/settings.properties new file mode 100644 index 0000000000..a03c2eea97 --- /dev/null +++ b/restx/src/main/resources/restx/demo/settings.properties @@ -0,0 +1 @@ +app.name=restx-demo \ No newline at end of file diff --git a/restx/src/main/webapp/WEB-INF/web.xml b/restx/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..c651794526 --- /dev/null +++ b/restx/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,15 @@ + + + + restx + restx.servlet.RestxMainRouterServlet + 1 + + + restx + /api/* + + diff --git a/restx/src/test/java/restx/demo/rest/HelloResourceSpecUnitTest.java b/restx/src/test/java/restx/demo/rest/HelloResourceSpecUnitTest.java new file mode 100644 index 0000000000..f67e4565b3 --- /dev/null +++ b/restx/src/test/java/restx/demo/rest/HelloResourceSpecUnitTest.java @@ -0,0 +1,23 @@ +package restx.demo.rest; + +import org.junit.runner.RunWith; + +import restx.tests.FindSpecsIn; +import restx.tests.RestxSpecTestsRunner; + +@RunWith(RestxSpecTestsRunner.class) +@FindSpecsIn("specs/hello") +public class HelloResourceSpecUnitTest { + + /** + * Useless, thanks to both @RunWith(RestxSpecTestsRunner.class) & @FindSpecsIn() + * + * @Rule + * public RestxSpecRule rule = new RestxSpecRule(); + * + * @Test + * public void test_spec() throws Exception { + * rule.runTest(specTestPath); + * } + */ +} diff --git a/restx/src/test/resources/specs/hello/should_admin_say_hello.spec.yaml b/restx/src/test/resources/specs/hello/should_admin_say_hello.spec.yaml new file mode 100644 index 0000000000..1b7b8f0f90 --- /dev/null +++ b/restx/src/test/resources/specs/hello/should_admin_say_hello.spec.yaml @@ -0,0 +1,10 @@ +title: should admin say hello +given: + - time: 2013-08-28T01:18:00.822+02:00 + - uuids: [ "e2b4430f-9541-4602-9a3a-413d17c56a6b" ] +wts: + - when: | + GET message + $RestxSession: {"_expires":"2013-09-27T01:18:00.822+02:00","principal":"admin","sessionKey":"e2b4430f-9541-4602-9a3a-413d17c56a6b"} + then: | + {"message":"hello admin, it's 01:18:00"} diff --git a/restx/src/test/resources/specs/hello/should_anyone_say_hello.spec.yaml b/restx/src/test/resources/specs/hello/should_anyone_say_hello.spec.yaml new file mode 100644 index 0000000000..29b6faca34 --- /dev/null +++ b/restx/src/test/resources/specs/hello/should_anyone_say_hello.spec.yaml @@ -0,0 +1,8 @@ +title: should admin say hello +given: + - time: 2013-08-28T01:18:00.822+02:00 +wts: + - when: | + GET hello?who=xavier + then: | + {"message":"hello xavier, it's 01:18:00"} diff --git a/restx/src/test/resources/specs/hello/should_missing_value_triggers_validation_error.spec.yaml b/restx/src/test/resources/specs/hello/should_missing_value_triggers_validation_error.spec.yaml new file mode 100644 index 0000000000..d0c6323caf --- /dev/null +++ b/restx/src/test/resources/specs/hello/should_missing_value_triggers_validation_error.spec.yaml @@ -0,0 +1,17 @@ +title: should missing post value triggers a validation error +given: + - time: 2013-08-28T01:18:00.822+02:00 + - uuids: [ "e2b4430f-9541-4602-9a3a-413d17c56a6b" ] +wts: + - when: | + POST mypojo + $RestxSession: {"_expires":"2013-09-27T01:18:00.822+02:00","principal":"user1","sessionKey":"e2b4430f-9541-4602-9a3a-413d17c56a6b"} + {} + then: | + 400 + - when: | + POST mypojo + $RestxSession: {"_expires":"2013-09-27T01:18:00.822+02:00","principal":"user1","sessionKey":"e2b4430f-9541-4602-9a3a-413d17c56a6b"} + {"value":"world"} + then: | + {"value":"hello world"} diff --git a/restx/src/test/resources/specs/hello/should_user1_say_hello.spec.yaml b/restx/src/test/resources/specs/hello/should_user1_say_hello.spec.yaml new file mode 100644 index 0000000000..791a3a2776 --- /dev/null +++ b/restx/src/test/resources/specs/hello/should_user1_say_hello.spec.yaml @@ -0,0 +1,10 @@ +title: should user1 say hello +given: + - time: 2013-08-28T01:18:00.822+02:00 + - uuids: [ "e2b4430f-9541-4602-9a3a-413d17c56a6b" ] +wts: + - when: | + GET message + $RestxSession: {"_expires":"2013-09-27T01:18:00.822+02:00","principal":"user1","sessionKey":"e2b4430f-9541-4602-9a3a-413d17c56a6b"} + then: | + {"message":"hello user1, it's 01:18:00"} diff --git a/restx/src/test/resources/specs/hello/should_user2_not_say_hello.spec.yaml b/restx/src/test/resources/specs/hello/should_user2_not_say_hello.spec.yaml new file mode 100644 index 0000000000..ead5af8d0c --- /dev/null +++ b/restx/src/test/resources/specs/hello/should_user2_not_say_hello.spec.yaml @@ -0,0 +1,10 @@ +title: should user2 not say hello +given: + - time: 2013-08-28T01:19:44.770+02:00 + - uuids: [ "56f71fcc-42d3-422f-9458-8ad37fc4a0b5" ] +wts: + - when: | + GET message + $RestxSession: {"_expires":"2013-09-27T01:19:44.770+02:00","principal":"user2","sessionKey":"56f71fcc-42d3-422f-9458-8ad37fc4a0b5"} + then: | + 403 diff --git a/spring-5-reactive/src/main/java/com/baeldung/debugging/consumer/ConsumerSSEApplication.java b/spring-5-reactive/src/main/java/com/baeldung/debugging/consumer/ConsumerSSEApplication.java new file mode 100644 index 0000000000..55db3d7392 --- /dev/null +++ b/spring-5-reactive/src/main/java/com/baeldung/debugging/consumer/ConsumerSSEApplication.java @@ -0,0 +1,33 @@ +package com.baeldung.debugging.consumer; + +import java.util.Collections; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.security.config.web.server.ServerHttpSecurity; +import org.springframework.security.web.server.SecurityWebFilterChain; + +import reactor.core.publisher.Hooks; + +@SpringBootApplication(exclude = MongoReactiveAutoConfiguration.class) +@EnableScheduling +public class ConsumerSSEApplication { + + public static void main(String[] args) { + Hooks.onOperatorDebug(); + SpringApplication app = new SpringApplication(ConsumerSSEApplication.class); + app.setDefaultProperties(Collections.singletonMap("server.port", "8082")); + app.run(args); + } + + @Bean + public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { + http.authorizeExchange() + .anyExchange() + .permitAll(); + return http.build(); + } +} diff --git a/spring-5-reactive/src/main/java/com/baeldung/debugging/consumer/chronjobs/ChronJobs.java b/spring-5-reactive/src/main/java/com/baeldung/debugging/consumer/chronjobs/ChronJobs.java new file mode 100644 index 0000000000..09cbc34a6f --- /dev/null +++ b/spring-5-reactive/src/main/java/com/baeldung/debugging/consumer/chronjobs/ChronJobs.java @@ -0,0 +1,122 @@ +package com.baeldung.debugging.consumer.chronjobs; + +import java.time.Duration; +import java.util.concurrent.ThreadLocalRandom; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; +import org.springframework.web.reactive.function.client.WebClient; + +import com.baeldung.debugging.consumer.model.Foo; +import com.baeldung.debugging.consumer.model.FooDto; +import com.baeldung.debugging.consumer.service.FooService; + +import reactor.core.publisher.Flux; + +@Component +public class ChronJobs { + + private static Logger logger = LoggerFactory.getLogger(ChronJobs.class); + private WebClient client = WebClient.create("http://localhost:8081"); + + @Autowired + private FooService service; + + @Scheduled(fixedRate = 10000) + public void consumeInfiniteFlux() { + Flux fluxFoo = client.get() + .uri("/functional-reactive/periodic-foo") + .accept(MediaType.TEXT_EVENT_STREAM) + .retrieve() + .bodyToFlux(FooDto.class) + .delayElements(Duration.ofMillis(100)) + .map(dto -> { + logger.debug("process 1 with dto id {} name{}", dto.getId(), dto.getName()); + return new Foo(dto); + }); + Integer random = ThreadLocalRandom.current() + .nextInt(0, 3); + switch (random) { + case 0: + logger.info("process 1 with approach 1"); + service.processFoo(fluxFoo); + break; + case 1: + logger.info("process 1 with approach 1 EH"); + service.processUsingApproachOneWithErrorHandling(fluxFoo); + break; + default: + logger.info("process 1 with approach 2"); + service.processFooInAnotherScenario(fluxFoo); + break; + + } + } + + @Scheduled(fixedRate = 20000) + public void consumeFiniteFlux2() { + Flux fluxFoo = client.get() + .uri("/functional-reactive/periodic-foo-2") + .accept(MediaType.TEXT_EVENT_STREAM) + .retrieve() + .bodyToFlux(FooDto.class) + .delayElements(Duration.ofMillis(100)) + .map(dto -> { + logger.debug("process 2 with dto id {} name{}", dto.getId(), dto.getName()); + return new Foo(dto); + }); + Integer random = ThreadLocalRandom.current() + .nextInt(0, 3); + switch (random) { + case 0: + logger.info("process 2 with approach 1"); + service.processFoo(fluxFoo); + break; + case 1: + logger.info("process 2 with approach 1 EH"); + service.processUsingApproachOneWithErrorHandling(fluxFoo); + break; + default: + logger.info("process 2 with approach 2"); + service.processFooInAnotherScenario(fluxFoo); + break; + + } + } + + @Scheduled(fixedRate = 20000) + public void consumeFiniteFlux3() { + Flux fluxFoo = client.get() + .uri("/functional-reactive/periodic-foo-2") + .accept(MediaType.TEXT_EVENT_STREAM) + .retrieve() + .bodyToFlux(FooDto.class) + .delayElements(Duration.ofMillis(100)) + .map(dto -> { + logger.debug("process 3 with dto id {} name{}", dto.getId(), dto.getName()); + return new Foo(dto); + }); + logger.info("process 3 with approach 3"); + service.processUsingApproachThree(fluxFoo); + } + + @Scheduled(fixedRate = 20000) + public void consumeFiniteFluxWithCheckpoint4() { + Flux fluxFoo = client.get() + .uri("/functional-reactive/periodic-foo-2") + .accept(MediaType.TEXT_EVENT_STREAM) + .retrieve() + .bodyToFlux(FooDto.class) + .delayElements(Duration.ofMillis(100)) + .map(dto -> { + logger.debug("process 4 with dto id {} name{}", dto.getId(), dto.getName()); + return new Foo(dto); + }); + logger.info("process 4 with approach 4"); + service.processUsingApproachFourWithCheckpoint(fluxFoo); + } +} diff --git a/spring-5-reactive/src/main/java/com/baeldung/debugging/consumer/controllers/ReactiveConfigsToggleRestController.java b/spring-5-reactive/src/main/java/com/baeldung/debugging/consumer/controllers/ReactiveConfigsToggleRestController.java new file mode 100644 index 0000000000..3dcdc6c7c0 --- /dev/null +++ b/spring-5-reactive/src/main/java/com/baeldung/debugging/consumer/controllers/ReactiveConfigsToggleRestController.java @@ -0,0 +1,23 @@ +package com.baeldung.debugging.consumer.controllers; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import reactor.core.publisher.Hooks; + +@RestController +public class ReactiveConfigsToggleRestController { + + @GetMapping("/debug-hook-on") + public String setReactiveDebugOn() { + Hooks.onOperatorDebug(); + return "DEBUG HOOK ON"; + } + + @GetMapping("/debug-hook-off") + public String setReactiveDebugOff() { + Hooks.resetOnOperatorDebug(); + return "DEBUG HOOK OFF"; + } + +} diff --git a/spring-5-reactive/src/main/java/com/baeldung/debugging/consumer/model/Foo.java b/spring-5-reactive/src/main/java/com/baeldung/debugging/consumer/model/Foo.java new file mode 100644 index 0000000000..e101457b84 --- /dev/null +++ b/spring-5-reactive/src/main/java/com/baeldung/debugging/consumer/model/Foo.java @@ -0,0 +1,26 @@ +package com.baeldung.debugging.consumer.model; + +import java.util.concurrent.ThreadLocalRandom; + +import org.springframework.data.annotation.Id; + +import lombok.AllArgsConstructor; +import lombok.Data; + +@Data +@AllArgsConstructor +public class Foo { + + @Id + private Integer id; + private String formattedName; + private Integer quantity; + + public Foo(FooDto dto) { + this.id = (ThreadLocalRandom.current() + .nextInt(0, 100) == 0) ? null : dto.getId(); + this.formattedName = dto.getName(); + this.quantity = ThreadLocalRandom.current() + .nextInt(0, 10); + } +} diff --git a/spring-5-reactive/src/main/java/com/baeldung/debugging/consumer/model/FooDto.java b/spring-5-reactive/src/main/java/com/baeldung/debugging/consumer/model/FooDto.java new file mode 100644 index 0000000000..50508fd216 --- /dev/null +++ b/spring-5-reactive/src/main/java/com/baeldung/debugging/consumer/model/FooDto.java @@ -0,0 +1,12 @@ +package com.baeldung.debugging.consumer.model; + +import lombok.AllArgsConstructor; +import lombok.Data; + +@Data +@AllArgsConstructor +public class FooDto { + + private Integer id; + private String name; +} diff --git a/spring-5-reactive/src/main/java/com/baeldung/debugging/consumer/service/FooNameHelper.java b/spring-5-reactive/src/main/java/com/baeldung/debugging/consumer/service/FooNameHelper.java new file mode 100644 index 0000000000..772b360437 --- /dev/null +++ b/spring-5-reactive/src/main/java/com/baeldung/debugging/consumer/service/FooNameHelper.java @@ -0,0 +1,45 @@ +package com.baeldung.debugging.consumer.service; + +import java.util.concurrent.ThreadLocalRandom; + +import com.baeldung.debugging.consumer.model.Foo; + +import reactor.core.publisher.Flux; + +public class FooNameHelper { + + public static Flux concatAndSubstringFooName(Flux flux) { + flux = concatFooName(flux); + flux = substringFooName(flux); + return flux; + } + + public static Flux concatFooName(Flux flux) { + flux = flux.map(foo -> { + String processedName = null; + Integer random = ThreadLocalRandom.current() + .nextInt(0, 80); + processedName = (random != 0) ? foo.getFormattedName() : foo.getFormattedName() + "-bael"; + foo.setFormattedName(processedName); + return foo; + }); + return flux; + } + + public static Flux substringFooName(Flux flux) { + return flux.map(foo -> { + String processedName; + Integer random = ThreadLocalRandom.current() + .nextInt(0, 100); + + processedName = (random == 0) ? foo.getFormattedName() + .substring(10, 15) + : foo.getFormattedName() + .substring(0, 5); + + foo.setFormattedName(processedName); + return foo; + }); + } + +} diff --git a/spring-5-reactive/src/main/java/com/baeldung/debugging/consumer/service/FooQuantityHelper.java b/spring-5-reactive/src/main/java/com/baeldung/debugging/consumer/service/FooQuantityHelper.java new file mode 100644 index 0000000000..615239313d --- /dev/null +++ b/spring-5-reactive/src/main/java/com/baeldung/debugging/consumer/service/FooQuantityHelper.java @@ -0,0 +1,31 @@ +package com.baeldung.debugging.consumer.service; + +import java.util.concurrent.ThreadLocalRandom; + +import com.baeldung.debugging.consumer.model.Foo; + +import reactor.core.publisher.Flux; + +public class FooQuantityHelper { + + public static Flux processFooReducingQuantity(Flux flux) { + flux = flux.map(foo -> { + Integer result; + Integer random = ThreadLocalRandom.current() + .nextInt(0, 90); + result = (random == 0) ? result = 0 : foo.getQuantity() + 2; + foo.setQuantity(result); + return foo; + }); + return divideFooQuantity(flux); + } + + public static Flux divideFooQuantity(Flux flux) { + return flux.map(foo -> { + Integer result = Math.round(5 / foo.getQuantity()); + foo.setQuantity(result); + return foo; + }); + } + +} diff --git a/spring-5-reactive/src/main/java/com/baeldung/debugging/consumer/service/FooReporter.java b/spring-5-reactive/src/main/java/com/baeldung/debugging/consumer/service/FooReporter.java new file mode 100644 index 0000000000..f53cd238e0 --- /dev/null +++ b/spring-5-reactive/src/main/java/com/baeldung/debugging/consumer/service/FooReporter.java @@ -0,0 +1,26 @@ +package com.baeldung.debugging.consumer.service; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.baeldung.debugging.consumer.model.Foo; + +import reactor.core.publisher.Flux; + +public class FooReporter { + + private static Logger logger = LoggerFactory.getLogger(FooReporter.class); + + public static Flux reportResult(Flux input, String approach) { + return input.map(foo -> { + if (foo.getId() == null) + throw new IllegalArgumentException("Null id is not valid!"); + logger.info("Reporting for approach {}: Foo with id '{}' name '{}' and quantity '{}'", approach, foo.getId(), foo.getFormattedName(), foo.getQuantity()); + return foo; + }); + } + + public static Flux reportResult(Flux input) { + return reportResult(input, "default"); + } +} diff --git a/spring-5-reactive/src/main/java/com/baeldung/debugging/consumer/service/FooService.java b/spring-5-reactive/src/main/java/com/baeldung/debugging/consumer/service/FooService.java new file mode 100644 index 0000000000..937e445ef5 --- /dev/null +++ b/spring-5-reactive/src/main/java/com/baeldung/debugging/consumer/service/FooService.java @@ -0,0 +1,91 @@ +package com.baeldung.debugging.consumer.service; + +import static com.baeldung.debugging.consumer.service.FooNameHelper.concatAndSubstringFooName; +import static com.baeldung.debugging.consumer.service.FooNameHelper.substringFooName; +import static com.baeldung.debugging.consumer.service.FooQuantityHelper.divideFooQuantity; +import static com.baeldung.debugging.consumer.service.FooQuantityHelper.processFooReducingQuantity; +import static com.baeldung.debugging.consumer.service.FooReporter.reportResult; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import com.baeldung.debugging.consumer.model.Foo; + +import reactor.core.publisher.Flux; + +@Component +public class FooService { + + private static Logger logger = LoggerFactory.getLogger(FooService.class); + + public void processFoo(Flux flux) { + flux = FooNameHelper.concatFooName(flux); + flux = FooNameHelper.substringFooName(flux); + flux = flux.log(); + flux = FooReporter.reportResult(flux); + flux = flux.doOnError(error -> { + logger.error("The following error happened on processFoo method!", error); + }); + flux.subscribe(); + } + + public void processFooInAnotherScenario(Flux flux) { + flux = FooNameHelper.substringFooName(flux); + flux = FooQuantityHelper.divideFooQuantity(flux); + flux.subscribe(); + } + + public void processUsingApproachOneWithErrorHandling(Flux flux) { + logger.info("starting approach one w error handling!"); + flux = concatAndSubstringFooName(flux); + flux = concatAndSubstringFooName(flux); + flux = substringFooName(flux); + flux = processFooReducingQuantity(flux); + flux = processFooReducingQuantity(flux); + flux = processFooReducingQuantity(flux); + flux = reportResult(flux, "ONE w/ EH"); + flux = flux.doOnError(error -> { + logger.error("Approach 1 with Error Handling failed!", error); + }); + flux.subscribe(); + } + + public void processUsingApproachThree(Flux flux) { + logger.info("starting approach three!"); + flux = concatAndSubstringFooName(flux); + flux = reportResult(flux, "THREE"); + flux = flux.doOnError(error -> { + logger.error("Approach 3 failed!", error); + }); + flux.subscribe(); + } + + public void processUsingApproachFourWithCheckpoint(Flux flux) { + logger.info("starting approach four!"); + flux = concatAndSubstringFooName(flux); + flux = flux.checkpoint("CHECKPOINT 1"); + flux = concatAndSubstringFooName(flux); + flux = divideFooQuantity(flux); + flux = flux.checkpoint("CHECKPOINT 2", true); + flux = reportResult(flux, "FOUR"); + flux = concatAndSubstringFooName(flux).doOnError(error -> { + logger.error("Approach 4 failed!", error); + }); + flux.subscribe(); + } + + public void processUsingApproachFourWithInitialCheckpoint(Flux flux) { + logger.info("starting approach four!"); + flux = concatAndSubstringFooName(flux); + flux = flux.checkpoint("CHECKPOINT 1", true); + flux = concatAndSubstringFooName(flux); + flux = divideFooQuantity(flux); + flux = reportResult(flux, "FOUR"); + flux = flux.doOnError(error -> { + logger.error("Approach 4-2 failed!", error); + }); + flux.subscribe(); + } + +} diff --git a/spring-5-reactive/src/main/java/com/baeldung/debugging/server/ServerSSEApplication.java b/spring-5-reactive/src/main/java/com/baeldung/debugging/server/ServerSSEApplication.java new file mode 100644 index 0000000000..6b24ee39f0 --- /dev/null +++ b/spring-5-reactive/src/main/java/com/baeldung/debugging/server/ServerSSEApplication.java @@ -0,0 +1,29 @@ +package com.baeldung.debugging.server; + +import java.util.Collections; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.security.config.web.server.ServerHttpSecurity; +import org.springframework.security.web.server.SecurityWebFilterChain; +import org.springframework.web.reactive.config.EnableWebFlux; + +@EnableWebFlux +@SpringBootApplication +public class ServerSSEApplication { + + public static void main(String[] args) { + SpringApplication app = new SpringApplication(ServerSSEApplication.class); + app.setDefaultProperties(Collections.singletonMap("server.port", "8081")); + app.run(args); + } + + @Bean + public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { + http.authorizeExchange() + .anyExchange() + .permitAll(); + return http.build(); + } +} diff --git a/spring-5-reactive/src/main/java/com/baeldung/debugging/server/handlers/ServerHandler.java b/spring-5-reactive/src/main/java/com/baeldung/debugging/server/handlers/ServerHandler.java new file mode 100644 index 0000000000..759cd9b01d --- /dev/null +++ b/spring-5-reactive/src/main/java/com/baeldung/debugging/server/handlers/ServerHandler.java @@ -0,0 +1,47 @@ +package com.baeldung.debugging.server.handlers; + +import java.time.Duration; +import java.util.concurrent.ThreadLocalRandom; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.MediaType; +import org.springframework.stereotype.Component; +import org.springframework.web.reactive.function.server.ServerRequest; +import org.springframework.web.reactive.function.server.ServerResponse; + +import com.baeldung.debugging.server.model.Foo; + +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +@Component +public class ServerHandler { + + private static Logger logger = LoggerFactory.getLogger(ServerHandler.class); + + public Mono useHandler(final ServerRequest request) { + // there are chances that something goes wrong here... + return ServerResponse.ok() + .contentType(MediaType.TEXT_EVENT_STREAM) + .body(Flux.interval(Duration.ofSeconds(1)) + .map(sequence -> { + logger.info("retrieving Foo. Sequence: {}", sequence); + if (ThreadLocalRandom.current() + .nextInt(0, 50) == 1) { + throw new RuntimeException("There was an error retrieving the Foo!"); + } + return new Foo(sequence, "name" + sequence); + + }), Foo.class); + } + + public Mono useHandlerFinite(final ServerRequest request) { + return ServerResponse.ok() + .contentType(MediaType.TEXT_EVENT_STREAM) + .body(Flux.range(0, 50) + .map(sequence -> { + return new Foo(new Long(sequence), "theFooNameNumber" + sequence); + }), Foo.class); + } +} diff --git a/spring-5-reactive/src/main/java/com/baeldung/debugging/server/model/Foo.java b/spring-5-reactive/src/main/java/com/baeldung/debugging/server/model/Foo.java new file mode 100644 index 0000000000..a60e468e7f --- /dev/null +++ b/spring-5-reactive/src/main/java/com/baeldung/debugging/server/model/Foo.java @@ -0,0 +1,16 @@ +package com.baeldung.debugging.server.model; + +import org.springframework.data.annotation.Id; + +import lombok.AllArgsConstructor; +import lombok.Data; + +@Data +@AllArgsConstructor +public class Foo { + + @Id + private Long id; + private String name; + +} diff --git a/spring-5-reactive/src/main/java/com/baeldung/debugging/server/routers/ServerRouter.java b/spring-5-reactive/src/main/java/com/baeldung/debugging/server/routers/ServerRouter.java new file mode 100644 index 0000000000..6378b2213d --- /dev/null +++ b/spring-5-reactive/src/main/java/com/baeldung/debugging/server/routers/ServerRouter.java @@ -0,0 +1,22 @@ +package com.baeldung.debugging.server.routers; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.reactive.function.server.RequestPredicates; +import org.springframework.web.reactive.function.server.RouterFunction; +import org.springframework.web.reactive.function.server.RouterFunctions; +import org.springframework.web.reactive.function.server.ServerResponse; + +import com.baeldung.debugging.server.handlers.ServerHandler; + +@Configuration +public class ServerRouter { + + @Bean + public RouterFunction responseRoute(@Autowired ServerHandler handler) { + return RouterFunctions.route(RequestPredicates.GET("/functional-reactive/periodic-foo"), handler::useHandler) + .andRoute(RequestPredicates.GET("/functional-reactive/periodic-foo-2"), handler::useHandlerFinite); + } + +} diff --git a/logging-modules/log4j/src/main/java/com/baeldung/webFluxLogging/WebFluxLoggingExample.java b/spring-5-reactive/src/main/java/com/baeldung/webflux/logging/WebFluxLoggingExample.java similarity index 91% rename from logging-modules/log4j/src/main/java/com/baeldung/webFluxLogging/WebFluxLoggingExample.java rename to spring-5-reactive/src/main/java/com/baeldung/webflux/logging/WebFluxLoggingExample.java index f429fd57f3..c4881b2296 100644 --- a/logging-modules/log4j/src/main/java/com/baeldung/webFluxLogging/WebFluxLoggingExample.java +++ b/spring-5-reactive/src/main/java/com/baeldung/webflux/logging/WebFluxLoggingExample.java @@ -1,4 +1,4 @@ -package com.baeldung.webFluxLogging; +package com.baeldung.webflux.logging; import reactor.core.publisher.Flux; diff --git a/spring-5-reactive/src/main/resources/application.properties b/spring-5-reactive/src/main/resources/application.properties index 92f3116f84..4b49e8e8a2 100644 --- a/spring-5-reactive/src/main/resources/application.properties +++ b/spring-5-reactive/src/main/resources/application.properties @@ -1,2 +1 @@ -logging.level.root=INFO - +logging.level.root=INFO \ No newline at end of file diff --git a/spring-5-reactive/src/test/java/com/baeldung/debugging/consumer/ConsumerFooServiceIntegrationTest.java b/spring-5-reactive/src/test/java/com/baeldung/debugging/consumer/ConsumerFooServiceIntegrationTest.java new file mode 100644 index 0000000000..b7ed031ec7 --- /dev/null +++ b/spring-5-reactive/src/test/java/com/baeldung/debugging/consumer/ConsumerFooServiceIntegrationTest.java @@ -0,0 +1,65 @@ +package com.baeldung.debugging.consumer; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Optional; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import com.baeldung.debugging.consumer.model.Foo; +import com.baeldung.debugging.consumer.service.FooService; +import com.baeldung.debugging.consumer.utils.ListAppender; + +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.classic.spi.IThrowableProxy; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Hooks; + +public class ConsumerFooServiceIntegrationTest { + + FooService service = new FooService(); + + @BeforeEach + public void clearLogList() { + Hooks.onOperatorDebug(); + ListAppender.clearEventList(); + } + + @Test + public void givenFooWithNullId_whenProcessFoo_thenLogsWithDebugTrace() { + Foo one = new Foo(1, "nameverylong", 8); + Foo two = new Foo(null, "nameverylong", 4); + Flux flux = Flux.just(one, two); + + service.processFoo(flux); + + Collection allLoggedEntries = ListAppender.getEvents() + .stream() + .map(ILoggingEvent::getFormattedMessage) + .collect(Collectors.toList()); + + Collection allSuppressedEntries = ListAppender.getEvents() + .stream() + .map(ILoggingEvent::getThrowableProxy) + .flatMap(t -> { + return Optional.ofNullable(t) + .map(IThrowableProxy::getSuppressed) + .map(Arrays::stream) + .orElse(Stream.empty()); + }) + .map(IThrowableProxy::getMessage) + .collect(Collectors.toList()); + assertThat(allLoggedEntries).anyMatch(entry -> entry.contains("The following error happened on processFoo method!")) + .anyMatch(entry -> entry.contains("| onSubscribe")) + .anyMatch(entry -> entry.contains("| cancel()")); + + assertThat(allSuppressedEntries).anyMatch(entry -> entry.contains("Assembly trace from producer")) + .anyMatch(entry -> entry.contains("Error has been observed by the following operator(s)")); + } + +} diff --git a/spring-5-reactive/src/test/java/com/baeldung/debugging/consumer/ConsumerFooServiceLiveTest.java b/spring-5-reactive/src/test/java/com/baeldung/debugging/consumer/ConsumerFooServiceLiveTest.java new file mode 100644 index 0000000000..af9bdfbc9b --- /dev/null +++ b/spring-5-reactive/src/test/java/com/baeldung/debugging/consumer/ConsumerFooServiceLiveTest.java @@ -0,0 +1,49 @@ +package com.baeldung.debugging.consumer; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.springframework.test.web.reactive.server.WebTestClient; +import org.springframework.test.web.reactive.server.WebTestClient.ResponseSpec; + +import com.baeldung.debugging.consumer.service.FooService; + +public class ConsumerFooServiceLiveTest { + + FooService service = new FooService(); + + private static final String BASE_URL = "http://localhost:8082"; + private static final String DEBUG_HOOK_ON = BASE_URL + "/debug-hook-on"; + private static final String DEBUG_HOOK_OFF = BASE_URL + "/debug-hook-off"; + + private static WebTestClient client; + + @BeforeAll + public static void setup() { + client = WebTestClient.bindToServer() + .baseUrl(BASE_URL) + .build(); + } + + @Test + public void whenRequestingDebugHookOn_thenObtainExpectedMessage() { + ResponseSpec response = client.get() + .uri(DEBUG_HOOK_ON) + .exchange(); + response.expectStatus() + .isOk() + .expectBody(String.class) + .isEqualTo("DEBUG HOOK ON"); + } + + @Test + public void whenRequestingDebugHookOff_thenObtainExpectedMessage() { + ResponseSpec response = client.get() + .uri(DEBUG_HOOK_OFF) + .exchange(); + response.expectStatus() + .isOk() + .expectBody(String.class) + .isEqualTo("DEBUG HOOK OFF"); + } + +} diff --git a/spring-5-reactive/src/test/java/com/baeldung/debugging/consumer/utils/ListAppender.java b/spring-5-reactive/src/test/java/com/baeldung/debugging/consumer/utils/ListAppender.java new file mode 100644 index 0000000000..c8c1c110bb --- /dev/null +++ b/spring-5-reactive/src/test/java/com/baeldung/debugging/consumer/utils/ListAppender.java @@ -0,0 +1,25 @@ +package com.baeldung.debugging.consumer.utils; + +import java.util.ArrayList; +import java.util.List; + +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.AppenderBase; + +public class ListAppender extends AppenderBase { + + static private List events = new ArrayList<>(); + + @Override + protected void append(ILoggingEvent eventObject) { + events.add(eventObject); + } + + public static List getEvents() { + return events; + } + + public static void clearEventList() { + events.clear(); + } +} diff --git a/spring-5-reactive/src/test/resources/logback-test.xml b/spring-5-reactive/src/test/resources/logback-test.xml new file mode 100644 index 0000000000..514029e402 --- /dev/null +++ b/spring-5-reactive/src/test/resources/logback-test.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-5-security-oauth/pom.xml b/spring-5-security-oauth/pom.xml index f200052adf..59150a153f 100644 --- a/spring-5-security-oauth/pom.xml +++ b/spring-5-security-oauth/pom.xml @@ -31,7 +31,7 @@ org.thymeleaf.extras - thymeleaf-extras-springsecurity4 + thymeleaf-extras-springsecurity5 @@ -66,7 +66,8 @@ - 2.0.1.RELEASE + 2.1.0.RELEASE + 2.1.0.RELEASE com.baeldung.oauth2.SpringOAuthApplication diff --git a/spring-5-security-oauth/src/main/java/com/baeldung/oauth2/CustomAuthorizationRequestResolver.java b/spring-5-security-oauth/src/main/java/com/baeldung/oauth2/CustomAuthorizationRequestResolver.java new file mode 100644 index 0000000000..025064423d --- /dev/null +++ b/spring-5-security-oauth/src/main/java/com/baeldung/oauth2/CustomAuthorizationRequestResolver.java @@ -0,0 +1,50 @@ +package com.baeldung.oauth2; + +import java.util.HashMap; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; + +import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository; +import org.springframework.security.oauth2.client.web.DefaultOAuth2AuthorizationRequestResolver; +import org.springframework.security.oauth2.client.web.OAuth2AuthorizationRequestResolver; +import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest; + +public class CustomAuthorizationRequestResolver implements OAuth2AuthorizationRequestResolver { + + private OAuth2AuthorizationRequestResolver defaultResolver; + + public CustomAuthorizationRequestResolver(ClientRegistrationRepository repo, String authorizationRequestBaseUri){ + defaultResolver = new DefaultOAuth2AuthorizationRequestResolver(repo, authorizationRequestBaseUri); + } + + @Override + public OAuth2AuthorizationRequest resolve(HttpServletRequest request) { + OAuth2AuthorizationRequest req = defaultResolver.resolve(request); + if(req != null){ + req = customizeAuthorizationRequest(req); + } + return req; + } + + @Override + public OAuth2AuthorizationRequest resolve(HttpServletRequest request, String clientRegistrationId) { + OAuth2AuthorizationRequest req = defaultResolver.resolve(request, clientRegistrationId); + if(req != null){ + req = customizeAuthorizationRequest(req); + } + return req; + } + + private OAuth2AuthorizationRequest customizeAuthorizationRequest(OAuth2AuthorizationRequest req) { + Map extraParams = new HashMap(); + extraParams.putAll(req.getAdditionalParameters()); //VIP note + extraParams.put("test", "extra"); + System.out.println("here ====================="); + return OAuth2AuthorizationRequest.from(req).additionalParameters(extraParams).build(); + } + + private OAuth2AuthorizationRequest customizeAuthorizationRequest1(OAuth2AuthorizationRequest req) { + return OAuth2AuthorizationRequest.from(req).state("xyz").build(); + } +} diff --git a/spring-5-security-oauth/src/main/java/com/baeldung/oauth2/CustomRequestEntityConverter.java b/spring-5-security-oauth/src/main/java/com/baeldung/oauth2/CustomRequestEntityConverter.java new file mode 100644 index 0000000000..8884065769 --- /dev/null +++ b/spring-5-security-oauth/src/main/java/com/baeldung/oauth2/CustomRequestEntityConverter.java @@ -0,0 +1,26 @@ +package com.baeldung.oauth2; + +import org.springframework.core.convert.converter.Converter; +import org.springframework.http.RequestEntity; +import org.springframework.security.oauth2.client.endpoint.OAuth2AuthorizationCodeGrantRequest; +import org.springframework.security.oauth2.client.endpoint.OAuth2AuthorizationCodeGrantRequestEntityConverter; +import org.springframework.util.MultiValueMap; + +public class CustomRequestEntityConverter implements Converter> { + + private OAuth2AuthorizationCodeGrantRequestEntityConverter defaultConverter; + + public CustomRequestEntityConverter() { + defaultConverter = new OAuth2AuthorizationCodeGrantRequestEntityConverter(); + } + + @Override + public RequestEntity convert(OAuth2AuthorizationCodeGrantRequest req) { + RequestEntity entity = defaultConverter.convert(req); + MultiValueMap params = (MultiValueMap) entity.getBody(); + params.add("test2", "extra2"); + System.out.println(params.entrySet()); + return new RequestEntity<>(params, entity.getHeaders(), entity.getMethod(), entity.getUrl()); + } + +} diff --git a/spring-5-security-oauth/src/main/java/com/baeldung/oauth2/CustomTokenResponseConverter.java b/spring-5-security-oauth/src/main/java/com/baeldung/oauth2/CustomTokenResponseConverter.java new file mode 100644 index 0000000000..741f44871a --- /dev/null +++ b/spring-5-security-oauth/src/main/java/com/baeldung/oauth2/CustomTokenResponseConverter.java @@ -0,0 +1,67 @@ +package com.baeldung.oauth2; + +import java.util.Arrays; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.springframework.core.convert.converter.Converter; +import org.springframework.security.oauth2.core.OAuth2AccessToken; +import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse; +import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames; +import org.springframework.util.StringUtils; + +public class CustomTokenResponseConverter implements Converter, OAuth2AccessTokenResponse> { + private static final Set TOKEN_RESPONSE_PARAMETER_NAMES = Stream.of( + OAuth2ParameterNames.ACCESS_TOKEN, + OAuth2ParameterNames.TOKEN_TYPE, + OAuth2ParameterNames.EXPIRES_IN, + OAuth2ParameterNames.REFRESH_TOKEN, + OAuth2ParameterNames.SCOPE) .collect(Collectors.toSet()); + + @Override + public OAuth2AccessTokenResponse convert(Map tokenResponseParameters) { + String accessToken = tokenResponseParameters.get(OAuth2ParameterNames.ACCESS_TOKEN); + + OAuth2AccessToken.TokenType accessTokenType = null; + if (OAuth2AccessToken.TokenType.BEARER.getValue() + .equalsIgnoreCase(tokenResponseParameters.get(OAuth2ParameterNames.TOKEN_TYPE))) { + accessTokenType = OAuth2AccessToken.TokenType.BEARER; + } + + long expiresIn = 0; + if (tokenResponseParameters.containsKey(OAuth2ParameterNames.EXPIRES_IN)) { + try { + expiresIn = Long.valueOf(tokenResponseParameters.get(OAuth2ParameterNames.EXPIRES_IN)); + } catch (NumberFormatException ex) { + } + } + + Set scopes = Collections.emptySet(); + if (tokenResponseParameters.containsKey(OAuth2ParameterNames.SCOPE)) { + String scope = tokenResponseParameters.get(OAuth2ParameterNames.SCOPE); + scopes = Arrays.stream(StringUtils.delimitedListToStringArray(scope, " ")) + .collect(Collectors.toSet()); + } + + String refreshToken = tokenResponseParameters.get(OAuth2ParameterNames.REFRESH_TOKEN); + + Map additionalParameters = new LinkedHashMap<>(); + tokenResponseParameters.entrySet() + .stream() + .filter(e -> !TOKEN_RESPONSE_PARAMETER_NAMES.contains(e.getKey())) + .forEach(e -> additionalParameters.put(e.getKey(), e.getValue())); + + return OAuth2AccessTokenResponse.withToken(accessToken) + .tokenType(accessTokenType) + .expiresIn(expiresIn) + .scopes(scopes) + .refreshToken(refreshToken) + .additionalParameters(additionalParameters) + .build(); + } + +} diff --git a/spring-5-security-oauth/src/main/java/com/baeldung/oauth2/SecurityConfig.java b/spring-5-security-oauth/src/main/java/com/baeldung/oauth2/SecurityConfig.java index b45f325767..cf27b01a75 100644 --- a/spring-5-security-oauth/src/main/java/com/baeldung/oauth2/SecurityConfig.java +++ b/spring-5-security-oauth/src/main/java/com/baeldung/oauth2/SecurityConfig.java @@ -9,18 +9,22 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.core.env.Environment; +import org.springframework.http.converter.FormHttpMessageConverter; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.oauth2.client.CommonOAuth2Provider; -import org.springframework.security.oauth2.client.endpoint.NimbusAuthorizationCodeTokenResponseClient; +import org.springframework.security.oauth2.client.endpoint.DefaultAuthorizationCodeTokenResponseClient; import org.springframework.security.oauth2.client.endpoint.OAuth2AccessTokenResponseClient; import org.springframework.security.oauth2.client.endpoint.OAuth2AuthorizationCodeGrantRequest; +import org.springframework.security.oauth2.client.http.OAuth2ErrorResponseErrorHandler; import org.springframework.security.oauth2.client.registration.ClientRegistration; import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository; import org.springframework.security.oauth2.client.registration.InMemoryClientRegistrationRepository; import org.springframework.security.oauth2.client.web.AuthorizationRequestRepository; import org.springframework.security.oauth2.client.web.HttpSessionOAuth2AuthorizationRequestRepository; import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest; +import org.springframework.security.oauth2.core.http.converter.OAuth2AccessTokenResponseHttpMessageConverter; +import org.springframework.web.client.RestTemplate; @Configuration @PropertySource("application-oauth2.properties") @@ -37,6 +41,8 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { .oauth2Login() .loginPage("/oauth_login") .authorizationEndpoint() + .authorizationRequestResolver( new CustomAuthorizationRequestResolver(clientRegistrationRepository(),"/oauth2/authorize-client")) + .baseUri("/oauth2/authorize-client") .authorizationRequestRepository(authorizationRequestRepository()) .and() @@ -54,7 +60,15 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { @Bean public OAuth2AccessTokenResponseClient accessTokenResponseClient() { - return new NimbusAuthorizationCodeTokenResponseClient(); + DefaultAuthorizationCodeTokenResponseClient accessTokenResponseClient = new DefaultAuthorizationCodeTokenResponseClient(); + accessTokenResponseClient.setRequestEntityConverter(new CustomRequestEntityConverter()); + + OAuth2AccessTokenResponseHttpMessageConverter tokenResponseHttpMessageConverter = new OAuth2AccessTokenResponseHttpMessageConverter(); + tokenResponseHttpMessageConverter.setTokenResponseConverter(new CustomTokenResponseConverter()); + RestTemplate restTemplate = new RestTemplate(Arrays.asList(new FormHttpMessageConverter(), tokenResponseHttpMessageConverter)); + restTemplate.setErrorHandler(new OAuth2ErrorResponseErrorHandler()); + accessTokenResponseClient.setRestOperations(restTemplate); + return accessTokenResponseClient; } diff --git a/spring-boot-autoconfiguration/src/main/resources/META-INF/spring.factories b/spring-boot-autoconfiguration/src/main/resources/META-INF/spring.factories index 5f55544eff..11c775fc6c 100644 --- a/spring-boot-autoconfiguration/src/main/resources/META-INF/spring.factories +++ b/spring-boot-autoconfiguration/src/main/resources/META-INF/spring.factories @@ -1,3 +1 @@ -org.springframework.boot.diagnostics.FailureAnalyzer=com.baeldung.failureanalyzer.MyBeanNotOfRequiredTypeFailureAnalyzer - -org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.baeldung.autoconfiguration.MySQLAutoconfiguration \ No newline at end of file +org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.baeldung.autoconfiguration.MySQLAutoconfiguration diff --git a/spring-boot/src/main/java/com/baeldung/mongodb/Application.java b/spring-boot/src/main/java/com/baeldung/mongodb/Application.java index 092ce3352b..c0a9ad59a7 100644 --- a/spring-boot/src/main/java/com/baeldung/mongodb/Application.java +++ b/spring-boot/src/main/java/com/baeldung/mongodb/Application.java @@ -1,11 +1,19 @@ package com.baeldung.mongodb; +import com.baeldung.mongodb.daos.UserRepository; +import com.baeldung.mongodb.models.User; +import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; + +import java.util.List; @SpringBootApplication public class Application { + public static void main(String[] args) { SpringApplication.run(Application.class, args); } + } diff --git a/spring-boot/src/main/java/com/baeldung/mongodb/daos/UserRepository.java b/spring-boot/src/main/java/com/baeldung/mongodb/daos/UserRepository.java new file mode 100755 index 0000000000..7f58fdd1c8 --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/mongodb/daos/UserRepository.java @@ -0,0 +1,10 @@ +package com.baeldung.mongodb.daos; + + +import com.baeldung.mongodb.models.User; +import org.springframework.data.mongodb.repository.MongoRepository; + + +public interface UserRepository extends MongoRepository { + +} diff --git a/spring-boot/src/main/java/com/baeldung/mongodb/events/UserModelListener.java b/spring-boot/src/main/java/com/baeldung/mongodb/events/UserModelListener.java new file mode 100644 index 0000000000..24b53f3d2d --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/mongodb/events/UserModelListener.java @@ -0,0 +1,28 @@ +package com.baeldung.mongodb.events; + + +import com.baeldung.mongodb.models.User; +import com.baeldung.mongodb.services.SequenceGeneratorService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener; +import org.springframework.data.mongodb.core.mapping.event.BeforeConvertEvent; +import org.springframework.stereotype.Component; + + +@Component +public class UserModelListener extends AbstractMongoEventListener { + + private SequenceGeneratorService sequenceGenerator; + + @Autowired + public UserModelListener(SequenceGeneratorService sequenceGenerator) { + this.sequenceGenerator = sequenceGenerator; + } + + @Override + public void onBeforeConvert(BeforeConvertEvent event) { + event.getSource().setId(sequenceGenerator.generateSequence(User.SEQUENCE_NAME)); + } + + +} diff --git a/spring-boot/src/main/java/com/baeldung/mongodb/models/DatabaseSequence.java b/spring-boot/src/main/java/com/baeldung/mongodb/models/DatabaseSequence.java new file mode 100755 index 0000000000..c2c04f7ee6 --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/mongodb/models/DatabaseSequence.java @@ -0,0 +1,32 @@ +package com.baeldung.mongodb.models; + +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; + + +@Document(collection = "database_sequences") +public class DatabaseSequence { + + @Id + private String id; + + private long seq; + + public DatabaseSequence() {} + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public long getSeq() { + return seq; + } + + public void setSeq(long seq) { + this.seq = seq; + } +} diff --git a/spring-boot/src/main/java/com/baeldung/mongodb/models/User.java b/spring-boot/src/main/java/com/baeldung/mongodb/models/User.java new file mode 100755 index 0000000000..1f08074313 --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/mongodb/models/User.java @@ -0,0 +1,73 @@ +package com.baeldung.mongodb.models; + +import org.springframework.data.annotation.Id; +import org.springframework.data.annotation.Transient; +import org.springframework.data.mongodb.core.mapping.Document; + + +@Document(collection = "users") +public class User { + + @Transient + public static final String SEQUENCE_NAME = "users_sequence"; + + @Id + private long id; + + private String firstName; + + private String lastName; + + private String email; + + public User() { } + + public User(String firstName, String lastName, String email) { + this.firstName = firstName; + this.lastName = lastName; + this.email = email; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + + @Override + public String toString() { + return "User{" + + "id=" + id + + ", firstName='" + firstName + '\'' + + ", lastName='" + lastName + '\'' + + ", email='" + email + '\'' + + '}'; + } +} diff --git a/spring-boot/src/main/java/com/baeldung/mongodb/services/SequenceGeneratorService.java b/spring-boot/src/main/java/com/baeldung/mongodb/services/SequenceGeneratorService.java new file mode 100755 index 0000000000..52ece27d63 --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/mongodb/services/SequenceGeneratorService.java @@ -0,0 +1,35 @@ +package com.baeldung.mongodb.services; + +import com.baeldung.mongodb.models.DatabaseSequence; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.mongodb.core.MongoOperations; +import org.springframework.data.mongodb.core.query.Update; +import org.springframework.stereotype.Service; + +import java.util.Objects; + +import static org.springframework.data.mongodb.core.FindAndModifyOptions.options; +import static org.springframework.data.mongodb.core.query.Criteria.where; +import static org.springframework.data.mongodb.core.query.Query.query; + + +@Service +public class SequenceGeneratorService { + + + private MongoOperations mongoOperations; + + @Autowired + public SequenceGeneratorService(MongoOperations mongoOperations) { + this.mongoOperations = mongoOperations; + } + + public long generateSequence(String seqName) { + + DatabaseSequence counter = mongoOperations.findAndModify(query(where("_id").is(seqName)), + new Update().inc("seq",1), options().returnNew(true).upsert(true), + DatabaseSequence.class); + return !Objects.isNull(counter) ? counter.getSeq() : 1; + + } +} diff --git a/spring-boot/src/test/java/com/baeldung/mongodb/MongoDbAutoGeneratedFieldIntegrationTest.java b/spring-boot/src/test/java/com/baeldung/mongodb/MongoDbAutoGeneratedFieldIntegrationTest.java new file mode 100644 index 0000000000..3430bca69a --- /dev/null +++ b/spring-boot/src/test/java/com/baeldung/mongodb/MongoDbAutoGeneratedFieldIntegrationTest.java @@ -0,0 +1,36 @@ +package com.baeldung.mongodb; + +import com.baeldung.mongodb.daos.UserRepository; +import com.baeldung.mongodb.models.User; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.junit4.SpringRunner; +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.List; + + +@RunWith(SpringRunner.class) +public class MongoDbAutoGeneratedFieldIntegrationTest { + + @Autowired + private UserRepository userRepository; + + @Test + public void contextLoads() {} + + @Test + public void givenUserObject_whenSave_thenCreateNewUser() { + + User user = new User(); + user.setFirstName("John"); + user.setLastName("Doe"); + user.setEmail("john.doe@example.com"); + userRepository.save(user); + + assertThat(userRepository.findAll().size()).isGreaterThan(0); + } + + +}