Skip to content

new, better api #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
io.codearte.accurest.dsl.GroovyDsl.make {
request {
method 'POST'
url '/ingredients'
headers {
header 'Content-Type': 'application/vnd.pl.devoxx.aggregatr.v1+json'
}
body('''
{ "items" : ["HOP"] }
''')
}
response {
status 200
body(
ingredients: [
[type: 'HOP', quantity: 200]
]
)
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
io.codearte.accurest.dsl.GroovyDsl.make {
request {
method 'GET'
method 'POST'
url '/ingredients'
headers {
header 'Content-Type': 'application/vnd.pl.devoxx.aggregatr.v1+json'
}
body('''
{ "items" : ["MALT","WATER","HOP","YIEST"] }
''')
}
response {
status 200
body(
ingredients: [
[type: 'MALT', quantity: 100],
[type: 'MALT', quantity: 200],
[type: 'WATER', quantity: 200],
[type: 'HOP', quantity: 300],
[type: 'YIEST', quantity: 400]
[type: 'HOP', quantity: 200],
[type: 'YIEST', quantity: 200]
]
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
io.codearte.accurest.dsl.GroovyDsl.make {
request {
method 'POST'
url '/ingredients'
headers {
header 'Content-Type': 'application/vnd.pl.devoxx.aggregatr.v1+json'
}
body('''
{ "items" : ["MALT"] }
''')
}
response {
status 200
body(
ingredients: [
[type: 'MALT', quantity: 200]
]
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
io.codearte.accurest.dsl.GroovyDsl.make {
request {
method 'POST'
url '/ingredients'
headers {
header 'Content-Type': 'application/vnd.pl.devoxx.aggregatr.v1+json'
}
body('''
{ "items" : ["WATER"] }
''')
}
response {
status 200
body(
ingredients: [
[type: 'WATER', quantity: 200]
]
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
io.codearte.accurest.dsl.GroovyDsl.make {
request {
method 'POST'
url '/ingredients'
headers {
header 'Content-Type': 'application/vnd.pl.devoxx.aggregatr.v1+json'
}
body('''
{ "items" : ["YIEST"] }
''')
}
response {
status 200
body(
ingredients: [
[type: 'YIEST', quantity: 200]
]
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import pl.devoxx.aggregatr.aggregation.model.Ingredient;
import pl.devoxx.aggregatr.aggregation.model.IngredientType;
import pl.devoxx.aggregatr.aggregation.model.Ingredients;
import pl.devoxx.aggregatr.aggregation.model.Order;

import java.lang.invoke.MethodHandles;
import java.util.Arrays;
Expand Down Expand Up @@ -51,14 +52,14 @@ private String getMetricName(IngredientType ingredientType) {
return "ingredients." + ingredientType.toString().toLowerCase();
}

Ingredients fetchIngredients() {
final List<ListenableFuture<Ingredient>> futures = ingredientsProperties
.getListOfServiceNames()
Ingredients fetchIngredients(Order order) {
List<ListenableFuture<Ingredient>> futures = ingredientsProperties
.getListOfServiceNames(order)
.stream()
.map(ingredientHarvester::harvest)
.collect(Collectors.toList());
final ListenableFuture<List<Ingredient>> allDoneFuture = Futures.allAsList(futures);
final List<Ingredient> allIngredients = Futures.getUnchecked(allDoneFuture);
ListenableFuture<List<Ingredient>> allDoneFuture = Futures.allAsList(futures);
List<Ingredient> allIngredients = Futures.getUnchecked(allDoneFuture);
allIngredients.forEach(this::updateIngredientCache);
return getIngredientsStatus();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import pl.devoxx.aggregatr.aggregation.model.Ingredients;
import pl.devoxx.aggregatr.aggregation.model.Order;
import pl.devoxx.aggregatr.aggregation.model.Version;

@RestController
@RequestMapping("/ingredients")
@RequestMapping(value = "/ingredients", consumes = Version.V1, produces = MediaType.APPLICATION_JSON_VALUE)
public class IngredientsController {

private final IngredientsAggregator ingredientsAggregator;
Expand All @@ -18,9 +21,9 @@ public IngredientsController(IngredientsAggregator ingredientsAggregator) {
this.ingredientsAggregator = ingredientsAggregator;
}

@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
public Ingredients distributeIngredients() {
return ingredientsAggregator.fetchIngredients();
@RequestMapping(method = RequestMethod.POST)
public Ingredients distributeIngredients(@RequestBody Order order) {
return ingredientsAggregator.fetchIngredients(order);
}

}
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
package pl.devoxx.aggregatr.aggregation;

import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableMap;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import pl.devoxx.aggregatr.aggregation.model.IngredientType;
import pl.devoxx.aggregatr.aggregation.model.Order;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@ConfigurationProperties("ingredients")
@Data
public class IngredientsProperties {

private String serviceNames = "chmieleo,slodeo,wodeo,drozdzeo";
private Map<IngredientType, String> serviceNames = ImmutableMap.<IngredientType, String>builder()
.put(IngredientType.WATER, "wodeo")
.put(IngredientType.MALT, "slodeo")
.put(IngredientType.HOP, "chmieleo")
.put(IngredientType.YIEST, "drozdzeo")
.build();
private String rootUrl = "http://localhost:8030/";
private Integer threshold = 1000;

public List<String> getListOfServiceNames() {
return Splitter.on(',').omitEmptyStrings().trimResults().splitToList(serviceNames);
public List<String> getListOfServiceNames(Order order) {
return serviceNames.entrySet()
.stream()
.filter((entry -> order.getItems().contains(entry.getKey())))
.map((Map.Entry::getValue))
.collect(Collectors.toList());
}
}
11 changes: 11 additions & 0 deletions src/main/java/pl/devoxx/aggregatr/aggregation/model/Order.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package pl.devoxx.aggregatr.aggregation.model;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;

@Data
public class Order {
private List<IngredientType> items = new ArrayList<>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package pl.devoxx.aggregatr.aggregation.model;

public class Version {
public static final String V1 = "application/vnd.pl.devoxx.aggregatr.v1+json";
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package pl.devoxx.aggregatr.acceptance

import groovy.json.JsonSlurper
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.web.servlet.MvcResult
import pl.devoxx.aggregatr.aggregation.model.Version
import pl.devoxx.aggregatr.base.MicroserviceMvcWiremockSpec

import static java.net.URI.create
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print

@ContextConfiguration
Expand All @@ -20,7 +20,11 @@ class AcceptanceSpec extends MicroserviceMvcWiremockSpec {
}

private MvcResult getting_ingredients() {
return mockMvc.perform(get(create('/ingredients'))).andDo(print()).andReturn()
return mockMvc.perform(post(create('/ingredients'))
.header('Content-Type', Version.V1)
.content('{"items":["WATER","HOP","YIEST","MALT"]}'))
.andDo(print())
.andReturn()
}

private void aggregated_ingredients_are_present(MvcResult result) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package pl.devoxx.aggregatr.aggregation
import com.jayway.restassured.module.mockmvc.RestAssuredMockMvc
import pl.devoxx.aggregatr.aggregation.model.Ingredient
import pl.devoxx.aggregatr.aggregation.model.IngredientType
import pl.devoxx.aggregatr.aggregation.model.Ingredients
import pl.devoxx.aggregatr.aggregation.model.Order
import spock.lang.Specification

abstract class BaseMockMvcSpec extends Specification {

protected static final int QUANTITY = 200

IngredientsAggregator ingredientsAggregator = Stub()

def setup() {
Expand All @@ -15,12 +17,9 @@ abstract class BaseMockMvcSpec extends Specification {
}

void setupMocks() {
ingredientsAggregator.fetchIngredients() >> new Ingredients([
new Ingredient(IngredientType.MALT, 100),
new Ingredient(IngredientType.WATER, 200),
new Ingredient(IngredientType.HOP, 300),
new Ingredient(IngredientType.YIEST, 400)
])
ingredientsAggregator.fetchIngredients(_) >> { Order order ->
return new Ingredients(order.items.collect { new Ingredient(it, QUANTITY)})
}
}

}
5 changes: 1 addition & 4 deletions src/test/resources/application-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,4 @@ stubrunner.stubs:
group: com.ofg
module: stub-runner-examples

stubrunner.work-offline: true

ingredients:
serviceNames: chmieleo,slodeo,wodeo,drozdzeo
stubrunner.work-offline: true