products = service.getProducts();
+
+ context.put("products", products);
+
+ Template template = null;
+
+ try {
+ template = getTemplate("templates/index.vm");
+ response.setHeader("Template Returned", "Success");
+ } catch (Exception e) {
+ logger.error("Error while reading the template ", e);
+ }
+
+ return template;
+
+ }
+}
diff --git a/apache-velocity/src/main/resources/logback.xml b/apache-velocity/src/main/resources/logback.xml
new file mode 100644
index 0000000000..70a420a57a
--- /dev/null
+++ b/apache-velocity/src/main/resources/logback.xml
@@ -0,0 +1,23 @@
+
+
+
+
+
+
+
+ %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/apache-velocity/src/main/webapp/WEB-INF/velocity.properties b/apache-velocity/src/main/webapp/WEB-INF/velocity.properties
new file mode 100644
index 0000000000..00e0b7e410
--- /dev/null
+++ b/apache-velocity/src/main/webapp/WEB-INF/velocity.properties
@@ -0,0 +1,4 @@
+resource.loader=webapp
+webapp.resource.loader.class=org.apache.velocity.tools.view.WebappResourceLoader
+webapp.resource.loader.path = .
+webapp.resource.loader.cache = true
\ No newline at end of file
diff --git a/apache-velocity/src/main/webapp/WEB-INF/web.xml b/apache-velocity/src/main/webapp/WEB-INF/web.xml
new file mode 100644
index 0000000000..95b41b36dd
--- /dev/null
+++ b/apache-velocity/src/main/webapp/WEB-INF/web.xml
@@ -0,0 +1,49 @@
+
+
+
+ apache-velocity
+
+ ProductServlet
+ com.baeldung.apache.velocity.servlet.ProductServlet
+
+
+
+ LayoutServlet
+ com.baeldung.apache.velocity.servlet.LayoutServlet
+
+
+ velocityLayout
+ org.apache.velocity.tools.view.VelocityLayoutServlet
+
+
+ org.apache.velocity.properties
+ /WEB-INF/velocity.properties
+
+
+
+ ProductServlet
+ /
+
+
+
+ LayoutServlet
+ /layout
+
+
+ velocityLayout
+ *.vm
+
+
+
+
+ 30
+
+
+
+
+
+ index.html
+
+
diff --git a/apache-velocity/src/main/webapp/fragments/footer.vm b/apache-velocity/src/main/webapp/fragments/footer.vm
new file mode 100644
index 0000000000..41bb36ce5e
--- /dev/null
+++ b/apache-velocity/src/main/webapp/fragments/footer.vm
@@ -0,0 +1,4 @@
+
+ @Copyright baeldung.com
+
\ No newline at end of file
diff --git a/apache-velocity/src/main/webapp/fragments/header.vm b/apache-velocity/src/main/webapp/fragments/header.vm
new file mode 100644
index 0000000000..96700d3baf
--- /dev/null
+++ b/apache-velocity/src/main/webapp/fragments/header.vm
@@ -0,0 +1,5 @@
+
\ No newline at end of file
diff --git a/apache-velocity/src/main/webapp/layout/Default.vm b/apache-velocity/src/main/webapp/layout/Default.vm
new file mode 100644
index 0000000000..39a8b277a5
--- /dev/null
+++ b/apache-velocity/src/main/webapp/layout/Default.vm
@@ -0,0 +1,22 @@
+
+
+ Velocity
+
+
+
+ #parse("/fragments/header.vm")
+
+
+
+
+
+
+ $screen_content
+
+
+
+
+ #parse("/fragments/footer.vm")
+
+
+
\ No newline at end of file
diff --git a/apache-velocity/src/main/webapp/templates/index.vm b/apache-velocity/src/main/webapp/templates/index.vm
new file mode 100644
index 0000000000..0ca07caf42
--- /dev/null
+++ b/apache-velocity/src/main/webapp/templates/index.vm
@@ -0,0 +1,63 @@
+
+
+ Online Electronic Store
+
+
+
+
+
+ Today's Offers
+
+
+ $products.size() Products on Sale!
+
+ We are proud to offer these fine products
+ at these amazing prices.
+
+
+ #set( $count = 1 )
+
+
+ Serial # Product Name Price
+
+ #foreach( $product in $products )
+
+ $count)
+ $product.getName()
+ $product.getPrice()
+
+ #set( $count = $count + 1 )
+ #end
+
+
+
+
+
+
diff --git a/apache-velocity/src/main/webapp/templates/layoutdemo.vm b/apache-velocity/src/main/webapp/templates/layoutdemo.vm
new file mode 100644
index 0000000000..0626b655c9
--- /dev/null
+++ b/apache-velocity/src/main/webapp/templates/layoutdemo.vm
@@ -0,0 +1,27 @@
+#set( $layout = "layout.vm" )
+
+ Today's Offers
+
+
+ $products.size() Products on Sale!
+
+ We are proud to offer these fine products
+ at these amazing prices.
+
+
+ #set( $count = 1 )
+
+
+ Serial # Product Name Price
+
+ #foreach( $product in $products )
+
+ $count)
+ $product.getName()
+ $product.getPrice()
+
+ #set( $count = $count + 1 )
+ #end
+
+
+
\ No newline at end of file
diff --git a/apache-velocity/src/test/java/com/baeldung/apache/velocity/servlet/LayoutServletLiveTest.java b/apache-velocity/src/test/java/com/baeldung/apache/velocity/servlet/LayoutServletLiveTest.java
new file mode 100644
index 0000000000..f1f166b119
--- /dev/null
+++ b/apache-velocity/src/test/java/com/baeldung/apache/velocity/servlet/LayoutServletLiveTest.java
@@ -0,0 +1,26 @@
+package com.baeldung.apache.velocity.servlet;
+
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+
+public class LayoutServletLiveTest {
+
+ @Test
+ public void whenRequestUsingHttpClient_thenCorrectResponse() throws Exception {
+
+ HttpClient client = new DefaultHttpClient();
+ HttpGet method= new HttpGet("http://localhost:8080/layout");
+
+ HttpResponse httpResponse = client.execute(method);
+
+ assertEquals("Success", httpResponse.getHeaders("Template Returned")[0].getValue());
+
+ }
+
+}
diff --git a/apache-velocity/src/test/java/com/baeldung/apache/velocity/servlet/ProductServletLiveTest.java b/apache-velocity/src/test/java/com/baeldung/apache/velocity/servlet/ProductServletLiveTest.java
new file mode 100644
index 0000000000..397e575d4d
--- /dev/null
+++ b/apache-velocity/src/test/java/com/baeldung/apache/velocity/servlet/ProductServletLiveTest.java
@@ -0,0 +1,24 @@
+package com.baeldung.apache.velocity.servlet;
+
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class ProductServletLiveTest {
+
+ @Test
+ public void whenRequestUsingHttpClient_thenCorrectResponse() throws Exception {
+
+ HttpClient client = new DefaultHttpClient();
+ HttpGet method= new HttpGet("http://localhost:8080/");
+
+ HttpResponse httpResponse = client.execute(method);
+
+ assertEquals("Success", httpResponse.getHeaders("Template Returned")[0].getValue());
+
+ }
+}
diff --git a/core-java/0.004102810554955205 b/core-java/0.004102810554955205
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/core-java/0.04832801936270381 b/core-java/0.04832801936270381
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/core-java/0.5633433244738808 b/core-java/0.5633433244738808
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/core-java/0.5967303215007616 b/core-java/0.5967303215007616
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/core-java/0.6256429734439612 b/core-java/0.6256429734439612
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/core-java/0.9252611327674576 b/core-java/0.9252611327674576
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/core-java/0.9799201796740292 b/core-java/0.9799201796740292
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/core-java/README.md b/core-java/README.md
index cd16935864..341dbdf910 100644
--- a/core-java/README.md
+++ b/core-java/README.md
@@ -57,3 +57,4 @@
- [Guide to java.util.concurrent.Future](http://www.baeldung.com/java-future)
- [Guide to java.util.concurrent.BlockingQueue](http://www.baeldung.com/java-blocking-queue)
- [Guide to CountDownLatch in Java](http://www.baeldung.com/java-countdown-latch)
+- [How to Design a Genetic Algorithm in Java](http://www.baeldung.com/java-genetic-algorithm)
diff --git a/core-java/pom.xml b/core-java/pom.xml
index 85afee2968..b2c59989f1 100644
--- a/core-java/pom.xml
+++ b/core-java/pom.xml
@@ -1,5 +1,5 @@
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.baeldung
core-java
@@ -9,7 +9,7 @@
core-java
-
+
net.sourceforge.collections
@@ -63,7 +63,6 @@
grep4j
${grep4j.version}
-
@@ -154,6 +153,12 @@
${mockito.version}
test
+
+ com.jayway.awaitility
+ awaitility
+ ${avaitility.version}
+ test
+
commons-codec
@@ -263,7 +268,8 @@
true
-
+
org.baeldung.executable.ExecutableMavenJar
@@ -371,6 +377,7 @@
1.10.19
6.10
3.6.1
+ 1.7.0
3.6.0
diff --git a/core-java/src/main/java/com/baeldung/algorithms/RunAlgorithm.java b/core-java/src/main/java/com/baeldung/algorithms/RunAlgorithm.java
index 6d696dd272..3f7c8bf4b3 100644
--- a/core-java/src/main/java/com/baeldung/algorithms/RunAlgorithm.java
+++ b/core-java/src/main/java/com/baeldung/algorithms/RunAlgorithm.java
@@ -24,7 +24,8 @@ public class RunAlgorithm {
SlopeOne.slopeOne(3);
break;
case 3:
- SimpleGeneticAlgorithm.runAlgorithm(50, "1011000100000100010000100000100111001000000100000100000000001111");
+ SimpleGeneticAlgorithm ga = new SimpleGeneticAlgorithm();
+ ga.runAlgorithm(50, "1011000100000100010000100000100111001000000100000100000000001111");
break;
default:
System.out.println("Unknown option");
diff --git a/core-java/src/main/java/com/baeldung/algorithms/ga/binary/Individual.java b/core-java/src/main/java/com/baeldung/algorithms/ga/binary/Individual.java
index e9b6c8f66a..2a740777f3 100644
--- a/core-java/src/main/java/com/baeldung/algorithms/ga/binary/Individual.java
+++ b/core-java/src/main/java/com/baeldung/algorithms/ga/binary/Individual.java
@@ -5,40 +5,40 @@ import lombok.Data;
@Data
public class Individual {
- protected int defaultGeneLength = 64;
- private byte[] genes = new byte[defaultGeneLength];
- private int fitness = 0;
+ protected int defaultGeneLength = 64;
+ private byte[] genes = new byte[defaultGeneLength];
+ private int fitness = 0;
- public Individual() {
- for (int i = 0; i < genes.length; i++) {
- byte gene = (byte) Math.round(Math.random());
- genes[i] = gene;
- }
- }
+ public Individual() {
+ for (int i = 0; i < genes.length; i++) {
+ byte gene = (byte) Math.round(Math.random());
+ genes[i] = gene;
+ }
+ }
- protected byte getSingleGene(int index) {
- return genes[index];
- }
+ protected byte getSingleGene(int index) {
+ return genes[index];
+ }
- protected void setSingleGene(int index, byte value) {
- genes[index] = value;
- fitness = 0;
- }
+ protected void setSingleGene(int index, byte value) {
+ genes[index] = value;
+ fitness = 0;
+ }
- public int getFitness() {
- if (fitness == 0) {
- fitness = SimpleGeneticAlgorithm.getFitness(this);
- }
- return fitness;
- }
+ public int getFitness() {
+ if (fitness == 0) {
+ fitness = SimpleGeneticAlgorithm.getFitness(this);
+ }
+ return fitness;
+ }
- @Override
- public String toString() {
- String geneString = "";
- for (int i = 0; i < genes.length; i++) {
- geneString += getSingleGene(i);
- }
- return geneString;
- }
+ @Override
+ public String toString() {
+ String geneString = "";
+ for (int i = 0; i < genes.length; i++) {
+ geneString += getSingleGene(i);
+ }
+ return geneString;
+ }
}
diff --git a/core-java/src/main/java/com/baeldung/algorithms/ga/binary/Population.java b/core-java/src/main/java/com/baeldung/algorithms/ga/binary/Population.java
index dbd1e04e0f..47677d7d88 100644
--- a/core-java/src/main/java/com/baeldung/algorithms/ga/binary/Population.java
+++ b/core-java/src/main/java/com/baeldung/algorithms/ga/binary/Population.java
@@ -8,33 +8,33 @@ import lombok.Data;
@Data
public class Population {
- private List individuals;
+ private List individuals;
- public Population(int size, boolean createNew) {
- individuals = new ArrayList<>();
- if (createNew) {
- createNewPopulation(size);
- }
- }
+ public Population(int size, boolean createNew) {
+ individuals = new ArrayList<>();
+ if (createNew) {
+ createNewPopulation(size);
+ }
+ }
- protected Individual getIndividual(int index) {
- return individuals.get(index);
- }
+ protected Individual getIndividual(int index) {
+ return individuals.get(index);
+ }
- protected Individual getFittest() {
- Individual fittest = individuals.get(0);
- for (int i = 0; i < individuals.size(); i++) {
- if (fittest.getFitness() <= getIndividual(i).getFitness()) {
- fittest = getIndividual(i);
- }
- }
- return fittest;
- }
+ protected Individual getFittest() {
+ Individual fittest = individuals.get(0);
+ for (int i = 0; i < individuals.size(); i++) {
+ if (fittest.getFitness() <= getIndividual(i).getFitness()) {
+ fittest = getIndividual(i);
+ }
+ }
+ return fittest;
+ }
- private void createNewPopulation(int size) {
- for (int i = 0; i < size; i++) {
- Individual newIndividual = new Individual();
- individuals.add(i, newIndividual);
- }
- }
+ private void createNewPopulation(int size) {
+ for (int i = 0; i < size; i++) {
+ Individual newIndividual = new Individual();
+ individuals.add(i, newIndividual);
+ }
+ }
}
diff --git a/core-java/src/main/java/com/baeldung/algorithms/ga/binary/SimpleGeneticAlgorithm.java b/core-java/src/main/java/com/baeldung/algorithms/ga/binary/SimpleGeneticAlgorithm.java
index 0f640e676a..e62eab0d57 100644
--- a/core-java/src/main/java/com/baeldung/algorithms/ga/binary/SimpleGeneticAlgorithm.java
+++ b/core-java/src/main/java/com/baeldung/algorithms/ga/binary/SimpleGeneticAlgorithm.java
@@ -5,115 +5,113 @@ import lombok.Data;
@Data
public class SimpleGeneticAlgorithm {
- private static final double uniformRate = 0.5;
- private static final double mutationRate = 0.025;
- private static final int tournamentSize = 5;
- private static final boolean elitism = true;
- private static byte[] solution = new byte[64];
+ private static final double uniformRate = 0.5;
+ private static final double mutationRate = 0.025;
+ private static final int tournamentSize = 5;
+ private static final boolean elitism = true;
+ private static byte[] solution = new byte[64];
- public static boolean runAlgorithm(int populationSize, String solution) {
- if (solution.length() != SimpleGeneticAlgorithm.solution.length) {
- throw new RuntimeException(
- "The solution needs to have " + SimpleGeneticAlgorithm.solution.length + " bytes");
- }
- SimpleGeneticAlgorithm.setSolution(solution);
- Population myPop = new Population(populationSize, true);
+ public boolean runAlgorithm(int populationSize, String solution) {
+ if (solution.length() != SimpleGeneticAlgorithm.solution.length) {
+ throw new RuntimeException("The solution needs to have " + SimpleGeneticAlgorithm.solution.length + " bytes");
+ }
+ setSolution(solution);
+ Population myPop = new Population(populationSize, true);
- int generationCount = 1;
- while (myPop.getFittest().getFitness() < SimpleGeneticAlgorithm.getMaxFitness()) {
- System.out.println(
- "Generation: " + generationCount + " Correct genes found: " + myPop.getFittest().getFitness());
- myPop = SimpleGeneticAlgorithm.evolvePopulation(myPop);
- generationCount++;
- }
- System.out.println("Solution found!");
- System.out.println("Generation: " + generationCount);
- System.out.println("Genes: ");
- System.out.println(myPop.getFittest());
- return true;
- }
+ int generationCount = 1;
+ while (myPop.getFittest().getFitness() < getMaxFitness()) {
+ System.out.println("Generation: " + generationCount + " Correct genes found: " + myPop.getFittest().getFitness());
+ myPop = evolvePopulation(myPop);
+ generationCount++;
+ }
+ System.out.println("Solution found!");
+ System.out.println("Generation: " + generationCount);
+ System.out.println("Genes: ");
+ System.out.println(myPop.getFittest());
+ return true;
+ }
- public static Population evolvePopulation(Population pop) {
- int elitismOffset;
- Population newPopulation = new Population(pop.getIndividuals().size(), false);
+ public Population evolvePopulation(Population pop) {
+ int elitismOffset;
+ Population newPopulation = new Population(pop.getIndividuals().size(), false);
- if (elitism) {
- newPopulation.getIndividuals().add(0, pop.getFittest());
- elitismOffset = 1;
- } else {
- elitismOffset = 0;
- }
+ if (elitism) {
+ newPopulation.getIndividuals().add(0, pop.getFittest());
+ elitismOffset = 1;
+ } else {
+ elitismOffset = 0;
+ }
- for (int i = elitismOffset; i < pop.getIndividuals().size(); i++) {
- Individual indiv1 = tournamentSelection(pop);
- Individual indiv2 = tournamentSelection(pop);
- Individual newIndiv = crossover(indiv1, indiv2);
- newPopulation.getIndividuals().add(i, newIndiv);
- }
+ for (int i = elitismOffset; i < pop.getIndividuals().size(); i++) {
+ Individual indiv1 = tournamentSelection(pop);
+ Individual indiv2 = tournamentSelection(pop);
+ Individual newIndiv = crossover(indiv1, indiv2);
+ newPopulation.getIndividuals().add(i, newIndiv);
+ }
- for (int i = elitismOffset; i < newPopulation.getIndividuals().size(); i++) {
- mutate(newPopulation.getIndividual(i));
- }
+ for (int i = elitismOffset; i < newPopulation.getIndividuals().size(); i++) {
+ mutate(newPopulation.getIndividual(i));
+ }
- return newPopulation;
- }
+ return newPopulation;
+ }
- private static Individual crossover(Individual indiv1, Individual indiv2) {
- Individual newSol = new Individual();
- for (int i = 0; i < newSol.getDefaultGeneLength(); i++) {
- if (Math.random() <= uniformRate) {
- newSol.setSingleGene(i, indiv1.getSingleGene(i));
- } else {
- newSol.setSingleGene(i, indiv2.getSingleGene(i));
- }
- }
- return newSol;
- }
+ private Individual crossover(Individual indiv1, Individual indiv2) {
+ Individual newSol = new Individual();
+ for (int i = 0; i < newSol.getDefaultGeneLength(); i++) {
+ if (Math.random() <= uniformRate) {
+ newSol.setSingleGene(i, indiv1.getSingleGene(i));
+ } else {
+ newSol.setSingleGene(i, indiv2.getSingleGene(i));
+ }
+ }
+ return newSol;
+ }
- private static void mutate(Individual indiv) {
- for (int i = 0; i < indiv.getDefaultGeneLength(); i++) {
- if (Math.random() <= mutationRate) {
- byte gene = (byte) Math.round(Math.random());
- indiv.setSingleGene(i, gene);
- }
- }
- }
+ private void mutate(Individual indiv) {
+ for (int i = 0; i < indiv.getDefaultGeneLength(); i++) {
+ if (Math.random() <= mutationRate) {
+ byte gene = (byte) Math.round(Math.random());
+ indiv.setSingleGene(i, gene);
+ }
+ }
+ }
- private static Individual tournamentSelection(Population pop) {
- Population tournament = new Population(tournamentSize, false);
- for (int i = 0; i < tournamentSize; i++) {
- int randomId = (int) (Math.random() * pop.getIndividuals().size());
- tournament.getIndividuals().add(i, pop.getIndividual(randomId));
- }
- Individual fittest = tournament.getFittest();
- return fittest;
- }
+ private Individual tournamentSelection(Population pop) {
+ Population tournament = new Population(tournamentSize, false);
+ for (int i = 0; i < tournamentSize; i++) {
+ int randomId = (int) (Math.random() * pop.getIndividuals().size());
+ tournament.getIndividuals().add(i, pop.getIndividual(randomId));
+ }
+ Individual fittest = tournament.getFittest();
+ return fittest;
+ }
- protected static int getFitness(Individual individual) {
- int fitness = 0;
- for (int i = 0; i < individual.getDefaultGeneLength() && i < solution.length; i++) {
- if (individual.getSingleGene(i) == solution[i]) {
- fitness++;
- }
- }
- return fitness;
- }
+ protected static int getFitness(Individual individual) {
+ int fitness = 0;
+ for (int i = 0; i < individual.getDefaultGeneLength() && i < solution.length; i++) {
+ if (individual.getSingleGene(i) == solution[i]) {
+ fitness++;
+ }
+ }
+ return fitness;
+ }
- protected static int getMaxFitness() {
- int maxFitness = solution.length;
- return maxFitness;
- }
+ protected int getMaxFitness() {
+ int maxFitness = solution.length;
+ return maxFitness;
+ }
- protected static void setSolution(String newSolution) {
- solution = new byte[newSolution.length()];
- for (int i = 0; i < newSolution.length(); i++) {
- String character = newSolution.substring(i, i + 1);
- if (character.contains("0") || character.contains("1")) {
- solution[i] = Byte.parseByte(character);
- } else {
- solution[i] = 0;
- }
- }
- }
+ protected void setSolution(String newSolution) {
+ solution = new byte[newSolution.length()];
+ for (int i = 0; i < newSolution.length(); i++) {
+ String character = newSolution.substring(i, i + 1);
+ if (character.contains("0") || character.contains("1")) {
+ solution[i] = Byte.parseByte(character);
+ } else {
+ solution[i] = 0;
+ }
+ }
+ }
}
diff --git a/core-java/src/main/java/com/baeldung/concurrent/future/SquareCalculator.java b/core-java/src/main/java/com/baeldung/concurrent/future/SquareCalculator.java
index e53a2413d1..bcd559dd3b 100644
--- a/core-java/src/main/java/com/baeldung/concurrent/future/SquareCalculator.java
+++ b/core-java/src/main/java/com/baeldung/concurrent/future/SquareCalculator.java
@@ -15,6 +15,6 @@ public class SquareCalculator {
return executor.submit(() -> {
Thread.sleep(1000);
return input * input;
- });
+ });
}
}
diff --git a/core-java/src/main/java/com/baeldung/java_8_features/Person.java b/core-java/src/main/java/com/baeldung/java_8_features/Person.java
new file mode 100644
index 0000000000..83b5530ee8
--- /dev/null
+++ b/core-java/src/main/java/com/baeldung/java_8_features/Person.java
@@ -0,0 +1,27 @@
+package com.baeldung.java_8_features;
+
+public class Person {
+ private String name;
+ private Integer age;
+
+ public Person(String name, int age) {
+ this.name = name;
+ this.age = age;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public Integer getAge() {
+ return age;
+ }
+
+ public void setAge(Integer age) {
+ this.age = age;
+ }
+}
diff --git a/core-java/src/main/java/com/baeldung/java_8_features/groupingby/BlogPost.java b/core-java/src/main/java/com/baeldung/java_8_features/groupingby/BlogPost.java
new file mode 100644
index 0000000000..afc05e356a
--- /dev/null
+++ b/core-java/src/main/java/com/baeldung/java_8_features/groupingby/BlogPost.java
@@ -0,0 +1,36 @@
+package com.baeldung.java_8_features.groupingby;
+
+public class BlogPost {
+ private String title;
+ private String author;
+ private BlogPostType type;
+ private int likes;
+
+ public BlogPost(String title, String author, BlogPostType type, int likes) {
+ this.title = title;
+ this.author = author;
+ this.type = type;
+ this.likes = likes;
+ }
+
+ public String getTitle() {
+ return title;
+ }
+
+ public String getAuthor() {
+ return author;
+ }
+
+ public BlogPostType getType() {
+ return type;
+ }
+
+ public int getLikes() {
+ return likes;
+ }
+
+ @Override
+ public String toString() {
+ return "BlogPost{" + "title='" + title + '\'' + ", type=" + type + ", likes=" + likes + '}';
+ }
+}
diff --git a/core-java/src/main/java/com/baeldung/java_8_features/groupingby/BlogPostType.java b/core-java/src/main/java/com/baeldung/java_8_features/groupingby/BlogPostType.java
new file mode 100644
index 0000000000..2029784e91
--- /dev/null
+++ b/core-java/src/main/java/com/baeldung/java_8_features/groupingby/BlogPostType.java
@@ -0,0 +1,5 @@
+package com.baeldung.java_8_features.groupingby;
+
+public enum BlogPostType {
+ NEWS, REVIEW, GUIDE
+}
diff --git a/core-java/src/main/java/com/baeldung/strategy/ChristmasDiscounter.java b/core-java/src/main/java/com/baeldung/strategy/ChristmasDiscounter.java
new file mode 100644
index 0000000000..a0c36bb63e
--- /dev/null
+++ b/core-java/src/main/java/com/baeldung/strategy/ChristmasDiscounter.java
@@ -0,0 +1,11 @@
+package com.baeldung.strategy;
+
+import java.math.BigDecimal;
+
+public class ChristmasDiscounter implements Discounter {
+
+ @Override
+ public BigDecimal apply(BigDecimal amount) {
+ return amount.multiply(BigDecimal.valueOf(0.9));
+ }
+}
diff --git a/core-java/src/main/java/com/baeldung/strategy/Discounter.java b/core-java/src/main/java/com/baeldung/strategy/Discounter.java
new file mode 100644
index 0000000000..00bf4855d1
--- /dev/null
+++ b/core-java/src/main/java/com/baeldung/strategy/Discounter.java
@@ -0,0 +1,23 @@
+package com.baeldung.strategy;
+
+import java.math.BigDecimal;
+import java.util.function.UnaryOperator;
+
+public interface Discounter extends UnaryOperator {
+
+ default Discounter combine(Discounter after) {
+ return value -> after.apply(this.apply(value));
+ }
+
+ static Discounter christmas() {
+ return (amount) -> amount.multiply(BigDecimal.valueOf(0.9));
+ }
+
+ static Discounter newYear() {
+ return (amount) -> amount.multiply(BigDecimal.valueOf(0.8));
+ }
+
+ static Discounter easter() {
+ return (amount) -> amount.multiply(BigDecimal.valueOf(0.5));
+ }
+}
diff --git a/core-java/src/main/java/com/baeldung/strategy/EasterDiscounter.java b/core-java/src/main/java/com/baeldung/strategy/EasterDiscounter.java
new file mode 100644
index 0000000000..990d10073b
--- /dev/null
+++ b/core-java/src/main/java/com/baeldung/strategy/EasterDiscounter.java
@@ -0,0 +1,11 @@
+package com.baeldung.strategy;
+
+import java.math.BigDecimal;
+
+public class EasterDiscounter implements Discounter {
+
+ @Override
+ public BigDecimal apply(BigDecimal amount) {
+ return amount.multiply(BigDecimal.valueOf(0.5));
+ }
+}
diff --git a/core-java/src/main/java/com/baeldung/stream/InfiniteStreams.java b/core-java/src/main/java/com/baeldung/stream/InfiniteStreams.java
new file mode 100644
index 0000000000..097b516f8c
--- /dev/null
+++ b/core-java/src/main/java/com/baeldung/stream/InfiniteStreams.java
@@ -0,0 +1,27 @@
+package com.baeldung.stream;
+
+
+import java.util.stream.Stream;
+
+public class InfiniteStreams {
+ public static void main(String[] args) {
+ doWhileOldWay();
+
+ doWhileStreamWay();
+
+ }
+
+ private static void doWhileOldWay() {
+
+ int i = 0;
+ while (i < 10) {
+ System.out.println(i);
+ i++;
+ }
+ }
+
+ private static void doWhileStreamWay() {
+ Stream integers = Stream.iterate(0, i -> i + 1);
+ integers.limit(10).forEach(System.out::println);
+ }
+}
diff --git a/core-java/src/main/java/com/baeldung/string/JoinerSplitter.java b/core-java/src/main/java/com/baeldung/string/JoinerSplitter.java
new file mode 100644
index 0000000000..085be66801
--- /dev/null
+++ b/core-java/src/main/java/com/baeldung/string/JoinerSplitter.java
@@ -0,0 +1,36 @@
+package com.baeldung.string;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+public class JoinerSplitter {
+
+ public static String join ( String[] arrayOfString ) {
+ return Arrays.asList(arrayOfString)
+ .stream()
+ .map(x -> x)
+ .collect(Collectors.joining(","));
+ }
+
+ public static String joinWithPrefixPostFix ( String[] arrayOfString ) {
+ return Arrays.asList(arrayOfString)
+ .stream()
+ .map(x -> x)
+ .collect(Collectors.joining(",","[","]"));
+ }
+
+ public static List split ( String str ) {
+ return Stream.of(str.split(","))
+ .map (elem -> new String(elem))
+ .collect(Collectors.toList());
+ }
+
+ public static List splitToListOfChar ( String str ) {
+ return str.chars()
+ .mapToObj(item -> (char) item)
+ .collect(Collectors.toList());
+ }
+
+}
diff --git a/core-java/src/test/java/com/baeldung/CharArrayToStringUnitTest.java b/core-java/src/test/java/com/baeldung/CharArrayToStringUnitTest.java
index 2f7830bbf4..3488f8b390 100644
--- a/core-java/src/test/java/com/baeldung/CharArrayToStringUnitTest.java
+++ b/core-java/src/test/java/com/baeldung/CharArrayToStringUnitTest.java
@@ -8,55 +8,55 @@ public class CharArrayToStringUnitTest {
@Test
public void givenCharArray_whenCallingStringConstructor_shouldConvertToString() {
- char[] charArray = {'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r'};
+ char[] charArray = { 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r' };
String result = new String(charArray);
String expectedValue = "character";
-
+
assertEquals(expectedValue, result);
}
-
+
@Test
- public void givenCharArray_whenCallingStringConstructorWithOffsetAndLength_shouldConvertToString(){
- char[] charArray = {'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r'};
+ public void givenCharArray_whenCallingStringConstructorWithOffsetAndLength_shouldConvertToString() {
+ char[] charArray = { 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r' };
String result = new String(charArray, 4, 3);
String expectedValue = "act";
-
+
assertEquals(expectedValue, result);
}
-
+
@Test
- public void givenCharArray_whenCallingStringCopyValueOf_shouldConvertToString(){
- char[] charArray = {'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r'};
+ public void givenCharArray_whenCallingStringCopyValueOf_shouldConvertToString() {
+ char[] charArray = { 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r' };
String result = String.copyValueOf(charArray);
String expectedValue = "character";
-
+
assertEquals(expectedValue, result);
}
-
+
@Test
- public void givenCharArray_whenCallingStringCopyValueOfWithOffsetAndLength_shouldConvertToString(){
- char[] charArray = {'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r'};
+ public void givenCharArray_whenCallingStringCopyValueOfWithOffsetAndLength_shouldConvertToString() {
+ char[] charArray = { 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r' };
String result = String.copyValueOf(charArray, 0, 4);
String expectedValue = "char";
-
+
assertEquals(expectedValue, result);
}
-
+
@Test
- public void givenCharArray_whenCallingStringValueOf_shouldConvertToString(){
- char[] charArray = {'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r'};
+ public void givenCharArray_whenCallingStringValueOf_shouldConvertToString() {
+ char[] charArray = { 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r' };
String result = String.valueOf(charArray);
String expectedValue = "character";
-
+
assertEquals(expectedValue, result);
}
-
+
@Test
- public void givenCharArray_whenCallingStringValueOfWithOffsetAndLength_shouldConvertToString(){
- char[] charArray = {'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r'};
+ public void givenCharArray_whenCallingStringValueOfWithOffsetAndLength_shouldConvertToString() {
+ char[] charArray = { 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r' };
String result = String.valueOf(charArray, 3, 4);
String expectedValue = "ract";
-
+
assertEquals(expectedValue, result);
}
}
diff --git a/core-java/src/test/java/com/baeldung/StringToCharArrayUnitTest.java b/core-java/src/test/java/com/baeldung/StringToCharArrayUnitTest.java
index 2e7dc24a17..cd996e58e2 100644
--- a/core-java/src/test/java/com/baeldung/StringToCharArrayUnitTest.java
+++ b/core-java/src/test/java/com/baeldung/StringToCharArrayUnitTest.java
@@ -6,15 +6,15 @@ import org.junit.Test;
public class StringToCharArrayUnitTest {
-@Test
-public void givenString_whenCallingStringToCharArray_shouldConvertToCharArray() {
- String givenString = "characters";
+ @Test
+ public void givenString_whenCallingStringToCharArray_shouldConvertToCharArray() {
+ String givenString = "characters";
- char[] result = givenString.toCharArray();
+ char[] result = givenString.toCharArray();
- char[] expectedCharArray = { 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r', 's' };
+ char[] expectedCharArray = { 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r', 's' };
- assertArrayEquals(expectedCharArray, result);
-}
+ assertArrayEquals(expectedCharArray, result);
+ }
}
diff --git a/core-java/src/test/java/com/baeldung/algorithms/BinaryGeneticAlgorithmTest.java b/core-java/src/test/java/com/baeldung/algorithms/BinaryGeneticAlgorithmTest.java
deleted file mode 100644
index 8a16311dfb..0000000000
--- a/core-java/src/test/java/com/baeldung/algorithms/BinaryGeneticAlgorithmTest.java
+++ /dev/null
@@ -1,16 +0,0 @@
-package com.baeldung.algorithms;
-
-import org.junit.Assert;
-import org.junit.Test;
-
-import com.baeldung.algorithms.ga.binary.SimpleGeneticAlgorithm;
-
-public class BinaryGeneticAlgorithmTest {
-
- @Test
- public void testGA() {
- Assert.assertTrue(SimpleGeneticAlgorithm.runAlgorithm(50,
- "1011000100000100010000100000100111001000000100000100000000001111"));
- }
-
-}
diff --git a/core-java/src/test/java/com/baeldung/algorithms/BinaryGeneticAlgorithmUnitTest.java b/core-java/src/test/java/com/baeldung/algorithms/BinaryGeneticAlgorithmUnitTest.java
new file mode 100644
index 0000000000..2e92177c8c
--- /dev/null
+++ b/core-java/src/test/java/com/baeldung/algorithms/BinaryGeneticAlgorithmUnitTest.java
@@ -0,0 +1,16 @@
+package com.baeldung.algorithms;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.baeldung.algorithms.ga.binary.SimpleGeneticAlgorithm;
+
+public class BinaryGeneticAlgorithmUnitTest {
+
+ @Test
+ public void testGA() {
+ SimpleGeneticAlgorithm ga = new SimpleGeneticAlgorithm();
+ Assert.assertTrue(ga.runAlgorithm(50, "1011000100000100010000100000100111001000000100000100000000001111"));
+ }
+
+}
diff --git a/core-java/src/test/java/com/baeldung/concurrent/priorityblockingqueue/PriorityBlockingQueueUnitTest.java b/core-java/src/test/java/com/baeldung/concurrent/priorityblockingqueue/PriorityBlockingQueueUnitTest.java
index 5e6855e18a..0272726465 100644
--- a/core-java/src/test/java/com/baeldung/concurrent/priorityblockingqueue/PriorityBlockingQueueUnitTest.java
+++ b/core-java/src/test/java/com/baeldung/concurrent/priorityblockingqueue/PriorityBlockingQueueUnitTest.java
@@ -32,13 +32,14 @@ public class PriorityBlockingQueueUnitTest {
PriorityBlockingQueue queue = new PriorityBlockingQueue<>();
final Thread thread = new Thread(() -> {
- System.out.println("Polling...");
- while (true) {
- try {
- Integer poll = queue.take();
- System.out.println("Polled: " + poll);
- } catch (InterruptedException e) {}
- }
+ System.out.println("Polling...");
+ while (true) {
+ try {
+ Integer poll = queue.take();
+ System.out.println("Polled: " + poll);
+ } catch (InterruptedException e) {
+ }
+ }
});
thread.start();
diff --git a/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapAggregateStatusTest.java b/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapAggregateStatusManualTest.java
similarity index 97%
rename from core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapAggregateStatusTest.java
rename to core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapAggregateStatusManualTest.java
index 73a4cdc0cd..ec865f71c4 100644
--- a/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapAggregateStatusTest.java
+++ b/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapAggregateStatusManualTest.java
@@ -14,7 +14,7 @@ import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
-public class ConcurrentMapAggregateStatusTest {
+public class ConcurrentMapAggregateStatusManualTest {
private ExecutorService executorService;
private Map concurrentMap;
diff --git a/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapNullKeyValueTest.java b/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapNullKeyValueManualTest.java
similarity index 99%
rename from core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapNullKeyValueTest.java
rename to core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapNullKeyValueManualTest.java
index 62a3d10add..33e3326427 100644
--- a/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapNullKeyValueTest.java
+++ b/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapNullKeyValueManualTest.java
@@ -10,7 +10,7 @@ import java.util.concurrent.ConcurrentMap;
import static org.junit.Assert.assertNull;
-public class ConcurrentMapNullKeyValueTest {
+public class ConcurrentMapNullKeyValueManualTest {
ConcurrentMap concurrentMap;
diff --git a/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapPerformanceTest.java b/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapPerformanceManualTest.java
similarity index 98%
rename from core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapPerformanceTest.java
rename to core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapPerformanceManualTest.java
index a0efa89351..5c1612ca60 100644
--- a/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapPerformanceTest.java
+++ b/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapPerformanceManualTest.java
@@ -11,7 +11,7 @@ import java.util.concurrent.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
-public class ConcurrentMapPerformanceTest {
+public class ConcurrentMapPerformanceManualTest {
@Test
public void givenMaps_whenGetPut500KTimes_thenConcurrentMapFaster() throws Exception {
diff --git a/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentNavigableMapTests.java b/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentNavigableMapManualTests.java
similarity index 95%
rename from core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentNavigableMapTests.java
rename to core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentNavigableMapManualTests.java
index 93087626a4..d102680aa4 100644
--- a/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentNavigableMapTests.java
+++ b/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentNavigableMapManualTests.java
@@ -10,7 +10,7 @@ import java.util.concurrent.atomic.AtomicInteger;
import static org.testng.Assert.*;
-public class ConcurrentNavigableMapTests {
+public class ConcurrentNavigableMapManualTests {
@Test
public void givenSkipListMap_whenAccessInMultiThreads_thenOrderingStable() throws InterruptedException {
@@ -18,9 +18,7 @@ public class ConcurrentNavigableMapTests {
updateMapConcurrently(skipListMap, 4);
- Iterator skipListIter = skipListMap
- .keySet()
- .iterator();
+ Iterator skipListIter = skipListMap.keySet().iterator();
int previous = skipListIter.next();
while (skipListIter.hasNext()) {
int current = skipListIter.next();
diff --git a/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurretMapMemoryConsistencyTest.java b/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurretMapMemoryConsistencyManualTest.java
similarity index 97%
rename from core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurretMapMemoryConsistencyTest.java
rename to core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurretMapMemoryConsistencyManualTest.java
index 63a96dd5ee..43cbb2d293 100644
--- a/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurretMapMemoryConsistencyTest.java
+++ b/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurretMapMemoryConsistencyManualTest.java
@@ -10,7 +10,7 @@ import java.util.concurrent.TimeUnit;
import static org.junit.Assert.*;
-public class ConcurretMapMemoryConsistencyTest {
+public class ConcurretMapMemoryConsistencyManualTest {
@Test
public void givenConcurrentMap_whenSumParallel_thenCorrect() throws Exception {
diff --git a/core-java/src/test/java/com/baeldung/java/concurrentmodification/ConcurrentModificationUnitTest.java b/core-java/src/test/java/com/baeldung/java/concurrentmodification/ConcurrentModificationUnitTest.java
new file mode 100644
index 0000000000..f7a7bd5fe0
--- /dev/null
+++ b/core-java/src/test/java/com/baeldung/java/concurrentmodification/ConcurrentModificationUnitTest.java
@@ -0,0 +1,80 @@
+package com.baeldung.java.concurrentmodification;
+
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.ConcurrentModificationException;
+import java.util.Iterator;
+import java.util.List;
+
+import static java.util.stream.Collectors.toList;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.util.Lists.newArrayList;
+
+public class ConcurrentModificationUnitTest {
+ @Test(expected = ConcurrentModificationException.class)
+ public void givenIterating_whenRemoving_thenThrowException() throws InterruptedException {
+
+ List integers = newArrayList(1, 2, 3);
+
+ for (Integer integer : integers) {
+ integers.remove(1);
+ }
+ }
+
+ @Test
+ public void givenIterating_whenUsingIteratorRemove_thenNoError() throws InterruptedException {
+
+ List integers = newArrayList(1, 2, 3);
+
+ for (Iterator iterator = integers.iterator(); iterator.hasNext();) {
+ Integer integer = iterator.next();
+ if(integer == 2) {
+ iterator.remove();
+ }
+ }
+
+ assertThat(integers).containsExactly(1, 3);
+ }
+
+ @Test
+ public void givenIterating_whenUsingRemovalList_thenNoError() throws InterruptedException {
+
+ List integers = newArrayList(1, 2, 3);
+ List toRemove = newArrayList();
+
+ for (Integer integer : integers) {
+ if(integer == 2) {
+ toRemove.add(integer);
+ }
+ }
+ integers.removeAll(toRemove);
+
+ assertThat(integers).containsExactly(1, 3);
+ }
+
+ @Test
+ public void whenUsingRemoveIf_thenRemoveElements() throws InterruptedException {
+
+ Collection integers = newArrayList(1, 2, 3);
+
+ integers.removeIf(i -> i == 2);
+
+ assertThat(integers).containsExactly(1, 3);
+ }
+
+ @Test
+ public void whenUsingStream_thenRemoveElements() {
+ Collection integers = newArrayList(1, 2, 3);
+
+ List collected = integers
+ .stream()
+ .filter(i -> i != 2)
+ .map(Object::toString)
+ .collect(toList());
+
+ assertThat(collected).containsExactly("1", "3");
+ }
+
+}
diff --git a/core-java/src/test/java/com/baeldung/java8/Java8GroupingByCollectorUnitTest.java b/core-java/src/test/java/com/baeldung/java8/Java8GroupingByCollectorUnitTest.java
new file mode 100644
index 0000000000..4452b4db9a
--- /dev/null
+++ b/core-java/src/test/java/com/baeldung/java8/Java8GroupingByCollectorUnitTest.java
@@ -0,0 +1,231 @@
+package com.baeldung.java8;
+
+import com.baeldung.java_8_features.groupingby.BlogPost;
+import com.baeldung.java_8_features.groupingby.BlogPostType;
+import org.junit.Test;
+
+import java.util.*;
+import java.util.concurrent.ConcurrentMap;
+
+import static java.util.Comparator.comparingInt;
+import static java.util.stream.Collectors.*;
+import static org.junit.Assert.*;
+
+public class Java8GroupingByCollectorUnitTest {
+
+ private static final List posts = Arrays.asList(
+ new BlogPost("News item 1", "Author 1", BlogPostType.NEWS, 15),
+ new BlogPost("Tech review 1", "Author 2", BlogPostType.REVIEW, 5),
+ new BlogPost("Programming guide", "Author 1", BlogPostType.GUIDE, 20),
+ new BlogPost("News item 2", "Author 2", BlogPostType.NEWS, 35),
+ new BlogPost("Tech review 2", "Author 1", BlogPostType.REVIEW, 15));
+
+ @Test
+ public void givenAListOfPosts_whenGroupedByType_thenGetAMapBetweenTypeAndPosts() {
+ Map> postsPerType = posts
+ .stream()
+ .collect(groupingBy(BlogPost::getType));
+
+ assertEquals(2, postsPerType
+ .get(BlogPostType.NEWS)
+ .size());
+ assertEquals(1, postsPerType
+ .get(BlogPostType.GUIDE)
+ .size());
+ assertEquals(2, postsPerType
+ .get(BlogPostType.REVIEW)
+ .size());
+ }
+
+ @Test
+ public void givenAListOfPosts_whenGroupedByTypeAndTheirTitlesAreJoinedInAString_thenGetAMapBetweenTypeAndCsvTitles() {
+ Map postsPerType = posts
+ .stream()
+ .collect(groupingBy(BlogPost::getType, mapping(BlogPost::getTitle, joining(", ", "Post titles: [", "]"))));
+
+ assertEquals("Post titles: [News item 1, News item 2]", postsPerType.get(BlogPostType.NEWS));
+ assertEquals("Post titles: [Programming guide]", postsPerType.get(BlogPostType.GUIDE));
+ assertEquals("Post titles: [Tech review 1, Tech review 2]", postsPerType.get(BlogPostType.REVIEW));
+ }
+
+ @Test
+ public void givenAListOfPosts_whenGroupedByTypeAndSumTheLikes_thenGetAMapBetweenTypeAndPostLikes() {
+ Map likesPerType = posts
+ .stream()
+ .collect(groupingBy(BlogPost::getType, summingInt(BlogPost::getLikes)));
+
+ assertEquals(50, likesPerType
+ .get(BlogPostType.NEWS)
+ .intValue());
+ assertEquals(20, likesPerType
+ .get(BlogPostType.REVIEW)
+ .intValue());
+ assertEquals(20, likesPerType
+ .get(BlogPostType.GUIDE)
+ .intValue());
+ }
+
+ @Test
+ public void givenAListOfPosts_whenGroupedByTypeInAnEnumMap_thenGetAnEnumMapBetweenTypeAndPosts() {
+ EnumMap> postsPerType = posts
+ .stream()
+ .collect(groupingBy(BlogPost::getType, () -> new EnumMap<>(BlogPostType.class), toList()));
+
+ assertEquals(2, postsPerType
+ .get(BlogPostType.NEWS)
+ .size());
+ assertEquals(1, postsPerType
+ .get(BlogPostType.GUIDE)
+ .size());
+ assertEquals(2, postsPerType
+ .get(BlogPostType.REVIEW)
+ .size());
+ }
+
+ @Test
+ public void givenAListOfPosts_whenGroupedByTypeInSets_thenGetAMapBetweenTypesAndSetsOfPosts() {
+ Map> postsPerType = posts
+ .stream()
+ .collect(groupingBy(BlogPost::getType, toSet()));
+
+ assertEquals(2, postsPerType
+ .get(BlogPostType.NEWS)
+ .size());
+ assertEquals(1, postsPerType
+ .get(BlogPostType.GUIDE)
+ .size());
+ assertEquals(2, postsPerType
+ .get(BlogPostType.REVIEW)
+ .size());
+ }
+
+ @Test
+ public void givenAListOfPosts_whenGroupedByTypeConcurrently_thenGetAMapBetweenTypeAndPosts() {
+ ConcurrentMap> postsPerType = posts
+ .parallelStream()
+ .collect(groupingByConcurrent(BlogPost::getType));
+
+ assertEquals(2, postsPerType
+ .get(BlogPostType.NEWS)
+ .size());
+ assertEquals(1, postsPerType
+ .get(BlogPostType.GUIDE)
+ .size());
+ assertEquals(2, postsPerType
+ .get(BlogPostType.REVIEW)
+ .size());
+ }
+
+ @Test
+ public void givenAListOfPosts_whenGroupedByTypeAndAveragingLikes_thenGetAMapBetweenTypeAndAverageNumberOfLikes() {
+ Map averageLikesPerType = posts
+ .stream()
+ .collect(groupingBy(BlogPost::getType, averagingInt(BlogPost::getLikes)));
+
+ assertEquals(25, averageLikesPerType
+ .get(BlogPostType.NEWS)
+ .intValue());
+ assertEquals(20, averageLikesPerType
+ .get(BlogPostType.GUIDE)
+ .intValue());
+ assertEquals(10, averageLikesPerType
+ .get(BlogPostType.REVIEW)
+ .intValue());
+ }
+
+ @Test
+ public void givenAListOfPosts_whenGroupedByTypeAndCounted_thenGetAMapBetweenTypeAndNumberOfPosts() {
+ Map numberOfPostsPerType = posts
+ .stream()
+ .collect(groupingBy(BlogPost::getType, counting()));
+
+ assertEquals(2, numberOfPostsPerType
+ .get(BlogPostType.NEWS)
+ .intValue());
+ assertEquals(1, numberOfPostsPerType
+ .get(BlogPostType.GUIDE)
+ .intValue());
+ assertEquals(2, numberOfPostsPerType
+ .get(BlogPostType.REVIEW)
+ .intValue());
+ }
+
+ @Test
+ public void givenAListOfPosts_whenGroupedByTypeAndMaxingLikes_thenGetAMapBetweenTypeAndMaximumNumberOfLikes() {
+ Map> maxLikesPerPostType = posts
+ .stream()
+ .collect(groupingBy(BlogPost::getType, maxBy(comparingInt(BlogPost::getLikes))));
+
+ assertTrue(maxLikesPerPostType
+ .get(BlogPostType.NEWS)
+ .isPresent());
+ assertEquals(35, maxLikesPerPostType
+ .get(BlogPostType.NEWS)
+ .get()
+ .getLikes());
+
+ assertTrue(maxLikesPerPostType
+ .get(BlogPostType.GUIDE)
+ .isPresent());
+ assertEquals(20, maxLikesPerPostType
+ .get(BlogPostType.GUIDE)
+ .get()
+ .getLikes());
+
+ assertTrue(maxLikesPerPostType
+ .get(BlogPostType.REVIEW)
+ .isPresent());
+ assertEquals(15, maxLikesPerPostType
+ .get(BlogPostType.REVIEW)
+ .get()
+ .getLikes());
+ }
+
+ @Test
+ public void givenAListOfPosts_whenGroupedByAuthorAndThenByType_thenGetAMapBetweenAuthorAndMapsBetweenTypeAndBlogPosts() {
+ Map>> map = posts
+ .stream()
+ .collect(groupingBy(BlogPost::getAuthor, groupingBy(BlogPost::getType)));
+
+ assertEquals(1, map
+ .get("Author 1")
+ .get(BlogPostType.NEWS)
+ .size());
+ assertEquals(1, map
+ .get("Author 1")
+ .get(BlogPostType.GUIDE)
+ .size());
+ assertEquals(1, map
+ .get("Author 1")
+ .get(BlogPostType.REVIEW)
+ .size());
+
+ assertEquals(1, map
+ .get("Author 2")
+ .get(BlogPostType.NEWS)
+ .size());
+ assertEquals(1, map
+ .get("Author 2")
+ .get(BlogPostType.REVIEW)
+ .size());
+ assertNull(map
+ .get("Author 2")
+ .get(BlogPostType.GUIDE));
+ }
+
+ @Test
+ public void givenAListOfPosts_whenGroupedByTypeAndSummarizingLikes_thenGetAMapBetweenTypeAndSummary() {
+ Map likeStatisticsPerType = posts
+ .stream()
+ .collect(groupingBy(BlogPost::getType, summarizingInt(BlogPost::getLikes)));
+
+ IntSummaryStatistics newsLikeStatistics = likeStatisticsPerType.get(BlogPostType.NEWS);
+
+ assertEquals(2, newsLikeStatistics.getCount());
+ assertEquals(50, newsLikeStatistics.getSum());
+ assertEquals(25.0, newsLikeStatistics.getAverage(), 0.001);
+ assertEquals(35, newsLikeStatistics.getMax());
+ assertEquals(15, newsLikeStatistics.getMin());
+ }
+
+}
diff --git a/core-java/src/test/java/com/baeldung/java8/Java8MaxMinTest.java b/core-java/src/test/java/com/baeldung/java8/Java8MaxMinTest.java
new file mode 100644
index 0000000000..b0e514124d
--- /dev/null
+++ b/core-java/src/test/java/com/baeldung/java8/Java8MaxMinTest.java
@@ -0,0 +1,47 @@
+package com.baeldung.java8;
+
+import com.baeldung.java_8_features.Person;
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.List;
+import java.util.NoSuchElementException;
+
+import static org.junit.Assert.assertEquals;
+
+public class Java8MaxMinTest {
+
+ @Test
+ public void whenListIsOfIntegerThenMaxCanBeDoneUsingIntegerComparator() {
+ //given
+ final List listOfIntegers = Arrays.asList(1, 2, 3, 4, 56, 7, 89, 10);
+ final Integer expectedResult = 89;
+
+ //then
+ final Integer max = listOfIntegers
+ .stream()
+ .mapToInt(v -> v)
+ .max().orElseThrow(NoSuchElementException::new);
+
+ assertEquals("Should be 89", expectedResult, max);
+ }
+
+ @Test
+ public void whenListIsOfPersonObjectThenMinCanBeDoneUsingCustomComparatorThroughLambda() {
+ //given
+ final Person alex = new Person("Alex", 23);
+ final Person john = new Person("John", 40);
+ final Person peter = new Person("Peter", 32);
+ final List people = Arrays.asList(alex, john, peter);
+
+ //then
+ final Person minByAge = people
+ .stream()
+ .min(Comparator.comparing(Person::getAge))
+ .orElseThrow(NoSuchElementException::new);
+
+ assertEquals("Should be Alex", alex, minByAge);
+ }
+
+}
diff --git a/core-java/src/test/java/com/baeldung/test/comparison/DivisibilityTest.java b/core-java/src/test/java/com/baeldung/junit4vstestng/DivisibilityTest.java
similarity index 74%
rename from core-java/src/test/java/com/baeldung/test/comparison/DivisibilityTest.java
rename to core-java/src/test/java/com/baeldung/junit4vstestng/DivisibilityTest.java
index 9ae13f5934..dec7dbe6aa 100644
--- a/core-java/src/test/java/com/baeldung/test/comparison/DivisibilityTest.java
+++ b/core-java/src/test/java/com/baeldung/junit4vstestng/DivisibilityTest.java
@@ -1,4 +1,4 @@
-package com.baeldung.test.comparison;
+package com.baeldung.junit4vstestng;
import static org.junit.Assert.assertEquals;
@@ -15,7 +15,7 @@ public class DivisibilityTest {
}
@Test
- public void givenNumber_whenDivisiblebyTwo_thenCorrect() {
+ public void givenNumber_whenDivisibleByTwo_thenCorrect() {
assertEquals(number % 2, 0);
}
}
diff --git a/core-java/src/test/java/com/baeldung/junit4vstestng/ParametrizedTests.java b/core-java/src/test/java/com/baeldung/junit4vstestng/ParametrizedTests.java
new file mode 100644
index 0000000000..e9a9c6a07a
--- /dev/null
+++ b/core-java/src/test/java/com/baeldung/junit4vstestng/ParametrizedTests.java
@@ -0,0 +1,34 @@
+package com.baeldung.junit4vstestng;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+import java.util.Arrays;
+import java.util.Collection;
+
+@RunWith(value = Parameterized.class)
+public class ParametrizedTests {
+
+ private int value;
+ private boolean isEven;
+
+ public ParametrizedTests(int value, boolean isEven) {
+ this.value = value;
+ this.isEven = isEven;
+ }
+
+ @Parameters
+ public static Collection data() {
+ Object[][] data = new Object[][]{{1, false}, {2, true}, {4, true}};
+ return Arrays.asList(data);
+ }
+
+ @Test
+ public void givenParametrizedNumber_ifEvenCheckOK_thenCorrect() {
+ Assert.assertEquals(isEven, value % 2 == 0);
+ }
+}
diff --git a/core-java/src/test/java/com/baeldung/junit4vstestng/RegistrationTest.java b/core-java/src/test/java/com/baeldung/junit4vstestng/RegistrationTest.java
new file mode 100644
index 0000000000..4aa1fa1a17
--- /dev/null
+++ b/core-java/src/test/java/com/baeldung/junit4vstestng/RegistrationTest.java
@@ -0,0 +1,15 @@
+package com.baeldung.junit4vstestng;
+
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+public class RegistrationTest {
+ private static final Logger LOGGER = LoggerFactory.getLogger(RegistrationTest.class);
+
+ @Test
+ public void whenCalledFromSuite_thanOK() {
+ LOGGER.info("Registration successful");
+ }
+}
diff --git a/core-java/src/test/java/com/baeldung/junit4vstestng/SignInTest.java b/core-java/src/test/java/com/baeldung/junit4vstestng/SignInTest.java
new file mode 100644
index 0000000000..bb908ff37e
--- /dev/null
+++ b/core-java/src/test/java/com/baeldung/junit4vstestng/SignInTest.java
@@ -0,0 +1,14 @@
+package com.baeldung.junit4vstestng;
+
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class SignInTest {
+ private static final Logger LOGGER = LoggerFactory.getLogger(SignInTest.class);
+
+ @Test
+ public void whenCalledFromSuite_thanOK() {
+ LOGGER.info("SignIn successful");
+ }
+}
diff --git a/core-java/src/test/java/com/baeldung/test/comparison/StringCaseTest.java b/core-java/src/test/java/com/baeldung/junit4vstestng/StringCaseTest.java
similarity index 90%
rename from core-java/src/test/java/com/baeldung/test/comparison/StringCaseTest.java
rename to core-java/src/test/java/com/baeldung/junit4vstestng/StringCaseTest.java
index b4226b82e7..520017e4c1 100644
--- a/core-java/src/test/java/com/baeldung/test/comparison/StringCaseTest.java
+++ b/core-java/src/test/java/com/baeldung/junit4vstestng/StringCaseTest.java
@@ -1,4 +1,4 @@
-package com.baeldung.test.comparison;
+package com.baeldung.junit4vstestng;
import static org.junit.Assert.assertEquals;
diff --git a/core-java/src/test/java/com/baeldung/test/comparison/SuiteTest.java b/core-java/src/test/java/com/baeldung/junit4vstestng/SuiteTest.java
similarity index 52%
rename from core-java/src/test/java/com/baeldung/test/comparison/SuiteTest.java
rename to core-java/src/test/java/com/baeldung/junit4vstestng/SuiteTest.java
index a30e5d312a..effd7fa10a 100644
--- a/core-java/src/test/java/com/baeldung/test/comparison/SuiteTest.java
+++ b/core-java/src/test/java/com/baeldung/junit4vstestng/SuiteTest.java
@@ -1,10 +1,10 @@
-package com.baeldung.test.comparison;
+package com.baeldung.junit4vstestng;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
-@Suite.SuiteClasses({ StringCaseTest.class, DivisibilityTest.class })
+@Suite.SuiteClasses({ RegistrationTest.class, SignInTest.class })
public class SuiteTest {
}
diff --git a/core-java/src/test/java/com/baeldung/test/comparison/SummationServiceTest.java b/core-java/src/test/java/com/baeldung/junit4vstestng/SummationServiceTest.java
similarity index 94%
rename from core-java/src/test/java/com/baeldung/test/comparison/SummationServiceTest.java
rename to core-java/src/test/java/com/baeldung/junit4vstestng/SummationServiceTest.java
index b76a87c0fe..7d1bf3b7af 100644
--- a/core-java/src/test/java/com/baeldung/test/comparison/SummationServiceTest.java
+++ b/core-java/src/test/java/com/baeldung/junit4vstestng/SummationServiceTest.java
@@ -1,8 +1,4 @@
-package com.baeldung.test.comparison;
-
-import java.security.Security;
-import java.util.ArrayList;
-import java.util.List;
+package com.baeldung.junit4vstestng;
import org.junit.After;
import org.junit.AfterClass;
@@ -12,6 +8,9 @@ import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
+import java.util.ArrayList;
+import java.util.List;
+
public class SummationServiceTest {
private static List numbers;
diff --git a/core-java/src/test/java/com/baeldung/strategy/StrategyDesignPatternUnitTest.java b/core-java/src/test/java/com/baeldung/strategy/StrategyDesignPatternUnitTest.java
new file mode 100644
index 0000000000..7ca1d000be
--- /dev/null
+++ b/core-java/src/test/java/com/baeldung/strategy/StrategyDesignPatternUnitTest.java
@@ -0,0 +1,73 @@
+package com.baeldung.strategy;
+
+import org.junit.Test;
+
+import java.math.BigDecimal;
+import java.util.Arrays;
+import java.util.List;
+import java.util.function.Function;
+
+import static com.baeldung.strategy.Discounter.*;
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class StrategyDesignPatternUnitTest {
+ @Test
+ public void shouldDivideByTwo_WhenApplyingStaffDiscounter() {
+ Discounter staffDiscounter = new EasterDiscounter();
+
+ final BigDecimal discountedValue = staffDiscounter
+ .apply(BigDecimal.valueOf(100));
+
+ assertThat(discountedValue)
+ .isEqualByComparingTo(BigDecimal.valueOf(50));
+ }
+
+ @Test
+ public void shouldDivideByTwo_WhenApplyingStaffDiscounterWithAnonyousTypes() {
+ Discounter staffDiscounter = new Discounter() {
+ @Override
+ public BigDecimal apply( BigDecimal amount) {
+ return amount.multiply(BigDecimal.valueOf(0.5));
+ }
+ };
+
+ final BigDecimal discountedValue = staffDiscounter
+ .apply(BigDecimal.valueOf(100));
+
+ assertThat(discountedValue)
+ .isEqualByComparingTo(BigDecimal.valueOf(50));
+ }
+
+ @Test
+ public void shouldDivideByTwo_WhenApplyingStaffDiscounterWithLamda() {
+ Discounter staffDiscounter = amount -> amount.multiply(BigDecimal.valueOf(0.5));
+
+ final BigDecimal discountedValue = staffDiscounter
+ .apply(BigDecimal.valueOf(100));
+
+ assertThat(discountedValue)
+ .isEqualByComparingTo(BigDecimal.valueOf(50));
+ }
+
+ @Test
+ public void shouldApplyAllDiscounts() {
+ List discounters = Arrays.asList(christmas(), newYear(), easter());
+
+ BigDecimal amount = BigDecimal.valueOf(100);
+
+ final Discounter combinedDiscounter = discounters
+ .stream()
+ .reduce(v -> v, Discounter::combine);
+
+ combinedDiscounter.apply(amount);
+ }
+
+ @Test
+ public void shouldChainDiscounters() {
+ final Function combinedDiscounters = Discounter
+ .christmas()
+ .andThen(newYear());
+
+ combinedDiscounters.apply(BigDecimal.valueOf(100));
+ }
+}
diff --git a/core-java/src/test/java/com/baeldung/stream/InfiniteStreamTest.java b/core-java/src/test/java/com/baeldung/stream/InfiniteStreamTest.java
new file mode 100644
index 0000000000..a1537a1735
--- /dev/null
+++ b/core-java/src/test/java/com/baeldung/stream/InfiniteStreamTest.java
@@ -0,0 +1,48 @@
+package com.baeldung.stream;
+
+
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Random;
+import java.util.UUID;
+import java.util.function.Supplier;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import static org.junit.Assert.assertEquals;
+
+public class InfiniteStreamTest {
+
+ @Test
+ public void givenInfiniteStream_whenUseIntermediateLimitMethod_thenShouldTerminateInFiniteTime() {
+ //given
+ Stream infiniteStream = Stream.iterate(0, i -> i + 2);
+
+ //when
+ List collect = infiniteStream
+ .limit(10)
+ .collect(Collectors.toList());
+
+ //then
+ assertEquals(collect, Arrays.asList(0, 2, 4, 6, 8, 10, 12, 14, 16, 18));
+ }
+
+ @Test
+ public void givenInfiniteStreamOfRandomInts_whenUseLimit_shouldTerminateInFiniteTime() {
+ //given
+ Supplier randomUUIDSupplier = UUID::randomUUID;
+ Stream infiniteStreamOfRandomUUID = Stream.generate(randomUUIDSupplier);
+
+ //when
+ List randomInts = infiniteStreamOfRandomUUID
+ .skip(10)
+ .limit(10)
+ .collect(Collectors.toList());
+
+ //then
+ assertEquals(randomInts.size(), 10);
+ }
+
+}
diff --git a/core-java/src/test/java/com/baeldung/string/JoinerSplitterTest.java b/core-java/src/test/java/com/baeldung/string/JoinerSplitterTest.java
new file mode 100644
index 0000000000..a89f89b8d5
--- /dev/null
+++ b/core-java/src/test/java/com/baeldung/string/JoinerSplitterTest.java
@@ -0,0 +1,68 @@
+package com.baeldung.string;
+
+import static org.junit.Assert.*;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.junit.Test;
+
+import com.baeldung.string.JoinerSplitter;
+
+public class JoinerSplitterTest {
+
+ @Test
+ public void provided_array_convert_to_stream_and_convert_to_string() {
+
+ String[] programming_languages = {"java", "python", "nodejs", "ruby"};
+
+ String expectation = "java,python,nodejs,ruby";
+
+ String result = JoinerSplitter.join(programming_languages);
+ assertEquals(result, expectation);
+ }
+
+ @Test
+ public void givenArray_transformedToStream_convertToPrefixPostfixString() {
+
+ String[] programming_languages = {"java", "python",
+ "nodejs", "ruby"};
+ String expectation = "[java,python,nodejs,ruby]";
+
+ String result = JoinerSplitter.joinWithPrefixPostFix(programming_languages);
+ assertEquals(result, expectation);
+ }
+
+ @Test
+ public void givenString_transformedToStream_convertToList() {
+
+ String programming_languages = "java,python,nodejs,ruby";
+
+ List expectation = new ArrayList();
+ expectation.add("java");
+ expectation.add("python");
+ expectation.add("nodejs");
+ expectation.add("ruby");
+
+ List result = JoinerSplitter.split(programming_languages);
+
+ assertEquals(result, expectation);
+ }
+
+ @Test
+ public void givenString_transformedToStream_convertToListOfChar() {
+
+ String programming_languages = "java,python,nodejs,ruby";
+
+ List expectation = new ArrayList();
+ char[] charArray = programming_languages.toCharArray();
+ for (char c : charArray) {
+ expectation.add(c);
+ }
+
+ List result = JoinerSplitter.splitToListOfChar(programming_languages);
+ assertEquals(result, expectation);
+
+ }
+
+}
diff --git a/core-java/src/test/java/com/baeldung/test/comparison/DependentTests.java b/core-java/src/test/java/com/baeldung/test/comparison/DependentTests.java
deleted file mode 100644
index 3ef4949067..0000000000
--- a/core-java/src/test/java/com/baeldung/test/comparison/DependentTests.java
+++ /dev/null
@@ -1,46 +0,0 @@
-package com.baeldung.test.comparison;
-
-import org.testng.Assert;
-import org.testng.annotations.BeforeClass;
-import org.testng.annotations.Test;
-
-public class DependentTests {
-
- private EmailValidator emailValidator;
- private LoginValidator loginValidator;
- private String validEmail = "abc@qwe.com";
-
- @BeforeClass
- public void setup() {
- emailValidator = new EmailValidator();
- loginValidator = new LoginValidator();
- }
-
- @Test
- public void givenEmail_ifValid_thenTrue() {
- boolean valid = emailValidator.validate(validEmail);
- Assert.assertEquals(valid, true);
- }
-
- @Test(dependsOnMethods = { "givenEmail_ifValid_thenTrue" })
- public void givenValidEmail_whenLoggedin_thenTrue() {
- boolean valid = loginValidator.validate();
- Assert.assertEquals(valid, true);
- }
-}
-
-class EmailValidator {
-
- public boolean validate(String validEmail) {
- return true;
- }
-
-}
-
-class LoginValidator {
-
- public boolean validate() {
- return true;
- }
-
-}
diff --git a/core-java/src/test/java/com/baeldung/test/comparison/MyParameterisedUnitTest.java b/core-java/src/test/java/com/baeldung/test/comparison/MyParameterisedUnitTest.java
deleted file mode 100644
index 3372bbb577..0000000000
--- a/core-java/src/test/java/com/baeldung/test/comparison/MyParameterisedUnitTest.java
+++ /dev/null
@@ -1,49 +0,0 @@
-package com.baeldung.test.comparison;
-
-import java.util.Arrays;
-import java.util.Collection;
-
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
-import org.junit.runners.Parameterized.Parameters;
-
-@RunWith(value = Parameterized.class)
-public class MyParameterisedUnitTest {
-
- private String name;
- private NameCheck nameCheck;
-
- @Before
- public void initialSetup() {
- nameCheck = new NameCheck();
- }
-
- public MyParameterisedUnitTest(String myName) {
- this.name = myName;
- }
-
- @Parameters
- public static Collection data() {
- Object[][] data = new Object[][] { { "Peter" }, { "Sam" }, { "Tim" }, { "Lucy" } };
- return Arrays.asList(data);
- }
-
- @Test
- public void givenName_whenValidLength_thenTrue() {
- boolean valid = nameCheck.nameCheck(name);
- Assert.assertEquals(valid, true);
- }
-}
-
-class NameCheck {
-
- public boolean nameCheck(String name) {
- if (name.length() > 0)
- return true;
- return false;
- }
-
-}
diff --git a/core-java/src/test/java/com/baeldung/test/comparison/MyParameterisedUnitTestNg.java b/core-java/src/test/java/com/baeldung/test/comparison/MyParameterisedUnitTestNg.java
deleted file mode 100644
index 4096c3fb6f..0000000000
--- a/core-java/src/test/java/com/baeldung/test/comparison/MyParameterisedUnitTestNg.java
+++ /dev/null
@@ -1,81 +0,0 @@
-package com.baeldung.test.comparison;
-
-import org.testng.Assert;
-import org.testng.annotations.BeforeClass;
-import org.testng.annotations.DataProvider;
-import org.testng.annotations.Parameters;
-import org.testng.annotations.Test;
-
-public class MyParameterisedUnitTestNg {
-
- private PrimeNumberCheck primeNumberChecker;
-
- @BeforeClass
- public void intialSetup() {
- primeNumberChecker = new PrimeNumberCheck();
- }
-
- @Test(enabled = false)
- @Parameters({ "num", "expectedResult" })
- public void givenNumber_ifPrime_thenCorrect(int number, boolean expectedResult) {
- Assert.assertEquals(expectedResult, primeNumberChecker.validate(number));
- }
-
- @DataProvider(name = "test1")
- public static Object[][] primeNumbers() {
- return new Object[][] { { 2, true }, { 6, false }, { 19, true }, { 22, false }, { 23, true } };
- }
-
- @Test(dataProvider = "test1")
- public void givenNumber_whenPrime_thenCorrect(Integer inputNumber, Boolean expectedResult) {
- Assert.assertEquals(expectedResult, primeNumberChecker.validate(inputNumber));
- }
-
- @Test(dataProvider = "myDataProvider")
- public void parameterCheckTest(User user) {
- Assert.assertEquals("sam", user.getName());
- Assert.assertEquals(12, user.getAge());
- }
-
- @DataProvider(name = "myDataProvider")
- public Object[][] parameterProvider() {
- User usr = new User();
- usr.setName("sam");
- usr.setAge(12);
- return new Object[][] { { usr } };
- }
-
-}
-
-class PrimeNumberCheck {
-
- public Object validate(int number) {
- for (int i = 2; i < number; i++) {
- if (number % i == 0)
- return false;
- }
- return true;
- }
-
-}
-
-class User {
- private String name;
- private int age;
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public int getAge() {
- return age;
- }
-
- public void setAge(int age) {
- this.age = age;
- }
-}
diff --git a/core-java/src/test/java/com/baeldung/test/comparison/RegistrationTest.java b/core-java/src/test/java/com/baeldung/test/comparison/RegistrationTest.java
deleted file mode 100644
index 86836425a9..0000000000
--- a/core-java/src/test/java/com/baeldung/test/comparison/RegistrationTest.java
+++ /dev/null
@@ -1,11 +0,0 @@
-package com.baeldung.test.comparison;
-
-import org.testng.annotations.Test;
-
-public class RegistrationTest {
-
- @Test
- public void givenEmail_ifValid_thenCorrect() {
-
- }
-}
diff --git a/core-java/src/test/java/com/baeldung/test/comparison/SignInTest.java b/core-java/src/test/java/com/baeldung/test/comparison/SignInTest.java
deleted file mode 100644
index 9669f60b6b..0000000000
--- a/core-java/src/test/java/com/baeldung/test/comparison/SignInTest.java
+++ /dev/null
@@ -1,12 +0,0 @@
-package com.baeldung.test.comparison;
-
-import org.testng.annotations.Test;
-
-public class SignInTest {
-
- @Test
- public void givenUsername_ifValid_thenCorrect() {
-
- }
-
-}
diff --git a/core-java/src/test/java/com/baeldung/weakhashmap/WeakHashMapTest.java b/core-java/src/test/java/com/baeldung/weakhashmap/WeakHashMapTest.java
new file mode 100644
index 0000000000..1c5a261eea
--- /dev/null
+++ b/core-java/src/test/java/com/baeldung/weakhashmap/WeakHashMapTest.java
@@ -0,0 +1,72 @@
+package com.baeldung.weakhashmap;
+
+
+import org.junit.Test;
+
+import java.util.WeakHashMap;
+import java.util.concurrent.TimeUnit;
+
+import static com.jayway.awaitility.Awaitility.await;
+import static org.junit.Assert.assertTrue;
+
+public class WeakHashMapTest {
+
+ @Test
+ public void givenWeakHashMap_whenCacheValueThatHasNoReferenceToIt_GCShouldReclaimThatObject() {
+ //given
+ WeakHashMap map = new WeakHashMap<>();
+ BigImage bigImage = new BigImage("image_id");
+ UniqueImageName imageName = new UniqueImageName("name_of_big_image");
+
+ map.put(imageName, bigImage);
+ assertTrue(map.containsKey(imageName));
+
+ //when big image key is not reference anywhere
+ imageName = null;
+ System.gc();
+
+ //then GC will finally reclaim that object
+ await().atMost(10, TimeUnit.SECONDS).until(map::isEmpty);
+ }
+
+ @Test
+ public void givenWeakHashMap_whenCacheValueThatHasNoReferenceToIt_GCShouldReclaimThatObjectButLeaveReferencedObject() {
+ //given
+ WeakHashMap map = new WeakHashMap<>();
+ BigImage bigImageFirst = new BigImage("foo");
+ UniqueImageName imageNameFirst = new UniqueImageName("name_of_big_image");
+
+ BigImage bigImageSecond = new BigImage("foo_2");
+ UniqueImageName imageNameSecond = new UniqueImageName("name_of_big_image_2");
+
+ map.put(imageNameFirst, bigImageFirst);
+ map.put(imageNameSecond, bigImageSecond);
+ assertTrue(map.containsKey(imageNameFirst));
+ assertTrue(map.containsKey(imageNameSecond));
+
+ //when
+ imageNameFirst = null;
+ System.gc();
+
+ //then
+ await().atMost(10, TimeUnit.SECONDS).until(() -> map.size() == 1);
+ await().atMost(10, TimeUnit.SECONDS).until(() -> map.containsKey(imageNameSecond));
+ }
+
+
+ class BigImage {
+ public final String imageId;
+
+ BigImage(String imageId) {
+ this.imageId = imageId;
+ }
+ }
+
+ class UniqueImageName {
+ public final String imageName;
+
+ UniqueImageName(String imageName) {
+ this.imageName = imageName;
+ }
+ }
+}
diff --git a/core-java/src/test/java/org/baeldung/java/JavaRandomUnitTest.java b/core-java/src/test/java/org/baeldung/java/JavaRandomUnitTest.java
index 4febe7c9fc..08f98025c3 100644
--- a/core-java/src/test/java/org/baeldung/java/JavaRandomUnitTest.java
+++ b/core-java/src/test/java/org/baeldung/java/JavaRandomUnitTest.java
@@ -1,12 +1,12 @@
package org.baeldung.java;
-import java.nio.charset.Charset;
-import java.util.Random;
-
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.math3.random.RandomDataGenerator;
import org.junit.Test;
+import java.nio.charset.Charset;
+import java.util.Random;
+
public class JavaRandomUnitTest {
// tests - random long
@@ -164,7 +164,7 @@ public class JavaRandomUnitTest {
final int targetStringLength = 10;
final StringBuilder buffer = new StringBuilder(targetStringLength);
for (int i = 0; i < targetStringLength; i++) {
- final int randomLimitedInt = leftLimit + (int) (new Random().nextFloat() * (rightLimit - leftLimit));
+ final int randomLimitedInt = leftLimit + (int) (new Random().nextFloat() * (rightLimit - leftLimit + 1));
buffer.append((char) randomLimitedInt);
}
final String generatedString = buffer.toString();
diff --git a/core-java/src/test/java/org/baeldung/java/streams/ThreadPoolInParallelStreamTest.java b/core-java/src/test/java/org/baeldung/java/streams/ThreadPoolInParallelStreamTest.java
index 5fd3fa4cb0..c2eb1cff5d 100644
--- a/core-java/src/test/java/org/baeldung/java/streams/ThreadPoolInParallelStreamTest.java
+++ b/core-java/src/test/java/org/baeldung/java/streams/ThreadPoolInParallelStreamTest.java
@@ -1,5 +1,5 @@
package org.baeldung.java.streams;
-
+
import org.junit.Test;
import java.util.ArrayList;
@@ -14,28 +14,25 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class ThreadPoolInParallelStreamTest {
-
+
@Test
- public void giveRangeOfLongs_whenSummedInParallel_shouldBeEqualToExpectedTotal()
- throws InterruptedException, ExecutionException {
+ public void giveRangeOfLongs_whenSummedInParallel_shouldBeEqualToExpectedTotal() throws InterruptedException, ExecutionException {
long firstNum = 1;
long lastNum = 1_000_000;
- List aList = LongStream.rangeClosed(firstNum, lastNum).boxed()
- .collect(Collectors.toList());
+ List aList = LongStream.rangeClosed(firstNum, lastNum).boxed().collect(Collectors.toList());
ForkJoinPool customThreadPool = new ForkJoinPool(4);
- long actualTotal = customThreadPool.submit(() -> aList.parallelStream()
- .reduce(0L, Long::sum)).get();
-
- assertEquals((lastNum + firstNum) * lastNum / 2, actualTotal);
+ long actualTotal = customThreadPool.submit(() -> aList.parallelStream().reduce(0L, Long::sum)).get();
+
+ assertEquals((lastNum + firstNum) * lastNum / 2, actualTotal);
}
-
+
@Test
- public void givenList_whenCallingParallelStream_shouldBeParallelStream(){
+ public void givenList_whenCallingParallelStream_shouldBeParallelStream() {
List aList = new ArrayList<>();
Stream parallelStream = aList.parallelStream();
-
+
assertTrue(parallelStream.isParallel());
}
}
diff --git a/core-java/src/test/java/temp/SummationServiceTest.java b/core-java/src/test/java/temp/SummationServiceTest.java
deleted file mode 100644
index 3c2d39f5dd..0000000000
--- a/core-java/src/test/java/temp/SummationServiceTest.java
+++ /dev/null
@@ -1,49 +0,0 @@
-package temp;
-
-import static org.junit.Assert.*;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import org.junit.After;
-import org.junit.AfterClass;
-import org.junit.Before;
-import org.junit.BeforeClass;
-import org.junit.Test;
-
-import junit.framework.Assert;
-
-public class SummationServiceTest {
-
- private static List numbers;
-
- @BeforeClass
- public static void initialize() {
- numbers = new ArrayList<>();
- }
-
- @AfterClass
- public static void tearDown() {
- numbers = null;
- }
-
- @Before
- public void runBeforeEachTest() {
- numbers.add(1);
- numbers.add(2);
- numbers.add(3);
- }
-
- @After
- public void runAfterEachTest() {
- numbers.clear();
- }
-
- @Test
- public void givenNumbers_sumEquals_thenCorrect() {
- int sum = 0;
- for (int num : numbers)
- sum += num;
- assertEquals(6, sum);
- }
-}
\ No newline at end of file
diff --git a/core-java/src/test/resources/parameterised_test.xml b/core-java/src/test/resources/parameterised_test.xml
deleted file mode 100644
index 69a2c60460..0000000000
--- a/core-java/src/test/resources/parameterised_test.xml
+++ /dev/null
@@ -1,10 +0,0 @@
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/core-java/src/test/resources/test_group.xml b/core-java/src/test/resources/test_group.xml
deleted file mode 100644
index 0c9a6c73df..0000000000
--- a/core-java/src/test/resources/test_group.xml
+++ /dev/null
@@ -1,13 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/core-java/src/test/resources/test_suite.xml b/core-java/src/test/resources/test_suite.xml
deleted file mode 100644
index 36305aa5fc..0000000000
--- a/core-java/src/test/resources/test_suite.xml
+++ /dev/null
@@ -1,9 +0,0 @@
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/couchbase-sdk/pom.xml b/couchbase-sdk/pom.xml
index 301fd81c51..1200fab454 100644
--- a/couchbase-sdk/pom.xml
+++ b/couchbase-sdk/pom.xml
@@ -102,10 +102,10 @@
1.8
UTF-8
- 2.3.6
- 4.3.4.RELEASE
- 1.1.7
- 1.7.21
+ 2.4.0
+ 4.3.5.RELEASE
+ 1.1.8
+ 1.7.22
4.12
3.5
3.6.0
diff --git a/couchbase-sdk/src/main/java/com/baeldung/couchbase/mapreduce/CouchbaseKeyGenerator.java b/couchbase-sdk/src/main/java/com/baeldung/couchbase/mapreduce/CouchbaseKeyGenerator.java
new file mode 100644
index 0000000000..9ac1bbb3f7
--- /dev/null
+++ b/couchbase-sdk/src/main/java/com/baeldung/couchbase/mapreduce/CouchbaseKeyGenerator.java
@@ -0,0 +1,6 @@
+package com.baeldung.couchbase.mapreduce;
+
+public interface CouchbaseKeyGenerator {
+
+ String generateKey(T t);
+}
diff --git a/couchbase-sdk/src/main/java/com/baeldung/couchbase/mapreduce/DuplicateKeyException.java b/couchbase-sdk/src/main/java/com/baeldung/couchbase/mapreduce/DuplicateKeyException.java
new file mode 100644
index 0000000000..78baaa155c
--- /dev/null
+++ b/couchbase-sdk/src/main/java/com/baeldung/couchbase/mapreduce/DuplicateKeyException.java
@@ -0,0 +1,10 @@
+package com.baeldung.couchbase.mapreduce;
+
+@SuppressWarnings("serial")
+public class DuplicateKeyException extends Exception {
+
+ public DuplicateKeyException(String s) {
+ super(s);
+ }
+
+}
diff --git a/couchbase-sdk/src/main/java/com/baeldung/couchbase/mapreduce/RandomUUIDGenerator.java b/couchbase-sdk/src/main/java/com/baeldung/couchbase/mapreduce/RandomUUIDGenerator.java
new file mode 100644
index 0000000000..9baf4a4f43
--- /dev/null
+++ b/couchbase-sdk/src/main/java/com/baeldung/couchbase/mapreduce/RandomUUIDGenerator.java
@@ -0,0 +1,11 @@
+package com.baeldung.couchbase.mapreduce;
+
+import java.util.UUID;
+
+public class RandomUUIDGenerator implements CouchbaseKeyGenerator {
+
+ @Override
+ public String generateKey(T t) {
+ return UUID.randomUUID().toString();
+ }
+}
diff --git a/couchbase-sdk/src/main/java/com/baeldung/couchbase/mapreduce/StudentGrade.java b/couchbase-sdk/src/main/java/com/baeldung/couchbase/mapreduce/StudentGrade.java
new file mode 100644
index 0000000000..846aba716a
--- /dev/null
+++ b/couchbase-sdk/src/main/java/com/baeldung/couchbase/mapreduce/StudentGrade.java
@@ -0,0 +1,50 @@
+package com.baeldung.couchbase.mapreduce;
+
+public class StudentGrade {
+
+ private String name;
+ private String course;
+ private Integer grade;
+ private Integer hours;
+
+ public StudentGrade() { }
+
+ public StudentGrade(String name, String course, Integer grade, Integer hours) {
+ this.name = name;
+ this.course = course;
+ this.grade = grade;
+ this.hours = hours;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getCourse() {
+ return course;
+ }
+
+ public void setCourse(String course) {
+ this.course = course;
+ }
+
+ public Integer getGrade() {
+ return grade;
+ }
+
+ public void setGrade(Integer grade) {
+ this.grade = grade;
+ }
+
+ public Integer getHours() {
+ return hours;
+ }
+
+ public void setHours(Integer hours) {
+ this.hours = hours;
+ }
+}
diff --git a/couchbase-sdk/src/main/java/com/baeldung/couchbase/mapreduce/StudentGradeKeyGenerator.java b/couchbase-sdk/src/main/java/com/baeldung/couchbase/mapreduce/StudentGradeKeyGenerator.java
new file mode 100644
index 0000000000..680e37ba57
--- /dev/null
+++ b/couchbase-sdk/src/main/java/com/baeldung/couchbase/mapreduce/StudentGradeKeyGenerator.java
@@ -0,0 +1,9 @@
+package com.baeldung.couchbase.mapreduce;
+
+public class StudentGradeKeyGenerator implements CouchbaseKeyGenerator {
+
+ @Override
+ public String generateKey(StudentGrade g) {
+ return g.getName() + ":" + g.getCourse();
+ }
+}
diff --git a/couchbase-sdk/src/main/java/com/baeldung/couchbase/mapreduce/StudentGradeQueryBuilder.java b/couchbase-sdk/src/main/java/com/baeldung/couchbase/mapreduce/StudentGradeQueryBuilder.java
new file mode 100644
index 0000000000..37bb03645a
--- /dev/null
+++ b/couchbase-sdk/src/main/java/com/baeldung/couchbase/mapreduce/StudentGradeQueryBuilder.java
@@ -0,0 +1,70 @@
+package com.baeldung.couchbase.mapreduce;
+
+import com.couchbase.client.deps.com.fasterxml.jackson.databind.ObjectMapper;
+import com.couchbase.client.java.document.json.JsonArray;
+import com.couchbase.client.java.view.ViewQuery;
+
+public class StudentGradeQueryBuilder {
+
+ final ObjectMapper om = new ObjectMapper();
+
+ public ViewQuery findAll() {
+ return ViewQuery.from("studentGrades", "findByCourse");
+ }
+
+ public ViewQuery findByCourse(String course) {
+ return ViewQuery.from("studentGrades", "findByCourse")
+ .key(course);
+ }
+
+ public ViewQuery findByCourses(String... courses) {
+ return ViewQuery.from("studentGrades", "findByCourse")
+ .keys(JsonArray.from(courses));
+ }
+
+ public ViewQuery findByGradeInRange(int lower, int upper, boolean inclusiveEnd) {
+ return ViewQuery.from("studentGrades", "findByGrade")
+ .startKey(lower)
+ .endKey(upper)
+ .inclusiveEnd(inclusiveEnd);
+ }
+
+ public ViewQuery findByGradeLessThan(int upper) {
+ return ViewQuery.from("studentGrades", "findByGrade")
+ .endKey(upper)
+ .inclusiveEnd(false);
+ }
+
+ public ViewQuery findByGradeGreaterThan(int lower) {
+ return ViewQuery.from("studentGrades", "findByGrade")
+ .startKey(lower);
+ }
+
+ public ViewQuery findByCourseAndGradeInRange(String course, int minGrade, int maxGrade, boolean inclusiveEnd) {
+ return ViewQuery.from("studentGrades", "findByCourseAndGrade")
+ .startKey(JsonArray.from(course, minGrade))
+ .endKey(JsonArray.from(course, maxGrade))
+ .inclusiveEnd(inclusiveEnd);
+ }
+
+ public ViewQuery findTopGradesByCourse(String course, int limit) {
+ return ViewQuery.from("studentGrades", "findByCourseAndGrade")
+ .startKey(JsonArray.from(course, 100))
+ .endKey(JsonArray.from(course, 0))
+ .inclusiveEnd(true)
+ .descending()
+ .limit(limit);
+ }
+
+ public ViewQuery countStudentsByCourse() {
+ return ViewQuery.from("studentGrades", "countStudentsByCourse")
+ .reduce()
+ .groupLevel(1);
+ }
+
+ public ViewQuery sumCreditsByStudent() {
+ return ViewQuery.from("studentGrades", "sumCreditsByStudent")
+ .reduce()
+ .groupLevel(1);
+ }
+}
diff --git a/couchbase-sdk/src/main/java/com/baeldung/couchbase/mapreduce/StudentGradeService.java b/couchbase-sdk/src/main/java/com/baeldung/couchbase/mapreduce/StudentGradeService.java
new file mode 100644
index 0000000000..2d2c63f699
--- /dev/null
+++ b/couchbase-sdk/src/main/java/com/baeldung/couchbase/mapreduce/StudentGradeService.java
@@ -0,0 +1,169 @@
+package com.baeldung.couchbase.mapreduce;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import com.couchbase.client.deps.com.fasterxml.jackson.databind.ObjectMapper;
+import com.couchbase.client.java.Bucket;
+import com.couchbase.client.java.CouchbaseCluster;
+import com.couchbase.client.java.document.JsonDocument;
+import com.couchbase.client.java.document.json.JsonArray;
+import com.couchbase.client.java.document.json.JsonObject;
+import com.couchbase.client.java.view.ViewQuery;
+import com.couchbase.client.java.view.ViewResult;
+import com.couchbase.client.java.view.ViewRow;
+
+public class StudentGradeService {
+
+ final CouchbaseKeyGenerator keyGenerator;
+ final CouchbaseCluster cluster;
+ final Bucket bucket;
+ final ObjectMapper om = new ObjectMapper();
+ final StudentGradeQueryBuilder queryBuilder;
+
+ public StudentGradeService(CouchbaseKeyGenerator keyGenerator) {
+ this.keyGenerator = keyGenerator;
+ this.queryBuilder = new StudentGradeQueryBuilder();
+ cluster = CouchbaseCluster.create("127.0.0.1");
+ bucket = cluster.openBucket("baeldung-tutorial");
+ }
+
+ public String insert(StudentGrade studentGrade) throws DuplicateKeyException {
+ String id = keyGenerator.generateKey(studentGrade);
+ if(bucket.exists(id)) {
+ throw new DuplicateKeyException("document already exists with key " + id);
+ }
+ JsonObject content = JsonObject.empty()
+ .put("type", "StudentGrade")
+ .put("name", studentGrade.getName())
+ .put("course", studentGrade.getCourse())
+ .put("grade", studentGrade.getGrade())
+ .put("hours", studentGrade.getHours());
+ JsonDocument doc = JsonDocument.create(id, content);
+ bucket.insert(doc);
+ return id;
+ }
+
+ public List findAll() {
+ ViewQuery query = queryBuilder.findAll();
+ ViewResult result = bucket.query(query);
+ return extractDocuments(result);
+ }
+
+ private List extractDocuments(ViewResult result) {
+ List docs = new ArrayList<>();
+ for(ViewRow row : result.allRows()) {
+ JsonDocument doc = row.document();
+ docs.add(doc);
+ }
+ return docs;
+ }
+
+ public List findByCourse(String course) {
+ ViewQuery query = queryBuilder.findByCourse(course);
+ ViewResult result = bucket.query(query);
+ return extractDocuments(result);
+ }
+
+ public List findByCourses(String... courses) {
+ ViewQuery query = queryBuilder.findByCourses(courses);
+ ViewResult result = bucket.query(query);
+ return extractDocuments(result);
+ }
+
+ public List findByGradeInRange(int lower, int upper, boolean inclusiveEnd) {
+ ViewQuery query = queryBuilder.findByGradeInRange(lower, upper, inclusiveEnd);
+ ViewResult result = bucket.query(query);
+ return extractDocuments(result);
+ }
+
+ public List findByGradeLessThan(int upper) {
+ ViewQuery query = queryBuilder.findByGradeLessThan(upper);
+ ViewResult result = bucket.query(query);
+ return extractDocuments(result);
+ }
+
+ public List findByGradeGreaterThan(int lower) {
+ ViewQuery query = queryBuilder.findByGradeGreaterThan(lower);
+ ViewResult result = bucket.query(query);
+ return extractDocuments(result);
+ }
+
+ public List findByCourseAndGradeInRange(String course, int minGrade, int maxGrade, boolean inclusiveEnd) {
+ ViewQuery query = queryBuilder.findByCourseAndGradeInRange(course, minGrade, maxGrade, inclusiveEnd);
+ ViewResult result = bucket.query(query);
+ return extractDocuments(result);
+ }
+
+ public List findTopGradesByCourse(String course, int limit) {
+ ViewQuery query = queryBuilder.findTopGradesByCourse(course, limit);
+ ViewResult result = bucket.query(query);
+ return extractDocuments(result);
+ }
+
+ public Map countStudentsByCourse() {
+ ViewQuery query = ViewQuery.from("studentGrades", "countStudentsByCourse")
+ .reduce()
+ .groupLevel(1);
+ ViewResult result = bucket.query(query);
+
+ Map numStudentsByCourse = new HashMap<>();
+ for(ViewRow row : result.allRows()) {
+ JsonArray keyArray = (JsonArray) row.key();
+ String course = keyArray.getString(0);
+ long count = Long.valueOf(row.value().toString());
+ numStudentsByCourse.put(course, count);
+ }
+
+ return numStudentsByCourse;
+ }
+
+ public Map sumCreditHoursByStudent() {
+ ViewQuery query = ViewQuery.from("studentGrades", "sumHoursByStudent")
+ .reduce()
+ .groupLevel(1);
+ ViewResult result = bucket.query(query);
+
+ Map creditHoursByStudent = new HashMap<>();
+ for(ViewRow row : result.allRows()) {
+ String course = (String) row.key();
+ long sum = Long.valueOf(row.value().toString());
+ creditHoursByStudent.put(course, sum);
+ }
+
+ return creditHoursByStudent;
+ }
+
+ public Map sumGradePointsByStudent() {
+ ViewQuery query = ViewQuery.from("studentGrades", "sumGradePointsByStudent")
+ .reduce()
+ .groupLevel(1);
+ ViewResult result = bucket.query(query);
+
+ Map gradePointsByStudent = new HashMap<>();
+ for(ViewRow row : result.allRows()) {
+ String course = (String) row.key();
+ long sum = Long.valueOf(row.value().toString());
+ gradePointsByStudent.put(course, sum);
+ }
+
+ return gradePointsByStudent;
+ }
+
+ public Map calculateGpaByStudent() {
+ Map creditHoursByStudent = sumCreditHoursByStudent();
+ Map gradePointsByStudent = sumGradePointsByStudent();
+
+ Map result = new HashMap<>();
+ for(Entry creditHoursEntry : creditHoursByStudent.entrySet()) {
+ String name = creditHoursEntry.getKey();
+ long totalHours = creditHoursEntry.getValue();
+ long totalGradePoints = gradePointsByStudent.get(name);
+ result.put(name, ((float) totalGradePoints / totalHours));
+ }
+ return result;
+ }
+}
diff --git a/couchbase-sdk/src/test/java/com/baeldung/couchbase/mapreduce/StudentGradeServiceIntegrationTest.java b/couchbase-sdk/src/test/java/com/baeldung/couchbase/mapreduce/StudentGradeServiceIntegrationTest.java
new file mode 100644
index 0000000000..00d462e32a
--- /dev/null
+++ b/couchbase-sdk/src/test/java/com/baeldung/couchbase/mapreduce/StudentGradeServiceIntegrationTest.java
@@ -0,0 +1,150 @@
+package com.baeldung.couchbase.mapreduce;
+
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.couchbase.client.java.document.JsonDocument;
+import com.couchbase.client.java.view.ViewResult;
+import com.couchbase.client.java.view.ViewRow;
+
+public class StudentGradeServiceIntegrationTest {
+ private static final Logger logger = LoggerFactory.getLogger(StudentGradeServiceIntegrationTest.class);
+
+ static StudentGradeService studentGradeService;
+ static Set gradeIds = new HashSet<>();
+
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ studentGradeService = new StudentGradeService(new StudentGradeKeyGenerator());
+ insertStudentGrade(new StudentGrade("John Doe", "History", 80, 3));
+ insertStudentGrade(new StudentGrade("Jane Doe", "History", 89, 3));
+ insertStudentGrade(new StudentGrade("Bob Smith", "History", 90, 3));
+ insertStudentGrade(new StudentGrade("Mary Jones", "History", 92, 3));
+ insertStudentGrade(new StudentGrade("Jane Doe", "Math", 59, 3));
+ insertStudentGrade(new StudentGrade("Bob Smith", "Math", 91, 3));
+ insertStudentGrade(new StudentGrade("Mary Jones", "Math", 86, 3));
+ insertStudentGrade(new StudentGrade("John Doe", "Science", 85, 4));
+ insertStudentGrade(new StudentGrade("Bob Smith", "Science", 97, 4));
+ insertStudentGrade(new StudentGrade("Mary Jones", "Science", 84, 4));
+ }
+
+ private static void insertStudentGrade(StudentGrade studentGrade) {
+ try {
+ String id = studentGradeService.insert(studentGrade);
+ gradeIds.add(id);
+ } catch (DuplicateKeyException e) {
+ }
+ }
+
+ @Test
+ public final void whenFindAll_thenSuccess() {
+ List docs = studentGradeService.findAll();
+ printDocuments(docs);
+ }
+
+ @Test
+ public final void whenFindByCourse_thenSuccess() {
+ List docs = studentGradeService.findByCourse("History");
+ printDocuments(docs);
+ }
+
+ @Test
+ public final void whenFindByCourses_thenSuccess() {
+ List docs = studentGradeService.findByCourses("History", "Science");
+ printDocuments(docs);
+ }
+
+ @Test
+ public final void whenFindByGradeInRange_thenSuccess() {
+ List docs = studentGradeService.findByGradeInRange(80, 89, true);
+ printDocuments(docs);
+ }
+
+ @Test
+ public final void whenFindByGradeLessThan_thenSuccess() {
+ List docs = studentGradeService.findByGradeLessThan(60);
+ printDocuments(docs);
+ }
+
+ @Test
+ public final void whenFindByGradeGreaterThan_thenSuccess() {
+ List docs = studentGradeService.findByGradeGreaterThan(90);
+ printDocuments(docs);
+ }
+
+ @Test
+ public final void whenFindByCourseAndGradeInRange_thenSuccess() {
+ List docs = studentGradeService.findByCourseAndGradeInRange("Math", 80, 89, true);
+ printDocuments(docs);
+ }
+
+ @Test
+ public final void whenFindTopGradesByCourse_thenSuccess() {
+ List docs = studentGradeService.findTopGradesByCourse("Science", 2);
+ printDocuments(docs);
+ }
+
+ @Test
+ public final void whenCountStudentsByCourse_thenSuccess() {
+ Map map = studentGradeService.countStudentsByCourse();
+ printMap(map);
+ }
+
+ @Test
+ public final void whenSumCreditHoursByStudent_thenSuccess() {
+ Map map = studentGradeService.sumCreditHoursByStudent();
+ printMap(map);
+ }
+
+ @Test
+ public final void whenSumGradePointsByStudent_thenSuccess() {
+ Map map = studentGradeService.sumGradePointsByStudent();
+ printMap(map);
+ }
+
+ @Test
+ public final void whenCalculateGpaByStudent_thenSuccess() {
+ Map map = studentGradeService.calculateGpaByStudent();
+ printGpaMap(map);
+ }
+
+ private void printMap(Map map) {
+ for(Map.Entry entry : map.entrySet()) {
+ logger.info(entry.getKey() + "=" + entry.getValue());
+ }
+ }
+
+ private void printGpaMap(Map map) {
+ for(Map.Entry entry : map.entrySet()) {
+ logger.info(entry.getKey() + "=" + entry.getValue());
+ }
+ }
+
+ private void printDocuments(List docs) {
+ for(JsonDocument doc : docs) {
+ String key = doc.id();
+ logger.info(key + " = " + doc.content().toString());
+ }
+ }
+
+ private void printViewResultDocuments(ViewResult result) {
+ for(ViewRow row : result.allRows()) {
+ JsonDocument doc = row.document();
+ String key = doc.id();
+ logger.info(key + "=" + doc.content().toString());
+ }
+ }
+
+ private void printViewResultRows(ViewResult result) {
+ for(ViewRow row : result.allRows()) {
+ logger.info(row.toString());
+ }
+ }
+}
diff --git a/couchbase-sdk/src/test/resources/logback.xml b/couchbase-sdk/src/test/resources/logback.xml
new file mode 100644
index 0000000000..efcc6fb4c7
--- /dev/null
+++ b/couchbase-sdk/src/test/resources/logback.xml
@@ -0,0 +1,17 @@
+
+
+
+
+ web - %date [%thread] %-5level %logger{36} - %message%n
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/guava/pom.xml b/guava/pom.xml
index f65a9a2081..0edbb90efd 100644
--- a/guava/pom.xml
+++ b/guava/pom.xml
@@ -57,6 +57,32 @@
test
+
+
+
+ org.slf4j
+ slf4j-api
+ ${org.slf4j.version}
+
+
+ ch.qos.logback
+ logback-classic
+ ${logback.version}
+
+
+
+ org.slf4j
+ jcl-over-slf4j
+ ${org.slf4j.version}
+
+
+
+ org.slf4j
+ log4j-over-slf4j
+ ${org.slf4j.version}
+
+
+
org.assertj
assertj-core
@@ -112,6 +138,9 @@
3.6.0
2.19.1
+
+ 1.7.21
+ 1.1.7
\ No newline at end of file
diff --git a/guava/src/test/java/org/baeldung/guava/CustomEvent.java b/guava/src/main/java/org/baeldung/guava/CustomEvent.java
similarity index 99%
rename from guava/src/test/java/org/baeldung/guava/CustomEvent.java
rename to guava/src/main/java/org/baeldung/guava/CustomEvent.java
index 78348065b2..2d5c3382d4 100644
--- a/guava/src/test/java/org/baeldung/guava/CustomEvent.java
+++ b/guava/src/main/java/org/baeldung/guava/CustomEvent.java
@@ -1,6 +1,5 @@
package org.baeldung.guava;
-
public class CustomEvent {
private String action;
diff --git a/guava/src/main/java/org/baeldung/guava/EventListener.java b/guava/src/main/java/org/baeldung/guava/EventListener.java
new file mode 100644
index 0000000000..438fcade63
--- /dev/null
+++ b/guava/src/main/java/org/baeldung/guava/EventListener.java
@@ -0,0 +1,38 @@
+package org.baeldung.guava;
+
+import com.google.common.eventbus.DeadEvent;
+import com.google.common.eventbus.Subscribe;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class EventListener {
+
+ private static int eventsHandled;
+ private static final Logger LOG = LoggerFactory.getLogger(EventListener.class);
+
+ @Subscribe
+ public void stringEvent(String event) {
+ LOG.info("do event [" + event + "]");
+ eventsHandled++;
+ }
+
+ @Subscribe
+ public void someCustomEvent(CustomEvent customEvent) {
+ LOG.info("do event [" + customEvent.getAction() + "]");
+ eventsHandled++;
+ }
+
+ @Subscribe
+ public void handleDeadEvent(DeadEvent deadEvent) {
+ LOG.info("unhandled event [" + deadEvent.getEvent() + "]");
+ eventsHandled++;
+ }
+
+ public int getEventsHandled() {
+ return eventsHandled;
+ }
+
+ public void resetEventsHandled() {
+ eventsHandled = 0;
+ }
+}
diff --git a/guava/src/test/java/org/baeldung/guava/EventBusWrapper.java b/guava/src/test/java/org/baeldung/guava/EventBusWrapper.java
deleted file mode 100644
index eddaca0baf..0000000000
--- a/guava/src/test/java/org/baeldung/guava/EventBusWrapper.java
+++ /dev/null
@@ -1,22 +0,0 @@
-package org.baeldung.guava;
-
-import com.google.common.eventbus.EventBus;
-
-class EventBusWrapper {
-
- private static EventBus eventBus = new EventBus();
-
- static void register(Object object){
- eventBus.register(object);
- }
-
- static void unregister(Object object){
- eventBus.unregister(object);
- }
-
- static void post(Object object){
- eventBus.post(object);
- }
-
-
-}
diff --git a/guava/src/test/java/org/baeldung/guava/EventListener.java b/guava/src/test/java/org/baeldung/guava/EventListener.java
deleted file mode 100644
index 17a3ac093e..0000000000
--- a/guava/src/test/java/org/baeldung/guava/EventListener.java
+++ /dev/null
@@ -1,33 +0,0 @@
-package org.baeldung.guava;
-import com.google.common.eventbus.Subscribe;
-
-public class EventListener {
-
- private static int eventsHandled;
-
- /**
- * Handles events of type String *
- */
- @Subscribe
- public void stringEvent(String event){
- System.out.println("do event ["+event+"]");
- eventsHandled++;
- }
-
- /**
- * Handles events of type CustomEvent
- */
- @Subscribe
- public void someEvent(CustomEvent customEvent){
- System.out.println("do event ["+ customEvent.getAction()+"]");
- eventsHandled++;
- }
-
- public int getEventsHandled() {
- return eventsHandled;
- }
-
- public void resetEventsHandled(){
- eventsHandled = 0;
- }
-}
diff --git a/guava/src/test/java/org/baeldung/guava/GuavaCacheLoaderTest.java b/guava/src/test/java/org/baeldung/guava/GuavaCacheLoaderTest.java
new file mode 100644
index 0000000000..2aa2e6140b
--- /dev/null
+++ b/guava/src/test/java/org/baeldung/guava/GuavaCacheLoaderTest.java
@@ -0,0 +1,71 @@
+package org.baeldung.guava;
+
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Iterables;
+import org.assertj.core.api.Assertions;
+import org.junit.Test;
+
+import java.util.Map;
+import java.util.concurrent.ExecutionException;
+
+import static com.google.common.collect.Iterables.cycle;
+import static com.google.common.collect.Maps.newHashMap;
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class GuavaCacheLoaderTest {
+ int callCount = 0;
+
+ @Test
+ public void givenAMap_whenAddingValues_thenCanTreatThemAsCache() {
+ Map cache = newHashMap();
+ cache.put("foo", "cachedValueForFoo");
+ cache.put("bar", "cachedValueForBar");
+
+ assertThat(cache.get("foo")).isEqualTo("cachedValueForFoo");
+ assertThat(cache.get("bar")).isEqualTo("cachedValueForBar");
+ }
+
+ @Test
+ public void givenCacheLoader_whenGettingItemTwice_shouldOnlyCallOnce() throws ExecutionException {
+
+ final LoadingCache loadingCache = CacheBuilder.newBuilder()
+ .build(new CacheLoader() {
+ @Override
+ public String load(final String s) throws Exception {
+ return slowMethod(s);
+ }
+ });
+
+ String value = loadingCache.get("key");
+ value = loadingCache.get("key");
+
+ assertThat(callCount).isEqualTo(1);
+ assertThat(value).isEqualTo("key");
+ }
+
+ @Test
+ public void givenCacheLoader_whenRefreshingItem_shouldCallAgain() throws ExecutionException {
+
+ final LoadingCache loadingCache = CacheBuilder.newBuilder()
+ .build(new CacheLoader() {
+ @Override
+ public String load(final String s) throws Exception {
+ return slowMethod(s);
+ }
+ });
+
+ String value = loadingCache.get("key");
+ loadingCache.refresh("key");
+
+ assertThat(callCount).isEqualTo(2);
+ assertThat(value).isEqualTo("key");
+ }
+
+ private String slowMethod(final String s) {
+ callCount++;
+ return s;
+ }
+}
diff --git a/guava/src/test/java/org/baeldung/guava/GuavaEventBusTest.java b/guava/src/test/java/org/baeldung/guava/GuavaEventBusTest.java
index bd966187ab..1390eb05aa 100644
--- a/guava/src/test/java/org/baeldung/guava/GuavaEventBusTest.java
+++ b/guava/src/test/java/org/baeldung/guava/GuavaEventBusTest.java
@@ -1,5 +1,6 @@
package org.baeldung.guava;
+import com.google.common.eventbus.EventBus;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -9,34 +10,46 @@ import static org.junit.Assert.*;
public class GuavaEventBusTest {
private EventListener listener;
+ private EventBus eventBus;
@Before
- public void setUp() throws Exception {
+ public void setUp() {
+ eventBus = new EventBus();
listener = new EventListener();
- EventBusWrapper.register(listener);
+
+ eventBus.register(listener);
}
@After
- public void tearDown() throws Exception {
- EventBusWrapper.unregister(listener);
+ public void tearDown() {
+ eventBus.unregister(listener);
}
@Test
- public void givenStringEvent_whenEventHandled_thenSuccess() throws Exception {
+ public void givenStringEvent_whenEventHandled_thenSuccess() {
listener.resetEventsHandled();
- EventBusWrapper.post("String Event");
+ eventBus.post("String Event");
assertEquals(1, listener.getEventsHandled());
-
}
@Test
- public void givenCustomEvent_whenEventHandled_thenSuccess() throws Exception {
+ public void givenCustomEvent_whenEventHandled_thenSuccess() {
listener.resetEventsHandled();
CustomEvent customEvent = new CustomEvent("Custom Event");
- EventBusWrapper.post(customEvent);
+ eventBus.post(customEvent);
assertEquals(1, listener.getEventsHandled());
}
+
+ @Test
+ public void givenUnSubscribedEvent_whenEventHandledByDeadEvent_thenSuccess() throws InterruptedException {
+ listener.resetEventsHandled();
+
+ eventBus.post(12345);
+
+ assertEquals(1, listener.getEventsHandled());
+ }
+
}
diff --git a/guava/src/test/java/org/baeldung/guava/GuavaPreConditionsTest.java b/guava/src/test/java/org/baeldung/guava/GuavaPreConditionsTest.java
index 55e3c7db00..2d98418d48 100644
--- a/guava/src/test/java/org/baeldung/guava/GuavaPreConditionsTest.java
+++ b/guava/src/test/java/org/baeldung/guava/GuavaPreConditionsTest.java
@@ -3,7 +3,7 @@ package org.baeldung.guava;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import java.util.Arrays;
import org.junit.Test;
-import static com.google.common.base.Preconditions.*;
+import com.google.common.base.*;
public class GuavaPreConditionsTest {
@@ -11,10 +11,7 @@ public class GuavaPreConditionsTest {
public void whenCheckArgumentEvaluatesFalse_throwsException() {
int age = -18;
- assertThatThrownBy(() -> checkArgument(age > 0))
- .isInstanceOf(IllegalArgumentException.class)
- .hasMessage(null)
- .hasNoCause();
+ assertThatThrownBy(() -> Preconditions.checkArgument(age > 0)).isInstanceOf(IllegalArgumentException.class).hasMessage(null).hasNoCause();
}
@Test
@@ -22,10 +19,7 @@ public class GuavaPreConditionsTest {
final int age = -18;
final String message = "Age can't be zero or less than zero";
- assertThatThrownBy(() -> checkArgument(age > 0, message))
- .isInstanceOf(IllegalArgumentException.class)
- .hasMessage(message)
- .hasNoCause();
+ assertThatThrownBy(() -> Preconditions.checkArgument(age > 0, message)).isInstanceOf(IllegalArgumentException.class).hasMessage(message).hasNoCause();
}
@Test
@@ -33,19 +27,14 @@ public class GuavaPreConditionsTest {
final int age = -18;
final String message = "Age can't be zero or less than zero, you supplied %s.";
- assertThatThrownBy(() -> checkArgument(age > 0, message, age))
- .isInstanceOf(IllegalArgumentException.class)
- .hasMessage(message, age)
- .hasNoCause();
+ assertThatThrownBy(() -> Preconditions.checkArgument(age > 0, message, age)).isInstanceOf(IllegalArgumentException.class).hasMessage(message, age).hasNoCause();
}
@Test
public void givenArrayOfIntegers_whenCheckElementIndexEvaluatesFalse_throwsException() {
final int[] numbers = { 1, 2, 3, 4, 5 };
- assertThatThrownBy(() -> checkElementIndex(6, numbers.length - 1))
- .isInstanceOf(IndexOutOfBoundsException.class)
- .hasNoCause();
+ assertThatThrownBy(() -> Preconditions.checkElementIndex(6, numbers.length - 1)).isInstanceOf(IndexOutOfBoundsException.class).hasNoCause();
}
@Test
@@ -53,20 +42,7 @@ public class GuavaPreConditionsTest {
final int[] numbers = { 1, 2, 3, 4, 5 };
final String message = "Please check the bound of an array and retry";
- assertThatThrownBy(() -> checkElementIndex(6, numbers.length - 1, message))
- .isInstanceOf(IndexOutOfBoundsException.class)
- .hasMessageStartingWith(message)
- .hasNoCause();
- }
-
- @Test
- public void givenNullString_whenCheckNotNullCalled_throwsException() {
- final String nullObject = null;
-
- assertThatThrownBy(() -> checkNotNull(nullObject))
- .isInstanceOf(NullPointerException.class)
- .hasMessage(null)
- .hasNoCause();
+ assertThatThrownBy(() -> Preconditions.checkElementIndex(6, numbers.length - 1, message)).isInstanceOf(IndexOutOfBoundsException.class).hasMessageStartingWith(message).hasNoCause();
}
@Test
@@ -74,10 +50,7 @@ public class GuavaPreConditionsTest {
final String nullObject = null;
final String message = "Please check the Object supplied, its null!";
- assertThatThrownBy(() -> checkNotNull(nullObject, message))
- .isInstanceOf(NullPointerException.class)
- .hasMessage(message)
- .hasNoCause();
+ assertThatThrownBy(() -> Preconditions.checkNotNull(nullObject, message)).isInstanceOf(NullPointerException.class).hasMessage(message).hasNoCause();
}
@Test
@@ -85,19 +58,14 @@ public class GuavaPreConditionsTest {
final String nullObject = null;
final String message = "Please check the Object supplied, its %s!";
- assertThatThrownBy(() -> checkNotNull(nullObject, message, nullObject))
- .isInstanceOf(NullPointerException.class)
- .hasMessage(message, nullObject)
- .hasNoCause();
+ assertThatThrownBy(() -> Preconditions.checkNotNull(nullObject, message, new Object[] { null })).isInstanceOf(NullPointerException.class).hasMessage(message, nullObject).hasNoCause();
}
@Test
public void givenArrayOfIntegers_whenCheckPositionIndexEvaluatesFalse_throwsException() {
final int[] numbers = { 1, 2, 3, 4, 5 };
- assertThatThrownBy(() -> checkPositionIndex(6, numbers.length - 1))
- .isInstanceOf(IndexOutOfBoundsException.class)
- .hasNoCause();
+ assertThatThrownBy(() -> Preconditions.checkPositionIndex(6, numbers.length - 1)).isInstanceOf(IndexOutOfBoundsException.class).hasNoCause();
}
@Test
@@ -105,30 +73,14 @@ public class GuavaPreConditionsTest {
final int[] numbers = { 1, 2, 3, 4, 5 };
final String message = "Please check the bound of an array and retry";
- assertThatThrownBy(() -> checkPositionIndex(6, numbers.length - 1, message))
- .isInstanceOf(IndexOutOfBoundsException.class)
- .hasMessageStartingWith(message)
- .hasNoCause();
+ assertThatThrownBy(() -> Preconditions.checkPositionIndex(6, numbers.length - 1, message)).isInstanceOf(IndexOutOfBoundsException.class).hasMessageStartingWith(message).hasNoCause();
}
@Test
public void givenArrayOfIntegers_whenCheckPositionIndexesEvaluatesFalse_throwsException() {
final int[] numbers = { 1, 2, 3, 4, 5 };
- assertThatThrownBy(() -> checkPositionIndexes(6, 0, numbers.length - 1))
- .isInstanceOf(IndexOutOfBoundsException.class)
- .hasNoCause();
- }
-
- @Test
- public void givenValidStates_whenCheckStateEvaluatesFalse_throwsException() {
- final int[] validStates = { -1, 0, 1 };
- final int givenState = 10;
-
- assertThatThrownBy(() -> checkState(Arrays.binarySearch(validStates, givenState) > 0))
- .isInstanceOf(IllegalStateException.class)
- .hasMessage(null)
- .hasNoCause();
+ assertThatThrownBy(() -> Preconditions.checkPositionIndexes(6, 0, numbers.length - 1)).isInstanceOf(IndexOutOfBoundsException.class).hasNoCause();
}
@Test
@@ -137,10 +89,7 @@ public class GuavaPreConditionsTest {
final int givenState = 10;
final String message = "You have entered an invalid state";
- assertThatThrownBy(() -> checkState(Arrays.binarySearch(validStates, givenState) > 0, message))
- .isInstanceOf(IllegalStateException.class)
- .hasMessageStartingWith(message)
- .hasNoCause();
+ assertThatThrownBy(() -> Preconditions.checkState(Arrays.binarySearch(validStates, givenState) > 0, message)).isInstanceOf(IllegalStateException.class).hasMessageStartingWith(message).hasNoCause();
}
@Test
@@ -149,9 +98,7 @@ public class GuavaPreConditionsTest {
final int givenState = 10;
final String message = "State can't be %s, It can be one of %s.";
- assertThatThrownBy(() -> checkState(Arrays.binarySearch(validStates, givenState) > 0, message, givenState, Arrays.toString(validStates)))
- .isInstanceOf(IllegalStateException.class)
- .hasMessage(message, givenState, Arrays.toString(validStates))
- .hasNoCause();
+ assertThatThrownBy(() -> Preconditions.checkState(Arrays.binarySearch(validStates, givenState) > 0, message, givenState, Arrays.toString(validStates))).isInstanceOf(IllegalStateException.class)
+ .hasMessage(message, givenState, Arrays.toString(validStates)).hasNoCause();
}
-}
+}
\ No newline at end of file
diff --git a/httpclient/pom.xml b/httpclient/pom.xml
index be0daae995..eec705b224 100644
--- a/httpclient/pom.xml
+++ b/httpclient/pom.xml
@@ -1,4 +1,5 @@
-
+
4.0.0
com.baeldung
httpclient
@@ -113,6 +114,13 @@
${mockito.version}
test
+
+ com.github.tomakehurst
+ wiremock
+ ${wiremock.version}
+ test
+
+
@@ -145,7 +153,7 @@
**/*LiveTest.java
-
+
@@ -202,6 +210,7 @@
1.3
4.12
1.10.19
+ 2.5.1
4.4.5
4.5.2
diff --git a/httpclient/src/test/java/org/baeldung/httpclient/advancedconfig/HttpClientAdvancedConfiguration.java b/httpclient/src/test/java/org/baeldung/httpclient/advancedconfig/HttpClientAdvancedConfiguration.java
new file mode 100644
index 0000000000..9b5cb3f293
--- /dev/null
+++ b/httpclient/src/test/java/org/baeldung/httpclient/advancedconfig/HttpClientAdvancedConfiguration.java
@@ -0,0 +1,154 @@
+package org.baeldung.httpclient.advancedconfig;
+
+
+import com.github.tomakehurst.wiremock.junit.WireMockRule;
+import org.apache.http.HttpHeaders;
+import org.apache.http.HttpHost;
+import org.apache.http.HttpResponse;
+import org.apache.http.auth.AuthScope;
+import org.apache.http.auth.UsernamePasswordCredentials;
+import org.apache.http.client.AuthCache;
+import org.apache.http.client.CredentialsProvider;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.protocol.HttpClientContext;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.auth.BasicScheme;
+import org.apache.http.impl.client.BasicAuthCache;
+import org.apache.http.impl.client.BasicCredentialsProvider;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
+import org.junit.Rule;
+import org.junit.Test;
+
+import java.io.IOException;
+
+import static com.github.tomakehurst.wiremock.client.WireMock.*;
+import static org.junit.Assert.assertEquals;
+
+public class HttpClientAdvancedConfiguration {
+
+ @Rule
+ public WireMockRule serviceMock = new WireMockRule(8089);
+
+ @Rule
+ public WireMockRule proxyMock = new WireMockRule(8090);
+
+ @Test
+ public void givenClientWithCustomUserAgentHeader_whenExecuteRequest_shouldReturn200() throws IOException {
+ //given
+ String userAgent = "BaeldungAgent/1.0";
+ serviceMock.stubFor(get(urlEqualTo("/detail"))
+ .withHeader("User-Agent", equalTo(userAgent))
+ .willReturn(aResponse()
+ .withStatus(200)));
+
+ HttpClient httpClient = HttpClients.createDefault();
+ HttpGet httpGet = new HttpGet("http://localhost:8089/detail");
+ httpGet.setHeader(HttpHeaders.USER_AGENT, userAgent);
+
+ //when
+ HttpResponse response = httpClient.execute(httpGet);
+
+ //then
+ assertEquals(response.getStatusLine().getStatusCode(), 200);
+ }
+
+ @Test
+ public void givenClientThatSendDataInBody_whenSendXmlInBody_shouldReturn200() throws IOException {
+ //given
+ String xmlBody = "1 ";
+ serviceMock.stubFor(post(urlEqualTo("/person"))
+ .withHeader("Content-Type", equalTo("application/xml"))
+ .withRequestBody(equalTo(xmlBody))
+ .willReturn(aResponse()
+ .withStatus(200)));
+
+ HttpClient httpClient = HttpClients.createDefault();
+ HttpPost httpPost = new HttpPost("http://localhost:8089/person");
+ httpPost.setHeader("Content-Type", "application/xml");
+ StringEntity xmlEntity = new StringEntity(xmlBody);
+ httpPost.setEntity(xmlEntity);
+
+ //when
+ HttpResponse response = httpClient.execute(httpPost);
+
+ //then
+ assertEquals(response.getStatusLine().getStatusCode(), 200);
+
+ }
+
+ @Test
+ public void givenServerThatIsBehindProxy_whenClientIsConfiguredToSendRequestViaProxy_shouldReturn200() throws IOException {
+ //given
+ proxyMock.stubFor(get(urlMatching(".*"))
+ .willReturn(aResponse().proxiedFrom("http://localhost:8089/")));
+
+ serviceMock.stubFor(get(urlEqualTo("/private"))
+ .willReturn(aResponse().withStatus(200)));
+
+
+ HttpHost proxy = new HttpHost("localhost", 8090);
+ DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
+ HttpClient httpclient = HttpClients.custom()
+ .setRoutePlanner(routePlanner)
+ .build();
+
+ //when
+ final HttpGet httpGet = new HttpGet("http://localhost:8089/private");
+ HttpResponse response = httpclient.execute(httpGet);
+
+ //then
+ assertEquals(response.getStatusLine().getStatusCode(), 200);
+ proxyMock.verify(getRequestedFor(urlEqualTo("/private")));
+ serviceMock.verify(getRequestedFor(urlEqualTo("/private")));
+ }
+
+ @Test
+ public void givenServerThatIsBehindAuthorizationProxy_whenClientSendRequest_shouldAuthorizeProperly() throws IOException {
+ //given
+ proxyMock.stubFor(get(urlMatching("/private"))
+ .willReturn(aResponse().proxiedFrom("http://localhost:8089/")));
+ serviceMock.stubFor(get(urlEqualTo("/private"))
+ .willReturn(aResponse().withStatus(200)));
+
+
+ HttpHost proxy = new HttpHost("localhost", 8090);
+ DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
+
+ // Client credentials
+ CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
+ credentialsProvider.setCredentials(new AuthScope(proxy),
+ new UsernamePasswordCredentials("username_admin", "secret_password"));
+
+
+ // Create AuthCache instance
+ AuthCache authCache = new BasicAuthCache();
+
+ // Generate BASIC scheme object and add it to the local auth cache
+ BasicScheme basicAuth = new BasicScheme();
+ authCache.put(proxy, basicAuth);
+ HttpClientContext context = HttpClientContext.create();
+ context.setCredentialsProvider(credentialsProvider);
+ context.setAuthCache(authCache);
+
+
+ HttpClient httpclient = HttpClients.custom()
+ .setRoutePlanner(routePlanner)
+ .setDefaultCredentialsProvider(credentialsProvider)
+ .build();
+
+
+ //when
+ final HttpGet httpGet = new HttpGet("http://localhost:8089/private");
+ HttpResponse response = httpclient.execute(httpGet, context);
+
+ //then
+ assertEquals(response.getStatusLine().getStatusCode(), 200);
+ proxyMock.verify(getRequestedFor(urlEqualTo("/private")).withHeader("Authorization", containing("Basic")));
+ serviceMock.verify(getRequestedFor(urlEqualTo("/private")));
+ }
+
+
+}
diff --git a/hystrix/pom.xml b/hystrix/pom.xml
index 5e050d726e..ba1a596631 100644
--- a/hystrix/pom.xml
+++ b/hystrix/pom.xml
@@ -11,7 +11,7 @@
org.springframework.boot
spring-boot-starter-parent
- 1.4.2.RELEASE
+ 1.4.4.RELEASE
diff --git a/jackson/pom.xml b/jackson/pom.xml
index 53fb82c61e..881ba8e24c 100644
--- a/jackson/pom.xml
+++ b/jackson/pom.xml
@@ -67,6 +67,12 @@
${jackson.version}
+
+ com.fasterxml.jackson.datatype
+ jackson-datatype-jdk8
+ ${jackson.version}
+
+
joda-time
joda-time
@@ -128,8 +134,8 @@
${mockito.version}
test
-
-
+
+
org.slf4j
@@ -152,7 +158,7 @@
org.slf4j
log4j-over-slf4j
${org.slf4j.version}
-
+
@@ -189,7 +195,7 @@
- 2.8.5
+ 2.8.6
1.7.21
@@ -198,7 +204,7 @@
19.0
3.5
- 2.5
+ 2.5
2.9.6
2.8.0
4.1
diff --git a/jackson/src/main/java/com/baeldung/jackson/entities/ActorJackson.java b/jackson/src/main/java/com/baeldung/jackson/entities/ActorJackson.java
index f6a8b0cf61..76f87d80b3 100644
--- a/jackson/src/main/java/com/baeldung/jackson/entities/ActorJackson.java
+++ b/jackson/src/main/java/com/baeldung/jackson/entities/ActorJackson.java
@@ -4,6 +4,7 @@ import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
+import java.util.Locale;
import java.util.TimeZone;
public class ActorJackson {
@@ -53,7 +54,7 @@ public class ActorJackson {
}
private String formatDateOfBirth() {
- final DateFormat formatter = new SimpleDateFormat("EEE MMM dd hh:mm:ss zzz yyyy");
+ final DateFormat formatter = new SimpleDateFormat("EEE MMM dd hh:mm:ss zzz yyyy", Locale.US);
formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
return formatter.format(dateOfBirth);
}
diff --git a/jackson/src/main/java/com/baeldung/jackson/miscellaneous/mixin/Book.java b/jackson/src/main/java/com/baeldung/jackson/miscellaneous/mixin/Book.java
new file mode 100644
index 0000000000..fb961efe85
--- /dev/null
+++ b/jackson/src/main/java/com/baeldung/jackson/miscellaneous/mixin/Book.java
@@ -0,0 +1,25 @@
+package com.baeldung.jackson.miscellaneous.mixin;
+
+import java.util.Optional;
+
+public class Book {
+
+ private String title;
+ private Optional subTitle;
+
+ public String getTitle() {
+ return title;
+ }
+
+ public void setTitle(String title) {
+ this.title = title;
+ }
+
+ public Optional getSubTitle() {
+ return subTitle;
+ }
+
+ public void setSubTitle(Optional subTitle) {
+ this.subTitle = subTitle;
+ }
+}
diff --git a/jackson/src/test/java/com/baeldung/jackson/deserialization/JacksonDeserializeTest.java b/jackson/src/test/java/com/baeldung/jackson/deserialization/JacksonDeserializeTest.java
index 7e91df5332..cb3bccb879 100644
--- a/jackson/src/test/java/com/baeldung/jackson/deserialization/JacksonDeserializeTest.java
+++ b/jackson/src/test/java/com/baeldung/jackson/deserialization/JacksonDeserializeTest.java
@@ -3,12 +3,10 @@ package com.baeldung.jackson.deserialization;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
-
import com.baeldung.jackson.entities.Movie;
-import org.junit.Assert;
import org.junit.Test;
-
import com.fasterxml.jackson.databind.ObjectMapper;
+import static org.junit.Assert.assertEquals;
public class JacksonDeserializeTest {
@@ -20,7 +18,7 @@ public class JacksonDeserializeTest {
final Movie movie = mapper.readValue(jsonInput, Movie.class);
final String expectedOutput = "Movie [imdbId=tt0472043, director=null, actors=[ActorJackson [imdbId=nm2199632, dateOfBirth=Tue Sep 21 11:00:00 GMT 1982, filmography=[Apocalypto, Beatdown, Wind Walkers]]]]";
- Assert.assertEquals(movie.toString(), expectedOutput);
+ assertEquals(expectedOutput, movie.toString());
}
@Test
@@ -35,7 +33,7 @@ public class JacksonDeserializeTest {
final Movie movie = mapper.readValue(jsonInput, Movie.class);
final String expectedOutput = "Movie [imdbId=tt0472043, director=Mel Gibson, actors=[ActorJackson [imdbId=nm2199632, dateOfBirth=Tue Sep 21 11:00:00 GMT 1982, filmography=[Apocalypto, Beatdown, Wind Walkers]]]]";
- Assert.assertEquals(movie.toString(), expectedOutput);
+ assertEquals(expectedOutput, movie.toString());
}
}
diff --git a/jackson/src/test/java/com/baeldung/jackson/miscellaneous/mixin/OptionalTypeTest.java b/jackson/src/test/java/com/baeldung/jackson/miscellaneous/mixin/OptionalTypeTest.java
new file mode 100644
index 0000000000..c6d51cd57f
--- /dev/null
+++ b/jackson/src/test/java/com/baeldung/jackson/miscellaneous/mixin/OptionalTypeTest.java
@@ -0,0 +1,61 @@
+package com.baeldung.jackson.miscellaneous.mixin;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
+import static io.restassured.path.json.JsonPath.from;
+import java.io.IOException;
+import java.util.Optional;
+import static org.assertj.core.api.Assertions.assertThat;
+import org.junit.Test;
+
+public class OptionalTypeTest {
+
+ ObjectMapper mapper = new ObjectMapper().registerModule(new Jdk8Module());
+
+ @Test
+ public void givenPresentOptional_whenSerializing_thenValueInJson() throws JsonProcessingException {
+
+ String subTitle = "The Parish Boy's Progress";
+ Book book = new Book();
+ book.setTitle("Oliver Twist");
+ book.setSubTitle(Optional.of(subTitle));
+
+ String result = mapper.writeValueAsString(book);
+
+ assertThat(from(result).getString("subTitle")).isEqualTo(subTitle);
+ }
+
+ @Test
+ public void givenEmptyOptional_whenSerializing_thenNullValue() throws JsonProcessingException {
+
+ Book book = new Book();
+ book.setTitle("Oliver Twist");
+ book.setSubTitle(Optional.empty());
+
+ String result = mapper.writeValueAsString(book);
+
+ assertThat(from(result).getString("subTitle")).isNull();
+ }
+
+ @Test
+ public void givenField_whenDeserializingIntoOptional_thenIsPresentWithValue() throws IOException {
+
+ String subTitle = "The Parish Boy's Progress";
+ String book = "{ \"title\": \"Oliver Twist\", \"subTitle\": \"" + subTitle + "\" }";
+
+ Book result = mapper.readValue(book, Book.class);
+
+ assertThat(result.getSubTitle()).isEqualTo(Optional.of(subTitle));
+ }
+
+ @Test
+ public void givenNullField_whenDeserializingIntoOptional_thenIsEmpty() throws IOException {
+
+ String book = "{ \"title\": \"Oliver Twist\", \"subTitle\": null }";
+
+ Book result = mapper.readValue(book, Book.class);
+
+ assertThat(result.getSubTitle()).isEmpty();
+ }
+}
diff --git a/jjwt/pom.xml b/jjwt/pom.xml
index c1332fa2d7..982b24987b 100644
--- a/jjwt/pom.xml
+++ b/jjwt/pom.xml
@@ -14,7 +14,7 @@
org.springframework.boot
spring-boot-starter-parent
- 1.4.2.RELEASE
+ 1.4.4.RELEASE
diff --git a/log-mdc/pom.xml b/log-mdc/pom.xml
index 28c8bb820e..931e68a178 100644
--- a/log-mdc/pom.xml
+++ b/log-mdc/pom.xml
@@ -2,9 +2,9 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.baeldung
- logmdc
+ log-mdc
0.0.1-SNAPSHOT
- logmdc
+ log-mdc
war
tutorial on logging with MDC and NDC
diff --git a/log-mdc/src/main/java/com/baeldung/mdc/TransactionFactory.java b/log-mdc/src/main/java/com/baeldung/mdc/TransactionFactory.java
index ec1887eea6..0904e4603f 100644
--- a/log-mdc/src/main/java/com/baeldung/mdc/TransactionFactory.java
+++ b/log-mdc/src/main/java/com/baeldung/mdc/TransactionFactory.java
@@ -7,15 +7,15 @@ import java.util.UUID;
public class TransactionFactory {
- private static final String[] NAMES = {"John", "Susan", "Marc", "Samantha"};
- private static long nextId = 1;
-
- public Transfer newInstance() {
- String transactionId = String.valueOf( nextId++ );
- String owner = NAMES[ (int) floor(random()*NAMES.length) ];
- long amount = (long) (random()*1500 + 500);
- Transfer tx = new Transfer(transactionId, owner, amount);
- return tx;
- }
-
+ private static final String[] NAMES = { "John", "Susan", "Marc", "Samantha" };
+ private static long nextId = 1;
+
+ public Transfer newInstance() {
+ String transactionId = String.valueOf(nextId++);
+ String owner = NAMES[(int) floor(random() * NAMES.length)];
+ long amount = (long) (random() * 1500 + 500);
+ Transfer tx = new Transfer(transactionId, owner, amount);
+ return tx;
+ }
+
}
diff --git a/log-mdc/src/main/java/com/baeldung/mdc/TransferDemo.java b/log-mdc/src/main/java/com/baeldung/mdc/TransferDemo.java
index daf256007c..259e9a8c5c 100644
--- a/log-mdc/src/main/java/com/baeldung/mdc/TransferDemo.java
+++ b/log-mdc/src/main/java/com/baeldung/mdc/TransferDemo.java
@@ -18,11 +18,11 @@ public class TransferDemo {
for (int i = 0; i < 10; i++) {
Transfer tx = transactionFactory.newInstance();
-
- //Runnable task = new Log4JRunnable(tx);
- //Runnable task = new Log4J2Runnable(tx);
+
+ // Runnable task = new Log4JRunnable(tx);
+ // Runnable task = new Log4J2Runnable(tx);
Runnable task = new Slf4jRunnable(tx);
-
+
executor.submit(task);
}
diff --git a/log-mdc/src/main/java/com/baeldung/ndc/controller/JBossLoggingController.java b/log-mdc/src/main/java/com/baeldung/ndc/controller/JBossLoggingController.java
index b024f3ec81..e581c45cd3 100644
--- a/log-mdc/src/main/java/com/baeldung/ndc/controller/JBossLoggingController.java
+++ b/log-mdc/src/main/java/com/baeldung/ndc/controller/JBossLoggingController.java
@@ -13,7 +13,6 @@ import org.springframework.web.bind.annotation.RestController;
import com.baeldung.ndc.Investment;
import com.baeldung.ndc.service.InvestmentService;
-
@RestController
public class JBossLoggingController {
@Autowired
diff --git a/log-mdc/src/test/java/com/baeldung/mdc/log4j/Demo.java b/log-mdc/src/test/java/com/baeldung/mdc/log4j/Demo.java
index f9a210606f..665168452a 100644
--- a/log-mdc/src/test/java/com/baeldung/mdc/log4j/Demo.java
+++ b/log-mdc/src/test/java/com/baeldung/mdc/log4j/Demo.java
@@ -17,7 +17,7 @@ public class Demo {
TransactionFactory transactionFactory = new TransactionFactory();
for (int i = 0; i < 10; i++) {
Transfer tx = transactionFactory.newInstance();
- Runnable task = new Log4JRunnable(tx);
+ Runnable task = new Log4JRunnable(tx);
executor.submit(task);
}
executor.shutdown();
diff --git a/log-mdc/src/test/java/com/baeldung/mdc/log4j2/Demo.java b/log-mdc/src/test/java/com/baeldung/mdc/log4j2/Demo.java
index 3f7c1d37d5..78c48c2a83 100644
--- a/log-mdc/src/test/java/com/baeldung/mdc/log4j2/Demo.java
+++ b/log-mdc/src/test/java/com/baeldung/mdc/log4j2/Demo.java
@@ -21,7 +21,7 @@ public class Demo {
TransactionFactory transactionFactory = new TransactionFactory();
for (int i = 0; i < 10; i++) {
Transfer tx = transactionFactory.newInstance();
- Runnable task = new Log4J2Runnable(tx);
+ Runnable task = new Log4J2Runnable(tx);
executor.submit(task);
}
executor.shutdown();
diff --git a/log-mdc/src/test/java/com/baeldung/mdc/slf4j/Demo.java b/log-mdc/src/test/java/com/baeldung/mdc/slf4j/Demo.java
index 98db698f47..de890f9f5d 100644
--- a/log-mdc/src/test/java/com/baeldung/mdc/slf4j/Demo.java
+++ b/log-mdc/src/test/java/com/baeldung/mdc/slf4j/Demo.java
@@ -21,7 +21,7 @@ public class Demo {
TransactionFactory transactionFactory = new TransactionFactory();
for (int i = 0; i < 10; i++) {
Transfer tx = transactionFactory.newInstance();
- Runnable task = new Slf4jRunnable(tx);
+ Runnable task = new Slf4jRunnable(tx);
executor.submit(task);
}
executor.shutdown();
diff --git a/mesos-marathon/Dockerfile b/mesos-marathon/Dockerfile
new file mode 100644
index 0000000000..ca79f2dc82
--- /dev/null
+++ b/mesos-marathon/Dockerfile
@@ -0,0 +1,4 @@
+FROM openjdk:8-jre-alpine
+ADD target/mesos-marathon-0.0.1-SNAPSHOT.jar app.jar
+EXPOSE 8082
+ENTRYPOINT ["java","-jar","/app.jar"]
\ No newline at end of file
diff --git a/mesos-marathon/dockerise.sh b/mesos-marathon/dockerise.sh
new file mode 100755
index 0000000000..50f5d38306
--- /dev/null
+++ b/mesos-marathon/dockerise.sh
@@ -0,0 +1,5 @@
+#!/usr/bin/env bash
+set -e
+docker login -u mogronalol -p $DOCKER_PASSWORD
+docker build -t baeldung/mesos-marathon-demo:$BUILD_NUMBER .
+docker push baeldung/mesos-marathon-demo:$BUILD_NUMBER
diff --git a/mesos-marathon/marathon.json b/mesos-marathon/marathon.json
new file mode 100644
index 0000000000..6471259e92
--- /dev/null
+++ b/mesos-marathon/marathon.json
@@ -0,0 +1,14 @@
+{
+ "id": "mesos-marathon-demo",
+ "container": {
+ "type": "DOCKER",
+ "docker": {
+ "image": "",
+ "network": "BRIDGE",
+ "portMappings": [
+ { "containerPort": 8082, "hostPort": 0 }
+ ]
+ },
+ "volumes": []
+ }
+}
\ No newline at end of file
diff --git a/mesos-marathon/pom.xml b/mesos-marathon/pom.xml
new file mode 100644
index 0000000000..ca17a5c4c4
--- /dev/null
+++ b/mesos-marathon/pom.xml
@@ -0,0 +1,46 @@
+
+
+ 4.0.0
+
+ com.baeldung
+ mesos-marathon
+ 0.0.1-SNAPSHOT
+
+
+ org.springframework.boot
+ spring-boot-starter-parent
+ 1.5.1.RELEASE
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-maven-plugin
+ 1.5.1.RELEASE
+
+
+
+ repackage
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/mesos-marathon/src/main/java/com/mogronalol/DemoApplication.java b/mesos-marathon/src/main/java/com/mogronalol/DemoApplication.java
new file mode 100644
index 0000000000..f757178026
--- /dev/null
+++ b/mesos-marathon/src/main/java/com/mogronalol/DemoApplication.java
@@ -0,0 +1,14 @@
+package com.mogronalol;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+import javax.annotation.PostConstruct;
+
+@SpringBootApplication
+public class DemoApplication {
+
+ public static void main(String[] args) {
+ SpringApplication.run(DemoApplication.class, args);
+ }
+}
diff --git a/mesos-marathon/src/main/java/com/mogronalol/HelloController.java b/mesos-marathon/src/main/java/com/mogronalol/HelloController.java
new file mode 100644
index 0000000000..2059280ba0
--- /dev/null
+++ b/mesos-marathon/src/main/java/com/mogronalol/HelloController.java
@@ -0,0 +1,17 @@
+package com.mogronalol;
+
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController(value = "/")
+public class HelloController {
+
+ @GetMapping
+ @ResponseBody
+ public String getMapping() {
+ return "Hello world";
+ }
+
+}
diff --git a/mesos-marathon/src/main/resources/application.properties b/mesos-marathon/src/main/resources/application.properties
new file mode 100644
index 0000000000..8d51d0c619
--- /dev/null
+++ b/mesos-marathon/src/main/resources/application.properties
@@ -0,0 +1 @@
+server.port=8082
\ No newline at end of file
diff --git a/mesos-marathon/src/test/java/com/mogronalol/DemoApplicationTests.java b/mesos-marathon/src/test/java/com/mogronalol/DemoApplicationTests.java
new file mode 100644
index 0000000000..5e88f9a70f
--- /dev/null
+++ b/mesos-marathon/src/test/java/com/mogronalol/DemoApplicationTests.java
@@ -0,0 +1,34 @@
+package com.mogronalol;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.boot.context.embedded.LocalServerPort;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import org.springframework.web.client.RestTemplate;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+@RunWith(SpringJUnit4ClassRunner.class)
+@SpringBootTest(classes = {DemoApplication.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
+public class DemoApplicationTests {
+
+ private RestTemplate restTemplate;
+
+ @LocalServerPort
+ private int port;
+
+ @Before
+ public void setUp() {
+ restTemplate = new RestTemplate();
+ }
+
+ @Test
+ public void contextLoads() {
+ final String result = restTemplate.getForObject("http://localhost:" + port + "/", String.class);
+ assertThat(result).isEqualTo("Hello world");
+ }
+
+}
diff --git a/metrics/src/test/java/com/baeldung/metrics/core/MetricsTest.java b/metrics/src/test/java/com/baeldung/metrics/core/MetricsTest.java
index f670acfaef..e876de6e65 100644
--- a/metrics/src/test/java/com/baeldung/metrics/core/MetricsTest.java
+++ b/metrics/src/test/java/com/baeldung/metrics/core/MetricsTest.java
@@ -138,15 +138,15 @@ public class MetricsTest {
long elapsed1 = context1.stop();
- assertEquals(5000000000L, elapsed1, 10000000);
+ assertEquals(5000000000L, elapsed1, 1000000000);
assertThat(timer.getCount(), equalTo(1L));
- assertEquals(0.2, timer.getMeanRate(), 0.1);
+ assertEquals(0.2, timer.getMeanRate(), 0.2);
Timer.Context context2 = timer.time();
TimeUnit.SECONDS.sleep(2);
context2.close();
assertThat(timer.getCount(), equalTo(2L));
- assertEquals(0.3, timer.getMeanRate(), 0.1);
+ assertEquals(0.3, timer.getMeanRate(), 0.2);
}
}
diff --git a/pom.xml b/pom.xml
index 41235dcc26..82df776044 100644
--- a/pom.xml
+++ b/pom.xml
@@ -11,6 +11,8 @@
UTF-8
+ refs/heads/master
+ false
@@ -79,6 +81,7 @@
mapstruct
metrics
+ mesos-marathon
mockito
mocks
@@ -147,6 +150,7 @@
spring-rest-docs
spring-rest
spring-security-basic-auth
+ spring-security-cache-control
spring-security-client/spring-security-jsp-authentication
spring-security-client/spring-security-jsp-authorize
spring-security-client/spring-security-jsp-config
@@ -155,7 +159,7 @@
spring-security-client/spring-security-thymeleaf-authorize
spring-security-client/spring-security-thymeleaf-config
spring-security-core
- spring-security-custom-permission
+ spring-security-mvc-boot
spring-security-mvc-custom
spring-security-mvc-digest-auth
spring-security-mvc-ldap
@@ -178,6 +182,7 @@
spring-reactor
testing
+ testng
video-tutorials
@@ -188,7 +193,17 @@
xstream
struts2
+ apache-velocity
-
+
+
+
+
+ com.vackosar.gitflowincrementalbuilder
+ gitflow-incremental-builder
+ 3.1
+
+
+
diff --git a/rxjava/pom.xml b/rxjava/pom.xml
index 63aa1f127e..b3936bf78d 100644
--- a/rxjava/pom.xml
+++ b/rxjava/pom.xml
@@ -26,10 +26,25 @@
rxjava
${rx.java.version}
+
+ junit
+ junit
+ ${junit.version}
+
+
+ org.hamcrest
+ hamcrest-all
+ ${hamcrest.version}
+ test
+
+
+
+ 4.12
1.2.5
+ 1.3
\ No newline at end of file
diff --git a/rxjava/src/main/java/com/baelding/rxjava/ColdObservableBackpressure.java b/rxjava/src/main/java/com/baelding/rxjava/ColdObservableBackpressure.java
deleted file mode 100644
index 9855123a3b..0000000000
--- a/rxjava/src/main/java/com/baelding/rxjava/ColdObservableBackpressure.java
+++ /dev/null
@@ -1,38 +0,0 @@
-package com.baelding.rxjava;
-
-import rx.Observable;
-import rx.schedulers.Schedulers;
-
-public class ColdObservableBackpressure {
- public static void main(String[] args) throws InterruptedException {
- Observable.range(1, 1_000_000).observeOn(Schedulers.computation()).subscribe(v -> ComputeFunction.compute(v), Throwable::printStackTrace);
-
- Thread.sleep(10_000);
-
- // Observable.range(1, 1_000_000) //implementation of reactive pull backpressure on cold observable
- // .subscribe(new Subscriber() {
- // @Override
- // public void onStart() {
- // request(1);
- // }
- //
- // public void onNext(Integer v) {
- // compute(v);
- //
- // request(1);
- // }
- //
- // @Override
- // public void onError(Throwable ex) {
- // ex.printStackTrace();
- // }
- //
- // @Override
- // public void onCompleted() {
- // System.out.println("Done!");
- // }
- // });
-
- }
-
-}
diff --git a/rxjava/src/main/java/com/baelding/rxjava/HotObservableBackpressureBatching.java b/rxjava/src/main/java/com/baelding/rxjava/HotObservableBackpressureBatching.java
deleted file mode 100644
index 6acda7eaad..0000000000
--- a/rxjava/src/main/java/com/baelding/rxjava/HotObservableBackpressureBatching.java
+++ /dev/null
@@ -1,18 +0,0 @@
-package com.baelding.rxjava;
-
-import rx.schedulers.Schedulers;
-import rx.subjects.PublishSubject;
-
-public class HotObservableBackpressureBatching {
- public static void main(String[] args) throws InterruptedException {
- PublishSubject source = PublishSubject.create();
-
- source.window(500).observeOn(Schedulers.computation()).subscribe(ComputeFunction::compute, Throwable::printStackTrace);
-
- for (int i = 0; i < 1_000_000; i++) {
- source.onNext(i);
- }
- Thread.sleep(10_000);
- }
-
-}
diff --git a/rxjava/src/main/java/com/baelding/rxjava/HotObservableBackpressureBuffering.java b/rxjava/src/main/java/com/baelding/rxjava/HotObservableBackpressureBuffering.java
deleted file mode 100644
index 50638f4c8a..0000000000
--- a/rxjava/src/main/java/com/baelding/rxjava/HotObservableBackpressureBuffering.java
+++ /dev/null
@@ -1,17 +0,0 @@
-package com.baelding.rxjava;
-
-import rx.schedulers.Schedulers;
-import rx.subjects.PublishSubject;
-
-public class HotObservableBackpressureBuffering {
- public static void main(String[] args) throws InterruptedException {
- PublishSubject source = PublishSubject.create();
-
- source.buffer(1024).observeOn(Schedulers.computation()).subscribe(ComputeFunction::compute, Throwable::printStackTrace);
-
- for (int i = 0; i < 1_000_000; i++) {
- source.onNext(i);
- }
- Thread.sleep(10_000);
- }
-}
diff --git a/rxjava/src/main/java/com/baelding/rxjava/HotObservableBackpressureSkipping.java b/rxjava/src/main/java/com/baelding/rxjava/HotObservableBackpressureSkipping.java
deleted file mode 100644
index f6f8b9f563..0000000000
--- a/rxjava/src/main/java/com/baelding/rxjava/HotObservableBackpressureSkipping.java
+++ /dev/null
@@ -1,21 +0,0 @@
-package com.baelding.rxjava;
-
-import rx.schedulers.Schedulers;
-import rx.subjects.PublishSubject;
-
-import java.util.concurrent.TimeUnit;
-
-public class HotObservableBackpressureSkipping {
- public static void main(String[] args) throws InterruptedException {
- PublishSubject source = PublishSubject.create();
-
- source.sample(100, TimeUnit.MILLISECONDS)
- // .throttleFirst(100, TimeUnit.MILLISECONDS)
- .observeOn(Schedulers.computation()).subscribe(ComputeFunction::compute, Throwable::printStackTrace);
-
- for (int i = 0; i < 1_000_000; i++) {
- source.onNext(i);
- }
- Thread.sleep(10_000);
- }
-}
diff --git a/rxjava/src/main/java/com/baelding/rxjava/HotObservableOnBackpressure.java b/rxjava/src/main/java/com/baelding/rxjava/HotObservableOnBackpressure.java
deleted file mode 100644
index afef8027bf..0000000000
--- a/rxjava/src/main/java/com/baelding/rxjava/HotObservableOnBackpressure.java
+++ /dev/null
@@ -1,18 +0,0 @@
-package com.baelding.rxjava;
-
-import rx.BackpressureOverflow;
-import rx.Observable;
-import rx.schedulers.Schedulers;
-
-public class HotObservableOnBackpressure {
- public static void main(String[] args) throws InterruptedException {
- Observable.range(1, 1_000_000).onBackpressureBuffer(16, () -> {
- }, BackpressureOverflow.ON_OVERFLOW_DROP_OLDEST).observeOn(Schedulers.computation()).subscribe(e -> {
- }, Throwable::printStackTrace);
-
- Observable.range(1, 1_000_000).onBackpressureDrop().observeOn(Schedulers.io()).doOnNext(ComputeFunction::compute).subscribe(v -> {
- }, Throwable::printStackTrace);
- Thread.sleep(10_000);
-
- }
-}
diff --git a/rxjava/src/main/java/com/baelding/rxjava/HotObservableWithoutBackpressure.java b/rxjava/src/main/java/com/baelding/rxjava/HotObservableWithoutBackpressure.java
deleted file mode 100644
index 7745dbe5c4..0000000000
--- a/rxjava/src/main/java/com/baelding/rxjava/HotObservableWithoutBackpressure.java
+++ /dev/null
@@ -1,20 +0,0 @@
-package com.baelding.rxjava;
-
-
-import rx.schedulers.Schedulers;
-import rx.subjects.PublishSubject;
-
-public class HotObservableWithoutBackpressure {
- public static void main(String[] args) throws InterruptedException {
- PublishSubject source = PublishSubject.create();
-
- source.observeOn(Schedulers.computation())
- .subscribe(ComputeFunction::compute, Throwable::printStackTrace);
-
-
- for (int i = 0; i < 1_000_000; i++) {
- source.onNext(i);
- }
- Thread.sleep(10_000);
- }
-}
diff --git a/rxjava/src/test/java/com/baeldung/rxjava/RxJavaBackpressureTest.java b/rxjava/src/test/java/com/baeldung/rxjava/RxJavaBackpressureTest.java
new file mode 100644
index 0000000000..8a495650b3
--- /dev/null
+++ b/rxjava/src/test/java/com/baeldung/rxjava/RxJavaBackpressureTest.java
@@ -0,0 +1,130 @@
+package com.baeldung.rxjava;
+
+import org.junit.Test;
+import rx.BackpressureOverflow;
+import rx.Observable;
+import rx.exceptions.MissingBackpressureException;
+import rx.observers.TestSubscriber;
+import rx.schedulers.Schedulers;
+import rx.subjects.PublishSubject;
+
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.IntStream;
+
+import static org.junit.Assert.assertTrue;
+
+public class RxJavaBackpressureTest {
+
+ @Test
+ public void givenColdObservable_shouldNotThrowException() {
+ // given
+ TestSubscriber testSubscriber = new TestSubscriber<>();
+
+ // when
+ Observable.range(1, 1_000_000).observeOn(Schedulers.computation()).subscribe(testSubscriber);
+
+ // then
+ testSubscriber.awaitTerminalEvent();
+ assertTrue(testSubscriber.getOnErrorEvents().size() == 0);
+
+ }
+
+ @Test
+ public void givenHotObservable_whenBackpressureNotDefined_shouldTrowException() {
+ // given
+ TestSubscriber testSubscriber = new TestSubscriber<>();
+ PublishSubject source = PublishSubject. create();
+
+ source.observeOn(Schedulers.computation()).subscribe(testSubscriber);
+
+ // when
+ IntStream.range(0, 1_000_000).forEach(source::onNext);
+
+ // then
+ testSubscriber.awaitTerminalEvent();
+ testSubscriber.assertError(MissingBackpressureException.class);
+ }
+
+ @Test
+ public void givenHotObservable_whenWindowIsDefined_shouldNotThrowException() {
+ // given
+ TestSubscriber> testSubscriber = new TestSubscriber<>();
+ PublishSubject source = PublishSubject. create();
+
+ // when
+ source.window(500).observeOn(Schedulers.computation()).subscribe(testSubscriber);
+
+ IntStream.range(0, 1_000).forEach(source::onNext);
+
+ // then
+ testSubscriber.awaitTerminalEvent(2, TimeUnit.SECONDS);
+ assertTrue(testSubscriber.getOnErrorEvents().size() == 0);
+
+ }
+
+ @Test
+ public void givenHotObservable_whenBufferIsDefined_shouldNotThrowException() {
+ // given
+ TestSubscriber> testSubscriber = new TestSubscriber<>();
+ PublishSubject source = PublishSubject. create();
+
+ // when
+ source.buffer(1024).observeOn(Schedulers.computation()).subscribe(testSubscriber);
+
+ IntStream.range(0, 1_000).forEach(source::onNext);
+
+ // then
+ testSubscriber.awaitTerminalEvent(2, TimeUnit.SECONDS);
+ assertTrue(testSubscriber.getOnErrorEvents().size() == 0);
+
+ }
+
+ @Test
+ public void givenHotObservable_whenSkippingOperationIsDefined_shouldNotThrowException() {
+ // given
+ TestSubscriber testSubscriber = new TestSubscriber<>();
+ PublishSubject source = PublishSubject. create();
+
+ // when
+ source.sample(100, TimeUnit.MILLISECONDS)
+ // .throttleFirst(100, TimeUnit.MILLISECONDS)
+ .observeOn(Schedulers.computation()).subscribe(testSubscriber);
+
+ IntStream.range(0, 1_000).forEach(source::onNext);
+
+ // then
+ testSubscriber.awaitTerminalEvent(2, TimeUnit.SECONDS);
+ assertTrue(testSubscriber.getOnErrorEvents().size() == 0);
+
+ }
+
+ @Test
+ public void givenHotObservable_whenOnBackpressureBufferDefined_shouldNotThrowException() {
+ // given
+ TestSubscriber testSubscriber = new TestSubscriber<>();
+
+ // when
+ Observable.range(1, 1_000_000).onBackpressureBuffer(16, () -> {
+ }, BackpressureOverflow.ON_OVERFLOW_DROP_OLDEST).observeOn(Schedulers.computation()).subscribe(testSubscriber);
+
+ // then
+ testSubscriber.awaitTerminalEvent(2, TimeUnit.SECONDS);
+ assertTrue(testSubscriber.getOnErrorEvents().size() == 0);
+
+ }
+
+ @Test
+ public void givenHotObservable_whenOnBackpressureDropDefined_shouldNotThrowException() {
+ // given
+ TestSubscriber testSubscriber = new TestSubscriber<>();
+
+ // when
+ Observable.range(1, 1_000_000).onBackpressureDrop().observeOn(Schedulers.computation()).subscribe(testSubscriber);
+
+ // then
+ testSubscriber.awaitTerminalEvent(2, TimeUnit.SECONDS);
+ assertTrue(testSubscriber.getOnErrorEvents().size() == 0);
+
+ }
+}
diff --git a/rxjava/src/test/java/com/baeldung/rxjava/RxJavaTesting.java b/rxjava/src/test/java/com/baeldung/rxjava/RxJavaTesting.java
new file mode 100644
index 0000000000..91c8ed540e
--- /dev/null
+++ b/rxjava/src/test/java/com/baeldung/rxjava/RxJavaTesting.java
@@ -0,0 +1,98 @@
+package com.baeldung.rxjava;
+
+import org.junit.Test;
+import rx.Observable;
+import rx.observers.TestSubscriber;
+import rx.schedulers.TestScheduler;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+import static org.hamcrest.Matchers.*;
+import static org.junit.Assert.assertThat;
+
+public class RxJavaTesting {
+ @Test
+ public void givenObservable_whenZip_shouldAssertBlockingInASameThread() {
+ // given
+ List letters = Arrays.asList("A", "B", "C", "D", "E");
+ List results = new ArrayList<>();
+ Observable observable = Observable.from(letters).zipWith(Observable.range(1, Integer.MAX_VALUE), (string, index) -> index + "-" + string);
+
+ // when
+ observable.subscribe(results::add);
+
+ // then
+ assertThat(results, notNullValue());
+ assertThat(results, hasSize(5));
+ assertThat(results, hasItems("1-A", "2-B", "3-C", "4-D", "5-E"));
+ }
+
+ @Test
+ public void givenObservable_whenZip_shouldAssertOnTestSubscriber() {
+ // given
+ List letters = Arrays.asList("A", "B", "C", "D", "E");
+ TestSubscriber subscriber = new TestSubscriber<>();
+
+ Observable observable = Observable.from(letters).zipWith(Observable.range(1, Integer.MAX_VALUE), ((string, index) -> index + "-" + string));
+
+ // when
+ observable.subscribe(subscriber);
+
+ // then
+ subscriber.assertCompleted();
+ subscriber.assertNoErrors();
+ subscriber.assertValueCount(5);
+ assertThat(subscriber.getOnNextEvents(), hasItems("1-A", "2-B", "3-C", "4-D", "5-E"));
+ }
+
+ @Test
+ public void givenTestObserver_whenExceptionWasThrowsOnObservable_observerShouldGetError() {
+ // given
+ List letters = Arrays.asList("A", "B", "C", "D", "E");
+ TestSubscriber subscriber = new TestSubscriber<>();
+
+ Observable observable = Observable.from(letters).zipWith(Observable.range(1, Integer.MAX_VALUE), ((string, index) -> index + "-" + string)).concatWith(Observable.error(new RuntimeException("error in Observable")));
+
+ // when
+ observable.subscribe(subscriber);
+
+ // then
+ subscriber.assertError(RuntimeException.class);
+ subscriber.assertNotCompleted();
+ }
+
+ @Test
+ public void givenObservableThatEmitsEventPerSecond_whenUseAdvanceByTime_shouldEmitEventPerSecond() {
+ // given
+ List letters = Arrays.asList("A", "B", "C", "D", "E");
+ TestScheduler scheduler = new TestScheduler();
+ TestSubscriber subscriber = new TestSubscriber<>();
+ Observable tick = Observable.interval(1, TimeUnit.SECONDS, scheduler);
+
+ Observable observable = Observable.from(letters).zipWith(tick, (string, index) -> index + "-" + string);
+
+ observable.subscribeOn(scheduler).subscribe(subscriber);
+
+ // expect
+ subscriber.assertNoValues();
+ subscriber.assertNotCompleted();
+
+ // when
+ scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
+
+ // then
+ subscriber.assertNoErrors();
+ subscriber.assertValueCount(1);
+ subscriber.assertValues("0-A");
+
+ // when
+ scheduler.advanceTimeTo(6, TimeUnit.SECONDS);
+ subscriber.assertCompleted();
+ subscriber.assertNoErrors();
+ subscriber.assertValueCount(5);
+ assertThat(subscriber.getOnNextEvents(), hasItems("0-A", "1-B", "2-C", "3-D", "4-E"));
+ }
+}
diff --git a/spring-all/pom.xml b/spring-all/pom.xml
index deb6bd6f6a..f28fe1f10d 100644
--- a/spring-all/pom.xml
+++ b/spring-all/pom.xml
@@ -11,7 +11,7 @@
org.springframework.boot
spring-boot-starter-parent
- 1.4.2.RELEASE
+ 1.4.4.RELEASE
diff --git a/spring-all/src/test/java/org/baeldung/properties/external/ExternalPropertiesWithXmlIntegrationTest.java b/spring-all/src/test/java/org/baeldung/properties/external/ExternalPropertiesWithXmlManualTest.java
similarity index 95%
rename from spring-all/src/test/java/org/baeldung/properties/external/ExternalPropertiesWithXmlIntegrationTest.java
rename to spring-all/src/test/java/org/baeldung/properties/external/ExternalPropertiesWithXmlManualTest.java
index 2ea2822b9a..a8a7bda91c 100644
--- a/spring-all/src/test/java/org/baeldung/properties/external/ExternalPropertiesWithXmlIntegrationTest.java
+++ b/spring-all/src/test/java/org/baeldung/properties/external/ExternalPropertiesWithXmlManualTest.java
@@ -13,7 +13,7 @@ import org.springframework.test.context.support.AnnotationConfigContextLoader;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { ExternalPropertiesWithXmlConfig.class }, loader = AnnotationConfigContextLoader.class)
@Ignore("manual only")
-public class ExternalPropertiesWithXmlIntegrationTest {
+public class ExternalPropertiesWithXmlManualTest {
@Autowired
private Environment env;
diff --git a/spring-all/src/test/java/org/baeldung/springretry/SpringRetryTest.java b/spring-all/src/test/java/org/baeldung/springretry/SpringRetryIntegrationTest.java
similarity index 96%
rename from spring-all/src/test/java/org/baeldung/springretry/SpringRetryTest.java
rename to spring-all/src/test/java/org/baeldung/springretry/SpringRetryIntegrationTest.java
index 2f3411957e..d7d0943e6b 100644
--- a/spring-all/src/test/java/org/baeldung/springretry/SpringRetryTest.java
+++ b/spring-all/src/test/java/org/baeldung/springretry/SpringRetryIntegrationTest.java
@@ -12,7 +12,7 @@ import java.sql.SQLException;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AppConfig.class, loader = AnnotationConfigContextLoader.class)
-public class SpringRetryTest {
+public class SpringRetryIntegrationTest {
@Autowired
private MyService myService;
diff --git a/spring-all/src/test/java/org/baeldung/taskscheduler/ThreadPoolTaskSchedulerTest.java b/spring-all/src/test/java/org/baeldung/taskscheduler/ThreadPoolTaskSchedulerIntegrationTest.java
similarity index 90%
rename from spring-all/src/test/java/org/baeldung/taskscheduler/ThreadPoolTaskSchedulerTest.java
rename to spring-all/src/test/java/org/baeldung/taskscheduler/ThreadPoolTaskSchedulerIntegrationTest.java
index cc247cb384..d95857d384 100644
--- a/spring-all/src/test/java/org/baeldung/taskscheduler/ThreadPoolTaskSchedulerTest.java
+++ b/spring-all/src/test/java/org/baeldung/taskscheduler/ThreadPoolTaskSchedulerIntegrationTest.java
@@ -8,7 +8,8 @@ import org.springframework.test.context.support.AnnotationConfigContextLoader;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { ThreadPoolTaskSchedulerConfig.class }, loader = AnnotationConfigContextLoader.class)
-public class ThreadPoolTaskSchedulerTest {
+public class ThreadPoolTaskSchedulerIntegrationTest {
+
@Test
public void testThreadPoolTaskSchedulerAnnotation() throws InterruptedException {
Thread.sleep(2550);
diff --git a/spring-all/src/test/java/org/baeldung/test/IntegrationTestSuite.java b/spring-all/src/test/java/org/baeldung/test/IntegrationTestSuite.java
index f9845f42a7..470ae9e538 100644
--- a/spring-all/src/test/java/org/baeldung/test/IntegrationTestSuite.java
+++ b/spring-all/src/test/java/org/baeldung/test/IntegrationTestSuite.java
@@ -5,7 +5,7 @@ import org.baeldung.properties.basic.PropertiesWithMultipleXmlsIntegrationTest;
import org.baeldung.properties.basic.PropertiesWithXmlIntegrationTest;
import org.baeldung.properties.external.ExternalPropertiesWithJavaIntegrationTest;
import org.baeldung.properties.external.ExternalPropertiesWithMultipleXmlsIntegrationTest;
-import org.baeldung.properties.external.ExternalPropertiesWithXmlIntegrationTest;
+import org.baeldung.properties.external.ExternalPropertiesWithXmlManualTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@@ -15,7 +15,7 @@ import org.junit.runners.Suite.SuiteClasses;
PropertiesWithXmlIntegrationTest.class,
ExternalPropertiesWithJavaIntegrationTest.class,
ExternalPropertiesWithMultipleXmlsIntegrationTest.class,
- ExternalPropertiesWithXmlIntegrationTest.class,
+ ExternalPropertiesWithXmlManualTest.class,
ExtendedPropertiesWithJavaIntegrationTest.class,
PropertiesWithMultipleXmlsIntegrationTest.class,
})// @formatter:on
diff --git a/spring-boot/pom.xml b/spring-boot/pom.xml
index 68a5857865..e77ab10aff 100644
--- a/spring-boot/pom.xml
+++ b/spring-boot/pom.xml
@@ -1,5 +1,5 @@
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.baeldung
spring-boot
@@ -12,8 +12,8 @@
org.springframework.boot
spring-boot-starter-parent
- 1.4.2.RELEASE
-
+ 1.5.1.RELEASE
+
@@ -87,6 +87,14 @@
jquery
${jquery.version}
+
+
+ org.apache.tomcat
+ tomcat-servlet-api
+ ${tomee-servlet-api.version}
+ provided
+
+
@@ -166,7 +174,7 @@
- json
+ json
@@ -186,6 +194,7 @@
3.1.1
3.3.7-1
3.1.7
+ 8.5.11
diff --git a/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/SpringBootAnnotatedApp.java b/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/SpringBootAnnotatedApp.java
new file mode 100644
index 0000000000..b4d416dd96
--- /dev/null
+++ b/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/SpringBootAnnotatedApp.java
@@ -0,0 +1,25 @@
+package com.baeldung.annotation.servletcomponentscan;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.web.servlet.ServletComponentScan;
+
+/**
+ * using the following annotations are equivalent:
+ *
+ * @ServletComponentScan
+ *
+ * @ServletComponentScan(basePackages = "com.baeldung.annotation.servletcomponentscan.components")
+ *
+ * @ServletComponentScan(basePackageClasses = {AttrListener.class, HelloFilter.class, HelloServlet.class, EchoServlet.class})
+ *
+ */
+@SpringBootApplication
+@ServletComponentScan("com.baeldung.annotation.servletcomponentscan.components")
+public class SpringBootAnnotatedApp {
+
+ public static void main(String[] args) {
+ SpringApplication.run(SpringBootAnnotatedApp.class, args);
+ }
+
+}
diff --git a/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/SpringBootPlainApp.java b/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/SpringBootPlainApp.java
new file mode 100644
index 0000000000..8a39078aac
--- /dev/null
+++ b/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/SpringBootPlainApp.java
@@ -0,0 +1,13 @@
+package com.baeldung.annotation.servletcomponentscan;
+
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.annotation.ComponentScan;
+
+@SpringBootApplication
+@ComponentScan(basePackages = "com.baeldung.annotation.servletcomponentscan.components")
+public class SpringBootPlainApp {
+
+ public static void main(String[] args) {
+ }
+
+}
diff --git a/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/components/AttrListener.java b/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/components/AttrListener.java
new file mode 100644
index 0000000000..bad39c52c4
--- /dev/null
+++ b/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/components/AttrListener.java
@@ -0,0 +1,23 @@
+package com.baeldung.annotation.servletcomponentscan.components;
+
+import javax.servlet.ServletContextEvent;
+import javax.servlet.ServletContextListener;
+import javax.servlet.annotation.WebListener;
+
+@WebListener
+public class AttrListener implements ServletContextListener {
+
+ @Override
+ public void contextInitialized(ServletContextEvent servletContextEvent) {
+ servletContextEvent
+ .getServletContext()
+ .setAttribute("servlet-context-attr", "test");
+ System.out.println("context init");
+ }
+
+ @Override
+ public void contextDestroyed(ServletContextEvent servletContextEvent) {
+ System.out.println("context destroy");
+ }
+
+}
diff --git a/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/components/EchoServlet.java b/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/components/EchoServlet.java
new file mode 100644
index 0000000000..3419cd0eaf
--- /dev/null
+++ b/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/components/EchoServlet.java
@@ -0,0 +1,29 @@
+package com.baeldung.annotation.servletcomponentscan.components;
+
+import javax.servlet.annotation.WebServlet;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.StandardCopyOption;
+
+@WebServlet(name = "echo servlet", urlPatterns = "/echo")
+public class EchoServlet extends HttpServlet {
+
+ @Override
+ public void doPost(HttpServletRequest request, HttpServletResponse response) {
+ try {
+ Path path = File
+ .createTempFile("echo", "tmp")
+ .toPath();
+ Files.copy(request.getInputStream(), path, StandardCopyOption.REPLACE_EXISTING);
+ Files.copy(path, response.getOutputStream());
+ Files.delete(path);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+}
diff --git a/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/components/HelloFilter.java b/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/components/HelloFilter.java
new file mode 100644
index 0000000000..dc2368c5b2
--- /dev/null
+++ b/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/components/HelloFilter.java
@@ -0,0 +1,32 @@
+package com.baeldung.annotation.servletcomponentscan.components;
+
+import javax.servlet.*;
+import javax.servlet.annotation.WebFilter;
+import javax.servlet.annotation.WebInitParam;
+import java.io.IOException;
+
+@WebFilter(urlPatterns = "/hello", description = "a filter for hello servlet", initParams = { @WebInitParam(name = "msg", value = "filtering ") }, filterName = "hello filter", servletNames = { "echo servlet" })
+public class HelloFilter implements Filter {
+
+ private FilterConfig filterConfig;
+
+ @Override
+ public void init(FilterConfig filterConfig) throws ServletException {
+ System.out.println("filter init");
+ this.filterConfig = filterConfig;
+ }
+
+ @Override
+ public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
+ servletResponse
+ .getOutputStream()
+ .print(filterConfig.getInitParameter("msg"));
+ filterChain.doFilter(servletRequest, servletResponse);
+ }
+
+ @Override
+ public void destroy() {
+ System.out.println("filter destroy");
+ }
+
+}
diff --git a/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/components/HelloServlet.java b/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/components/HelloServlet.java
new file mode 100644
index 0000000000..aeae7aecc9
--- /dev/null
+++ b/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/components/HelloServlet.java
@@ -0,0 +1,32 @@
+package com.baeldung.annotation.servletcomponentscan.components;
+
+import javax.servlet.ServletConfig;
+import javax.servlet.annotation.WebInitParam;
+import javax.servlet.annotation.WebServlet;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+
+@WebServlet(urlPatterns = "/hello", initParams = { @WebInitParam(name = "msg", value = "hello")})
+public class HelloServlet extends HttpServlet {
+
+ private ServletConfig servletConfig;
+
+ @Override
+ public void init(ServletConfig servletConfig){
+ this.servletConfig = servletConfig;
+ }
+
+ @Override
+ public void doGet(HttpServletRequest request, HttpServletResponse response) {
+ try {
+ response
+ .getOutputStream()
+ .write(servletConfig.getInitParameter("msg").getBytes());
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+}
diff --git a/spring-boot/src/main/java/com/baeldung/utils/Application.java b/spring-boot/src/main/java/com/baeldung/utils/Application.java
new file mode 100644
index 0000000000..a3d9f9130c
--- /dev/null
+++ b/spring-boot/src/main/java/com/baeldung/utils/Application.java
@@ -0,0 +1,18 @@
+package com.baeldung.utils;
+
+import javax.annotation.security.RolesAllowed;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.annotation.ComponentScan;
+
+@SpringBootApplication
+@ComponentScan(basePackages="com.baeldung.utils")
+public class Application {
+
+ @RolesAllowed("*")
+ public static void main(String[] args) {
+ SpringApplication.run(Application.class, args);
+ }
+
+}
diff --git a/spring-boot/src/main/java/com/baeldung/utils/controller/UtilsController.java b/spring-boot/src/main/java/com/baeldung/utils/controller/UtilsController.java
new file mode 100644
index 0000000000..7b4827cdf2
--- /dev/null
+++ b/spring-boot/src/main/java/com/baeldung/utils/controller/UtilsController.java
@@ -0,0 +1,49 @@
+package com.baeldung.utils.controller;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.web.bind.ServletRequestBindingException;
+import org.springframework.web.bind.ServletRequestUtils;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.util.WebUtils;
+
+@Controller
+public class UtilsController {
+
+ @GetMapping("/utils")
+ public String webUtils(Model model) {
+ return "utils";
+ }
+
+ @PostMapping("/setParam")
+ public String post(HttpServletRequest request, Model model) {
+ String param = ServletRequestUtils.getStringParameter(request, "param", "DEFAULT");
+
+// Long param = ServletRequestUtils.getLongParameter(request, "param",1L);
+// boolean param = ServletRequestUtils.getBooleanParameter(request, "param", true);
+// double param = ServletRequestUtils.getDoubleParameter(request, "param", 1000);
+// float param = ServletRequestUtils.getFloatParameter(request, "param", (float) 1.00);
+// int param = ServletRequestUtils.getIntParameter(request, "param", 100);
+
+// try {
+// ServletRequestUtils.getRequiredStringParameter(request, "param");
+// } catch (ServletRequestBindingException e) {
+// e.printStackTrace();
+// }
+
+ WebUtils.setSessionAttribute(request, "parameter", param);
+ model.addAttribute("parameter", "You set: "+(String) WebUtils.getSessionAttribute(request, "parameter"));
+ return "utils";
+ }
+
+ @GetMapping("/other")
+ public String other(HttpServletRequest request, Model model) {
+ String param = (String) WebUtils.getSessionAttribute(request, "parameter");
+ model.addAttribute("parameter", param);
+ return "other";
+ }
+
+}
diff --git a/spring-boot/src/main/java/com/baeldung/TestController.java b/spring-boot/src/main/java/com/baeldung/webjar/TestController.java
similarity index 91%
rename from spring-boot/src/main/java/com/baeldung/TestController.java
rename to spring-boot/src/main/java/com/baeldung/webjar/TestController.java
index 0e28ca67f8..e8e7fd5ce9 100644
--- a/spring-boot/src/main/java/com/baeldung/TestController.java
+++ b/spring-boot/src/main/java/com/baeldung/webjar/TestController.java
@@ -1,4 +1,4 @@
-package com.baeldung;
+package com.baeldung.webjar;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
diff --git a/spring-boot/src/main/java/com/baeldung/WebjarsdemoApplication.java b/spring-boot/src/main/java/com/baeldung/webjar/WebjarsdemoApplication.java
similarity index 77%
rename from spring-boot/src/main/java/com/baeldung/WebjarsdemoApplication.java
rename to spring-boot/src/main/java/com/baeldung/webjar/WebjarsdemoApplication.java
index 35490131c6..d2135754c9 100644
--- a/spring-boot/src/main/java/com/baeldung/WebjarsdemoApplication.java
+++ b/spring-boot/src/main/java/com/baeldung/webjar/WebjarsdemoApplication.java
@@ -1,7 +1,8 @@
-package com.baeldung;
+package com.baeldung.webjar;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
public class WebjarsdemoApplication {
diff --git a/spring-boot/src/main/java/org/baeldung/common/error/SpringHelloServletRegistrationBean.java b/spring-boot/src/main/java/org/baeldung/common/error/SpringHelloServletRegistrationBean.java
index 78680baf7d..723afddd06 100644
--- a/spring-boot/src/main/java/org/baeldung/common/error/SpringHelloServletRegistrationBean.java
+++ b/spring-boot/src/main/java/org/baeldung/common/error/SpringHelloServletRegistrationBean.java
@@ -1,8 +1,8 @@
package org.baeldung.common.error;
-import javax.servlet.Servlet;
+import org.springframework.boot.web.servlet.ServletRegistrationBean;
-import org.springframework.boot.context.embedded.ServletRegistrationBean;
+import javax.servlet.Servlet;
public class SpringHelloServletRegistrationBean extends ServletRegistrationBean {
diff --git a/spring-boot/src/main/java/org/baeldung/common/properties/MyServletContainerCustomizationBean.java b/spring-boot/src/main/java/org/baeldung/common/properties/MyServletContainerCustomizationBean.java
index 97130bed6a..9b5a0aa948 100644
--- a/spring-boot/src/main/java/org/baeldung/common/properties/MyServletContainerCustomizationBean.java
+++ b/spring-boot/src/main/java/org/baeldung/common/properties/MyServletContainerCustomizationBean.java
@@ -2,7 +2,7 @@ package org.baeldung.common.properties;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
-import org.springframework.boot.context.embedded.ErrorPage;
+import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
diff --git a/spring-boot/src/main/java/org/baeldung/controller/GenericEntityController.java b/spring-boot/src/main/java/org/baeldung/controller/GenericEntityController.java
index 7d6293056a..7d1ad7d899 100644
--- a/spring-boot/src/main/java/org/baeldung/controller/GenericEntityController.java
+++ b/spring-boot/src/main/java/org/baeldung/controller/GenericEntityController.java
@@ -54,8 +54,6 @@ public class GenericEntityController {
@GetMapping("/entity/findbyversion")
public ResponseEntity findByVersion(@Version String version) {
- return version != null
- ? new ResponseEntity(entityList.stream().findFirst().get(), HttpStatus.OK)
- : new ResponseEntity(HttpStatus.NOT_FOUND);
+ return version != null ? new ResponseEntity(entityList.stream().findFirst().get(), HttpStatus.OK) : new ResponseEntity(HttpStatus.NOT_FOUND);
}
}
diff --git a/spring-boot/src/main/resources/templates/other.html b/spring-boot/src/main/resources/templates/other.html
new file mode 100644
index 0000000000..d13373f9fe
--- /dev/null
+++ b/spring-boot/src/main/resources/templates/other.html
@@ -0,0 +1,16 @@
+
+
+
+
+Spring Utils Demo
+
+
+
+ Parameter set by you:
+
+
\ No newline at end of file
diff --git a/spring-boot/src/main/resources/templates/utils.html b/spring-boot/src/main/resources/templates/utils.html
new file mode 100644
index 0000000000..93030f424f
--- /dev/null
+++ b/spring-boot/src/main/resources/templates/utils.html
@@ -0,0 +1,23 @@
+
+
+
+
+Spring Utils Demo
+
+
+
+
+
+Another Page
+
+
\ No newline at end of file
diff --git a/spring-boot/src/test/java/com/baeldung/annotation/servletcomponentscan/SpringBootWithServletComponentIntegrationTest.java b/spring-boot/src/test/java/com/baeldung/annotation/servletcomponentscan/SpringBootWithServletComponentIntegrationTest.java
new file mode 100644
index 0000000000..8d5eb56bf4
--- /dev/null
+++ b/spring-boot/src/test/java/com/baeldung/annotation/servletcomponentscan/SpringBootWithServletComponentIntegrationTest.java
@@ -0,0 +1,65 @@
+package com.baeldung.annotation.servletcomponentscan;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.test.web.client.TestRestTemplate;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.test.context.TestPropertySource;
+import org.springframework.test.context.junit4.SpringRunner;
+
+import javax.servlet.FilterRegistration;
+import javax.servlet.ServletContext;
+
+import static org.junit.Assert.*;
+
+@RunWith(SpringRunner.class)
+@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = SpringBootAnnotatedApp.class)
+@AutoConfigureMockMvc
+@TestPropertySource(properties = { "security.basic.enabled=false" })
+public class SpringBootWithServletComponentIntegrationTest {
+
+ @Autowired private ServletContext servletContext;
+
+ @Test
+ public void givenServletContext_whenAccessAttrs_thenFoundAttrsPutInServletListner() {
+ assertNotNull(servletContext);
+ assertNotNull(servletContext.getAttribute("servlet-context-attr"));
+ assertEquals("test", servletContext.getAttribute("servlet-context-attr"));
+ }
+
+ @Test
+ public void givenServletContext_whenCheckHelloFilterMappings_thenCorrect() {
+ assertNotNull(servletContext);
+ FilterRegistration filterRegistration = servletContext.getFilterRegistration("hello filter");
+
+ assertNotNull(filterRegistration);
+ assertTrue(filterRegistration
+ .getServletNameMappings()
+ .contains("echo servlet"));
+ }
+
+ @Autowired private TestRestTemplate restTemplate;
+
+ @Test
+ public void givenServletFilter_whenGetHello_thenRequestFiltered() {
+ ResponseEntity responseEntity = this.restTemplate.getForEntity("/hello", String.class);
+ assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
+ assertEquals("filtering hello", responseEntity.getBody());
+ }
+
+ @Test
+ public void givenFilterAndServlet_whenPostEcho_thenEchoFiltered() {
+ ResponseEntity responseEntity = this.restTemplate.postForEntity("/echo", "echo", String.class);
+ assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
+ assertEquals("filtering echo", responseEntity.getBody());
+ }
+
+
+
+}
+
+
diff --git a/spring-boot/src/test/java/com/baeldung/annotation/servletcomponentscan/SpringBootWithoutServletComponentIntegrationTest.java b/spring-boot/src/test/java/com/baeldung/annotation/servletcomponentscan/SpringBootWithoutServletComponentIntegrationTest.java
new file mode 100644
index 0000000000..64507ad02c
--- /dev/null
+++ b/spring-boot/src/test/java/com/baeldung/annotation/servletcomponentscan/SpringBootWithoutServletComponentIntegrationTest.java
@@ -0,0 +1,50 @@
+package com.baeldung.annotation.servletcomponentscan;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.test.web.client.TestRestTemplate;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.test.context.TestPropertySource;
+import org.springframework.test.context.junit4.SpringRunner;
+
+import javax.servlet.FilterRegistration;
+import javax.servlet.ServletContext;
+
+import static org.junit.Assert.*;
+
+@RunWith(SpringRunner.class)
+@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = SpringBootPlainApp.class)
+@AutoConfigureMockMvc
+@TestPropertySource(properties = { "security.basic.enabled=false" })
+public class SpringBootWithoutServletComponentIntegrationTest {
+
+ @Autowired private ServletContext servletContext;
+
+ @Autowired private TestRestTemplate restTemplate;
+
+ @Test
+ public void givenServletContext_whenAccessAttrs_thenNotFound() {
+ assertNull(servletContext.getAttribute("servlet-context-attr"));
+ }
+
+ @Test
+ public void givenServletFilter_whenGetHello_thenEndpointNotFound() {
+ ResponseEntity responseEntity = this.restTemplate.getForEntity("/hello", String.class);
+ assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
+ }
+
+ @Test
+ public void givenServletContext_whenCheckFilterMappings_thenEmpty() {
+ assertNotNull(servletContext);
+ FilterRegistration filterRegistration = servletContext.getFilterRegistration("hello filter");
+
+ assertNull(filterRegistration);
+ }
+
+}
+
+
diff --git a/spring-boot/src/test/java/com/baeldung/intro/AppLiveTest.java b/spring-boot/src/test/java/com/baeldung/intro/AppLiveTest.java
index 772709dc30..af46fe0423 100644
--- a/spring-boot/src/test/java/com/baeldung/intro/AppLiveTest.java
+++ b/spring-boot/src/test/java/com/baeldung/intro/AppLiveTest.java
@@ -6,6 +6,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
+import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
@@ -17,6 +18,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
+@TestPropertySource(properties = { "security.basic.enabled=false" })
public class AppLiveTest {
@Autowired
diff --git a/spring-boot/src/test/java/com/baeldung/utils/UtilsControllerTest.java b/spring-boot/src/test/java/com/baeldung/utils/UtilsControllerTest.java
new file mode 100644
index 0000000000..7aed45dbb0
--- /dev/null
+++ b/spring-boot/src/test/java/com/baeldung/utils/UtilsControllerTest.java
@@ -0,0 +1,41 @@
+package com.baeldung.utils;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.InjectMocks;
+import org.mockito.MockitoAnnotations;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+
+
+import com.baeldung.utils.controller.UtilsController;
+
+public class UtilsControllerTest {
+
+ @InjectMocks
+ private UtilsController utilsController;
+
+ private MockMvc mockMvc;
+
+ @Before
+ public void setup() {
+ MockitoAnnotations.initMocks(this);
+ this.mockMvc = MockMvcBuilders.standaloneSetup(utilsController)
+ .build();
+
+ }
+
+ @Test
+ public void givenParameter_setRequestParam_andSetSessionAttribute() throws Exception {
+ String param = "testparam";
+ this.mockMvc.perform(
+ post("/setParam")
+ .param("param", param)
+ .sessionAttr("parameter", param))
+ .andExpect(status().isOk());
+ }
+
+}
diff --git a/spring-boot/src/test/java/com/baeldung/WebjarsdemoApplicationIntegrationTest.java b/spring-boot/src/test/java/com/baeldung/webjar/WebjarsdemoApplicationIntegrationTest.java
similarity index 68%
rename from spring-boot/src/test/java/com/baeldung/WebjarsdemoApplicationIntegrationTest.java
rename to spring-boot/src/test/java/com/baeldung/webjar/WebjarsdemoApplicationIntegrationTest.java
index 3558682b97..d6e71dcf6b 100644
--- a/spring-boot/src/test/java/com/baeldung/WebjarsdemoApplicationIntegrationTest.java
+++ b/spring-boot/src/test/java/com/baeldung/webjar/WebjarsdemoApplicationIntegrationTest.java
@@ -1,13 +1,13 @@
-package com.baeldung;
+package com.baeldung.webjar;
import org.junit.Test;
import org.junit.runner.RunWith;
-import org.springframework.boot.test.SpringApplicationConfiguration;
+import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
@RunWith(SpringJUnit4ClassRunner.class)
-@SpringApplicationConfiguration(classes = WebjarsdemoApplication.class)
+@SpringBootTest(classes = WebjarsdemoApplication.class)
@WebAppConfiguration
public class WebjarsdemoApplicationIntegrationTest {
diff --git a/spring-boot/src/test/java/org/baeldung/SpringBootApplicationIntegrationTest.java b/spring-boot/src/test/java/org/baeldung/SpringBootApplicationIntegrationTest.java
index 8cdcdb2216..87c59a4662 100644
--- a/spring-boot/src/test/java/org/baeldung/SpringBootApplicationIntegrationTest.java
+++ b/spring-boot/src/test/java/org/baeldung/SpringBootApplicationIntegrationTest.java
@@ -9,7 +9,7 @@ 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.SpringApplicationConfiguration;
+import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
@@ -22,7 +22,7 @@ import org.springframework.web.context.WebApplicationContext;
import java.nio.charset.Charset;
@RunWith(SpringJUnit4ClassRunner.class)
-@SpringApplicationConfiguration(classes = Application.class)
+@SpringBootTest(classes = Application.class)
@WebAppConfiguration
public class SpringBootApplicationIntegrationTest {
@Autowired
@@ -45,29 +45,22 @@ public class SpringBootApplicationIntegrationTest {
public void givenRequestHasBeenMade_whenMeetsFindByDateOfGivenConditions_thenCorrect() throws Exception {
MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
- mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbydate/{date}", "2011-12-03T10:15:30"))
- .andExpect(MockMvcResultMatchers.status().isOk())
- .andExpect(MockMvcResultMatchers.content().contentType(contentType))
- .andExpect(jsonPath("$.id", equalTo(1)));
+ mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbydate/{date}", "2011-12-03T10:15:30")).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(contentType))
+ .andExpect(jsonPath("$.id", equalTo(1)));
}
@Test
public void givenRequestHasBeenMade_whenMeetsFindByModeOfGivenConditions_thenCorrect() throws Exception {
MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
- mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbymode/{mode}", Modes.ALPHA.name()))
- .andExpect(MockMvcResultMatchers.status().isOk())
- .andExpect(MockMvcResultMatchers.content().contentType(contentType))
- .andExpect(jsonPath("$.id", equalTo(1)));
+ mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbymode/{mode}", Modes.ALPHA.name())).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(contentType)).andExpect(jsonPath("$.id", equalTo(1)));
}
@Test
public void givenRequestHasBeenMade_whenMeetsFindByVersionOfGivenConditions_thenCorrect() throws Exception {
MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
- mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbyversion").header("Version", "1.0.0"))
- .andExpect(MockMvcResultMatchers.status().isOk())
- .andExpect(MockMvcResultMatchers.content().contentType(contentType))
- .andExpect(jsonPath("$.id", equalTo(1)));
+ mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbyversion").header("Version", "1.0.0")).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(contentType))
+ .andExpect(jsonPath("$.id", equalTo(1)));
}
}
diff --git a/spring-boot/src/test/java/org/baeldung/SpringBootJPAIntegrationTest.java b/spring-boot/src/test/java/org/baeldung/SpringBootJPAIntegrationTest.java
index 233684bc24..d4b19e6a1d 100644
--- a/spring-boot/src/test/java/org/baeldung/SpringBootJPAIntegrationTest.java
+++ b/spring-boot/src/test/java/org/baeldung/SpringBootJPAIntegrationTest.java
@@ -5,14 +5,14 @@ import org.baeldung.repository.GenericEntityRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.boot.test.SpringApplicationConfiguration;
+import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@RunWith(SpringJUnit4ClassRunner.class)
-@SpringApplicationConfiguration(classes = Application.class)
+@SpringBootTest(classes = Application.class)
public class SpringBootJPAIntegrationTest {
@Autowired
private GenericEntityRepository genericEntityRepository;
diff --git a/spring-boot/src/test/java/org/baeldung/SpringBootMailIntegrationTest.java b/spring-boot/src/test/java/org/baeldung/SpringBootMailIntegrationTest.java
index cec25f20f9..10e3d6d60b 100644
--- a/spring-boot/src/test/java/org/baeldung/SpringBootMailIntegrationTest.java
+++ b/spring-boot/src/test/java/org/baeldung/SpringBootMailIntegrationTest.java
@@ -5,7 +5,7 @@ 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.SpringApplicationConfiguration;
+import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -23,7 +23,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
@RunWith(SpringJUnit4ClassRunner.class)
-@SpringApplicationConfiguration(classes = Application.class)
+@SpringBootTest(classes = Application.class)
public class SpringBootMailIntegrationTest {
@Autowired
private JavaMailSender javaMailSender;
diff --git a/spring-cloud-data-flow/batch-job/pom.xml b/spring-cloud-data-flow/batch-job/pom.xml
index 3d05732027..5e519e9a35 100644
--- a/spring-cloud-data-flow/batch-job/pom.xml
+++ b/spring-cloud-data-flow/batch-job/pom.xml
@@ -14,7 +14,7 @@
org.springframework.boot
spring-boot-starter-parent
- 1.4.2.RELEASE
+ 1.4.4.RELEASE
diff --git a/spring-cloud-data-flow/data-flow-server/pom.xml b/spring-cloud-data-flow/data-flow-server/pom.xml
index b1f920c94e..1ed2d4fb74 100644
--- a/spring-cloud-data-flow/data-flow-server/pom.xml
+++ b/spring-cloud-data-flow/data-flow-server/pom.xml
@@ -14,7 +14,7 @@
org.springframework.boot
spring-boot-starter-parent
- 1.4.2.RELEASE
+ 1.4.4.RELEASE
diff --git a/spring-cloud-data-flow/data-flow-shell/pom.xml b/spring-cloud-data-flow/data-flow-shell/pom.xml
index 55fa995052..d3bd297152 100644
--- a/spring-cloud-data-flow/data-flow-shell/pom.xml
+++ b/spring-cloud-data-flow/data-flow-shell/pom.xml
@@ -14,7 +14,7 @@
org.springframework.boot
spring-boot-starter-parent
- 1.4.2.RELEASE
+ 1.4.4.RELEASE
diff --git a/spring-cloud-data-flow/log-sink/pom.xml b/spring-cloud-data-flow/log-sink/pom.xml
index 6443ed88c8..dc21132b9f 100644
--- a/spring-cloud-data-flow/log-sink/pom.xml
+++ b/spring-cloud-data-flow/log-sink/pom.xml
@@ -14,7 +14,7 @@
org.springframework.boot
spring-boot-starter-parent
- 1.4.2.RELEASE
+ 1.4.4.RELEASE
diff --git a/spring-cloud-data-flow/time-processor/pom.xml b/spring-cloud-data-flow/time-processor/pom.xml
index d7553b110f..51a66014e0 100644
--- a/spring-cloud-data-flow/time-processor/pom.xml
+++ b/spring-cloud-data-flow/time-processor/pom.xml
@@ -14,7 +14,7 @@
org.springframework.boot
spring-boot-starter-parent
- 1.4.2.RELEASE
+ 1.4.4.RELEASE
diff --git a/spring-cloud-data-flow/time-source/pom.xml b/spring-cloud-data-flow/time-source/pom.xml
index 2523dfabea..57933c2833 100644
--- a/spring-cloud-data-flow/time-source/pom.xml
+++ b/spring-cloud-data-flow/time-source/pom.xml
@@ -14,7 +14,7 @@
org.springframework.boot
spring-boot-starter-parent
- 1.4.2.RELEASE
+ 1.4.4.RELEASE
diff --git a/spring-cloud/spring-cloud-bootstrap/application-config/resource.properties b/spring-cloud/spring-cloud-bootstrap/application-config/book-service.properties
similarity index 91%
rename from spring-cloud/spring-cloud-bootstrap/application-config/resource.properties
rename to spring-cloud/spring-cloud-bootstrap/application-config/book-service.properties
index 9fb610d655..e1244a0cf0 100644
--- a/spring-cloud/spring-cloud-bootstrap/application-config/resource.properties
+++ b/spring-cloud/spring-cloud-bootstrap/application-config/book-service.properties
@@ -1,4 +1,4 @@
-spring.application.name=resource
+spring.application.name=book-service
server.port=8083
resource.returnString=hello cloud
diff --git a/spring-cloud/spring-cloud-bootstrap/application-config/gateway.properties b/spring-cloud/spring-cloud-bootstrap/application-config/gateway.properties
index 6f52a59db9..09f7f3bf4a 100644
--- a/spring-cloud/spring-cloud-bootstrap/application-config/gateway.properties
+++ b/spring-cloud/spring-cloud-bootstrap/application-config/gateway.properties
@@ -6,9 +6,13 @@ eureka.client.registryFetchIntervalSeconds = 5
management.security.sessions=always
-zuul.routes.resource.path=/resource/**
-zuul.routes.resource.sensitive-headers=Set-Cookie,Authorization
-hystrix.command.resource.execution.isolation.thread.timeoutInMilliseconds=600000
+zuul.routes.book-service.path=/book-service/**
+zuul.routes.book-service.sensitive-headers=Set-Cookie,Authorization
+hystrix.command.book-service.execution.isolation.thread.timeoutInMilliseconds=600000
+
+zuul.routes.rating-service.path=/rating-service/**
+zuul.routes.rating-service.sensitive-headers=Set-Cookie,Authorization
+hystrix.command.rating-service.execution.isolation.thread.timeoutInMilliseconds=600000
zuul.routes.discovery.path=/discovery/**
zuul.routes.discovery.sensitive-headers=Set-Cookie,Authorization
diff --git a/spring-cloud/spring-cloud-bootstrap/application-config/rating-service.properties b/spring-cloud/spring-cloud-bootstrap/application-config/rating-service.properties
new file mode 100644
index 0000000000..4817d12c83
--- /dev/null
+++ b/spring-cloud/spring-cloud-bootstrap/application-config/rating-service.properties
@@ -0,0 +1,17 @@
+spring.application.name=rating-service
+server.port=8084
+
+resource.returnString=hello cloud
+resource.user.returnString=hello cloud user
+resource.admin.returnString=hello cloud admin
+
+eureka.client.region = default
+eureka.client.registryFetchIntervalSeconds = 5
+
+management.security.sessions=never
+
+logging.level.org.springframework.web.=debug
+logging.level.org.springframework.security=debug
+
+spring.redis.host=localhost
+spring.redis.port=6379
diff --git a/spring-cloud/spring-cloud-bootstrap/config/pom.xml b/spring-cloud/spring-cloud-bootstrap/config/pom.xml
index 24d054a87b..2c8ad65102 100644
--- a/spring-cloud/spring-cloud-bootstrap/config/pom.xml
+++ b/spring-cloud/spring-cloud-bootstrap/config/pom.xml
@@ -10,7 +10,7 @@
org.springframework.boot
spring-boot-starter-parent
- 1.4.2.RELEASE
+ 1.4.4.RELEASE
diff --git a/spring-cloud/spring-cloud-bootstrap/discovery/pom.xml b/spring-cloud/spring-cloud-bootstrap/discovery/pom.xml
index be3bfbb0be..40a186350c 100644
--- a/spring-cloud/spring-cloud-bootstrap/discovery/pom.xml
+++ b/spring-cloud/spring-cloud-bootstrap/discovery/pom.xml
@@ -10,7 +10,7 @@
org.springframework.boot
spring-boot-starter-parent
- 1.4.2.RELEASE
+ 1.4.4.RELEASE
diff --git a/spring-cloud/spring-cloud-bootstrap/gateway/pom.xml b/spring-cloud/spring-cloud-bootstrap/gateway/pom.xml
index 9186f12226..044730ba22 100644
--- a/spring-cloud/spring-cloud-bootstrap/gateway/pom.xml
+++ b/spring-cloud/spring-cloud-bootstrap/gateway/pom.xml
@@ -10,7 +10,7 @@
org.springframework.boot
spring-boot-starter-parent
- 1.4.2.RELEASE
+ 1.4.4.RELEASE
diff --git a/spring-cloud/spring-cloud-bootstrap/gateway/src/main/java/com/baeldung/spring/cloud/bootstrap/gateway/SecurityConfig.java b/spring-cloud/spring-cloud-bootstrap/gateway/src/main/java/com/baeldung/spring/cloud/bootstrap/gateway/SecurityConfig.java
index 60dccf9042..9e5c424403 100644
--- a/spring-cloud/spring-cloud-bootstrap/gateway/src/main/java/com/baeldung/spring/cloud/bootstrap/gateway/SecurityConfig.java
+++ b/spring-cloud/spring-cloud-bootstrap/gateway/src/main/java/com/baeldung/spring/cloud/bootstrap/gateway/SecurityConfig.java
@@ -22,14 +22,14 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
- .antMatchers("/resource/hello/cloud").permitAll()
+ .antMatchers("/book-service/books").permitAll()
.antMatchers("/eureka/**").hasRole("ADMIN")
.anyRequest().authenticated()
.and()
.formLogin()
.and()
.logout().permitAll()
- .logoutSuccessUrl("/resource/hello/cloud").permitAll()
+ .logoutSuccessUrl("/book-service/books").permitAll()
.and()
.csrf()
.disable();
diff --git a/spring-cloud/spring-cloud-bootstrap/gateway/src/test/java/com/baeldung/spring/cloud/bootstrap/gateway/GatewayApplicationLiveTest.java b/spring-cloud/spring-cloud-bootstrap/gateway/src/test/java/com/baeldung/spring/cloud/bootstrap/gateway/GatewayApplicationLiveTest.java
deleted file mode 100644
index cea431d461..0000000000
--- a/spring-cloud/spring-cloud-bootstrap/gateway/src/test/java/com/baeldung/spring/cloud/bootstrap/gateway/GatewayApplicationLiveTest.java
+++ /dev/null
@@ -1,69 +0,0 @@
-package com.baeldung.spring.cloud.bootstrap.gateway;
-
-import org.junit.Assert;
-import org.junit.Test;
-import org.springframework.boot.test.web.client.TestRestTemplate;
-import org.springframework.http.*;
-import org.springframework.util.LinkedMultiValueMap;
-import org.springframework.util.MultiValueMap;
-
-public class GatewayApplicationLiveTest {
-
- @Test
- public void testAccess() throws Exception {
- TestRestTemplate testRestTemplate = new TestRestTemplate();
- String testUrl = "http://localhost:8080";
-
- ResponseEntity response = testRestTemplate.getForEntity(testUrl + "/resource/hello/cloud", String.class);
- Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
- Assert.assertEquals("hello cloud", response.getBody());
-
- //try the protected resource and confirm the redirect to login
- response = testRestTemplate.getForEntity(testUrl + "/resource/hello/user", String.class);
- Assert.assertEquals(HttpStatus.FOUND, response.getStatusCode());
- Assert.assertEquals("http://localhost:8080/login", response.getHeaders().get("Location").get(0));
-
- //login as user/password
- MultiValueMap form = new LinkedMultiValueMap<>();
- form.add("username", "user");
- form.add("password", "password");
- response = testRestTemplate.postForEntity(testUrl + "/login", form, String.class);
-
- //extract the session from the cookie and propagate it to the next request
- String sessionCookie = response.getHeaders().get("Set-Cookie").get(0).split(";")[0];
- HttpHeaders headers = new HttpHeaders();
- headers.add("Cookie", sessionCookie);
- HttpEntity httpEntity = new HttpEntity<>(headers);
-
- //request the protected resource
- response = testRestTemplate.exchange(testUrl + "/resource/hello/user", HttpMethod.GET, httpEntity, String.class);
- Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
- Assert.assertEquals("hello cloud user", response.getBody());
-
- //request the admin protected resource to determine it is still protected
- response = testRestTemplate.exchange(testUrl + "/resource/hello/admin", HttpMethod.GET, httpEntity, String.class);
- Assert.assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode());
-
- //login as the admin
- form.clear();
- form.add("username", "admin");
- form.add("password", "admin");
- response = testRestTemplate.postForEntity(testUrl + "/login", form, String.class);
-
- //extract the session from the cookie and propagate it to the next request
- sessionCookie = response.getHeaders().get("Set-Cookie").get(0).split(";")[0];
- headers = new HttpHeaders();
- headers.add("Cookie", sessionCookie);
- httpEntity = new HttpEntity<>(headers);
-
- //request the protected resource
- response = testRestTemplate.exchange(testUrl + "/resource/hello/admin", HttpMethod.GET, httpEntity, String.class);
- Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
- Assert.assertEquals("hello cloud admin", response.getBody());
-
- //request the discovery resources as the admin
- response = testRestTemplate.exchange(testUrl + "/discovery", HttpMethod.GET, httpEntity, String.class);
- Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
- }
-
-}
\ No newline at end of file
diff --git a/spring-cloud/spring-cloud-bootstrap/gateway/src/test/java/com/baeldung/spring/cloud/bootstrap/gateway/IntegrationLiveTest.java b/spring-cloud/spring-cloud-bootstrap/gateway/src/test/java/com/baeldung/spring/cloud/bootstrap/gateway/IntegrationLiveTest.java
new file mode 100644
index 0000000000..47a4b744eb
--- /dev/null
+++ b/spring-cloud/spring-cloud-bootstrap/gateway/src/test/java/com/baeldung/spring/cloud/bootstrap/gateway/IntegrationLiveTest.java
@@ -0,0 +1,201 @@
+package com.baeldung.spring.cloud.bootstrap.gateway;
+
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
+import org.apache.http.entity.ContentType;
+import org.junit.Assert;
+import org.junit.Test;
+import org.springframework.boot.test.web.client.TestRestTemplate;
+import org.springframework.http.*;
+import org.springframework.util.LinkedMultiValueMap;
+import org.springframework.util.MultiValueMap;
+
+public class IntegrationLiveTest {
+
+ private TestRestTemplate testRestTemplate = new TestRestTemplate();
+ private String testUrl = "http://localhost:8080";
+
+ @Test
+ public void testAccess() throws Exception {
+ ResponseEntity response = testRestTemplate.getForEntity(testUrl + "/book-service/books", String.class);
+ Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
+ Assert.assertNotNull(response.getBody());
+
+ //try the protected resource and confirm the redirect to login
+ response = testRestTemplate.getForEntity(testUrl + "/book-service/books/1", String.class);
+ Assert.assertEquals(HttpStatus.FOUND, response.getStatusCode());
+ Assert.assertEquals("http://localhost:8080/login", response.getHeaders().get("Location").get(0));
+
+ //login as user/password
+ MultiValueMap form = new LinkedMultiValueMap<>();
+ form.add("username", "user");
+ form.add("password", "password");
+ response = testRestTemplate.postForEntity(testUrl + "/login", form, String.class);
+
+ //extract the session from the cookie and propagate it to the next request
+ String sessionCookie = response.getHeaders().get("Set-Cookie").get(0).split(";")[0];
+ HttpHeaders headers = new HttpHeaders();
+ headers.add("Cookie", sessionCookie);
+ HttpEntity httpEntity = new HttpEntity<>(headers);
+
+ addBook();
+
+ //request the protected resource
+ response = testRestTemplate.exchange(testUrl + "/book-service/books/1", HttpMethod.GET, httpEntity, String.class);
+ Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
+ Assert.assertNotNull(response.getBody());
+
+ addRatings();
+
+ //request the admin protected resource to determine it is still protected
+ response = testRestTemplate.exchange(testUrl + "/rating-service/ratings", HttpMethod.GET, httpEntity, String.class);
+ Assert.assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode());
+
+ //login as the admin
+ form.clear();
+ form.add("username", "admin");
+ form.add("password", "admin");
+ response = testRestTemplate.postForEntity(testUrl + "/login", form, String.class);
+
+ //extract the session from the cookie and propagate it to the next request
+ sessionCookie = response.getHeaders().get("Set-Cookie").get(0).split(";")[0];
+ headers = new HttpHeaders();
+ headers.add("Cookie", sessionCookie);
+ httpEntity = new HttpEntity<>(headers);
+
+ //request the protected resource
+ response = testRestTemplate.exchange(testUrl + "/rating-service/ratings", HttpMethod.GET, httpEntity, String.class);
+ Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
+ Assert.assertNotNull(response.getBody());
+
+ //request the discovery resources as the admin
+ response = testRestTemplate.exchange(testUrl + "/discovery", HttpMethod.GET, httpEntity, String.class);
+ Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
+ }
+
+ private void addRatings() {
+ //login as user/password
+ MultiValueMap form = new LinkedMultiValueMap<>();
+ form.add("username", "user");
+ form.add("password", "password");
+ ResponseEntity response = testRestTemplate.postForEntity(testUrl + "/login", form, String.class);
+
+ //extract the session from the cookie and propagate it to the next request
+ String sessionCookie = response.getHeaders().get("Set-Cookie").get(0).split(";")[0];
+ HttpHeaders headers = new HttpHeaders();
+ headers.add("Cookie", sessionCookie);
+ headers.add("ContentType", ContentType.APPLICATION_JSON.getMimeType());
+ Rating rating = new Rating(1L, 4);
+
+ HttpEntity httpEntity = new HttpEntity<>(rating, headers);
+
+ //request the protected resource
+ ResponseEntity bookResponse = testRestTemplate.postForEntity(testUrl + "/rating-service/ratings", httpEntity, Rating.class);
+ Assert.assertEquals(HttpStatus.OK, bookResponse.getStatusCode());
+ Assert.assertEquals(rating.getBookId(), bookResponse.getBody().getBookId());
+ Assert.assertEquals(rating.getStars(), bookResponse.getBody().getStars());
+ }
+
+ private void addBook(){
+ //login as user/password
+ MultiValueMap form = new LinkedMultiValueMap<>();
+ form.add("username", "admin");
+ form.add("password", "admin");
+ ResponseEntity response = testRestTemplate.postForEntity(testUrl + "/login", form, String.class);
+
+ //extract the session from the cookie and propagate it to the next request
+ String sessionCookie = response.getHeaders().get("Set-Cookie").get(0).split(";")[0];
+ HttpHeaders headers = new HttpHeaders();
+ headers.add("Cookie", sessionCookie);
+ headers.add("ContentType", ContentType.APPLICATION_JSON.getMimeType());
+ Book book = new Book("Baeldung", "How to spring cloud");
+
+ HttpEntity httpEntity = new HttpEntity<>(book, headers);
+
+ //request the protected resource
+ ResponseEntity bookResponse = testRestTemplate.postForEntity(testUrl + "/book-service/books", httpEntity, Book.class);
+ Assert.assertEquals(HttpStatus.OK, bookResponse.getStatusCode());
+ Assert.assertEquals(book.getAuthor(), bookResponse.getBody().getAuthor());
+ Assert.assertEquals(book.getTitle(), bookResponse.getBody().getTitle());
+ }
+
+ @JsonIgnoreProperties(ignoreUnknown = true)
+ public static class Book {
+
+ private Long id;
+ private String author;
+ private String title;
+
+ public Book() {
+ }
+
+ public Book(String author, String title) {
+ this.author = author;
+ this.title = title;
+ }
+
+ public String getAuthor() {
+ return author;
+ }
+
+ public void setAuthor(String author) {
+ this.author = author;
+ }
+
+ public String getTitle() {
+ return title;
+ }
+
+ public void setTitle(String title) {
+ this.title = title;
+ }
+
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+ }
+
+ @JsonIgnoreProperties(ignoreUnknown = true)
+ public static class Rating {
+ private Long id;
+ private Long bookId;
+ private int stars;
+
+ public Rating() {
+ }
+
+ public Rating(Long bookId, int stars) {
+ this.bookId = bookId;
+ this.stars = stars;
+ }
+
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ public Long getBookId() {
+ return bookId;
+ }
+
+ public void setBookId(Long bookId) {
+ this.bookId = bookId;
+ }
+
+ public int getStars() {
+ return stars;
+ }
+
+ public void setStars(int stars) {
+ this.stars = stars;
+ }
+ }
+
+
+}
\ No newline at end of file
diff --git a/spring-cloud/spring-cloud-bootstrap/pom.xml b/spring-cloud/spring-cloud-bootstrap/pom.xml
index 9a1b2e6d0e..ccfbdb9735 100644
--- a/spring-cloud/spring-cloud-bootstrap/pom.xml
+++ b/spring-cloud/spring-cloud-bootstrap/pom.xml
@@ -14,7 +14,8 @@
config
discovery
gateway
- resource
+ svc-book
+ svc-rating
diff --git a/spring-cloud/spring-cloud-bootstrap/resource/src/main/java/com/baeldung/spring/cloud/bootstrap/resource/ResourceApplication.java b/spring-cloud/spring-cloud-bootstrap/resource/src/main/java/com/baeldung/spring/cloud/bootstrap/resource/ResourceApplication.java
deleted file mode 100644
index accef18a14..0000000000
--- a/spring-cloud/spring-cloud-bootstrap/resource/src/main/java/com/baeldung/spring/cloud/bootstrap/resource/ResourceApplication.java
+++ /dev/null
@@ -1,41 +0,0 @@
-package com.baeldung.spring.cloud.bootstrap.resource;
-
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.boot.SpringApplication;
-import org.springframework.boot.autoconfigure.SpringBootApplication;
-import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
-
-@SpringBootApplication
-@EnableEurekaClient
-@RestController
-public class ResourceApplication {
- public static void main(String[] args) {
- SpringApplication.run(ResourceApplication.class, args);
- }
-
- @Value("${resource.returnString}")
- private String returnString;
-
- @Value("${resource.user.returnString}")
- private String userReturnString;
-
- @Value("${resource.admin.returnString}")
- private String adminReturnString;
-
- @RequestMapping("/hello/cloud")
- public String getString() {
- return returnString;
- }
-
- @RequestMapping("/hello/user")
- public String getUserString() {
- return userReturnString;
- }
-
- @RequestMapping("/hello/admin")
- public String getAdminString() {
- return adminReturnString;
- }
-}
diff --git a/spring-cloud/spring-cloud-bootstrap/resource/pom.xml b/spring-cloud/spring-cloud-bootstrap/svc-book/pom.xml
similarity index 86%
rename from spring-cloud/spring-cloud-bootstrap/resource/pom.xml
rename to spring-cloud/spring-cloud-bootstrap/svc-book/pom.xml
index 1472693de8..c351c444f6 100644
--- a/spring-cloud/spring-cloud-bootstrap/resource/pom.xml
+++ b/spring-cloud/spring-cloud-bootstrap/svc-book/pom.xml
@@ -4,13 +4,14 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
- resource
+ com.baeldung.spring.cloud
+ svc-book
1.0.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
- 1.4.2.RELEASE
+ 1.4.4.RELEASE
@@ -41,6 +42,17 @@
spring-boot-starter-data-redis
+
+ org.springframework.boot
+ spring-boot-starter-data-jpa
+
+
+
+ com.h2database
+ h2
+ runtime
+
+
org.springframework.boot
spring-boot-starter-test
diff --git a/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/BookServiceApplication.java b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/BookServiceApplication.java
new file mode 100644
index 0000000000..c5499cd924
--- /dev/null
+++ b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/BookServiceApplication.java
@@ -0,0 +1,13 @@
+package com.baeldung.spring.cloud.bootstrap.svcbook;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
+
+@SpringBootApplication
+@EnableEurekaClient
+public class BookServiceApplication {
+ public static void main(String[] args) {
+ SpringApplication.run(BookServiceApplication.class, args);
+ }
+}
diff --git a/spring-cloud/spring-cloud-bootstrap/resource/src/main/java/com/baeldung/spring/cloud/bootstrap/resource/SecurityConfig.java b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/SecurityConfig.java
similarity index 72%
rename from spring-cloud/spring-cloud-bootstrap/resource/src/main/java/com/baeldung/spring/cloud/bootstrap/resource/SecurityConfig.java
rename to spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/SecurityConfig.java
index 0b0de6ec20..6aa996c575 100644
--- a/spring-cloud/spring-cloud-bootstrap/resource/src/main/java/com/baeldung/spring/cloud/bootstrap/resource/SecurityConfig.java
+++ b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/SecurityConfig.java
@@ -1,7 +1,8 @@
-package com.baeldung.spring.cloud.bootstrap.resource;
+package com.baeldung.spring.cloud.bootstrap.svcbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
+import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
@@ -22,9 +23,11 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
http.httpBasic()
.disable()
.authorizeRequests()
- .antMatchers("/hello/cloud").permitAll()
- .antMatchers("/hello/user").hasAnyRole("USER", "ADMIN")
- .antMatchers("/hello/admin").hasRole("ADMIN")
+ .antMatchers(HttpMethod.GET, "/books").permitAll()
+ .antMatchers(HttpMethod.GET, "/books/*").permitAll()
+ .antMatchers(HttpMethod.POST, "/books").hasRole("ADMIN")
+ .antMatchers(HttpMethod.PATCH, "/books/*").hasRole("ADMIN")
+ .antMatchers(HttpMethod.DELETE, "/books/*").hasRole("ADMIN")
.anyRequest().authenticated()
.and()
.csrf()
diff --git a/spring-cloud/spring-cloud-bootstrap/resource/src/main/java/com/baeldung/spring/cloud/bootstrap/resource/SessionConfig.java b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/SessionConfig.java
similarity index 87%
rename from spring-cloud/spring-cloud-bootstrap/resource/src/main/java/com/baeldung/spring/cloud/bootstrap/resource/SessionConfig.java
rename to spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/SessionConfig.java
index f11f316826..dbde068069 100644
--- a/spring-cloud/spring-cloud-bootstrap/resource/src/main/java/com/baeldung/spring/cloud/bootstrap/resource/SessionConfig.java
+++ b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/SessionConfig.java
@@ -1,4 +1,4 @@
-package com.baeldung.spring.cloud.bootstrap.resource;
+package com.baeldung.spring.cloud.bootstrap.svcbook;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
diff --git a/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/book/Book.java b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/book/Book.java
new file mode 100644
index 0000000000..33ea8dcb81
--- /dev/null
+++ b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/book/Book.java
@@ -0,0 +1,45 @@
+package com.baeldung.spring.cloud.bootstrap.svcbook.book;
+
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+
+@Entity
+@JsonIgnoreProperties(ignoreUnknown = true)
+public class Book {
+ @Id
+ @GeneratedValue(strategy = GenerationType.AUTO)
+ private Long id;
+ private String author;
+ private String title;
+
+ public Book() {
+ }
+
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ public String getAuthor() {
+ return author;
+ }
+
+ public void setAuthor(String author) {
+ this.author = author;
+ }
+
+ public String getTitle() {
+ return title;
+ }
+
+ public void setTitle(String title) {
+ this.title = title;
+ }
+}
diff --git a/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/book/BookController.java b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/book/BookController.java
new file mode 100644
index 0000000000..d00f114b8c
--- /dev/null
+++ b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/book/BookController.java
@@ -0,0 +1,40 @@
+package com.baeldung.spring.cloud.bootstrap.svcbook.book;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.List;
+import java.util.Map;
+
+@RestController
+@RequestMapping("/books")
+public class BookController {
+
+ @Autowired
+ private BookService bookService;
+
+ @GetMapping("")
+ public List findAllBooks() {
+ return bookService.findAllBooks();
+ }
+
+ @GetMapping("/{bookId}")
+ public Book findBook(@PathVariable Long bookId) {
+ return bookService.findBookById(bookId);
+ }
+
+ @PostMapping("")
+ public Book createBook(@RequestBody Book book) {
+ return bookService.createBook(book);
+ }
+
+ @DeleteMapping("/{bookId}")
+ public void deleteBook(@PathVariable Long bookId) {
+ bookService.deleteBook(bookId);
+ }
+
+ @PatchMapping("/{bookId")
+ public Book updateBook(@RequestBody Map updates, @PathVariable Long bookId) {
+ return bookService.updateBook(updates, bookId);
+ }
+}
diff --git a/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/book/BookNotFoundException.java b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/book/BookNotFoundException.java
new file mode 100644
index 0000000000..f0a4797387
--- /dev/null
+++ b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/book/BookNotFoundException.java
@@ -0,0 +1,11 @@
+package com.baeldung.spring.cloud.bootstrap.svcbook.book;
+
+import org.springframework.http.HttpStatus;
+import org.springframework.web.bind.annotation.ResponseStatus;
+
+@ResponseStatus(HttpStatus.NOT_FOUND)
+class BookNotFoundException extends RuntimeException {
+ BookNotFoundException(String message) {
+ super(message);
+ }
+}
diff --git a/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/book/BookRepository.java b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/book/BookRepository.java
new file mode 100644
index 0000000000..66fd3880c5
--- /dev/null
+++ b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/book/BookRepository.java
@@ -0,0 +1,6 @@
+package com.baeldung.spring.cloud.bootstrap.svcbook.book;
+
+import org.springframework.data.jpa.repository.JpaRepository;
+
+interface BookRepository extends JpaRepository{
+}
diff --git a/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/book/BookService.java b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/book/BookService.java
new file mode 100644
index 0000000000..cfcbf15757
--- /dev/null
+++ b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/book/BookService.java
@@ -0,0 +1,55 @@
+package com.baeldung.spring.cloud.bootstrap.svcbook.book;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Propagation;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+
+@Service
+@Transactional(readOnly = true)
+public class BookService {
+
+ @Autowired
+ private BookRepository bookRepository;
+
+ public List findAllBooks() {
+ return bookRepository.findAll();
+ }
+
+ public Book findBookById(Long bookId) {
+ return Optional.ofNullable(bookRepository.findOne(bookId))
+ .orElseThrow(() -> new BookNotFoundException("Book not found. ID: " + bookId));
+ }
+
+ @Transactional(propagation = Propagation.REQUIRED)
+ public Book createBook(Book book) {
+ Book newBook = new Book();
+ newBook.setTitle(book.getTitle());
+ newBook.setAuthor(book.getAuthor());
+ return bookRepository.save(newBook);
+ }
+
+ @Transactional(propagation = Propagation.REQUIRED)
+ public void deleteBook(Long bookId) {
+ bookRepository.delete(bookId);
+ }
+
+ @Transactional(propagation = Propagation.REQUIRED)
+ public Book updateBook(Map updates, Long bookId) {
+ Book book = findBookById(bookId);
+ updates.keySet().forEach(key -> {
+ switch (key) {
+ case "author":
+ book.setAuthor(updates.get(key));
+ break;
+ case "title":
+ book.setTitle(updates.get(key));
+ }
+ });
+ return bookRepository.save(book);
+ }
+}
diff --git a/spring-cloud/spring-cloud-bootstrap/resource/src/main/resources/bootstrap.properties b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/resources/bootstrap.properties
similarity index 87%
rename from spring-cloud/spring-cloud-bootstrap/resource/src/main/resources/bootstrap.properties
rename to spring-cloud/spring-cloud-bootstrap/svc-book/src/main/resources/bootstrap.properties
index 9ecfb46125..8f3a3261ac 100644
--- a/spring-cloud/spring-cloud-bootstrap/resource/src/main/resources/bootstrap.properties
+++ b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/resources/bootstrap.properties
@@ -1,4 +1,4 @@
-spring.cloud.config.name=resource
+spring.cloud.config.name=book-service
spring.cloud.config.discovery.service-id=config
spring.cloud.config.discovery.enabled=true
spring.cloud.config.username=configUser
diff --git a/spring-cloud/spring-cloud-bootstrap/svc-rating/pom.xml b/spring-cloud/spring-cloud-bootstrap/svc-rating/pom.xml
new file mode 100644
index 0000000000..2285286812
--- /dev/null
+++ b/spring-cloud/spring-cloud-bootstrap/svc-rating/pom.xml
@@ -0,0 +1,96 @@
+
+
+ 4.0.0
+
+ com.baeldung.spring.cloud
+ svc-rating
+ 1.0.0-SNAPSHOT
+
+
+ org.springframework.boot
+ spring-boot-starter-parent
+ 1.4.4.RELEASE
+
+
+
+
+
+ org.springframework.cloud
+ spring-cloud-starter-config
+
+
+ org.springframework.cloud
+ spring-cloud-starter-eureka
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+ org.springframework.boot
+ spring-boot-starter-security
+
+
+
+ org.springframework.session
+ spring-session
+
+
+ org.springframework.boot
+ spring-boot-starter-data-redis
+
+
+
+ org.springframework.boot
+ spring-boot-starter-data-jpa
+
+
+
+ com.h2database
+ h2
+ runtime
+
+
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
+
+
+
+
+
+ org.springframework.cloud
+ spring-cloud-dependencies
+ ${spring-cloud-dependencies.version}
+ pom
+ import
+
+
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-maven-plugin
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+ ${maven-compiler-plugin.version}
+
+ 1.8
+ 1.8
+
+
+
+
+
+ Brixton.SR7
+ 3.6.0
+
+
\ No newline at end of file
diff --git a/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/RatingServiceApplication.java b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/RatingServiceApplication.java
new file mode 100644
index 0000000000..61074e0bcc
--- /dev/null
+++ b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/RatingServiceApplication.java
@@ -0,0 +1,13 @@
+package com.baeldung.spring.cloud.bootstrap.svcrating;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
+
+@SpringBootApplication
+@EnableEurekaClient
+public class RatingServiceApplication {
+ public static void main(String[] args) {
+ SpringApplication.run(RatingServiceApplication.class, args);
+ }
+}
\ No newline at end of file
diff --git a/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/SecurityConfig.java b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/SecurityConfig.java
new file mode 100644
index 0000000000..171fbba7af
--- /dev/null
+++ b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/SecurityConfig.java
@@ -0,0 +1,36 @@
+package com.baeldung.spring.cloud.bootstrap.svcrating;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.http.HttpMethod;
+import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
+import org.springframework.security.config.annotation.web.builders.HttpSecurity;
+import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
+import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
+
+@EnableWebSecurity
+@Configuration
+public class SecurityConfig extends WebSecurityConfigurerAdapter {
+
+ @Autowired
+ public void configureGlobal1(AuthenticationManagerBuilder auth) throws Exception {
+ //try in memory auth with no users to support the case that this will allow for users that are logged in to go anywhere
+ auth.inMemoryAuthentication();
+ }
+
+ @Override
+ protected void configure(HttpSecurity http) throws Exception {
+ http.httpBasic()
+ .disable()
+ .authorizeRequests()
+ .regexMatchers("^/ratings\\?bookId.*$").authenticated()
+ .antMatchers(HttpMethod.POST,"/ratings").authenticated()
+ .antMatchers(HttpMethod.PATCH,"/ratings/*").hasRole("ADMIN")
+ .antMatchers(HttpMethod.DELETE,"/ratings/*").hasRole("ADMIN")
+ .antMatchers(HttpMethod.GET,"/ratings").hasRole("ADMIN")
+ .anyRequest().authenticated()
+ .and()
+ .csrf()
+ .disable();
+ }
+}
diff --git a/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/SessionConfig.java b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/SessionConfig.java
new file mode 100644
index 0000000000..62bc701868
--- /dev/null
+++ b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/SessionConfig.java
@@ -0,0 +1,10 @@
+package com.baeldung.spring.cloud.bootstrap.svcrating;
+
+import org.springframework.context.annotation.Configuration;
+import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
+import org.springframework.session.web.context.AbstractHttpSessionApplicationInitializer;
+
+@Configuration
+@EnableRedisHttpSession
+public class SessionConfig extends AbstractHttpSessionApplicationInitializer {
+}
diff --git a/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/rating/Rating.java b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/rating/Rating.java
new file mode 100644
index 0000000000..ae44f9ae2e
--- /dev/null
+++ b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/rating/Rating.java
@@ -0,0 +1,52 @@
+package com.baeldung.spring.cloud.bootstrap.svcrating.rating;
+
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+
+@Entity
+@JsonIgnoreProperties(ignoreUnknown = true)
+public class Rating {
+
+ @Id
+ @GeneratedValue(strategy = GenerationType.AUTO)
+ private Long id;
+ private Long bookId;
+ private int stars;
+
+ public Rating() {
+ }
+
+ public Rating(Long id, Long bookId, int stars) {
+ this.id = id;
+ this.bookId = bookId;
+ this.stars = stars;
+ }
+
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ public Long getBookId() {
+ return bookId;
+ }
+
+ public void setBookId(Long bookId) {
+ this.bookId = bookId;
+ }
+
+ public int getStars() {
+ return stars;
+ }
+
+ public void setStars(int stars) {
+ this.stars = stars;
+ }
+}
diff --git a/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/rating/RatingController.java b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/rating/RatingController.java
new file mode 100644
index 0000000000..83452ad747
--- /dev/null
+++ b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/rating/RatingController.java
@@ -0,0 +1,38 @@
+package com.baeldung.spring.cloud.bootstrap.svcrating.rating;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.List;
+import java.util.Map;
+
+@RestController
+@RequestMapping("/ratings")
+public class RatingController {
+
+ @Autowired
+ private RatingService ratingService;
+
+ @GetMapping("")
+ public List findRatingsByBookId(@RequestParam(required = false, defaultValue = "0") Long bookId) {
+ if (bookId.equals(0L)) {
+ return ratingService.findAllRatings();
+ }
+ return ratingService.findRatingsByBookId(bookId);
+ }
+
+ @PostMapping("")
+ public Rating createRating(@RequestBody Rating rating) {
+ return ratingService.createRating(rating);
+ }
+
+ @DeleteMapping("/{ratingId}")
+ public void deleteRating(@PathVariable Long ratingId) {
+ ratingService.deleteRating(ratingId);
+ }
+
+ @PatchMapping("/{ratingId")
+ public Rating updateRating(@RequestBody Map updates, @PathVariable Long ratingId) {
+ return ratingService.updateRating(updates, ratingId);
+ }
+}
diff --git a/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/rating/RatingNotFoundException.java b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/rating/RatingNotFoundException.java
new file mode 100644
index 0000000000..473d636a71
--- /dev/null
+++ b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/rating/RatingNotFoundException.java
@@ -0,0 +1,11 @@
+package com.baeldung.spring.cloud.bootstrap.svcrating.rating;
+
+import org.springframework.http.HttpStatus;
+import org.springframework.web.bind.annotation.ResponseStatus;
+
+@ResponseStatus(HttpStatus.NOT_FOUND)
+class RatingNotFoundException extends RuntimeException {
+ RatingNotFoundException(String message) {
+ super(message);
+ }
+}
diff --git a/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/rating/RatingRepository.java b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/rating/RatingRepository.java
new file mode 100644
index 0000000000..08d781b5a3
--- /dev/null
+++ b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/rating/RatingRepository.java
@@ -0,0 +1,9 @@
+package com.baeldung.spring.cloud.bootstrap.svcrating.rating;
+
+import org.springframework.data.jpa.repository.JpaRepository;
+
+import java.util.List;
+
+interface RatingRepository extends JpaRepository{
+ List findRatingsByBookId(Long bookId);
+}
diff --git a/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/rating/RatingService.java b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/rating/RatingService.java
new file mode 100644
index 0000000000..a2360b7be5
--- /dev/null
+++ b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/rating/RatingService.java
@@ -0,0 +1,57 @@
+package com.baeldung.spring.cloud.bootstrap.svcrating.rating;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Propagation;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+
+@Service
+@Transactional(readOnly = true)
+public class RatingService {
+
+ @Autowired
+ private RatingRepository ratingRepository;
+
+ public Rating findRatingById(Long ratingId) {
+ return Optional.ofNullable(ratingRepository.findOne(ratingId))
+ .orElseThrow(() -> new RatingNotFoundException("Rating not found. ID: " + ratingId));
+ }
+
+ public List findRatingsByBookId(Long bookId) {
+ return ratingRepository.findRatingsByBookId(bookId);
+ }
+
+ public List findAllRatings() {
+ return ratingRepository.findAll();
+ }
+
+ @Transactional(propagation = Propagation.REQUIRED)
+ public Rating createRating(Rating rating) {
+ Rating newRating = new Rating();
+ newRating.setBookId(rating.getBookId());
+ newRating.setStars(rating.getStars());
+ return ratingRepository.save(newRating);
+ }
+
+ @Transactional(propagation = Propagation.REQUIRED)
+ public void deleteRating(Long ratingId) {
+ ratingRepository.delete(ratingId);
+ }
+
+ @Transactional(propagation = Propagation.REQUIRED)
+ public Rating updateRating(Map updates, Long ratingId) {
+ Rating rating = findRatingById(ratingId);
+ updates.keySet().forEach(key -> {
+ switch (key) {
+ case "stars":
+ rating.setStars(Integer.parseInt(updates.get(key)));
+ break;
+ }
+ });
+ return ratingRepository.save(rating);
+ }
+}
diff --git a/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/resources/bootstrap.properties b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/resources/bootstrap.properties
new file mode 100644
index 0000000000..be5cf7f1e1
--- /dev/null
+++ b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/resources/bootstrap.properties
@@ -0,0 +1,7 @@
+spring.cloud.config.name=rating-service
+spring.cloud.config.discovery.service-id=config
+spring.cloud.config.discovery.enabled=true
+spring.cloud.config.username=configUser
+spring.cloud.config.password=configPassword
+
+eureka.client.serviceUrl.defaultZone=http://discUser:discPassword@localhost:8082/eureka/
diff --git a/spring-cloud/spring-cloud-config/pom.xml b/spring-cloud/spring-cloud-config/pom.xml
index d25037a49f..56ff377956 100644
--- a/spring-cloud/spring-cloud-config/pom.xml
+++ b/spring-cloud/spring-cloud-config/pom.xml
@@ -16,7 +16,7 @@
org.springframework.boot
spring-boot-starter-parent
- 1.4.2.RELEASE
+ 1.4.4.RELEASE
diff --git a/spring-cloud/spring-cloud-ribbon-client/pom.xml b/spring-cloud/spring-cloud-ribbon-client/pom.xml
index 8aee41f5c4..d70de51a58 100644
--- a/spring-cloud/spring-cloud-ribbon-client/pom.xml
+++ b/spring-cloud/spring-cloud-ribbon-client/pom.xml
@@ -11,7 +11,7 @@
org.springframework.boot
spring-boot-starter-parent
- 1.4.2.RELEASE
+ 1.4.4.RELEASE
diff --git a/spring-core/README.md b/spring-core/README.md
index f6aaaf44a0..a32d30939f 100644
--- a/spring-core/README.md
+++ b/spring-core/README.md
@@ -3,4 +3,4 @@
- [Exploring the Spring BeanFactory API](http://www.baeldung.com/spring-beanfactory)
- [How to use the Spring FactoryBean?](http://www.baeldung.com/spring-factorybean)
- [Constructor Dependency Injection in Spring](http://www.baeldung.com/constructor-injection-in-spring)
-- [Constructor Injection in Spring with Lombok](http://inprogress.baeldung.com/constructor-injection-in-spring-with-lombok)
+- [Constructor Injection in Spring with Lombok](http://www.baeldung.com/spring-injection-lombok)
diff --git a/spring-cucumber/pom.xml b/spring-cucumber/pom.xml
index 644ddff1d8..55c0af670a 100644
--- a/spring-cucumber/pom.xml
+++ b/spring-cucumber/pom.xml
@@ -14,7 +14,7 @@
org.springframework.boot
spring-boot-starter-parent
- 1.4.2.RELEASE
+ 1.4.4.RELEASE
diff --git a/spring-data-dynamodb/pom.xml b/spring-data-dynamodb/pom.xml
index 0ce9b3a399..8b06c0b36f 100644
--- a/spring-data-dynamodb/pom.xml
+++ b/spring-data-dynamodb/pom.xml
@@ -11,7 +11,7 @@
spring-boot-starter-parent
org.springframework.boot
- 1.4.2.RELEASE
+ 1.4.4.RELEASE
diff --git a/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/ProductInfoRepositoryIntegrationTest.java b/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/ProductInfoRepositoryIntegrationTest.java
index 8fe3e96940..05badc74b4 100644
--- a/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/ProductInfoRepositoryIntegrationTest.java
+++ b/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/ProductInfoRepositoryIntegrationTest.java
@@ -44,8 +44,8 @@ public class ProductInfoRepositoryIntegrationTest {
private static final String EXPECTED_PRICE = "50";
@Before
- @Ignore //TODO Remove Ignore annotations when running locally with Local DynamoDB instance
- public void setup() throws Exception {
+ @Ignore // TODO Remove Ignore annotations when running locally with Local DynamoDB instance
+ public void setup() throws Exception {
try {
dynamoDBMapper = new DynamoDBMapper(amazonDynamoDB);
@@ -64,7 +64,7 @@ public class ProductInfoRepositoryIntegrationTest {
}
@Test
- @Ignore //TODO Remove Ignore annotations when running locally with Local DynamoDB instance
+ @Ignore // TODO Remove Ignore annotations when running locally with Local DynamoDB instance
public void givenItemWithExpectedCost_whenRunFindAll_thenItemIsFound() {
ProductInfo productInfo = new ProductInfo(EXPECTED_COST, EXPECTED_PRICE);
diff --git a/spring-data-mongodb/src/main/java/org/baeldung/config/SimpleMongoConfig.java b/spring-data-mongodb/src/main/java/org/baeldung/config/SimpleMongoConfig.java
new file mode 100644
index 0000000000..6140382f82
--- /dev/null
+++ b/spring-data-mongodb/src/main/java/org/baeldung/config/SimpleMongoConfig.java
@@ -0,0 +1,25 @@
+package org.baeldung.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.data.mongodb.core.MongoTemplate;
+import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
+
+import com.mongodb.Mongo;
+import com.mongodb.MongoClient;
+
+@Configuration
+@EnableMongoRepositories(basePackages = "org.baeldung.repository")
+public class SimpleMongoConfig {
+
+ @Bean
+ public Mongo mongo() throws Exception {
+ return new MongoClient("localhost");
+ }
+
+ @Bean
+ public MongoTemplate mongoTemplate() throws Exception {
+ return new MongoTemplate(mongo(), "test");
+ }
+
+}
diff --git a/spring-data-mongodb/src/test/java/org/baeldung/mongotemplate/MongoTemplateProjectionLiveTest.java b/spring-data-mongodb/src/test/java/org/baeldung/mongotemplate/MongoTemplateProjectionLiveTest.java
index 04e474f71b..61115faede 100644
--- a/spring-data-mongodb/src/test/java/org/baeldung/mongotemplate/MongoTemplateProjectionLiveTest.java
+++ b/spring-data-mongodb/src/test/java/org/baeldung/mongotemplate/MongoTemplateProjectionLiveTest.java
@@ -1,8 +1,10 @@
package org.baeldung.mongotemplate;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
-import org.baeldung.config.MongoConfig;
+import org.baeldung.config.SimpleMongoConfig;
import org.baeldung.model.User;
import org.junit.After;
import org.junit.Before;
@@ -15,7 +17,7 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
-@ContextConfiguration(classes = MongoConfig.class)
+@ContextConfiguration(classes = SimpleMongoConfig.class)
public class MongoTemplateProjectionLiveTest {
@Autowired
@@ -38,7 +40,7 @@ public class MongoTemplateProjectionLiveTest {
mongoTemplate.insert(new User("John", 30));
mongoTemplate.insert(new User("Ringo", 35));
- Query query = new Query();
+ final Query query = new Query();
query.fields()
.include("name");
@@ -55,7 +57,7 @@ public class MongoTemplateProjectionLiveTest {
mongoTemplate.insert(new User("John", 30));
mongoTemplate.insert(new User("Ringo", 35));
- Query query = new Query();
+ final Query query = new Query();
query.fields()
.exclude("_id");
@@ -64,7 +66,7 @@ public class MongoTemplateProjectionLiveTest {
assertNull(user.getId());
assertNotNull(user.getAge());
});
-
+
}
}
diff --git a/spring-data-neo4j/pom.xml b/spring-data-neo4j/pom.xml
index ce10313b2e..96606d597b 100644
--- a/spring-data-neo4j/pom.xml
+++ b/spring-data-neo4j/pom.xml
@@ -7,10 +7,41 @@
1.0
+
+ org.neo4j
+ neo4j
+ 3.1.0
+
+
+
+ org.neo4j
+ neo4j-ogm-core
+ 2.1.1
+
+
+
+ org.neo4j
+ neo4j-ogm-embedded-driver
+ 2.1.1
+
+
+
+ org.neo4j.driver
+ neo4j-java-driver
+ 1.1.1
+
+
+
+ org.springframework.data
+ spring-data-neo4j
+ 4.2.0.RELEASE
+
+
org.springframework.data
spring-data-neo4j
${spring-data-neo4j.version}
+ test-jar
@@ -27,13 +58,6 @@
test
-
- org.springframework.data
- spring-data-neo4j
- ${spring-data-neo4j.version}
- test-jar
-
-
org.neo4j
neo4j-kernel
@@ -72,9 +96,9 @@
spring-test
${spring-test.version}
-
+
@@ -130,16 +154,18 @@
+ 1.8
+ 1.8
1.8
UTF-8
UTF-8
- 3.0.7
+ 3.1.0
4.1.6.RELEASE
1.1
1.4.3.RELEASE
4.3.5.RELEASE
- 2.0.6
+ 2.1.1
4.12
diff --git a/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/config/MovieDatabaseNeo4jConfiguration.java b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/config/MovieDatabaseNeo4jConfiguration.java
index fb4fda1497..344282d665 100644
--- a/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/config/MovieDatabaseNeo4jConfiguration.java
+++ b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/config/MovieDatabaseNeo4jConfiguration.java
@@ -4,15 +4,12 @@ import org.neo4j.ogm.session.SessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
-import org.springframework.data.neo4j.config.Neo4jConfiguration;
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
-import org.springframework.scheduling.annotation.EnableScheduling;
-import org.springframework.transaction.annotation.EnableTransactionManagement;
@ComponentScan(basePackages = { "com.baeldung.spring.data.neo4j.services" })
@Configuration
@EnableNeo4jRepositories(basePackages = "com.baeldung.spring.data.neo4j.repostory")
-public class MovieDatabaseNeo4jConfiguration extends Neo4jConfiguration {
+public class MovieDatabaseNeo4jConfiguration {
public static final String URL = System.getenv("NEO4J_URL") != null ? System.getenv("NEO4J_URL") : "http://neo4j:movies@localhost:7474";
@@ -23,7 +20,7 @@ public class MovieDatabaseNeo4jConfiguration extends Neo4jConfiguration {
return config;
}
- @Override
+ @Bean
public SessionFactory getSessionFactory() {
return new SessionFactory(getConfiguration(), "com.baeldung.spring.data.neo4j.domain");
}
diff --git a/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/config/MovieDatabaseNeo4jTestConfiguration.java b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/config/MovieDatabaseNeo4jTestConfiguration.java
index 81935b2293..7bb1b78a09 100644
--- a/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/config/MovieDatabaseNeo4jTestConfiguration.java
+++ b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/config/MovieDatabaseNeo4jTestConfiguration.java
@@ -5,9 +5,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
-import org.springframework.data.neo4j.config.Neo4jConfiguration;
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
-import org.springframework.data.neo4j.server.Neo4jServer;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@EnableTransactionManagement
@@ -15,7 +13,7 @@ import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableNeo4jRepositories(basePackages = "com.baeldung.spring.data.neo4j.repostory")
@Profile({ "embedded", "test" })
-public class MovieDatabaseNeo4jTestConfiguration extends Neo4jConfiguration {
+public class MovieDatabaseNeo4jTestConfiguration {
@Bean
public org.neo4j.ogm.config.Configuration getConfiguration() {
@@ -24,7 +22,7 @@ public class MovieDatabaseNeo4jTestConfiguration extends Neo4jConfiguration {
return config;
}
- @Override
+ @Bean
public SessionFactory getSessionFactory() {
return new SessionFactory(getConfiguration(), "com.baeldung.spring.data.neo4j.domain");
}
diff --git a/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/domain/Car.java b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/domain/Car.java
new file mode 100644
index 0000000000..f2325a334f
--- /dev/null
+++ b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/domain/Car.java
@@ -0,0 +1,47 @@
+package com.baeldung.spring.data.neo4j.domain;
+
+import org.neo4j.ogm.annotation.GraphId;
+import org.neo4j.ogm.annotation.NodeEntity;
+import org.neo4j.ogm.annotation.Relationship;
+
+@NodeEntity
+public class Car {
+ @GraphId
+ private Long id;
+
+ private String make;
+
+ @Relationship(direction = "INCOMING")
+ private Company company;
+
+ public Car(String make, String model) {
+ this.make = make;
+ this.model = model;
+ }
+
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ public String getMake() {
+ return make;
+ }
+
+ public void setMake(String make) {
+ this.make = make;
+ }
+
+ public String getModel() {
+ return model;
+ }
+
+ public void setModel(String model) {
+ this.model = model;
+ }
+
+ private String model;
+}
diff --git a/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/domain/Company.java b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/domain/Company.java
new file mode 100644
index 0000000000..4422ade44f
--- /dev/null
+++ b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/domain/Company.java
@@ -0,0 +1,42 @@
+package com.baeldung.spring.data.neo4j.domain;
+
+import org.neo4j.ogm.annotation.NodeEntity;
+import org.neo4j.ogm.annotation.Relationship;
+
+@NodeEntity
+public class Company {
+ private Long id;
+
+ private String name;
+
+ @Relationship(type="owns")
+ private Car car;
+
+ public Company(String name) {
+ this.name = name;
+ }
+
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public Car getCar() {
+ return car;
+ }
+
+ public void setCar(Car car) {
+ this.car = car;
+ }
+}
diff --git a/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/repostory/MovieRepository.java b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/repostory/MovieRepository.java
index 1bd605a7bc..afb82551e7 100644
--- a/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/repostory/MovieRepository.java
+++ b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/repostory/MovieRepository.java
@@ -12,6 +12,7 @@ import java.util.Map;
@Repository
public interface MovieRepository extends GraphRepository {
+
Movie findByTitle(@Param("title") String title);
@Query("MATCH (m:Movie) WHERE m.title =~ ('(?i).*'+{title}+'.*') RETURN m")
diff --git a/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/repostory/PersonRepository.java b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/repostory/PersonRepository.java
index f7f694c07f..4ac40ef75b 100644
--- a/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/repostory/PersonRepository.java
+++ b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/repostory/PersonRepository.java
@@ -6,5 +6,4 @@ import org.springframework.stereotype.Repository;
@Repository
public interface PersonRepository extends GraphRepository {
-
}
diff --git a/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/services/MovieService.java b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/services/MovieService.java
index d760d19066..ae1f6eb8e5 100644
--- a/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/services/MovieService.java
+++ b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/services/MovieService.java
@@ -12,11 +12,11 @@ import java.util.*;
public class MovieService {
@Autowired
- MovieRepository movieRepository;
+ private MovieRepository movieRepository;
private Map toD3Format(Iterator> result) {
- List> nodes = new ArrayList>();
- List> rels = new ArrayList>();
+ List> nodes = new ArrayList<>();
+ List> rels = new ArrayList<>();
int i = 0;
while (result.hasNext()) {
Map row = result.next();
@@ -37,7 +37,7 @@ public class MovieService {
}
private Map map(String key1, Object value1, String key2, Object value2) {
- Map result = new HashMap(2);
+ Map result = new HashMap<>(2);
result.put(key1, value1);
result.put(key2, value2);
return result;
diff --git a/spring-data-neo4j/src/test/java/com/baeldung/neo4j/Neo4JServerTest.java b/spring-data-neo4j/src/test/java/com/baeldung/neo4j/Neo4JServerTest.java
new file mode 100644
index 0000000000..e959e0237d
--- /dev/null
+++ b/spring-data-neo4j/src/test/java/com/baeldung/neo4j/Neo4JServerTest.java
@@ -0,0 +1,60 @@
+package com.baeldung.neo4j;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.sql.Statement;
+
+import org.junit.Assert;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.neo4j.driver.v1.AuthTokens;
+import org.neo4j.driver.v1.Driver;
+import org.neo4j.driver.v1.GraphDatabase;
+import org.neo4j.driver.v1.Session;
+import org.neo4j.driver.v1.StatementResult;
+
+@Ignore
+public class Neo4JServerTest {
+
+ @Test
+ public void standAloneDriver() {
+ Driver driver = GraphDatabase.driver("bolt://localhost:7687", AuthTokens.basic("neo4j", "12345"));
+ Session session = driver.session();
+
+ session.run("CREATE (baeldung:Company {name:\"Baeldung\"}) " +
+ "-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" +
+ "RETURN baeldung, tesla");
+
+ StatementResult result = session.run("MATCH (company:Company)-[:owns]-> (car:Car)" +
+ "WHERE car.make='tesla' and car.model='modelX'" +
+ "RETURN company.name");
+
+ Assert.assertTrue(result.hasNext());
+ Assert.assertEquals(result.next().get("company.name").asString(), "Baeldung");
+
+ session.close();
+ driver.close();
+ }
+
+ @Test
+ public void standAloneJdbc() throws Exception {
+ Connection con = DriverManager.getConnection("jdbc:neo4j:bolt://localhost/?user=neo4j,password=12345,scheme=basic");
+
+ // Querying
+ try (Statement stmt = con.createStatement()) {
+ stmt.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " +
+ "-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" +
+ "RETURN baeldung, tesla");
+
+ ResultSet rs = stmt.executeQuery("MATCH (company:Company)-[:owns]-> (car:Car)" +
+ "WHERE car.make='tesla' and car.model='modelX'" +
+ "RETURN company.name");
+
+ while (rs.next()) {
+ Assert.assertEquals(rs.getString("company.name"), "Baeldung");
+ }
+ }
+ con.close();
+ }
+}
diff --git a/spring-data-neo4j/src/test/java/com/baeldung/neo4j/Neo4jLiveTest.java b/spring-data-neo4j/src/test/java/com/baeldung/neo4j/Neo4jLiveTest.java
new file mode 100644
index 0000000000..1ff01b93a1
--- /dev/null
+++ b/spring-data-neo4j/src/test/java/com/baeldung/neo4j/Neo4jLiveTest.java
@@ -0,0 +1,167 @@
+package com.baeldung.neo4j;
+
+
+import java.io.File;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.neo4j.graphdb.GraphDatabaseService;
+import org.neo4j.graphdb.Label;
+import org.neo4j.graphdb.Node;
+import org.neo4j.graphdb.NotFoundException;
+import org.neo4j.graphdb.RelationshipType;
+import org.neo4j.graphdb.Result;
+import org.neo4j.graphdb.factory.GraphDatabaseFactory;
+
+public class Neo4jLiveTest {
+
+ private static GraphDatabaseService graphDb;
+
+ @Before
+ public void setUp() {
+ GraphDatabaseFactory graphDbFactory = new GraphDatabaseFactory();
+ graphDb = graphDbFactory.newEmbeddedDatabase(new File("data/cars"));
+ }
+
+ @After
+ public void tearDown() {
+ graphDb.shutdown();
+ }
+
+ @Test
+ public void testPersonCar() {
+ graphDb.beginTx();
+ Node car = graphDb.createNode(Label.label("Car"));
+ car.setProperty("make", "tesla");
+ car.setProperty("model", "model3");
+
+ Node owner = graphDb.createNode(Label.label("Person"));
+ owner.setProperty("firstName", "baeldung");
+ owner.setProperty("lastName", "baeldung");
+
+ owner.createRelationshipTo(car, RelationshipType.withName("owner"));
+
+ Result result = graphDb.execute("MATCH (c:Car) <-[owner]- (p:Person) " +
+ "WHERE c.make = 'tesla'" +
+ "RETURN p.firstName, p.lastName");
+
+ Map firstResult = result.next();
+ Assert.assertEquals("baeldung", firstResult.get("p.firstName"));
+ }
+
+ @Test
+ public void testCreateNode() {
+
+ graphDb.beginTx();
+
+ Result result = graphDb.execute("CREATE (baeldung:Company {name:\"Baeldung\"})" +
+ "RETURN baeldung");
+
+ Map firstResult = result.next();
+ Node firstNode = (Node) firstResult.get("baeldung");
+ Assert.assertEquals(firstNode.getProperty("name"), "Baeldung");
+ }
+
+ @Test
+ public void testCreateNodeAndLink() {
+ graphDb.beginTx();
+
+ Result result = graphDb.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " +
+ "-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" +
+ "RETURN baeldung, tesla");
+
+ Map firstResult = result.next();
+ Assert.assertTrue(firstResult.containsKey("baeldung"));
+ Assert.assertTrue(firstResult.containsKey("tesla"));
+ }
+
+ @Test
+ public void testFindAndReturn() {
+ graphDb.beginTx();
+
+ graphDb.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " +
+ "-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" +
+ "RETURN baeldung, tesla");
+
+ Result result = graphDb.execute("MATCH (company:Company)-[:owns]-> (car:Car)" +
+ "WHERE car.make='tesla' and car.model='modelX'" +
+ "RETURN company.name");
+
+ Map firstResult = result.next();
+ Assert.assertEquals(firstResult.get("company.name"), "Baeldung");
+ }
+
+ @Test
+ public void testUpdate() {
+ graphDb.beginTx();
+
+ graphDb.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " +
+ "-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" +
+ "RETURN baeldung, tesla");
+
+ Result result = graphDb.execute("MATCH (car:Car)" +
+ "WHERE car.make='tesla'" +
+ " SET car.milage=120" +
+ " SET car :Car:Electro" +
+ " SET car.model=NULL" +
+ " RETURN car");
+
+ Map firstResult = result.next();
+ Node car = (Node) firstResult.get("car");
+
+ Assert.assertEquals(car.getProperty("milage"), 120L);
+ Assert.assertEquals(car.getLabels(), Arrays.asList(Label.label("Car"), Label.label("Electro")));
+
+ try {
+ car.getProperty("model");
+ Assert.fail();
+ } catch (NotFoundException e) {
+ // expected
+ }
+ }
+
+ @Test
+ public void testDelete() {
+ graphDb.beginTx();
+
+ graphDb.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " +
+ "-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" +
+ "RETURN baeldung, tesla");
+
+ graphDb.execute("MATCH (company:Company)" +
+ " WHERE company.name='Baeldung'" +
+ " DELETE company");
+
+ Result result = graphDb.execute("MATCH (company:Company)" +
+ " WHERE company.name='Baeldung'" +
+ " RETURN company");
+
+ Assert.assertFalse(result.hasNext());
+ }
+
+ @Test
+ public void testBindings() {
+ graphDb.beginTx();
+
+ Map params = new HashMap<>();
+ params.put("name", "baeldung");
+ params.put("make", "tesla");
+ params.put("model", "modelS");
+
+ Result result = graphDb.execute("CREATE (baeldung:Company {name:$name}) " +
+ "-[:owns]-> (tesla:Car {make: $make, model: $model})" +
+ "RETURN baeldung, tesla", params);
+
+ Map firstResult = result.next();
+ Assert.assertTrue(firstResult.containsKey("baeldung"));
+ Assert.assertTrue(firstResult.containsKey("tesla"));
+
+ Node car = (Node) firstResult.get("tesla");
+ Assert.assertEquals(car.getProperty("model"), "modelS");
+ }
+}
diff --git a/spring-data-neo4j/src/test/java/com/baeldung/neo4j/Neo4jOgmLiveTest.java b/spring-data-neo4j/src/test/java/com/baeldung/neo4j/Neo4jOgmLiveTest.java
new file mode 100644
index 0000000000..06b31667dd
--- /dev/null
+++ b/spring-data-neo4j/src/test/java/com/baeldung/neo4j/Neo4jOgmLiveTest.java
@@ -0,0 +1,49 @@
+package com.baeldung.neo4j;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.neo4j.ogm.config.Configuration;
+import org.neo4j.ogm.model.Result;
+import org.neo4j.ogm.session.Session;
+import org.neo4j.ogm.session.SessionFactory;
+
+import com.baeldung.spring.data.neo4j.domain.Car;
+import com.baeldung.spring.data.neo4j.domain.Company;
+import org.neo4j.ogm.transaction.Transaction;
+
+public class Neo4jOgmLiveTest {
+
+ @Test
+ public void testOgm() {
+ Configuration conf = new Configuration();
+ conf.driverConfiguration().setDriverClassName("org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver");
+
+ SessionFactory factory = new SessionFactory(conf, "com.baeldung.spring.data.neo4j.domain");
+ Session session = factory.openSession();
+
+ Car tesla = new Car("tesla", "modelS");
+ Company baeldung = new Company("baeldung");
+
+ baeldung.setCar(tesla);
+
+ session.save(baeldung);
+
+ Assert.assertEquals(1, session.countEntitiesOfType(Company.class));
+
+ Map params = new HashMap<>();
+ params.put("make", "tesla");
+ Result result = session.query("MATCH (car:Car) <-[:owns]- (company:Company)" +
+ " WHERE car.make=$make" +
+ " RETURN company", params);
+
+ Map firstResult = result.iterator().next();
+
+ Assert.assertEquals(firstResult.size(), 1);
+
+ Company actual = (Company) firstResult.get("company");
+ Assert.assertEquals(actual.getName(), baeldung.getName());
+ }
+}
diff --git a/spring-data-neo4j/src/test/resources/logback.xml b/spring-data-neo4j/src/test/resources/logback.xml
new file mode 100644
index 0000000000..39a6538324
--- /dev/null
+++ b/spring-data-neo4j/src/test/resources/logback.xml
@@ -0,0 +1,16 @@
+
+
+
+
+
+ %d %5p %40.40c:%4L - %m%n
+
+
+
+
+
+
+
+
+
+
diff --git a/spring-data-rest/pom.xml b/spring-data-rest/pom.xml
index da5d844211..4e8001ae7b 100644
--- a/spring-data-rest/pom.xml
+++ b/spring-data-rest/pom.xml
@@ -14,7 +14,7 @@
org.springframework.boot
spring-boot-starter-parent
- 1.4.2.RELEASE
+ 1.4.4.RELEASE
diff --git a/spring-data-rest/src/main/java/com/baeldung/config/ValidatorEventRegister.java b/spring-data-rest/src/main/java/com/baeldung/config/ValidatorEventRegister.java
index 89ab848e81..8f14d6c1c6 100644
--- a/spring-data-rest/src/main/java/com/baeldung/config/ValidatorEventRegister.java
+++ b/spring-data-rest/src/main/java/com/baeldung/config/ValidatorEventRegister.java
@@ -22,9 +22,15 @@ public class ValidatorEventRegister implements InitializingBean {
@Override
public void afterPropertiesSet() throws Exception {
List events = Arrays.asList("beforeCreate", "afterCreate", "beforeSave", "afterSave", "beforeLinkSave", "afterLinkSave", "beforeDelete", "afterDelete");
-
+
for (Map.Entry entry : validators.entrySet()) {
- events.stream().filter(p -> entry.getKey().startsWith(p)).findFirst().ifPresent(p -> validatingRepositoryEventListener.addValidator(p, entry.getValue()));
+ events
+ .stream()
+ .filter(p -> entry
+ .getKey()
+ .startsWith(p))
+ .findFirst()
+ .ifPresent(p -> validatingRepositoryEventListener.addValidator(p, entry.getValue()));
}
}
}
diff --git a/spring-data-rest/src/main/java/com/baeldung/exception/handlers/RestResponseEntityExceptionHandler.java b/spring-data-rest/src/main/java/com/baeldung/exception/handlers/RestResponseEntityExceptionHandler.java
index ee84738e7a..aa24fccac7 100644
--- a/spring-data-rest/src/main/java/com/baeldung/exception/handlers/RestResponseEntityExceptionHandler.java
+++ b/spring-data-rest/src/main/java/com/baeldung/exception/handlers/RestResponseEntityExceptionHandler.java
@@ -1,16 +1,17 @@
package com.baeldung.exception.handlers;
-import java.util.stream.Collectors;
-
import org.springframework.data.rest.core.RepositoryConstraintViolationException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
+import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
+import java.util.stream.Collectors;
+
@ControllerAdvice
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
@@ -18,8 +19,13 @@ public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionH
public ResponseEntity handleAccessDeniedException(Exception ex, WebRequest request) {
RepositoryConstraintViolationException nevEx = (RepositoryConstraintViolationException) ex;
- String errors = nevEx.getErrors().getAllErrors().stream().map(p -> p.toString()).collect(Collectors.joining("\n"));
- return new ResponseEntity(errors, new HttpHeaders(), HttpStatus.NOT_ACCEPTABLE);
+ String errors = nevEx
+ .getErrors()
+ .getAllErrors()
+ .stream()
+ .map(ObjectError::toString)
+ .collect(Collectors.joining("\n"));
+ return new ResponseEntity<>(errors, new HttpHeaders(), HttpStatus.NOT_ACCEPTABLE);
}
}
\ No newline at end of file
diff --git a/spring-data-solr/pom.xml b/spring-data-solr/pom.xml
index e43b3ff774..2aa9f86a96 100644
--- a/spring-data-solr/pom.xml
+++ b/spring-data-solr/pom.xml
@@ -51,6 +51,12 @@
${spring.version}
test
+
diff --git a/spring-data-solr/src/test/java/com/baeldung/solrjava/SolrJavaIntegrationTest.java b/spring-data-solr/src/test/java/com/baeldung/solrjava/SolrJavaIntegrationTest.java
new file mode 100644
index 0000000000..ce90ccaf16
--- /dev/null
+++ b/spring-data-solr/src/test/java/com/baeldung/solrjava/SolrJavaIntegrationTest.java
@@ -0,0 +1,70 @@
+package com.baeldung.solrjava;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.IOException;
+
+import org.apache.solr.client.solrj.SolrQuery;
+import org.apache.solr.client.solrj.SolrServerException;
+import org.apache.solr.client.solrj.impl.HttpSolrClient;
+import org.apache.solr.client.solrj.impl.XMLResponseParser;
+import org.apache.solr.client.solrj.response.QueryResponse;
+import org.apache.solr.common.SolrDocument;
+import org.apache.solr.common.SolrDocumentList;
+import org.apache.solr.common.SolrInputDocument;
+import org.junit.Before;
+import org.junit.Test;
+
+public class SolrJavaIntegrationTest {
+
+ private HttpSolrClient solr;
+ private SolrInputDocument document;
+
+ @Before
+ public void setUp() throws Exception {
+
+ solr = new HttpSolrClient("http://localhost:8983/solr/bigboxstore");
+ solr.setParser(new XMLResponseParser());
+
+ document = new SolrInputDocument();
+ document.addField("id", "123456");
+ document.addField("name", "Kenmore Dishwasher");
+ document.addField("price", "599.99");
+ solr.add(document);
+ solr.commit();
+ }
+
+ @Test
+ public void whenAdd_thenVerifyAdded() throws SolrServerException, IOException {
+
+ SolrQuery query = new SolrQuery();
+ query.set("q", "id:123456");
+ QueryResponse response = null;
+
+ response = solr.query(query);
+
+ SolrDocumentList docList = response.getResults();
+ assertEquals(docList.getNumFound(), 1);
+
+ for (SolrDocument doc : docList) {
+ assertEquals((String) doc.getFieldValue("id"), "123456");
+ assertEquals((Double) doc.getFieldValue("price"), (Double) 599.99);
+ }
+ }
+
+ @Test
+ public void whenDelete_thenVerifyDeleted() throws SolrServerException, IOException {
+
+ solr.deleteById("123456");
+ solr.commit();
+
+ SolrQuery query = new SolrQuery();
+ query.set("q", "id:123456");
+ QueryResponse response = null;
+
+ response = solr.query(query);
+
+ SolrDocumentList docList = response.getResults();
+ assertEquals(docList.getNumFound(), 0);
+ }
+}
diff --git a/spring-hibernate4/.gitignore b/spring-hibernate4/.gitignore
index 478cca6dac..d31cc4c619 100644
--- a/spring-hibernate4/.gitignore
+++ b/spring-hibernate4/.gitignore
@@ -12,3 +12,4 @@
*.war
*.ear
/target/
+/target/
diff --git a/spring-hibernate4/src/main/java/com/baeldung/hibernate/oneToMany/main/HibernateOneToManyAnnotationMain.java b/spring-hibernate4/src/main/java/com/baeldung/hibernate/oneToMany/main/HibernateOneToManyAnnotationMain.java
index 63b6450bf4..2bc5e514f7 100644
--- a/spring-hibernate4/src/main/java/com/baeldung/hibernate/oneToMany/main/HibernateOneToManyAnnotationMain.java
+++ b/spring-hibernate4/src/main/java/com/baeldung/hibernate/oneToMany/main/HibernateOneToManyAnnotationMain.java
@@ -16,16 +16,15 @@ public class HibernateOneToManyAnnotationMain {
public static void main(String[] args) {
Cart cart = new Cart();
- cart.setName("MyCart");
- Items item1 = new Items("I10", 10, 1, cart);
- Items item2 = new Items("I20", 20, 2, cart);
+ Items item1 = new Items(cart);
+ Items item2 = new Items(cart);
Set itemsSet = new HashSet();
itemsSet.add(item1);
itemsSet.add(item2);
cart.setItems(itemsSet);
- cart.setTotal(10 * 1 + 20 * 2);
+
SessionFactory sessionFactory = null;
Session session = null;
diff --git a/spring-hibernate4/src/main/java/com/baeldung/hibernate/oneToMany/model/Cart.java b/spring-hibernate4/src/main/java/com/baeldung/hibernate/oneToMany/model/Cart.java
index 61a32ba43f..b8b991831e 100644
--- a/spring-hibernate4/src/main/java/com/baeldung/hibernate/oneToMany/model/Cart.java
+++ b/spring-hibernate4/src/main/java/com/baeldung/hibernate/oneToMany/model/Cart.java
@@ -19,11 +19,6 @@ public class Cart {
@Column(name = "cart_id")
private long id;
- @Column(name = "total")
- private double total;
-
- @Column(name = "name")
- private String name;
@OneToMany(mappedBy = "cart")
private Set items;
@@ -36,21 +31,6 @@ public class Cart {
this.id = id;
}
- public double getTotal() {
- return total;
- }
-
- public void setTotal(double total) {
- this.total = total;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
public Set getItems() {
return items;
diff --git a/spring-hibernate4/src/main/java/com/baeldung/hibernate/oneToMany/model/Items.java b/spring-hibernate4/src/main/java/com/baeldung/hibernate/oneToMany/model/Items.java
index 40ee6fdea1..f63a4855b5 100644
--- a/spring-hibernate4/src/main/java/com/baeldung/hibernate/oneToMany/model/Items.java
+++ b/spring-hibernate4/src/main/java/com/baeldung/hibernate/oneToMany/model/Items.java
@@ -18,14 +18,6 @@ public class Items {
@Column(name = "id")
private long id;
- @Column(name = "item_id")
- private String itemId;
-
- @Column(name = "item_total")
- private double itemTotal;
-
- @Column(name = "quantity")
- private int quantity;
@ManyToOne
@JoinColumn(name = "cart_id", nullable = false)
@@ -35,37 +27,10 @@ public class Items {
public Items() {
}
- public Items(String itemId, double total, int qty, Cart c) {
- this.itemId = itemId;
- this.itemTotal = total;
- this.quantity = qty;
+ public Items(Cart c) {
this.cart = c;
}
- public String getItemId() {
- return itemId;
- }
-
- public void setItemId(String itemId) {
- this.itemId = itemId;
- }
-
- public double getItemTotal() {
- return itemTotal;
- }
-
- public void setItemTotal(double itemTotal) {
- this.itemTotal = itemTotal;
- }
-
- public int getQuantity() {
- return quantity;
- }
-
- public void setQuantity(int quantity) {
- this.quantity = quantity;
- }
-
public Cart getCart() {
return cart;
}
diff --git a/spring-hibernate4/src/main/resources/one_to_many.sql b/spring-hibernate4/src/main/resources/one_to_many.sql
index 7887ff7d9c..2eb48fc262 100644
--- a/spring-hibernate4/src/main/resources/one_to_many.sql
+++ b/spring-hibernate4/src/main/resources/one_to_many.sql
@@ -1,16 +1,11 @@
CREATE TABLE `Cart` (
`cart_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
- `total` decimal(10,0) NOT NULL,
- `name` varchar(10) DEFAULT NULL,
PRIMARY KEY (`cart_id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
CREATE TABLE `Items` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`cart_id` int(11) unsigned NOT NULL,
- `item_id` varchar(10) NOT NULL,
- `item_total` decimal(10,0) NOT NULL,
- `quantity` int(3) NOT NULL,
PRIMARY KEY (`id`),
KEY `cart_id` (`cart_id`),
CONSTRAINT `items_ibfk_1` FOREIGN KEY (`cart_id`) REFERENCES `Cart` (`cart_id`)
diff --git a/spring-hibernate4/src/test/java/com/baeldung/hibernate/oneToMany/main/HibernateOneToManyAnnotationMainTest.java b/spring-hibernate4/src/test/java/com/baeldung/hibernate/oneToMany/main/HibernateOneToManyAnnotationMainTest.java
index 688329e329..e2d2bf9143 100644
--- a/spring-hibernate4/src/test/java/com/baeldung/hibernate/oneToMany/main/HibernateOneToManyAnnotationMainTest.java
+++ b/spring-hibernate4/src/test/java/com/baeldung/hibernate/oneToMany/main/HibernateOneToManyAnnotationMainTest.java
@@ -63,9 +63,6 @@ public class HibernateOneToManyAnnotationMainTest {
cartItems = cart.getItems();
Assert.assertNull(cartItems);
Items item1 = new Items();
- item1.setItemId("I10");
- item1.setItemTotal(10);
- item1.setQuantity(1);
item1.setCart(cart);
assertNotNull(item1);
Set itemsSet = new HashSet();
diff --git a/spring-jersey/pom.xml b/spring-jersey/pom.xml
index 293850d41e..41ebb9a6b5 100644
--- a/spring-jersey/pom.xml
+++ b/spring-jersey/pom.xml
@@ -9,7 +9,7 @@
war
- 2.25
+ 2.25.1
1.7.22
1.1.8
4.12
diff --git a/spring-jersey/src/main/java/com/baeldung/client/rest/RestClient.java b/spring-jersey/src/main/java/com/baeldung/client/rest/RestClient.java
index 0e45b68b14..34f7d45601 100644
--- a/spring-jersey/src/main/java/com/baeldung/client/rest/RestClient.java
+++ b/spring-jersey/src/main/java/com/baeldung/client/rest/RestClient.java
@@ -18,7 +18,7 @@ public class RestClient {
}
public Employee getJsonEmployee(int id) {
- return client.target(REST_URI).path(new Integer(id).toString()).request(MediaType.APPLICATION_JSON).get(Employee.class);
+ return client.target(REST_URI).path(String.valueOf(id)).request(MediaType.APPLICATION_JSON).get(Employee.class);
}
public Response createXmlEmployee(Employee emp) {
@@ -26,6 +26,6 @@ public class RestClient {
}
public Employee getXmlEmployee(int id) {
- return client.target(REST_URI).path(new Integer(id).toString()).request(MediaType.APPLICATION_XML).get(Employee.class);
+ return client.target(REST_URI).path(String.valueOf(id)).request(MediaType.APPLICATION_XML).get(Employee.class);
}
}
diff --git a/spring-jooq/pom.xml b/spring-jooq/pom.xml
index bf0dffd68c..195c0b8514 100644
--- a/spring-jooq/pom.xml
+++ b/spring-jooq/pom.xml
@@ -1,215 +1,215 @@
- 4.0.0
- com.baeldung
- jooq-spring
- 0.0.1-SNAPSHOT
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ 4.0.0
+ com.baeldung
+ jooq-spring
+ 0.0.1-SNAPSHOT
-
-
-
-
- org.springframework.boot
- spring-boot-dependencies
- 1.4.2.RELEASE
- pom
- import
-
-
-
+
+
+
+
+ org.springframework.boot
+ spring-boot-dependencies
+ 1.4.4.RELEASE
+ pom
+ import
+
+
+
-
-
-
- org.jooq
- jooq
-
+
+
+
+ org.jooq
+ jooq
+
-
-
- com.h2database
- h2
-
+
+
+ com.h2database
+ h2
+
-
-
- org.springframework
- spring-context
-
-
- org.springframework
- spring-jdbc
-
-
- org.springframework.boot
- spring-boot-starter-jooq
-
+
+
+ org.springframework
+ spring-context
+
+
+ org.springframework
+ spring-jdbc
+
+
+ org.springframework.boot
+ spring-boot-starter-jooq
+
-
-
- org.slf4j
- slf4j-api
- runtime
-
-
- ch.qos.logback
- logback-classic
- runtime
-
+
+
+ org.slf4j
+ slf4j-api
+ runtime
+
+
+ ch.qos.logback
+ logback-classic
+ runtime
+
-
-
- junit
- junit
- test
-
-
- org.springframework
- spring-test
- test
-
-
- org.springframework.boot
- spring-boot-starter-test
-
-
-
+
+
+ junit
+ junit
+ test
+
+
+ org.springframework
+ spring-test
+ test
+
+
+ org.springframework.boot
+ spring-boot-starter-test
+
-
-
-
- org.codehaus.mojo
- properties-maven-plugin
- ${properties-maven-plugin.version}
-
-
- initialize
-
- read-project-properties
-
-
-
- src/main/resources/intro_config.properties
-
-
-
-
-
+
-
- org.codehaus.mojo
- sql-maven-plugin
- ${sql-maven-plugin.version}
-
-
- initialize
-
- execute
-
-
- ${db.driver}
- ${db.url}
- ${db.username}
- ${db.password}
-
- src/main/resources/intro_schema.sql
-
-
-
-
-
-
- com.h2database
- h2
- ${com.h2database.version}
-
-
-
+
+
+
+ org.codehaus.mojo
+ properties-maven-plugin
+ ${properties-maven-plugin.version}
+
+
+ initialize
+
+ read-project-properties
+
+
+
+ src/main/resources/intro_config.properties
+
+
+
+
+
-
- org.jooq
- jooq-codegen-maven
- ${org.jooq.version}
-
-
- generate-sources
-
- generate
-
-
-
- ${db.driver}
- ${db.url}
- ${db.username}
- ${db.password}
-
-
-
- com.baeldung.jooq.introduction.db
- src/main/java
-
-
-
-
-
-
+
+ org.codehaus.mojo
+ sql-maven-plugin
+ ${sql-maven-plugin.version}
+
+
+ initialize
+
+ execute
+
+
+ ${db.driver}
+ ${db.url}
+ ${db.username}
+ ${db.password}
+
+ src/main/resources/intro_schema.sql
+
+
+
+
+
+
+ com.h2database
+ h2
+ ${com.h2database.version}
+
+
+
-
- org.apache.maven.plugins
- maven-compiler-plugin
- ${maven-compiler-plugin.version}
-
- 1.8
- 1.8
-
-
+
+ org.jooq
+ jooq-codegen-maven
+ ${org.jooq.version}
+
+
+ generate-sources
+
+ generate
+
+
+
+ ${db.driver}
+ ${db.url}
+ ${db.username}
+ ${db.password}
+
+
+
+ com.baeldung.jooq.introduction.db
+ src/main/java
+
+
+
+
+
+
-
- org.apache.maven.plugins
- maven-surefire-plugin
-
-
- **/*IntegrationTest.java
- **/*LiveTest.java
-
-
-
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+ ${maven-compiler-plugin.version}
+
+ 1.8
+ 1.8
+
+
-
-
-
-
-
- org.eclipse.m2e
- lifecycle-mapping
- ${lifecycle-mapping.version}
-
-
-
-
-
- org.jooq
-
- jooq-codegen-maven
-
-
- [3.7.3,)
-
-
- generate
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+ org.apache.maven.plugins
+ maven-surefire-plugin
+
+
+ **/*IntegrationTest.java
+ **/*LiveTest.java
+
+
+
+
+
+
+
+
+
+ org.eclipse.m2e
+ lifecycle-mapping
+ ${lifecycle-mapping.version}
+
+
+
+
+
+ org.jooq
+
+ jooq-codegen-maven
+
+
+ [3.7.3,)
+
+
+ generate
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -245,21 +245,21 @@