Search in sources :

Example 51 with TypeReference

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);
}
Also used : Response(com.jayway.restassured.response.Response) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Map(java.util.Map)

Example 52 with 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;
}
Also used : Response(com.jayway.restassured.response.Response) IntStream(java.util.stream.IntStream) EventCategory(org.zalando.nakadi.domain.EventCategory) DateTimeZone(org.joda.time.DateTimeZone) RequestSpecification(com.jayway.restassured.specification.RequestSpecification) RandomSubscriptionBuilder(org.zalando.nakadi.utils.RandomSubscriptionBuilder) SubscriptionEventTypeStats(org.zalando.nakadi.domain.SubscriptionEventTypeStats) HttpStatus(org.apache.http.HttpStatus) RestAssured.given(com.jayway.restassured.RestAssured.given) Subscription(org.zalando.nakadi.domain.Subscription) EnrichmentStrategyDescriptor(org.zalando.nakadi.domain.EnrichmentStrategyDescriptor) Response(com.jayway.restassured.response.Response) MessageFormat.format(java.text.MessageFormat.format) JSONObject(org.json.JSONObject) JsonConfig(org.zalando.nakadi.config.JsonConfig) ImmutableList(com.google.common.collect.ImmutableList) EventTypeStatistics(org.zalando.nakadi.domain.EventTypeStatistics) Map(java.util.Map) TypeReference(com.fasterxml.jackson.core.type.TypeReference) ItemsWrapper(org.zalando.nakadi.domain.ItemsWrapper) IntFunction(java.util.function.IntFunction) EventType(org.zalando.nakadi.domain.EventType) RestAssured.when(com.jayway.restassured.RestAssured.when) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DateTime(org.joda.time.DateTime) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) JSON(com.jayway.restassured.http.ContentType.JSON) UUID(java.util.UUID) EventTypeTestBuilder(org.zalando.nakadi.utils.EventTypeTestBuilder) Collectors(java.util.stream.Collectors) List(java.util.List) SubscriptionBase(org.zalando.nakadi.domain.SubscriptionBase) PartitionStrategy(org.zalando.nakadi.partitioning.PartitionStrategy) TimelineView(org.zalando.nakadi.view.TimelineView) SubscriptionCursor(org.zalando.nakadi.view.SubscriptionCursor) RestAssured(com.jayway.restassured.RestAssured) Assert(org.junit.Assert) OK(org.springframework.http.HttpStatus.OK) ContentType(com.jayway.restassured.http.ContentType) JSONArray(org.json.JSONArray) SubscriptionEventTypeStats(org.zalando.nakadi.domain.SubscriptionEventTypeStats) TypeReference(com.fasterxml.jackson.core.type.TypeReference) IOException(java.io.IOException)

Example 53 with TypeReference

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"));
}
Also used : HashMap(java.util.HashMap) TypeReference(com.fasterxml.jackson.core.type.TypeReference) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 54 with TypeReference

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"));
}
Also used : HashMap(java.util.HashMap) TypeReference(com.fasterxml.jackson.core.type.TypeReference) HashMap(java.util.HashMap) Map(java.util.Map) LinkedList(java.util.LinkedList) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 55 with TypeReference

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;
}
Also used : PoolDTO(org.candlepin.dto.rules.v1.PoolDTO) RuleExecutionException(org.candlepin.policy.js.RuleExecutionException) EntitlementDTO(org.candlepin.dto.rules.v1.EntitlementDTO) ConsumerDTO(org.candlepin.dto.rules.v1.ConsumerDTO) JsonJsContext(org.candlepin.policy.js.JsonJsContext) TypeReference(com.fasterxml.jackson.core.type.TypeReference) RuleExecutionException(org.candlepin.policy.js.RuleExecutionException) Map(java.util.Map)

Aggregations

TypeReference (com.fasterxml.jackson.core.type.TypeReference)316 IOException (java.io.IOException)130 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)113 Test (org.junit.Test)95 ArrayList (java.util.ArrayList)74 Map (java.util.Map)74 List (java.util.List)60 HashMap (java.util.HashMap)58 File (java.io.File)34 TextPageLink (org.thingsboard.server.common.data.page.TextPageLink)27 Collectors (java.util.stream.Collectors)25 InputStream (java.io.InputStream)23 JsonNode (com.fasterxml.jackson.databind.JsonNode)19 ImmutableMap (com.google.common.collect.ImmutableMap)19 lombok.val (lombok.val)18 Matchers.containsString (org.hamcrest.Matchers.containsString)17 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)14 Collections (java.util.Collections)13 ISE (org.apache.druid.java.util.common.ISE)12 URL (java.net.URL)10