diff --git a/core-java/src/test/java/com/baeldung/arraycopy/ArrayCopyUtilTest.java b/core-java/src/test/java/com/baeldung/arraycopy/ArrayCopyUtilTest.java index 060f3c3109..2235e55338 100644 --- a/core-java/src/test/java/com/baeldung/arraycopy/ArrayCopyUtilTest.java +++ b/core-java/src/test/java/com/baeldung/arraycopy/ArrayCopyUtilTest.java @@ -15,14 +15,18 @@ public class ArrayCopyUtilTest { @BeforeClass public static void setup(){ - employees = new Employee[MAX]; - Employee employee; - for(int i = 0; i < MAX; i++) { - employee = new Employee(); - employee.setName("Emp"+i); - employee.setId(i); - employees[i] = employee; - } + createEmployeesArray(); + } + + private static void createEmployeesArray() { + employees = new Employee[MAX]; + Employee employee; + for(int i = 0; i < MAX; i++) { + employee = new Employee(); + employee.setName("Emp"+i); + employee.setId(i); + employees[i] = employee; + } } @Test @@ -32,10 +36,7 @@ public class ArrayCopyUtilTest { System.arraycopy(array, 0, copiedArray, 0, 3); - Assert.assertTrue(array.length == copiedArray.length); - Assert.assertTrue(copiedArray[0] == array[0]); - Assert.assertTrue(copiedArray[1] == array[1]); - Assert.assertTrue(copiedArray[2] == array[2]); + Assert.assertArrayEquals(copiedArray, array); } @Test @@ -70,12 +71,7 @@ public class ArrayCopyUtilTest { int[] copiedArray = Arrays.copyOf(array, newLength); - Assert.assertNotNull(copiedArray); - Assert.assertTrue(copiedArray.length == array.length); - Assert.assertTrue(copiedArray[0] == array[0]); - Assert.assertTrue(copiedArray[1] == array[1]); - Assert.assertTrue(copiedArray[2] == array[2]); - Assert.assertTrue(copiedArray[3] == array[3]); + Assert.assertArrayEquals(copiedArray, array); array[0] = 9; Assert.assertTrue(copiedArray[0] != array[0]); copiedArray[1] = 12; @@ -86,8 +82,7 @@ public class ArrayCopyUtilTest { public void givenArrayOfNonPrimitiveType_whenCopiedViaArraysCopyOf_thenDoShallowCopy(){ Employee[] copiedArray = Arrays.copyOf(employees, employees.length); - Assert.assertNotNull(copiedArray); - Assert.assertTrue(copiedArray.length == employees.length); + Assert.assertArrayEquals(copiedArray, employees); employees[0].setName(employees[0].getName()+"_Changed"); //change in employees' element caused change in the copied array Assert.assertTrue(copiedArray[0].getName().equals(employees[0].getName())); @@ -99,12 +94,7 @@ public class ArrayCopyUtilTest { int[] copiedArray = array.clone(); - Assert.assertNotNull(copiedArray); - Assert.assertTrue(copiedArray.length == array.length); - Assert.assertTrue(copiedArray[0] == array[0]); - Assert.assertTrue(copiedArray[1] == array[1]); - Assert.assertTrue(copiedArray[2] == array[2]); - Assert.assertTrue(copiedArray[3] == array[3]); + Assert.assertArrayEquals(copiedArray, array); array[0] = 9; Assert.assertTrue(copiedArray[0] != array[0]); copiedArray[1] = 12; @@ -115,8 +105,7 @@ public class ArrayCopyUtilTest { public void givenArraysOfNonPrimitiveType_whenCopiedViaArrayClone_thenDoShallowCopy(){ Employee[] copiedArray = employees.clone(); - Assert.assertNotNull(copiedArray); - Assert.assertTrue(copiedArray.length == employees.length); + Assert.assertArrayEquals(copiedArray, employees);; employees[0].setName(employees[0].getName()+"_Changed"); //change in employees' element changed the copied array Assert.assertTrue(copiedArray[0].getName().equals(employees[0].getName())); @@ -128,29 +117,25 @@ public class ArrayCopyUtilTest { Address[] copiedArray = addresses.clone(); - Assert.assertNotNull(copiedArray); - Assert.assertTrue(copiedArray.length == addresses.length); addresses[0].setCity(addresses[0].getCity()+"_Changed"); - Assert.assertTrue(copiedArray[0].getCity().equals(addresses[0].getCity())); + Assert.assertArrayEquals(copiedArray, addresses); } @Test public void givenArraysOfSerializableNonPrimitiveType_whenCopiedViaSerializationUtils_thenDoDeepCopy(){ Employee[] copiedArray = SerializationUtils.clone(employees); - - Assert.assertNotNull(copiedArray); - Assert.assertTrue(copiedArray.length == employees.length); + employees[0].setName(employees[0].getName()+"_Changed"); //change in employees' element didn't change in the copied array - Assert.assertFalse(copiedArray[0].getName().equals(employees[0].getName())); + Assert.assertFalse( + copiedArray[0].getName().equals(employees[0].getName())); } @Test public void givenArraysOfNonPrimitiveType_whenCopiedViaStream_thenDoShallowCopy(){ Employee[] copiedArray = Arrays.stream(employees).toArray(Employee[]::new); - Assert.assertNotNull(copiedArray); - Assert.assertTrue(copiedArray.length == employees.length); + Assert.assertArrayEquals(copiedArray, employees); employees[0].setName(employees[0].getName()+"_Changed"); //change in employees' element didn't change in the copied array Assert.assertTrue(copiedArray[0].getName().equals(employees[0].getName())); @@ -162,11 +147,7 @@ public class ArrayCopyUtilTest { String[] copiedArray = Arrays.stream(strArray).toArray(String[]::new); - Assert.assertNotNull(copiedArray); - Assert.assertTrue(copiedArray.length == strArray.length); - Assert.assertTrue(copiedArray[0] == strArray[0]); - Assert.assertTrue(copiedArray[1] == strArray[1]); - Assert.assertTrue(copiedArray[2] == strArray[2]); + Assert.assertArrayEquals(copiedArray, strArray); } private Address[] createAddressArray(){ diff --git a/core-java/src/test/java/com/baeldung/list/flattennestedlist/FlattenNestedListTest.java b/core-java/src/test/java/com/baeldung/list/flattennestedlist/FlattenNestedListTest.java index fdf4934cb7..b7939d09da 100644 --- a/core-java/src/test/java/com/baeldung/list/flattennestedlist/FlattenNestedListTest.java +++ b/core-java/src/test/java/com/baeldung/list/flattennestedlist/FlattenNestedListTest.java @@ -1,51 +1,29 @@ package com.baeldung.list.flattennestedlist; -import static org.junit.Assert.assertEquals; +import static java.util.Arrays.asList; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.stream.Collectors; -import org.junit.After; -import org.junit.Before; +import org.hamcrest.collection.IsIterableContainingInOrder; import org.junit.Test; public class FlattenNestedListTest { - private List> lol = new ArrayList<>(); - List ls1 = Arrays.asList("one:one", "one:two", "one:three"); - List ls2 = Arrays.asList("two:one", "two:two", "two:three"); - List ls3 = Arrays.asList("three:one", "three:two", "three:three"); - - @Before - public void setup() { - lol.addAll(Arrays.asList(ls1, ls2, ls3)); - } - - @After - public void tearDown() { - lol = null; - } + List> lol = asList(asList("one:one"), asList("two:one", "two:two", "two:three"), asList("three:one", "three:two", "three:three", "three:four")); @Test public void givenString_flattenNestedList1() { List ls = flattenListOfListsImperatively(lol); assertNotNull(ls); - assertTrue(ls.size() == 9); + assertTrue(ls.size() == 8); // assert content - assertEquals(ls.get(0), "one:one"); - assertEquals(ls.get(1), "one:two"); - assertEquals(ls.get(2), "one:three"); - assertEquals(ls.get(3), "two:one"); - assertEquals(ls.get(4), "two:two"); - assertEquals(ls.get(5), "two:three"); - assertEquals(ls.get(6), "three:one"); - assertEquals(ls.get(7), "three:two"); - assertEquals(ls.get(8), "three:three"); + assertThat(ls, IsIterableContainingInOrder.contains("one:one", "two:one", "two:two", "two:three", "three:one", "three:two", "three:three", "three:four")); } @Test @@ -53,17 +31,9 @@ public class FlattenNestedListTest { List ls = flattenListOfListsStream(lol); assertNotNull(ls); - assertTrue(ls.size() == 9); + assertTrue(ls.size() == 8); // assert content - assertEquals(ls.get(0), "one:one"); - assertEquals(ls.get(1), "one:two"); - assertEquals(ls.get(2), "one:three"); - assertEquals(ls.get(3), "two:one"); - assertEquals(ls.get(4), "two:two"); - assertEquals(ls.get(5), "two:three"); - assertEquals(ls.get(6), "three:one"); - assertEquals(ls.get(7), "three:two"); - assertEquals(ls.get(8), "three:three"); + assertThat(ls, IsIterableContainingInOrder.contains("one:one", "two:one", "two:two", "two:three", "three:one", "three:two", "three:three", "three:four")); } public List flattenListOfListsImperatively(List> list) { diff --git a/lagom/.gitignore b/lagom/.gitignore new file mode 100644 index 0000000000..5e521241aa --- /dev/null +++ b/lagom/.gitignore @@ -0,0 +1,4 @@ +greeting-impl/logs/* +lagom-hello-world/greeting-impl/bin/classes/* +lagom-hello-world/logs/application.log +lagom-hello-world/weather-impl/logs/application.log diff --git a/lagom/README b/lagom/README new file mode 100644 index 0000000000..0d81a4b3c1 --- /dev/null +++ b/lagom/README @@ -0,0 +1,40 @@ +Steps to setup from scratch + +1) Create sbt build file "build.sbt" +2) Create plugins file project/plugins.sbt +3) Create build properties file project/build.properties +4) Run sbt command from project root to generate project directories, generated projects at target/lagom-dynamic-projects + Service Locator project: lagom-internal-meta-project-service-locator + cassandra project: lagom-internal-meta-project-cassandra +5) Create folders in all projects to follow maven like directory structure layout for project source code: src/main/java and src/main/java/resources and test folders. +6) Weather microservice + a) WeatherService Interface with method: + weatherStatsForToday() + b) WeatherServiceImpl to return weatherstats from a random list of weather stats + +7) Greeting Microservice + GreetingService Interface: + handleGreetFrom(String user) + + a) Reply back to user with greeting message("Hello") along with weather stats fetched from weather microservice + b) Update the greeting message("Hello Again") for an existing user. + +8) GreetingEntity: PersistentEntity + a) handles ReceivedGreetingCommand + b) Stores ReceivedGreetingEvent events to cassandra + c) Processes ReceivedGreetingEvent to update GreetingState("Hello" to "Hello Again") if required. + +9) Register modules with lagom using application.conf files. +10) Run project: sbt lagom:runAll + + +Sample Run: +curl http://localhost:9000/api/greeting/Nikhil; +Hello Nikhil! Today's weather stats: Going to be very humid, Take Water + +curl http://localhost:9000/api/greeting/Nikhil; +Hello Again Nikhil! Today's weather stats: Going to Rain, Take Umbrella + + + + diff --git a/lagom/build.sbt b/lagom/build.sbt new file mode 100644 index 0000000000..064d67468e --- /dev/null +++ b/lagom/build.sbt @@ -0,0 +1,41 @@ +organization in ThisBuild := "org.baeldung" + +// the Scala version that will be used for cross-compiled libraries +scalaVersion in ThisBuild := "2.11.7" + +lagomKafkaEnabled in ThisBuild := false + +lazy val greetingApi = project("greeting-api") + .settings( + version := "1.0-SNAPSHOT", + libraryDependencies ++= Seq( + lagomJavadslApi + ) + ) + +lazy val greetingImpl = project("greeting-impl") + .enablePlugins(LagomJava) + .settings( + version := "1.0-SNAPSHOT", + libraryDependencies ++= Seq( + lagomJavadslPersistenceCassandra + ) + ) + .dependsOn(greetingApi, weatherApi) + +lazy val weatherApi = project("weather-api") + .settings( + version := "1.0-SNAPSHOT", + libraryDependencies ++= Seq( + lagomJavadslApi + ) + ) + +lazy val weatherImpl = project("weather-impl") + .enablePlugins(LagomJava) + .settings( + version := "1.0-SNAPSHOT" + ) + .dependsOn(weatherApi) + +def project(id: String) = Project(id, base = file(id)) \ No newline at end of file diff --git a/lagom/greeting-api/src/main/java/org/baeldung/lagom/helloworld/greeting/api/GreetingService.java b/lagom/greeting-api/src/main/java/org/baeldung/lagom/helloworld/greeting/api/GreetingService.java new file mode 100644 index 0000000000..93567f0185 --- /dev/null +++ b/lagom/greeting-api/src/main/java/org/baeldung/lagom/helloworld/greeting/api/GreetingService.java @@ -0,0 +1,23 @@ +package org.baeldung.lagom.helloworld.greeting.api; + +import static com.lightbend.lagom.javadsl.api.Service.named; +import static com.lightbend.lagom.javadsl.api.Service.restCall; + +import com.lightbend.lagom.javadsl.api.Descriptor; +import com.lightbend.lagom.javadsl.api.Service; +import com.lightbend.lagom.javadsl.api.ServiceCall; +import com.lightbend.lagom.javadsl.api.transport.Method; + +import akka.NotUsed; + +public interface GreetingService extends Service { + + public ServiceCall handleGreetFrom(String user); + + @Override + default Descriptor descriptor() { + return named("greetingservice").withCalls( + restCall(Method.GET, "/api/greeting/:fromUser", this::handleGreetFrom) + ).withAutoAcl(true); + } +} \ No newline at end of file diff --git a/lagom/greeting-impl/bin/application.conf b/lagom/greeting-impl/bin/application.conf new file mode 100644 index 0000000000..1e78ce4a79 --- /dev/null +++ b/lagom/greeting-impl/bin/application.conf @@ -0,0 +1 @@ +play.modules.enabled += org.baeldung.lagom.helloworld.greeting.impl.GreetingServiceModule \ No newline at end of file diff --git a/lagom/greeting-impl/bin/classes/application.conf b/lagom/greeting-impl/bin/classes/application.conf new file mode 100644 index 0000000000..1e78ce4a79 --- /dev/null +++ b/lagom/greeting-impl/bin/classes/application.conf @@ -0,0 +1 @@ +play.modules.enabled += org.baeldung.lagom.helloworld.greeting.impl.GreetingServiceModule \ No newline at end of file diff --git a/lagom/greeting-impl/src/main/java/org/baeldung/lagom/helloworld/greeting/impl/GreetingCommand.java b/lagom/greeting-impl/src/main/java/org/baeldung/lagom/helloworld/greeting/impl/GreetingCommand.java new file mode 100644 index 0000000000..be9e713ec9 --- /dev/null +++ b/lagom/greeting-impl/src/main/java/org/baeldung/lagom/helloworld/greeting/impl/GreetingCommand.java @@ -0,0 +1,29 @@ +package org.baeldung.lagom.helloworld.greeting.impl; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.google.common.base.Preconditions; +import com.lightbend.lagom.javadsl.persistence.PersistentEntity; +import com.lightbend.lagom.serialization.CompressedJsonable; +import com.lightbend.lagom.serialization.Jsonable; + +public interface GreetingCommand extends Jsonable { + + @SuppressWarnings("serial") + @JsonDeserialize + public final class ReceivedGreetingCommand implements GreetingCommand, + CompressedJsonable, PersistentEntity.ReplyType { + private final String fromUser; + + @JsonCreator + public ReceivedGreetingCommand(String fromUser) { + this.fromUser = Preconditions.checkNotNull(fromUser, "fromUser"); + } + + public String getFromUser() { + return fromUser; + } + + } + +} diff --git a/lagom/greeting-impl/src/main/java/org/baeldung/lagom/helloworld/greeting/impl/GreetingEntity.java b/lagom/greeting-impl/src/main/java/org/baeldung/lagom/helloworld/greeting/impl/GreetingEntity.java new file mode 100644 index 0000000000..91fc74039d --- /dev/null +++ b/lagom/greeting-impl/src/main/java/org/baeldung/lagom/helloworld/greeting/impl/GreetingEntity.java @@ -0,0 +1,32 @@ +package org.baeldung.lagom.helloworld.greeting.impl; + +import java.util.Optional; + +import org.baeldung.lagom.helloworld.greeting.impl.GreetingCommand.ReceivedGreetingCommand; +import org.baeldung.lagom.helloworld.greeting.impl.GreetingEvent.ReceivedGreetingEvent; + +import com.lightbend.lagom.javadsl.persistence.PersistentEntity; + +public class GreetingEntity extends PersistentEntity { + + @Override + public Behavior initialBehavior(Optional snapshotState) { + BehaviorBuilder b = newBehaviorBuilder(new GreetingState("Hello ")); + + b.setCommandHandler(ReceivedGreetingCommand.class, + (cmd, ctx) -> { + String fromUser = cmd.getFromUser(); + String currentGreeting = state().getMessage(); + return ctx.thenPersist( + new ReceivedGreetingEvent(fromUser), + evt -> ctx.reply(currentGreeting + fromUser + "!")); + }); + + b.setEventHandler(ReceivedGreetingEvent.class, + // We simply update the current state + evt -> state().withMessage("Hello Again ")); + + return b.build(); + } +} diff --git a/lagom/greeting-impl/src/main/java/org/baeldung/lagom/helloworld/greeting/impl/GreetingEvent.java b/lagom/greeting-impl/src/main/java/org/baeldung/lagom/helloworld/greeting/impl/GreetingEvent.java new file mode 100644 index 0000000000..e454f6cd1b --- /dev/null +++ b/lagom/greeting-impl/src/main/java/org/baeldung/lagom/helloworld/greeting/impl/GreetingEvent.java @@ -0,0 +1,23 @@ +package org.baeldung.lagom.helloworld.greeting.impl; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.lightbend.lagom.serialization.Jsonable; + + +public interface GreetingEvent extends Jsonable { + + class ReceivedGreetingEvent implements GreetingEvent { + private final String fromUser; + + @JsonCreator + public ReceivedGreetingEvent(String fromUser) { + this.fromUser = fromUser; + } + + public String getFromUser() { + return fromUser; + } + + } + +} diff --git a/lagom/greeting-impl/src/main/java/org/baeldung/lagom/helloworld/greeting/impl/GreetingServiceImpl.java b/lagom/greeting-impl/src/main/java/org/baeldung/lagom/helloworld/greeting/impl/GreetingServiceImpl.java new file mode 100644 index 0000000000..c28687291e --- /dev/null +++ b/lagom/greeting-impl/src/main/java/org/baeldung/lagom/helloworld/greeting/impl/GreetingServiceImpl.java @@ -0,0 +1,48 @@ +package org.baeldung.lagom.helloworld.greeting.impl; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; + +import org.baeldung.lagom.helloworld.greeting.api.GreetingService; +import org.baeldung.lagom.helloworld.greeting.impl.GreetingCommand.ReceivedGreetingCommand; +import org.baeldung.lagom.helloworld.weather.api.WeatherService; +import org.baeldung.lagom.helloworld.weather.api.WeatherStats; + +import com.google.inject.Inject; +import com.lightbend.lagom.javadsl.api.ServiceCall; +import com.lightbend.lagom.javadsl.persistence.PersistentEntityRef; +import com.lightbend.lagom.javadsl.persistence.PersistentEntityRegistry; + +import akka.NotUsed; + +public class GreetingServiceImpl implements GreetingService { + + private final PersistentEntityRegistry persistentEntityRegistry; + private final WeatherService weatherService; + + @Inject + public GreetingServiceImpl(PersistentEntityRegistry persistentEntityRegistry, WeatherService weatherService) { + this.persistentEntityRegistry = persistentEntityRegistry; + this.weatherService = weatherService; + persistentEntityRegistry.register(GreetingEntity.class); + } + + @Override + public ServiceCall handleGreetFrom(String user) { + return request -> { + // Look up the hello world entity for the given ID. + PersistentEntityRef ref = persistentEntityRegistry.refFor(GreetingEntity.class, user); + CompletableFuture greetingResponse = ref.ask(new ReceivedGreetingCommand(user)) + .toCompletableFuture(); + CompletableFuture todaysWeatherInfo = + (CompletableFuture) weatherService.weatherStatsForToday().invoke(); + try { + return CompletableFuture.completedFuture(greetingResponse.get() + + " Today's weather stats: " + todaysWeatherInfo.get().getMessage()); + } catch (InterruptedException | ExecutionException e) { + return CompletableFuture.completedFuture("Sorry Some Error at our end, working on it"); + } + }; + } + +} diff --git a/lagom/greeting-impl/src/main/java/org/baeldung/lagom/helloworld/greeting/impl/GreetingServiceModule.java b/lagom/greeting-impl/src/main/java/org/baeldung/lagom/helloworld/greeting/impl/GreetingServiceModule.java new file mode 100644 index 0000000000..4813e91a7c --- /dev/null +++ b/lagom/greeting-impl/src/main/java/org/baeldung/lagom/helloworld/greeting/impl/GreetingServiceModule.java @@ -0,0 +1,18 @@ +package org.baeldung.lagom.helloworld.greeting.impl; + +import org.baeldung.lagom.helloworld.greeting.api.GreetingService; +import org.baeldung.lagom.helloworld.weather.api.WeatherService; + +import com.google.inject.AbstractModule; +import com.lightbend.lagom.javadsl.server.ServiceGuiceSupport; + +/** + * The module that binds the GreetingService so that it can be served. + */ +public class GreetingServiceModule extends AbstractModule implements ServiceGuiceSupport { + @Override + protected void configure() { + bindServices(serviceBinding(GreetingService.class, GreetingServiceImpl.class)); + bindClient(WeatherService.class); + } +} diff --git a/lagom/greeting-impl/src/main/java/org/baeldung/lagom/helloworld/greeting/impl/GreetingState.java b/lagom/greeting-impl/src/main/java/org/baeldung/lagom/helloworld/greeting/impl/GreetingState.java new file mode 100644 index 0000000000..125795601e --- /dev/null +++ b/lagom/greeting-impl/src/main/java/org/baeldung/lagom/helloworld/greeting/impl/GreetingState.java @@ -0,0 +1,24 @@ +package org.baeldung.lagom.helloworld.greeting.impl; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; + +@JsonDeserialize +public class GreetingState { + + private final String message; + + @JsonCreator + public GreetingState(String message) { + this.message = message; + } + + GreetingState withMessage(String message) { + return new GreetingState(message); + } + + public String getMessage() { + return message; + } + +} diff --git a/lagom/greeting-impl/src/main/resources/application.conf b/lagom/greeting-impl/src/main/resources/application.conf new file mode 100644 index 0000000000..1e78ce4a79 --- /dev/null +++ b/lagom/greeting-impl/src/main/resources/application.conf @@ -0,0 +1 @@ +play.modules.enabled += org.baeldung.lagom.helloworld.greeting.impl.GreetingServiceModule \ No newline at end of file diff --git a/lagom/project/build.properties b/lagom/project/build.properties new file mode 100644 index 0000000000..43b8278c68 --- /dev/null +++ b/lagom/project/build.properties @@ -0,0 +1 @@ +sbt.version=0.13.11 diff --git a/lagom/project/plugins.sbt b/lagom/project/plugins.sbt new file mode 100644 index 0000000000..8b8e36a743 --- /dev/null +++ b/lagom/project/plugins.sbt @@ -0,0 +1,2 @@ +addSbtPlugin("com.lightbend.lagom" % "lagom-sbt-plugin" % "1.3.1") +addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "3.0.0") diff --git a/lagom/weather-api/src/main/java/org/baeldung/lagom/helloworld/weather/api/WeatherService.java b/lagom/weather-api/src/main/java/org/baeldung/lagom/helloworld/weather/api/WeatherService.java new file mode 100644 index 0000000000..888109322b --- /dev/null +++ b/lagom/weather-api/src/main/java/org/baeldung/lagom/helloworld/weather/api/WeatherService.java @@ -0,0 +1,28 @@ +package org.baeldung.lagom.helloworld.weather.api; + +import static com.lightbend.lagom.javadsl.api.Service.named; +import static com.lightbend.lagom.javadsl.api.Service.*; + +import com.lightbend.lagom.javadsl.api.Descriptor; +import com.lightbend.lagom.javadsl.api.Service; +import com.lightbend.lagom.javadsl.api.ServiceCall; +import com.lightbend.lagom.javadsl.api.transport.Method; + +import akka.NotUsed; + +/** + * WeatherService Interface. + */ +public interface WeatherService extends Service { + + // Fetch Today's Weather Stats service call + public ServiceCall weatherStatsForToday(); + + @Override + default Descriptor descriptor() { + return named("weatherservice").withCalls( + restCall(Method.GET, "/api/weather", this::weatherStatsForToday) + ).withAutoAcl(true); + } + +} \ No newline at end of file diff --git a/lagom/weather-api/src/main/java/org/baeldung/lagom/helloworld/weather/api/WeatherStats.java b/lagom/weather-api/src/main/java/org/baeldung/lagom/helloworld/weather/api/WeatherStats.java new file mode 100644 index 0000000000..23530a297d --- /dev/null +++ b/lagom/weather-api/src/main/java/org/baeldung/lagom/helloworld/weather/api/WeatherStats.java @@ -0,0 +1,32 @@ +package org.baeldung.lagom.helloworld.weather.api; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Random; + +public enum WeatherStats { + + STATS_RAINY("Going to Rain, Take Umbrella"), STATS_HUMID("Going to be very humid, Take Water"); + + private final String message; + + private static final List VALUES = Collections.unmodifiableList(Arrays.asList(values())); + + private static final int SIZE = VALUES.size(); + + private static final Random RANDOM = new Random(); + + WeatherStats(String msg) { + this.message = msg; + } + + public static WeatherStats forToday() { + return VALUES.get(RANDOM.nextInt(SIZE)); + } + + public String getMessage() { + return message; + } + +} diff --git a/lagom/weather-impl/bin/application.conf b/lagom/weather-impl/bin/application.conf new file mode 100644 index 0000000000..cf6cec2115 --- /dev/null +++ b/lagom/weather-impl/bin/application.conf @@ -0,0 +1 @@ +play.modules.enabled += org.baeldung.lagom.helloworld.weather.impl.WeatherServiceModule \ No newline at end of file diff --git a/lagom/weather-impl/src/main/java/org/baeldung/lagom/helloworld/weather/impl/WeatherServiceImpl.java b/lagom/weather-impl/src/main/java/org/baeldung/lagom/helloworld/weather/impl/WeatherServiceImpl.java new file mode 100644 index 0000000000..d7f97f9105 --- /dev/null +++ b/lagom/weather-impl/src/main/java/org/baeldung/lagom/helloworld/weather/impl/WeatherServiceImpl.java @@ -0,0 +1,19 @@ +package org.baeldung.lagom.helloworld.weather.impl; + +import java.util.concurrent.CompletableFuture; + +import org.baeldung.lagom.helloworld.weather.api.WeatherService; +import org.baeldung.lagom.helloworld.weather.api.WeatherStats; + +import com.lightbend.lagom.javadsl.api.ServiceCall; + +import akka.NotUsed; + +public class WeatherServiceImpl implements WeatherService { + + @Override + public ServiceCall weatherStatsForToday() { + return (req) -> CompletableFuture.completedFuture(WeatherStats.forToday()); + } + +} diff --git a/lagom/weather-impl/src/main/java/org/baeldung/lagom/helloworld/weather/impl/WeatherServiceModule.java b/lagom/weather-impl/src/main/java/org/baeldung/lagom/helloworld/weather/impl/WeatherServiceModule.java new file mode 100644 index 0000000000..ac2834ff5c --- /dev/null +++ b/lagom/weather-impl/src/main/java/org/baeldung/lagom/helloworld/weather/impl/WeatherServiceModule.java @@ -0,0 +1,16 @@ +package org.baeldung.lagom.helloworld.weather.impl; + +import org.baeldung.lagom.helloworld.weather.api.WeatherService; + +import com.google.inject.AbstractModule; +import com.lightbend.lagom.javadsl.server.ServiceGuiceSupport; + +/** + * The module that binds the GreetingService so that it can be served. + */ +public class WeatherServiceModule extends AbstractModule implements ServiceGuiceSupport { + @Override + protected void configure() { + bindServices(serviceBinding(WeatherService.class, WeatherServiceImpl.class)); + } +} diff --git a/lagom/weather-impl/src/main/resources/application.conf b/lagom/weather-impl/src/main/resources/application.conf new file mode 100644 index 0000000000..cf6cec2115 --- /dev/null +++ b/lagom/weather-impl/src/main/resources/application.conf @@ -0,0 +1 @@ +play.modules.enabled += org.baeldung.lagom.helloworld.weather.impl.WeatherServiceModule \ No newline at end of file diff --git a/spring-rest/src/test/java/org/baeldung/client/TestRestTemplateBasicLiveTest.java b/spring-rest/src/test/java/org/baeldung/client/TestRestTemplateBasicLiveTest.java index 9f4319d857..a4d8eb0908 100644 --- a/spring-rest/src/test/java/org/baeldung/client/TestRestTemplateBasicLiveTest.java +++ b/spring-rest/src/test/java/org/baeldung/client/TestRestTemplateBasicLiveTest.java @@ -21,7 +21,7 @@ public class TestRestTemplateBasicLiveTest { private RestTemplate restTemplate; private static final String FOO_RESOURCE_URL = "http://localhost:" + APPLICATION_PORT + "/spring-rest/myfoos"; - private static final String URL_SECURED_BY_AUTHENTICATION = "http://browserspy.dk/password-ok.php"; + private static final String URL_SECURED_BY_AUTHENTICATION = "http://httpbin.org/basic-auth/user/passwd"; private static final String BASE_URL = "http://localhost:" + APPLICATION_PORT + "/spring-rest"; @Before @@ -55,16 +55,16 @@ public class TestRestTemplateBasicLiveTest { @Test public void givenRestTemplateWrapperWithCredentials_whenSendGetForEntity_thenStatusOk() { - TestRestTemplate testRestTemplate = new TestRestTemplate(restTemplate, "test", "test"); - ResponseEntity response = testRestTemplate.getForEntity(URL_SECURED_BY_AUTHENTICATION + "/1", + TestRestTemplate testRestTemplate = new TestRestTemplate(restTemplate, "user", "passwd"); + ResponseEntity response = testRestTemplate.getForEntity(URL_SECURED_BY_AUTHENTICATION, String.class); assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); } @Test public void givenTestRestTemplateWithCredentials_whenSendGetForEntity_thenStatusOk() { - TestRestTemplate testRestTemplate = new TestRestTemplate("test", "test"); - ResponseEntity response = testRestTemplate.getForEntity(URL_SECURED_BY_AUTHENTICATION + "/1", + TestRestTemplate testRestTemplate = new TestRestTemplate("user", "passwd"); + ResponseEntity response = testRestTemplate.getForEntity(URL_SECURED_BY_AUTHENTICATION, String.class); assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); } @@ -72,16 +72,16 @@ public class TestRestTemplateBasicLiveTest { @Test public void givenTestRestTemplateWithBasicAuth_whenSendGetForEntity_thenStatusOk() { TestRestTemplate testRestTemplate = new TestRestTemplate(); - ResponseEntity response = testRestTemplate.withBasicAuth("test", "test"). - getForEntity(URL_SECURED_BY_AUTHENTICATION + "/1", String.class); + ResponseEntity response = testRestTemplate.withBasicAuth("user", "passwd"). + getForEntity(URL_SECURED_BY_AUTHENTICATION, String.class); assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); } @Test public void givenTestRestTemplateWithCredentialsAndEnabledCookies_whenSendGetForEntity_thenStatusOk() { - TestRestTemplate testRestTemplate = new TestRestTemplate("test", "test", TestRestTemplate. + TestRestTemplate testRestTemplate = new TestRestTemplate("user", "passwd", TestRestTemplate. HttpClientOption.ENABLE_COOKIES); - ResponseEntity response = testRestTemplate.getForEntity(URL_SECURED_BY_AUTHENTICATION + "/1", + ResponseEntity response = testRestTemplate.getForEntity(URL_SECURED_BY_AUTHENTICATION, String.class); assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); } @@ -97,18 +97,17 @@ public class TestRestTemplateBasicLiveTest { // POST @Test public void givenService_whenPostForObject_thenCreatedObjectIsReturned() { - TestRestTemplate testRestTemplate = new TestRestTemplate("test", "test"); + TestRestTemplate testRestTemplate = new TestRestTemplate("user", "passwd"); final RequestBody body = RequestBody.create(okhttp3.MediaType.parse("text/html; charset=utf-8"), "{\"id\":1,\"name\":\"Jim\"}"); final Request request = new Request.Builder().url(BASE_URL + "/users/detail").post(body).build(); - Object response = testRestTemplate.postForObject(URL_SECURED_BY_AUTHENTICATION, request, String.class); - assertTrue(response.toString().contains("Success")); + testRestTemplate.postForObject(URL_SECURED_BY_AUTHENTICATION, request, String.class); } // PUT @Test public void givenService_whenPutForObject_thenCreatedObjectIsReturned() { - TestRestTemplate testRestTemplate = new TestRestTemplate("test", "test"); + TestRestTemplate testRestTemplate = new TestRestTemplate("user", "passwd"); final RequestBody body = RequestBody.create(okhttp3.MediaType.parse("text/html; charset=utf-8"), "{\"id\":1,\"name\":\"Jim\"}"); final Request request = new Request.Builder().url(BASE_URL + "/users/detail").post(body).build(); diff --git a/spring-state-machine/src/test/java/com/baeldung/spring/statemachine/ForkJoinStateMachineTest.java b/spring-state-machine/src/test/java/com/baeldung/spring/statemachine/ForkJoinStateMachineTest.java index 03cb101a9d..0de61da3bd 100644 --- a/spring-state-machine/src/test/java/com/baeldung/spring/statemachine/ForkJoinStateMachineTest.java +++ b/spring-state-machine/src/test/java/com/baeldung/spring/statemachine/ForkJoinStateMachineTest.java @@ -22,7 +22,7 @@ import static org.junit.Assert.assertTrue; public class ForkJoinStateMachineTest { @Resource - private StateMachine stateMachine; + private StateMachine stateMachine; @Before public void setUp() { diff --git a/spring-state-machine/src/test/java/com/baeldung/spring/statemachine/HierarchicalStateMachineTest.java b/spring-state-machine/src/test/java/com/baeldung/spring/statemachine/HierarchicalStateMachineTest.java index 950414bb0e..76105368d8 100644 --- a/spring-state-machine/src/test/java/com/baeldung/spring/statemachine/HierarchicalStateMachineTest.java +++ b/spring-state-machine/src/test/java/com/baeldung/spring/statemachine/HierarchicalStateMachineTest.java @@ -21,7 +21,7 @@ import static org.junit.Assert.assertEquals; public class HierarchicalStateMachineTest { @Resource - private StateMachine stateMachine; + private StateMachine stateMachine; @Before public void setUp() { diff --git a/spring-state-machine/src/test/java/com/baeldung/spring/statemachine/JunctionStateMachineTest.java b/spring-state-machine/src/test/java/com/baeldung/spring/statemachine/JunctionStateMachineTest.java index 64930162fd..b91e6af5c5 100644 --- a/spring-state-machine/src/test/java/com/baeldung/spring/statemachine/JunctionStateMachineTest.java +++ b/spring-state-machine/src/test/java/com/baeldung/spring/statemachine/JunctionStateMachineTest.java @@ -19,7 +19,7 @@ import javax.annotation.Resource; public class JunctionStateMachineTest { @Resource - private StateMachine stateMachine; + private StateMachine stateMachine; @Before public void setUp() { diff --git a/spring-state-machine/src/test/java/com/baeldung/spring/statemachine/StateEnumMachineTest.java b/spring-state-machine/src/test/java/com/baeldung/spring/statemachine/StateEnumMachineTest.java index b7cbebe145..cd9e58eb8e 100644 --- a/spring-state-machine/src/test/java/com/baeldung/spring/statemachine/StateEnumMachineTest.java +++ b/spring-state-machine/src/test/java/com/baeldung/spring/statemachine/StateEnumMachineTest.java @@ -23,7 +23,7 @@ import static org.junit.Assert.assertTrue; public class StateEnumMachineTest { @Resource - private StateMachine stateMachine; + private StateMachine stateMachine; @Before public void setUp() { diff --git a/spring-state-machine/src/test/java/com/baeldung/spring/statemachine/StateMachineIntegrationTest.java b/spring-state-machine/src/test/java/com/baeldung/spring/statemachine/StateMachineIntegrationTest.java index 8f61d93105..1c1ab58917 100644 --- a/spring-state-machine/src/test/java/com/baeldung/spring/statemachine/StateMachineIntegrationTest.java +++ b/spring-state-machine/src/test/java/com/baeldung/spring/statemachine/StateMachineIntegrationTest.java @@ -22,7 +22,7 @@ import javax.annotation.Resource; public class StateMachineIntegrationTest { @Resource - private StateMachine stateMachine; + private StateMachine stateMachine; @Before public void setUp() {