Search in sources :

Example 46 with JSONObjectAdapterImpl

use of org.sagebionetworks.schema.adapter.org.json.JSONObjectAdapterImpl in project Synapse-Repository-Services by Sage-Bionetworks.

the class ObjectTypeSerializerTest method testPLFM_834Paginated.

@Test
public void testPLFM_834Paginated() throws HttpMessageNotWritableException, IOException, JSONObjectAdapterException {
    // Make sure we can write an entity to json
    List<EntityHeader> results = new ArrayList<EntityHeader>();
    EntityHeader h1 = new EntityHeader();
    h1.setId("123");
    h1.setName("Joe");
    h1.setType("type");
    results.add(h1);
    PaginatedResults<EntityHeader> paged = new PaginatedResults<EntityHeader>("/repo/v1/somePath", results, 101, 50, 1, "sortOn", true);
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    HttpOutputMessage message = new HttpOutputMessage() {

        @Override
        public HttpHeaders getHeaders() {
            return new HttpHeaders();
        }

        @Override
        public OutputStream getBody() throws IOException {
            return out;
        }
    };
    // Write this to the stream
    objectTypeSerializer.write(paged, mediaType, message);
    String outString = new String(out.toByteArray(), Charset.forName("UTF-8"));
    System.out.println(outString);
    assertFalse("The resulting JSON should not contain the schema", outString.indexOf("jsonschema") > -1);
    // Make sure we can create a new entity with the path.
    JSONObjectAdapter adapter = new JSONObjectAdapterImpl(outString);
    PaginatedResults<EntityHeader> clone = new PaginatedResults<EntityHeader>(EntityHeader.class);
    clone.initializeFromJSONObject(adapter);
    assertEquals(paged, clone);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) EntityHeader(org.sagebionetworks.repo.model.EntityHeader) PaginatedResults(org.sagebionetworks.repo.model.PaginatedResults) HttpOutputMessage(org.springframework.http.HttpOutputMessage) JSONObjectAdapter(org.sagebionetworks.schema.adapter.JSONObjectAdapter) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JSONObjectAdapterImpl(org.sagebionetworks.schema.adapter.org.json.JSONObjectAdapterImpl) Test(org.junit.Test)

Example 47 with JSONObjectAdapterImpl

use of org.sagebionetworks.schema.adapter.org.json.JSONObjectAdapterImpl in project Synapse-Repository-Services by Sage-Bionetworks.

the class StorageUsageControllerTest method testGrandTotals.

@Test
public void testGrandTotals() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("GET");
    request.addHeader("Accept", "application/json");
    request.setRequestURI(UrlHelpers.STORAGE_SUMMARY);
    request.setParameter(AuthorizationConstants.USER_ID_PARAM, "0");
    MockHttpServletResponse response = new MockHttpServletResponse();
    HttpServlet servlet = DispatchServletSingleton.getInstance();
    servlet.service(request, response);
    Assert.assertEquals(200, response.getStatus());
    String jsonStr = response.getContentAsString();
    JSONObjectAdapter adapter = new JSONObjectAdapterImpl(jsonStr);
    StorageUsageSummaryList sus = new StorageUsageSummaryList();
    sus.initializeFromJSONObject(adapter);
    Assert.assertNotNull(sus);
    Assert.assertEquals(0L, sus.getTotalSize().longValue());
    Assert.assertEquals(0, sus.getSummaryList().size());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpServlet(javax.servlet.http.HttpServlet) JSONObjectAdapter(org.sagebionetworks.schema.adapter.JSONObjectAdapter) StorageUsageSummaryList(org.sagebionetworks.repo.model.storage.StorageUsageSummaryList) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) JSONObjectAdapterImpl(org.sagebionetworks.schema.adapter.org.json.JSONObjectAdapterImpl) Test(org.junit.Test)

Example 48 with JSONObjectAdapterImpl

use of org.sagebionetworks.schema.adapter.org.json.JSONObjectAdapterImpl in project Synapse-Repository-Services by Sage-Bionetworks.

the class AnnotationJSONTest method testBackwardsCompatibility.

@Test
public void testBackwardsCompatibility() throws JsonGenerationException, JsonMappingException, IOException, JSONObjectAdapterException {
    ObjectMapper objectMapper = new ObjectMapper();
    Annotations annos = new Annotations();
    annos.setId("123");
    annos.setEtag("456");
    annos.setCreationDate(new Date(System.currentTimeMillis()));
    annos.setUri("http://localhost:8080/");
    annos.addAnnotation("binary", "This will be binary".getBytes("UTF-8"));
    annos.addAnnotation("date", new Date(System.currentTimeMillis()));
    annos.addAnnotation("double", new Double(123.5));
    annos.addAnnotation("long", new Long(345));
    annos.addAnnotation("string", "String value");
    annos.getStringAnnotations().put("nullList", null);
    annos.getStringAnnotations().put("empty", new ArrayList<String>());
    List<String> withNull = new ArrayList<String>();
    withNull.add(null);
    annos.getStringAnnotations().put("nullValueInList", withNull);
    StringWriter writer = new StringWriter();
    objectMapper.writeValue(writer, annos);
    String objectMapperJson = writer.toString();
    System.out.println(objectMapperJson);
    // Make sure we are generating the same JSON as before
    JSONObjectAdapter adapter = new JSONObjectAdapterImpl();
    annos.writeToJSONObject(adapter);
    // The strings should be the same
    String fromAdapterJson = adapter.toJSONString();
    System.out.println(fromAdapterJson);
    // Make sure one can load the other
    Annotations cloneOne = objectMapper.readValue(fromAdapterJson, Annotations.class);
    assertEquals(annos, cloneOne);
    // Now go the other way
    Annotations cloneTwo = EntityFactory.createEntityFromJSONString(objectMapperJson, Annotations.class);
    assertEquals(annos, cloneTwo);
}
Also used : Annotations(org.sagebionetworks.repo.model.Annotations) StringWriter(java.io.StringWriter) JSONObjectAdapter(org.sagebionetworks.schema.adapter.JSONObjectAdapter) ArrayList(java.util.ArrayList) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Date(java.util.Date) JSONObjectAdapterImpl(org.sagebionetworks.schema.adapter.org.json.JSONObjectAdapterImpl) Test(org.junit.Test)

Example 49 with JSONObjectAdapterImpl

use of org.sagebionetworks.schema.adapter.org.json.JSONObjectAdapterImpl in project Synapse-Repository-Services by Sage-Bionetworks.

the class JSONEntityHttpMessageConverter method readEntity.

/**
 * Read an entity from the reader.
 * @param reader
 * @return
 * @throws IOException
 * @throws JSONObjectAdapterException
 */
public static Entity readEntity(Reader reader) throws IOException, JSONObjectAdapterException {
    // First read in the string
    String jsonString = readToString(reader);
    // Read it into an adapter
    JSONObjectAdapter adapter = new JSONObjectAdapterImpl(jsonString);
    return createEntityFromeAdapter(adapter);
}
Also used : JSONObjectAdapter(org.sagebionetworks.schema.adapter.JSONObjectAdapter) JSONObjectAdapterImpl(org.sagebionetworks.schema.adapter.org.json.JSONObjectAdapterImpl)

Example 50 with JSONObjectAdapterImpl

use of org.sagebionetworks.schema.adapter.org.json.JSONObjectAdapterImpl in project Synapse-Repository-Services by Sage-Bionetworks.

the class GenotypeDataTest method testRoundTripLayer.

@Test
public void testRoundTripLayer() throws JSONObjectAdapterException {
    GenotypeData l1 = new GenotypeData();
    JSONObjectAdapter adapter1 = new JSONObjectAdapterImpl();
    JSONObjectAdapter adapter2 = new JSONObjectAdapterImpl();
    Date d = new Date();
    l1.setAccessControlList("/acl");
    l1.setAnnotations("/annotations");
    l1.setCreatedBy("createdBy");
    l1.setCreatedOn(d);
    l1.setDescription("description");
    l1.setEtag("1");
    l1.setId("1");
    l1.setModifiedBy("modifiedBy");
    l1.setModifiedOn(d);
    l1.setName("name");
    l1.setParentId("0");
    l1.setUri("uri");
    l1.setVersionComment("versionComment");
    l1.setVersionLabel("versionLabel");
    l1.setVersionNumber(1L);
    l1.setVersionUrl("/versions/1");
    l1.setVersions("/versions");
    l1.setContentType("text");
    l1.setMd5("01234567890123456789012345678901");
    List<LocationData> ll = new ArrayList<LocationData>();
    LocationData l = new LocationData();
    l.setPath("path");
    l.setType(LocationTypeNames.sage);
    ll.add(l);
    l1.setLocations(ll);
    l1.setNumSamples(1000L);
    l1.setNumSamples(100L);
    l1.setDisease("disease");
    l1.setSpecies("species");
    l1.setS3Token("S3token");
    l1.setPlatform("platform");
    adapter1 = l1.writeToJSONObject(adapter1);
    String s = adapter1.toJSONString();
    adapter2 = new JSONObjectAdapterImpl(s);
    GenotypeData l2 = new GenotypeData(adapter2);
    assertEquals(l1, l2);
}
Also used : JSONObjectAdapter(org.sagebionetworks.schema.adapter.JSONObjectAdapter) ArrayList(java.util.ArrayList) JSONObjectAdapterImpl(org.sagebionetworks.schema.adapter.org.json.JSONObjectAdapterImpl) Date(java.util.Date) Test(org.junit.Test)

Aggregations

JSONObjectAdapterImpl (org.sagebionetworks.schema.adapter.org.json.JSONObjectAdapterImpl)62 JSONObjectAdapter (org.sagebionetworks.schema.adapter.JSONObjectAdapter)49 Test (org.junit.Test)30 JSONObjectAdapterException (org.sagebionetworks.schema.adapter.JSONObjectAdapterException)24 JSONObject (org.json.JSONObject)19 ArrayList (java.util.ArrayList)15 Date (java.util.Date)12 ObjectSchema (org.sagebionetworks.schema.ObjectSchema)11 PaginatedResults (org.sagebionetworks.repo.model.PaginatedResults)10 SynapseException (org.sagebionetworks.client.exceptions.SynapseException)9 VariableContentPaginatedResults (org.sagebionetworks.repo.model.VariableContentPaginatedResults)7 EntityHeader (org.sagebionetworks.repo.model.EntityHeader)6 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)6 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)6 StringEntity (org.apache.http.entity.StringEntity)5 EntityBundle (org.sagebionetworks.repo.model.EntityBundle)5 UserProfile (org.sagebionetworks.repo.model.UserProfile)5 AccessRequirement (org.sagebionetworks.repo.model.AccessRequirement)4 Annotations (org.sagebionetworks.repo.model.Annotations)4 HttpServlet (javax.servlet.http.HttpServlet)3