Search in sources :

Example 71 with Json

use of org.infinispan.commons.dataconversion.internal.Json in project infinispan by infinispan.

the class ComplexValue method testValueInMultipleFormats.

@Test
public void testValueInMultipleFormats() throws Exception {
    remoteCache.clear();
    String quote = "I find your lack of faith disturbing";
    byte[] protostreamMarshalledQuote = marshall(quote);
    // Write to the cache using the default marshaller
    remoteCache.put(1, quote);
    // Read it back as raw bytes using the same key
    Object asBinary = remoteCache.withDataFormat(DataFormat.builder().valueMarshaller(IdentityMarshaller.INSTANCE).build()).get(1);
    assertArrayEquals(protostreamMarshalledQuote, ((byte[]) asBinary));
    // Read it back as UTF-8 byte[] using the same key
    Object asUTF8 = remoteCache.withDataFormat(DataFormat.builder().valueType(TEXT_PLAIN).valueMarshaller(IdentityMarshaller.INSTANCE).build()).get(1);
    assertArrayEquals(quote.getBytes(UTF_8), (byte[]) asUTF8);
    // Read it back as String using the default marshaller for text/plain
    Object asString = remoteCache.withDataFormat(DataFormat.builder().valueType(TEXT_PLAIN).build()).get(1);
    assertEquals(quote, asString);
    // Same, but with metadata
    MetadataValue<Object> metadataValue = remoteCache.withDataFormat(DataFormat.builder().valueType(TEXT_PLAIN).build()).getWithMetadata(1);
    assertEquals(quote, metadataValue.getValue());
    // Get all entries avoiding de-serialization
    Map<Object, Object> allEntries = remoteCache.withDataFormat(DataFormat.builder().valueMarshaller(IdentityMarshaller.INSTANCE).build()).getAll(new HashSet<>(Collections.singletonList(1)));
    assertArrayEquals(protostreamMarshalledQuote, (byte[]) allEntries.get(1));
    // Read value as JSON in the byte[] form, using the same key
    Json expectedJson = Json.object("_type", "string").set("_value", quote);
    Object asJSon = remoteCache.withDataFormat(DataFormat.builder().valueType(APPLICATION_JSON).build()).get(1);
    assertEquals(expectedJson, Json.read(new String((byte[]) asJSon)));
    Json asJsonNode = (Json) remoteCache.withDataFormat(DataFormat.builder().valueType(APPLICATION_JSON).valueMarshaller(new JsonMarshaller()).build()).get(1);
    assertEquals(expectedJson, asJsonNode);
    // Iterate values without unmarshalling
    Object raw = remoteCache.withDataFormat(DataFormat.builder().valueType(APPLICATION_PROTOSTREAM).valueMarshaller(IdentityMarshaller.INSTANCE).build()).values().iterator().next();
    assertArrayEquals(protostreamMarshalledQuote, ((byte[]) raw));
    // Iterate values converting to JsonNode objects
    asJsonNode = (Json) remoteCache.withDataFormat(DataFormat.builder().valueType(APPLICATION_JSON).valueMarshaller(new JsonMarshaller()).build()).values().iterator().next();
    assertEquals(expectedJson, asJsonNode);
}
Also used : Json(org.infinispan.commons.dataconversion.internal.Json) Test(org.testng.annotations.Test) SingleHotRodServerTest(org.infinispan.client.hotrod.test.SingleHotRodServerTest)

Example 72 with Json

use of org.infinispan.commons.dataconversion.internal.Json in project infinispan by infinispan.

the class RestCacheManagerClientOkHttp method restore.

@Override
public CompletionStage<RestResponse> restore(String name, File backup, Map<String, List<String>> resources) {
    Json json = resources != null ? Json.factory().make(resources) : Json.object();
    RequestBody zipBody = new FileRestEntityOkHttp(MediaType.APPLICATION_ZIP, backup).toRequestBody();
    RequestBody multipartBody = new MultipartBody.Builder().addFormDataPart("resources", json.toString()).addFormDataPart("backup", backup.getName(), zipBody).setType(MultipartBody.FORM).build();
    Request.Builder builder = restore(name).post(multipartBody);
    return client.execute(builder);
}
Also used : Request(okhttp3.Request) Json(org.infinispan.commons.dataconversion.internal.Json) RequestBody(okhttp3.RequestBody)

Example 73 with Json

use of org.infinispan.commons.dataconversion.internal.Json in project infinispan by infinispan.

the class BaseJsonTest method writeCurrencyViaJson.

private void writeCurrencyViaJson(String key, String description, int rank) {
    Json currency = Json.object();
    currency.set(TYPE, getJsonType());
    currency.set("description", description);
    currency.set("rank", rank);
    RestEntity value = RestEntity.create(MediaType.APPLICATION_JSON, currency.toString());
    RestResponse response = join(restCacheClient.put(key, value));
    assertEquals(response.getStatus(), 204);
}
Also used : RestEntity(org.infinispan.client.rest.RestEntity) RestResponse(org.infinispan.client.rest.RestResponse) Json(org.infinispan.commons.dataconversion.internal.Json)

Example 74 with Json

use of org.infinispan.commons.dataconversion.internal.Json in project infinispan by infinispan.

the class BaseJsonTest method readCurrencyViaJson.

private CryptoCurrency readCurrencyViaJson(String key) {
    RestResponse response = join(restCacheClient.get(key, MediaType.APPLICATION_JSON_TYPE));
    String json = response.getBody();
    Json jsonNode = Json.read(json);
    Json description = jsonNode.at("description");
    Json rank = jsonNode.at("rank");
    return new CryptoCurrency(description.asString(), rank.asInteger());
}
Also used : RestResponse(org.infinispan.client.rest.RestResponse) Json(org.infinispan.commons.dataconversion.internal.Json)

Example 75 with Json

use of org.infinispan.commons.dataconversion.internal.Json in project infinispan by infinispan.

the class PatchInfo method fromJson.

public static PatchInfo fromJson(Json json) {
    Json brandName = json.at(BRAND_NAME);
    Json sourceVersion = json.at(SOURCE_VERSION);
    Json targetVersion = json.at(TARGET_VERSION);
    Json qualifier = json.at(QUALIFIER);
    Json creationDate = json.at(CREATION_DATE);
    Json installationDate = json.at(INSTALLATION_DATE);
    Json operations = json.at(OPERATIONS);
    String brandValue = brandName != null ? brandName.asString() : null;
    String sourceValue = sourceVersion != null ? sourceVersion.asString() : null;
    String targetValue = targetVersion != null ? targetVersion.asString() : null;
    String qualifierValue = qualifier != null ? qualifier.asString() : null;
    Date creationValue = creationDate != null ? creationDate.isNull() ? null : new Date(creationDate.asLong()) : null;
    Date installationValue = installationDate != null ? installationDate.isNull() ? null : new Date(installationDate.asLong()) : null;
    List<PatchOperation> operationsValue = operations != null ? operations.asJsonList().stream().map(PatchOperation::fromJson).collect(Collectors.toList()) : null;
    return new PatchInfo(brandValue, sourceValue, targetValue, qualifierValue, creationValue, installationValue, operationsValue);
}
Also used : Json(org.infinispan.commons.dataconversion.internal.Json) Date(java.util.Date)

Aggregations

Json (org.infinispan.commons.dataconversion.internal.Json)130 RestResponse (org.infinispan.client.rest.RestResponse)51 Test (org.testng.annotations.Test)51 RestClient (org.infinispan.client.rest.RestClient)15 Util.getResourceAsString (org.infinispan.commons.util.Util.getResourceAsString)13 Test (org.junit.Test)13 RestCacheClient (org.infinispan.client.rest.RestCacheClient)12 MultipleCacheManagersTest (org.infinispan.test.MultipleCacheManagersTest)12 AbstractMultipleSitesTest (org.infinispan.xsite.AbstractMultipleSitesTest)7 ArrayList (java.util.ArrayList)6 List (java.util.List)6 Map (java.util.Map)6 NettyRestResponse (org.infinispan.rest.NettyRestResponse)6 ResourceUtil.addEntityAsJson (org.infinispan.rest.resources.ResourceUtil.addEntityAsJson)6 HashMap (java.util.HashMap)5 RestEntity (org.infinispan.client.rest.RestEntity)5 IOException (java.io.IOException)4 HashSet (java.util.HashSet)4 Request (okhttp3.Request)4 RestSchemaClient (org.infinispan.client.rest.RestSchemaClient)4