Search in sources :

Example 1 with TypeToken

use of co.cask.cdap.internal.guava.reflect.TypeToken in project cdap by caskdata.

the class ReflectionSchemaGenerator method generateRecord.

@Override
protected Schema generateRecord(TypeToken<?> typeToken, Set<String> knowRecords, boolean acceptRecursion) throws UnsupportedTypeException {
    String recordName = typeToken.getRawType().getName();
    Map<String, TypeToken<?>> recordFieldTypes = typeToken.getRawType().isInterface() ? collectByMethods(typeToken, new TreeMap<>()) : collectByFields(typeToken, new TreeMap<>());
    // Recursively generate field type schema.
    List<Schema.Field> fields = new ArrayList<>();
    for (Map.Entry<String, TypeToken<?>> fieldType : recordFieldTypes.entrySet()) {
        Set<String> records = new HashSet<>(knowRecords);
        records.add(recordName);
        Schema fieldSchema = doGenerate(fieldType.getValue(), records, acceptRecursion);
        if (!fieldType.getValue().getRawType().isPrimitive()) {
            boolean isNotNull = typeToken.getRawType().isAnnotationPresent(Nonnull.class);
            boolean isNull = typeToken.getRawType().isAnnotationPresent(Nullable.class);
            // ii) if it is not nullable by default and nullable annotation is present
            if ((isNullableByDefault && !isNotNull) || (!isNullableByDefault && isNull)) {
                fieldSchema = Schema.unionOf(fieldSchema, Schema.of(Schema.Type.NULL));
            }
        }
        fields.add(Schema.Field.of(fieldType.getKey(), fieldSchema));
    }
    return Schema.recordOf(recordName, Collections.unmodifiableList(fields));
}
Also used : Schema(co.cask.cdap.api.data.schema.Schema) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) Field(java.lang.reflect.Field) TypeToken(co.cask.cdap.internal.guava.reflect.TypeToken) TreeMap(java.util.TreeMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 2 with TypeToken

use of co.cask.cdap.internal.guava.reflect.TypeToken in project cdap by caskdata.

the class AbstractSpecificationCodec method deserializeSet.

protected final <V> Set<V> deserializeSet(JsonElement json, JsonDeserializationContext context, Class<V> valueType) {
    Type type = new TypeToken<Set<V>>() {
    }.where(new TypeParameter<V>() {
    }, valueType).getType();
    Set<V> set = context.deserialize(json, type);
    return set == null ? Collections.<V>emptySet() : set;
}
Also used : Type(java.lang.reflect.Type) TypeParameter(co.cask.cdap.internal.guava.reflect.TypeParameter) TypeToken(co.cask.cdap.internal.guava.reflect.TypeToken)

Example 3 with TypeToken

use of co.cask.cdap.internal.guava.reflect.TypeToken in project cdap by caskdata.

the class AbstractSpecificationCodec method deserializeList.

protected final <V> List<V> deserializeList(JsonElement json, JsonDeserializationContext context, Class<V> valueType) {
    Type type = new TypeToken<List<V>>() {
    }.where(new TypeParameter<V>() {
    }, valueType).getType();
    List<V> list = context.deserialize(json, type);
    return list == null ? Collections.<V>emptyList() : list;
}
Also used : Type(java.lang.reflect.Type) TypeParameter(co.cask.cdap.internal.guava.reflect.TypeParameter) TypeToken(co.cask.cdap.internal.guava.reflect.TypeToken)

Example 4 with TypeToken

use of co.cask.cdap.internal.guava.reflect.TypeToken in project cdap by caskdata.

the class AbstractSpecificationCodec method deserializeMap.

protected final <V> Map<String, V> deserializeMap(JsonElement json, JsonDeserializationContext context, Class<V> valueType) {
    Type type = new TypeToken<Map<String, V>>() {
    }.where(new TypeParameter<V>() {
    }, valueType).getType();
    Map<String, V> map = context.deserialize(json, type);
    return map == null ? Collections.<String, V>emptyMap() : map;
}
Also used : Type(java.lang.reflect.Type) TypeParameter(co.cask.cdap.internal.guava.reflect.TypeParameter) TypeToken(co.cask.cdap.internal.guava.reflect.TypeToken)

Example 5 with TypeToken

use of co.cask.cdap.internal.guava.reflect.TypeToken in project cdap by caskdata.

the class AdminAppTestRun method testAdminService.

@Test
public void testAdminService() throws Exception {
    // Start the service
    ServiceManager serviceManager = appManager.getServiceManager(AdminApp.SERVICE_NAME).start();
    try {
        URI serviceURI = serviceManager.getServiceURL(10, TimeUnit.SECONDS).toURI();
        // dataset nn should not exist
        HttpResponse response = HttpRequests.execute(HttpRequest.get(serviceURI.resolve("exists/nn").toURL()).build());
        Assert.assertEquals(200, response.getResponseCode());
        Assert.assertEquals("false", response.getResponseBodyAsString());
        // create nn as a table
        response = HttpRequests.execute(HttpRequest.put(serviceURI.resolve("create/nn/table").toURL()).build());
        Assert.assertEquals(200, response.getResponseCode());
        // now nn should exist
        response = HttpRequests.execute(HttpRequest.get(serviceURI.resolve("exists/nn").toURL()).build());
        Assert.assertEquals(200, response.getResponseCode());
        Assert.assertEquals("true", response.getResponseBodyAsString());
        // create it again as a fileset -> should fail with conflict
        response = HttpRequests.execute(HttpRequest.put(serviceURI.resolve("create/nn/fileSet").toURL()).build());
        Assert.assertEquals(409, response.getResponseCode());
        // get the type for xx -> not found
        response = HttpRequests.execute(HttpRequest.get(serviceURI.resolve("type/xx").toURL()).build());
        Assert.assertEquals(404, response.getResponseCode());
        // get the type for nn -> table
        response = HttpRequests.execute(HttpRequest.get(serviceURI.resolve("type/nn").toURL()).build());
        Assert.assertEquals(200, response.getResponseCode());
        Assert.assertEquals("table", response.getResponseBodyAsString());
        // update xx's properties -> should get not-found
        Map<String, String> nnProps = TableProperties.builder().setTTL(1000L).build().getProperties();
        response = HttpRequests.execute(HttpRequest.put(serviceURI.resolve("update/xx").toURL()).withBody(GSON.toJson(nnProps)).build());
        Assert.assertEquals(404, response.getResponseCode());
        // update nn's properties
        response = HttpRequests.execute(HttpRequest.put(serviceURI.resolve("update/nn").toURL()).withBody(GSON.toJson(nnProps)).build());
        Assert.assertEquals(200, response.getResponseCode());
        // get properties for xx -> not found
        response = HttpRequests.execute(HttpRequest.get(serviceURI.resolve("props/xx").toURL()).build());
        Assert.assertEquals(404, response.getResponseCode());
        // get properties for nn and validate
        response = HttpRequests.execute(HttpRequest.get(serviceURI.resolve("props/nn").toURL()).build());
        Assert.assertEquals(200, response.getResponseCode());
        Map<String, String> returnedProps = GSON.fromJson(response.getResponseBodyAsString(), new TypeToken<Map<String, String>>() {
        }.getType());
        Assert.assertEquals(nnProps, returnedProps);
        // write some data to the table
        DataSetManager<Table> nnManager = getDataset("nn");
        nnManager.get().put(new Put("x", "y", "z"));
        nnManager.flush();
        // in a new tx, validate that data is in table
        Assert.assertFalse(nnManager.get().get(new Get("x")).isEmpty());
        Assert.assertEquals("z", nnManager.get().get(new Get("x", "y")).getString("y"));
        nnManager.flush();
        // truncate xx -> not found
        response = HttpRequests.execute(HttpRequest.post(serviceURI.resolve("truncate/xx").toURL()).build());
        Assert.assertEquals(404, response.getResponseCode());
        // truncate nn
        response = HttpRequests.execute(HttpRequest.post(serviceURI.resolve("truncate/nn").toURL()).build());
        Assert.assertEquals(200, response.getResponseCode());
        // validate table is empty
        Assert.assertTrue(nnManager.get().get(new Get("x")).isEmpty());
        nnManager.flush();
        // delete nn
        response = HttpRequests.execute(HttpRequest.delete(serviceURI.resolve("delete/nn").toURL()).build());
        Assert.assertEquals(200, response.getResponseCode());
        // delete again -> not found
        response = HttpRequests.execute(HttpRequest.delete(serviceURI.resolve("delete/nn").toURL()).build());
        Assert.assertEquals(404, response.getResponseCode());
        // delete xx which never existed -> not found
        response = HttpRequests.execute(HttpRequest.delete(serviceURI.resolve("delete/xx").toURL()).build());
        Assert.assertEquals(404, response.getResponseCode());
        // exists should now return false for nn
        response = HttpRequests.execute(HttpRequest.get(serviceURI.resolve("exists/nn").toURL()).build());
        Assert.assertEquals(200, response.getResponseCode());
        Assert.assertEquals("false", response.getResponseBodyAsString());
        Assert.assertNull(getDataset("nn").get());
    } finally {
        serviceManager.stop();
    }
}
Also used : Table(co.cask.cdap.api.dataset.table.Table) KeyValueTable(co.cask.cdap.api.dataset.lib.KeyValueTable) ServiceManager(co.cask.cdap.test.ServiceManager) TypeToken(co.cask.cdap.internal.guava.reflect.TypeToken) Get(co.cask.cdap.api.dataset.table.Get) HttpResponse(co.cask.common.http.HttpResponse) URI(java.net.URI) Put(co.cask.cdap.api.dataset.table.Put) Test(org.junit.Test)

Aggregations

TypeToken (co.cask.cdap.internal.guava.reflect.TypeToken)5 TypeParameter (co.cask.cdap.internal.guava.reflect.TypeParameter)3 Type (java.lang.reflect.Type)3 Schema (co.cask.cdap.api.data.schema.Schema)1 KeyValueTable (co.cask.cdap.api.dataset.lib.KeyValueTable)1 Get (co.cask.cdap.api.dataset.table.Get)1 Put (co.cask.cdap.api.dataset.table.Put)1 Table (co.cask.cdap.api.dataset.table.Table)1 ServiceManager (co.cask.cdap.test.ServiceManager)1 HttpResponse (co.cask.common.http.HttpResponse)1 Field (java.lang.reflect.Field)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 Test (org.junit.Test)1