Search in sources :

Example 1 with JsonMapper

use of com.fasterxml.jackson.databind.json.JsonMapper in project spring-boot by spring-projects.

the class CompositeHealthTests method serializeV2WithJacksonAndDisabledCanOverrideAccessModifiersReturnsValidJson.

// gh-26797
@Test
void serializeV2WithJacksonAndDisabledCanOverrideAccessModifiersReturnsValidJson() throws Exception {
    Map<String, HealthComponent> components = new LinkedHashMap<>();
    components.put("db1", Health.up().build());
    components.put("db2", Health.down().withDetail("a", "b").build());
    CompositeHealth health = new CompositeHealth(ApiVersion.V2, Status.UP, components);
    JsonMapper mapper = JsonMapper.builder().disable(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS).build();
    String json = mapper.writeValueAsString(health);
    assertThat(json).isEqualTo("{\"status\":\"UP\",\"details\":{\"db1\":{\"status\":\"UP\"}," + "\"db2\":{\"status\":\"DOWN\",\"details\":{\"a\":\"b\"}}}}");
}
Also used : JsonMapper(com.fasterxml.jackson.databind.json.JsonMapper) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.jupiter.api.Test)

Example 2 with JsonMapper

use of com.fasterxml.jackson.databind.json.JsonMapper in project dhis2-core by dhis2.

the class ProgramStageInstanceSupplierTest method setUp.

@BeforeEach
void setUp() {
    JsonMapper mapper = new JsonMapper();
    this.subject = new ProgramStageInstanceSupplier(jdbcTemplate, mapper, programSupplier);
}
Also used : JsonMapper(com.fasterxml.jackson.databind.json.JsonMapper) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 3 with JsonMapper

use of com.fasterxml.jackson.databind.json.JsonMapper in project drools by kiegroup.

the class AnnotationsTest method testNextDays.

@Test
public void testNextDays() throws Exception {
    final DMNRuntime runtime = createRuntime("nextDays.dmn", this.getClass());
    final DMNModel dmnModel = runtime.getModel("https://kiegroup.org/dmn/_8A1F9719-02AA-4517-97D4-5C4F5D22FE82", "nextDays");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    DMNContext context = DMNFactory.newContext();
    if (!isTypeSafe()) {
        context.set("few dates", Arrays.asList(LocalDate.of(2019, 12, 31), LocalDate.of(2020, 2, 21)));
    } else {
        JsonMapper mapper = JsonMapper.builder().addModule(new JavaTimeModule()).build();
        final String JSON = "{\n" + "    \"few dates\": [ \"2019-12-31\", \"2020-02-21\" ]\n" + "}";
        Class<?> inputSetClass = getStronglyClassByName(dmnModel, "InputSet");
        FEELPropertyAccessible inputSet = (FEELPropertyAccessible) mapper.readValue(JSON, inputSetClass);
        context = new DMNContextFPAImpl(inputSet);
    }
    final DMNResult dmnResult = evaluateModel(runtime, dmnModel, context);
    LOG.debug("{}", dmnResult);
    assertThat(DMNRuntimeUtil.formatMessages(dmnResult.getMessages()), dmnResult.hasErrors(), is(false));
    assertThat(dmnResult.getDecisionResultByName("Decision-1").getResult(), is(Arrays.asList(LocalDate.of(2020, 1, 1), LocalDate.of(2020, 2, 22))));
    if (strongly) {
        Class<?> inputSetClass = getStronglyClassByName(dmnModel, "InputSet");
        Field directionAsField = inputSetClass.getDeclaredField("few_32dates");
        org.eclipse.microprofile.openapi.annotations.media.Schema annMP = directionAsField.getDeclaredAnnotation(org.eclipse.microprofile.openapi.annotations.media.Schema.class);
        Assertions.assertThat(annMP).isNotNull();
        Assertions.assertThat(annMP.type()).isEqualTo(org.eclipse.microprofile.openapi.annotations.enums.SchemaType.ARRAY);
        io.swagger.v3.oas.annotations.media.Schema annIOSwagger = directionAsField.getDeclaredAnnotation(io.swagger.v3.oas.annotations.media.Schema.class);
        Assertions.assertThat(annIOSwagger).isNotNull();
        Assertions.assertThat(annIOSwagger.type()).isEqualTo("array");
    }
}
Also used : DMNContextFPAImpl(org.kie.dmn.core.impl.DMNContextFPAImpl) DMNResult(org.kie.dmn.api.core.DMNResult) FEELPropertyAccessible(org.kie.dmn.api.core.FEELPropertyAccessible) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) DMNContext(org.kie.dmn.api.core.DMNContext) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) JsonMapper(com.fasterxml.jackson.databind.json.JsonMapper) Field(java.lang.reflect.Field) DMNModel(org.kie.dmn.api.core.DMNModel) BaseVariantTest(org.kie.dmn.core.BaseVariantTest) Test(org.junit.Test)

Example 4 with JsonMapper

use of com.fasterxml.jackson.databind.json.JsonMapper in project drools by kiegroup.

the class DMNRuntimeTypesTest method testOneOfEachType.

@Test
public void testOneOfEachType() throws Exception {
    final DMNRuntime runtime = createRuntime("OneOfEachType.dmn", this.getClass());
    final DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/definitions/_4f5608e9-4d74-4c22-a47e-ab657257fc9c", "OneOfEachType");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    DMNContext context = DMNFactory.newContext();
    if (!isTypeSafe()) {
        context.set("InputString", "John Doe");
        context.set("InputNumber", BigDecimal.ONE);
        context.set("InputBoolean", true);
        context.set("InputDTDuration", Duration.parse("P1D"));
        context.set("InputYMDuration", Period.parse("P1M"));
        context.set("InputDateAndTime", LocalDateTime.of(2020, 4, 2, 9, 0));
        context.set("InputDate", LocalDate.of(2020, 4, 2));
        context.set("InputTime", LocalTime.of(9, 0));
    } else {
        JsonMapper mapper = JsonMapper.builder().addModule(new JavaTimeModule()).build();
        final String JSON = "{\n" + "    \"InputBoolean\": true,\n" + "    \"InputDTDuration\": \"P1D\",\n" + "    \"InputDate\": \"2020-04-02\",\n" + "    \"InputDateAndTime\": \"2020-04-02T09:00:00\",\n" + "    \"InputNumber\": 1,\n" + "    \"InputString\": \"John Doe\",\n" + "    \"InputTime\": \"09:00\",\n" + "    \"InputYMDuration\": \"P1M\"\n" + "}";
        Class<?> inputSetClass = getStronglyClassByName(dmnModel, "InputSet");
        FEELPropertyAccessible inputSet = (FEELPropertyAccessible) mapper.readValue(JSON, inputSetClass);
        context = new DMNContextFPAImpl(inputSet);
    }
    final DMNResult dmnResult = evaluateModel(runtime, dmnModel, context);
    LOG.debug("{}", dmnResult);
    assertThat(DMNRuntimeUtil.formatMessages(dmnResult.getMessages()), dmnResult.hasErrors(), is(false));
    assertThat(dmnResult.getDecisionResultByName("DecisionString").getResult(), is("Hello, John Doe"));
    assertThat(dmnResult.getDecisionResultByName("DecisionNumber").getResult(), is(new BigDecimal(2)));
    assertThat(dmnResult.getDecisionResultByName("DecisionBoolean").getResult(), is(false));
    assertThat(dmnResult.getDecisionResultByName("DecisionDTDuration").getResult(), is(Duration.parse("P2D")));
    assertThat(dmnResult.getDecisionResultByName("DecisionYMDuration").getResult(), is(ComparablePeriod.parse("P2M")));
    assertThat(dmnResult.getDecisionResultByName("DecisionDateAndTime").getResult(), is(LocalDateTime.of(2020, 4, 2, 10, 0)));
    assertThat(dmnResult.getDecisionResultByName("DecisionDate").getResult(), is(LocalDate.of(2020, 4, 3)));
    assertThat(dmnResult.getDecisionResultByName("DecisionTime").getResult(), is(LocalTime.of(10, 0)));
    if (isTypeSafe()) {
        FEELPropertyAccessible outputSet = ((DMNContextFPAImpl) dmnResult.getContext()).getFpa();
        Map<String, Object> allProperties = outputSet.allFEELProperties();
        assertThat(allProperties.get("DecisionString"), is("Hello, John Doe"));
        assertThat(allProperties.get("DecisionNumber"), is(new BigDecimal(2)));
        assertThat(allProperties.get("DecisionBoolean"), is(false));
        assertThat(allProperties.get("DecisionDTDuration"), is(Duration.parse("P2D")));
        assertThat(allProperties.get("DecisionYMDuration"), is(ComparablePeriod.parse("P2M")));
        assertThat(allProperties.get("DecisionDateAndTime"), is(LocalDateTime.of(2020, 4, 2, 10, 0)));
        assertThat(allProperties.get("DecisionDate"), is(LocalDate.of(2020, 4, 3)));
        assertThat(allProperties.get("DecisionTime"), is(LocalTime.of(10, 0)));
    }
}
Also used : DMNContextFPAImpl(org.kie.dmn.core.impl.DMNContextFPAImpl) DMNResult(org.kie.dmn.api.core.DMNResult) FEELPropertyAccessible(org.kie.dmn.api.core.FEELPropertyAccessible) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) DMNContext(org.kie.dmn.api.core.DMNContext) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) BigDecimal(java.math.BigDecimal) JsonMapper(com.fasterxml.jackson.databind.json.JsonMapper) DMNModel(org.kie.dmn.api.core.DMNModel) BaseVariantTest(org.kie.dmn.core.BaseVariantTest) DMNRuntimeTest(org.kie.dmn.core.DMNRuntimeTest) Test(org.junit.Test) DMNDecisionServicesTest(org.kie.dmn.core.v1_2.DMNDecisionServicesTest)

Example 5 with JsonMapper

use of com.fasterxml.jackson.databind.json.JsonMapper in project cloudstack by apache.

the class ScaleIOGatewayClientImpl method post.

private <T> T post(final String path, final Object obj, final Class<T> type, final boolean renewAndRetryOnAuthFailure) {
    renewClientSessionOnExpiry();
    HttpResponse response = null;
    boolean responseConsumed = false;
    try {
        // wait for authentication request (if any) to complete (and to pick the new session key)
        while (authenticating) ;
        final HttpPost request = new HttpPost(apiURI.toString() + path);
        request.setHeader(HttpHeaders.AUTHORIZATION, "Basic " + Base64.getEncoder().encodeToString((this.username + ":" + this.sessionKey).getBytes()));
        request.setHeader("Content-type", "application/json");
        if (obj != null) {
            if (obj instanceof String) {
                request.setEntity(new StringEntity((String) obj));
            } else {
                JsonMapper mapper = new JsonMapper();
                mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
                String json = mapper.writer().writeValueAsString(obj);
                request.setEntity(new StringEntity(json));
            }
        }
        LOG.debug("Sending POST request: " + request.toString());
        response = httpClient.execute(request);
        String responseStatus = (!isNullResponse(response)) ? (response.getStatusLine().getStatusCode() + " " + response.getStatusLine().getReasonPhrase()) : "nil";
        LOG.debug("Received response: " + responseStatus + ", for the sent POST request: " + request.toString());
        if (checkAuthFailure(response, renewAndRetryOnAuthFailure)) {
            EntityUtils.consumeQuietly(response.getEntity());
            responseConsumed = true;
            authenticate();
            return post(path, obj, type, false);
        }
        return processResponse(response, type);
    } catch (final IOException e) {
        LOG.error("Failed in POST method due to: " + e.getMessage() + getConnectionManagerStats(), e);
        checkResponseTimeOut(e);
    } finally {
        if (!responseConsumed && response != null) {
            EntityUtils.consumeQuietly(response.getEntity());
        }
    }
    return null;
}
Also used : JsonMapper(com.fasterxml.jackson.databind.json.JsonMapper) HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException)

Aggregations

JsonMapper (com.fasterxml.jackson.databind.json.JsonMapper)5 JavaTimeModule (com.fasterxml.jackson.datatype.jsr310.JavaTimeModule)2 Test (org.junit.Test)2 DMNContext (org.kie.dmn.api.core.DMNContext)2 DMNModel (org.kie.dmn.api.core.DMNModel)2 DMNResult (org.kie.dmn.api.core.DMNResult)2 DMNRuntime (org.kie.dmn.api.core.DMNRuntime)2 FEELPropertyAccessible (org.kie.dmn.api.core.FEELPropertyAccessible)2 BaseVariantTest (org.kie.dmn.core.BaseVariantTest)2 DMNContextFPAImpl (org.kie.dmn.core.impl.DMNContextFPAImpl)2 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 BigDecimal (java.math.BigDecimal)1 LinkedHashMap (java.util.LinkedHashMap)1 HttpResponse (org.apache.http.HttpResponse)1 HttpPost (org.apache.http.client.methods.HttpPost)1 StringEntity (org.apache.http.entity.StringEntity)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 Test (org.junit.jupiter.api.Test)1 DMNRuntimeTest (org.kie.dmn.core.DMNRuntimeTest)1