use of com.fasterxml.jackson.core.type.TypeReference in project nakadi by zalando.
the class NakadiTestUtils method listTimelines.
public static List<Map> listTimelines(final String eventType) throws IOException {
final Response response = given().accept(JSON).get(format("/event-types/{0}/timelines", eventType));
final String data = response.print();
final TypeReference<List<Map>> typeReference = new TypeReference<List<Map>>() {
};
return MAPPER.readValue(data, typeReference);
}
use of com.fasterxml.jackson.core.type.TypeReference in project nakadi by zalando.
the class NakadiTestUtils method getNumberOfAssignedStreams.
public static int getNumberOfAssignedStreams(final String sid) {
final Response response = when().get("/subscriptions/{sid}/stats", sid).thenReturn();
final ItemsWrapper<SubscriptionEventTypeStats> statsItems;
try {
statsItems = MAPPER.readValue(response.print(), new TypeReference<ItemsWrapper<SubscriptionEventTypeStats>>() {
});
} catch (final IOException e) {
throw new AssertionError("Failed to get stats", e);
}
final long assignedUniqueStreamsCount = statsItems.getItems().stream().flatMap(stat -> stat.getPartitions().stream()).filter(p -> "assigned".equals(p.getState())).map(SubscriptionEventTypeStats.Partition::getStreamId).distinct().count();
return (int) assignedUniqueStreamsCount;
}
use of com.fasterxml.jackson.core.type.TypeReference in project candlepin by candlepin.
the class ProductCuratorTest method testWithSimpleJsonAttribute.
@Test
public void testWithSimpleJsonAttribute() throws Exception {
Map<String, String> data = new HashMap<>();
data.put("a", "1");
data.put("b", "2");
ObjectMapper mapper = new ObjectMapper();
String jsonData = mapper.writeValueAsString(data);
Product prod = new Product("cptest-label", "My Product");
prod.setAttribute("content_sets", jsonData);
productCurator.create(prod);
Product lookedUp = productCurator.find(prod.getUuid());
assertEquals(jsonData, lookedUp.getAttributeValue("content_sets"));
data = mapper.readValue(lookedUp.getAttributeValue("content_sets"), new TypeReference<Map<String, String>>() {
});
assertEquals("1", data.get("a"));
assertEquals("2", data.get("b"));
}
use of com.fasterxml.jackson.core.type.TypeReference in project candlepin by candlepin.
the class ProductCuratorTest method testJsonListOfHashes.
@Test
public void testJsonListOfHashes() throws Exception {
List<Map<String, String>> data = new LinkedList<>();
Map<String, String> contentSet1 = new HashMap<>();
contentSet1.put("name", "cs1");
contentSet1.put("url", "url");
Map<String, String> contentSet2 = new HashMap<>();
contentSet2.put("name", "cs2");
contentSet2.put("url", "url2");
data.add(contentSet1);
data.add(contentSet2);
ObjectMapper mapper = new ObjectMapper();
String jsonData = mapper.writeValueAsString(data);
Product prod = TestUtil.createProduct("cptest-label", "My Product");
prod.setAttribute("content_sets", jsonData);
productCurator.create(prod);
Product lookedUp = productCurator.find(prod.getUuid());
assertEquals(jsonData, lookedUp.getAttributeValue("content_sets"));
data = mapper.readValue(lookedUp.getAttributeValue("content_sets"), new TypeReference<List<Map<String, String>>>() {
});
Map<String, String> cs1 = data.get(0);
assertEquals("cs1", cs1.get("name"));
Map<String, String> cs2 = data.get(1);
assertEquals("cs2", cs2.get("name"));
}
use of com.fasterxml.jackson.core.type.TypeReference in project candlepin by candlepin.
the class QuantityRules method getSuggestedQuantities.
/**
* Calculates the suggested quantities for many pools in one call. This allows for
* performant list pools queries with large numbers of pools and a large amount of
* entitlements to serialize.
*
* Map returned will map each pool ID to the suggested quantities for it. Every pool
* provided should have it's ID present in the result.
*
* @param pools
* @param c
* @param date
* @return suggested quantities for all pools requested
*/
@SuppressWarnings("checkstyle:indentation")
public Map<String, SuggestedQuantity> getSuggestedQuantities(List<Pool> pools, Consumer c, Date date) {
JsonJsContext args = new JsonJsContext(mapper);
Stream<PoolDTO> poolStream = pools == null ? Stream.empty() : pools.stream().map(this.translator.getStreamMapper(Pool.class, PoolDTO.class));
Stream<EntitlementDTO> entStream = c.getEntitlements() == null ? Stream.empty() : c.getEntitlements().stream().filter(ent -> ent.isValidOnDate(date)).map(this.translator.getStreamMapper(Entitlement.class, EntitlementDTO.class));
args.put("pools", poolStream.collect(Collectors.toSet()));
args.put("consumer", this.translator.translate(c, ConsumerDTO.class));
args.put("validEntitlements", entStream.collect(Collectors.toSet()));
args.put("log", log, false);
args.put("guestIds", c.getGuestIds());
String json = jsRules.runJsFunction(String.class, "get_suggested_quantities", args);
Map<String, SuggestedQuantity> resultMap;
TypeReference<Map<String, SuggestedQuantity>> typeref = new TypeReference<Map<String, SuggestedQuantity>>() {
};
try {
resultMap = mapper.toObject(json, typeref);
} catch (Exception e) {
throw new RuleExecutionException(e);
}
return resultMap;
}
Aggregations