Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
68b0c5327e
|
@ -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(){
|
||||
|
|
|
@ -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<List<String>> lol = new ArrayList<>();
|
||||
List<String> ls1 = Arrays.asList("one:one", "one:two", "one:three");
|
||||
List<String> ls2 = Arrays.asList("two:one", "two:two", "two:three");
|
||||
List<String> 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<List<String>> 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<String> 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<String> 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 <T> List<T> flattenListOfListsImperatively(List<List<T>> list) {
|
||||
|
|
|
@ -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
|
|
@ -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<GreetingCommand, GreetingEvent, GreetingState>
|
||||
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
|
||||
|
||||
|
||||
|
||||
|
|
@ -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))
|
|
@ -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<NotUsed, String> handleGreetFrom(String user);
|
||||
|
||||
@Override
|
||||
default Descriptor descriptor() {
|
||||
return named("greetingservice").withCalls(
|
||||
restCall(Method.GET, "/api/greeting/:fromUser", this::handleGreetFrom)
|
||||
).withAutoAcl(true);
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
play.modules.enabled += org.baeldung.lagom.helloworld.greeting.impl.GreetingServiceModule
|
|
@ -0,0 +1 @@
|
|||
play.modules.enabled += org.baeldung.lagom.helloworld.greeting.impl.GreetingServiceModule
|
|
@ -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<String> {
|
||||
private final String fromUser;
|
||||
|
||||
@JsonCreator
|
||||
public ReceivedGreetingCommand(String fromUser) {
|
||||
this.fromUser = Preconditions.checkNotNull(fromUser, "fromUser");
|
||||
}
|
||||
|
||||
public String getFromUser() {
|
||||
return fromUser;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -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<GreetingCommand,
|
||||
GreetingEvent, GreetingState> {
|
||||
|
||||
@Override
|
||||
public Behavior initialBehavior(Optional<GreetingState> 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();
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -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<NotUsed, String> handleGreetFrom(String user) {
|
||||
return request -> {
|
||||
// Look up the hello world entity for the given ID.
|
||||
PersistentEntityRef<GreetingCommand> ref = persistentEntityRegistry.refFor(GreetingEntity.class, user);
|
||||
CompletableFuture<String> greetingResponse = ref.ask(new ReceivedGreetingCommand(user))
|
||||
.toCompletableFuture();
|
||||
CompletableFuture<WeatherStats> todaysWeatherInfo =
|
||||
(CompletableFuture<WeatherStats>) 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");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
play.modules.enabled += org.baeldung.lagom.helloworld.greeting.impl.GreetingServiceModule
|
|
@ -0,0 +1 @@
|
|||
sbt.version=0.13.11
|
|
@ -0,0 +1,2 @@
|
|||
addSbtPlugin("com.lightbend.lagom" % "lagom-sbt-plugin" % "1.3.1")
|
||||
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "3.0.0")
|
|
@ -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<NotUsed, WeatherStats> weatherStatsForToday();
|
||||
|
||||
@Override
|
||||
default Descriptor descriptor() {
|
||||
return named("weatherservice").withCalls(
|
||||
restCall(Method.GET, "/api/weather", this::weatherStatsForToday)
|
||||
).withAutoAcl(true);
|
||||
}
|
||||
|
||||
}
|
|
@ -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<WeatherStats> 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;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
play.modules.enabled += org.baeldung.lagom.helloworld.weather.impl.WeatherServiceModule
|
|
@ -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<NotUsed, WeatherStats> weatherStatsForToday() {
|
||||
return (req) -> CompletableFuture.completedFuture(WeatherStats.forToday());
|
||||
}
|
||||
|
||||
}
|
|
@ -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));
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
play.modules.enabled += org.baeldung.lagom.helloworld.weather.impl.WeatherServiceModule
|
|
@ -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<String> response = testRestTemplate.getForEntity(URL_SECURED_BY_AUTHENTICATION + "/1",
|
||||
TestRestTemplate testRestTemplate = new TestRestTemplate(restTemplate, "user", "passwd");
|
||||
ResponseEntity<String> 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<String> response = testRestTemplate.getForEntity(URL_SECURED_BY_AUTHENTICATION + "/1",
|
||||
TestRestTemplate testRestTemplate = new TestRestTemplate("user", "passwd");
|
||||
ResponseEntity<String> 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<String> response = testRestTemplate.withBasicAuth("test", "test").
|
||||
getForEntity(URL_SECURED_BY_AUTHENTICATION + "/1", String.class);
|
||||
ResponseEntity<String> 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<String> response = testRestTemplate.getForEntity(URL_SECURED_BY_AUTHENTICATION + "/1",
|
||||
ResponseEntity<String> 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();
|
||||
|
|
|
@ -22,7 +22,7 @@ import static org.junit.Assert.assertTrue;
|
|||
public class ForkJoinStateMachineTest {
|
||||
|
||||
@Resource
|
||||
private StateMachine stateMachine;
|
||||
private StateMachine<String, String> stateMachine;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
|
|
@ -21,7 +21,7 @@ import static org.junit.Assert.assertEquals;
|
|||
public class HierarchicalStateMachineTest {
|
||||
|
||||
@Resource
|
||||
private StateMachine stateMachine;
|
||||
private StateMachine<String, String> stateMachine;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
|
|
@ -19,7 +19,7 @@ import javax.annotation.Resource;
|
|||
public class JunctionStateMachineTest {
|
||||
|
||||
@Resource
|
||||
private StateMachine stateMachine;
|
||||
private StateMachine<String, String> stateMachine;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
|
|
@ -23,7 +23,7 @@ import static org.junit.Assert.assertTrue;
|
|||
public class StateEnumMachineTest {
|
||||
|
||||
@Resource
|
||||
private StateMachine stateMachine;
|
||||
private StateMachine<ApplicationReviewStates, ApplicationReviewEvents> stateMachine;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
|
|
@ -22,7 +22,7 @@ import javax.annotation.Resource;
|
|||
public class StateMachineIntegrationTest {
|
||||
|
||||
@Resource
|
||||
private StateMachine stateMachine;
|
||||
private StateMachine<String, String> stateMachine;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
|
Loading…
Reference in New Issue