Search in sources :

Example 1 with ValueInstantiationException

use of com.fasterxml.jackson.databind.exc.ValueInstantiationException in project ksql by confluentinc.

the class CommandTest method shouldThrowExceptionWhenCommandVersionHigher.

@Test
public void shouldThrowExceptionWhenCommandVersionHigher() {
    final String commandStr = "{" + "\"statement\": \"test statement;\", " + "\"streamsProperties\": {\"foo\": \"bar\"}, " + "\"originalProperties\": {\"biz\": \"baz\"}, " + "\"version\": " + (Command.VERSION + 1) + "}";
    final ObjectMapper mapper = PlanJsonMapper.INSTANCE.get();
    final ValueInstantiationException thrown = assertThrows("Expected deserialization to throw, but it didn't", ValueInstantiationException.class, () -> mapper.readValue(commandStr, Command.class));
    assertTrue(thrown.getCause() instanceof IncompatibleKsqlCommandVersionException);
}
Also used : ValueInstantiationException(com.fasterxml.jackson.databind.exc.ValueInstantiationException) IncompatibleKsqlCommandVersionException(io.confluent.ksql.rest.server.resources.IncompatibleKsqlCommandVersionException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 2 with ValueInstantiationException

use of com.fasterxml.jackson.databind.exc.ValueInstantiationException in project ambry by linkedin.

the class AccountContainerTest method testAccountAndContainerSerDeWithoutIdAndName.

@Test
public void testAccountAndContainerSerDeWithoutIdAndName() throws IOException {
    assumeTrue(Container.getCurrentJsonVersion() == JSON_VERSION_2);
    // remove account id
    JSONObject newRefAccountJson = new JSONObject(refAccountJson, JSONObject.getNames(refAccountJson));
    newRefAccountJson.remove(ACCOUNT_ID_KEY);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try (Writer writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8)) {
        newRefAccountJson.write(writer);
    }
    try {
        objectMapper.readValue(outputStream.toByteArray(), Account.class);
        fail("Missing account id should fail");
    } catch (ValueInstantiationException e) {
        assertTrue(e.getCause() instanceof IllegalStateException);
    }
    newRefAccountJson = new JSONObject(refAccountJson, JSONObject.getNames(refAccountJson));
    newRefAccountJson.remove(ACCOUNT_NAME_KEY);
    outputStream = new ByteArrayOutputStream();
    try (Writer writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8)) {
        newRefAccountJson.write(writer);
    }
    try {
        objectMapper.readValue(outputStream.toByteArray(), Account.class);
        fail("Missing account name should fail");
    } catch (ValueInstantiationException e) {
        assertTrue(e.getCause() instanceof IllegalStateException);
    }
    newRefAccountJson = new JSONObject(refAccountJson, JSONObject.getNames(refAccountJson));
    newRefAccountJson.getJSONArray(CONTAINERS_KEY).getJSONObject(0).remove(CONTAINER_ID_KEY);
    outputStream = new ByteArrayOutputStream();
    try (Writer writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8)) {
        newRefAccountJson.write(writer);
    }
    try {
        objectMapper.readValue(outputStream.toByteArray(), Account.class);
        fail("Missing container id should fail");
    } catch (ValueInstantiationException e) {
        assertTrue(e.getCause() instanceof IllegalStateException);
    }
    newRefAccountJson = new JSONObject(refAccountJson, JSONObject.getNames(refAccountJson));
    newRefAccountJson.getJSONArray(CONTAINERS_KEY).getJSONObject(0).remove(CONTAINER_NAME_KEY);
    outputStream = new ByteArrayOutputStream();
    try (Writer writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8)) {
        newRefAccountJson.write(writer);
    }
    try {
        objectMapper.readValue(outputStream.toByteArray(), Account.class);
        fail("Missing container name should fail");
    } catch (ValueInstantiationException e) {
        assertTrue(e.getCause() instanceof IllegalStateException);
    }
}
Also used : JSONObject(org.json.JSONObject) ValueInstantiationException(com.fasterxml.jackson.databind.exc.ValueInstantiationException) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) Test(org.junit.Test)

Aggregations

ValueInstantiationException (com.fasterxml.jackson.databind.exc.ValueInstantiationException)2 Test (org.junit.Test)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IncompatibleKsqlCommandVersionException (io.confluent.ksql.rest.server.resources.IncompatibleKsqlCommandVersionException)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 JSONObject (org.json.JSONObject)1