Search in sources :

Example 6 with JSONObjectAdapterImpl

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

the class AccessApprovalUtilsTest method testRoundtripWithNulls.

@Test
public void testRoundtripWithNulls() throws Exception {
    AccessApproval dto = createDTO();
    dto.setId(null);
    DBOAccessApproval dbo = new DBOAccessApproval();
    String jsonString = (String) AccessApproval.class.getField(JSONEntity.EFFECTIVE_SCHEMA).get(null);
    ObjectSchema schema = new ObjectSchema(new JSONObjectAdapterImpl(jsonString));
    AccessApprovalUtils.copyDtoToDbo(dto, dbo);
    AccessApproval dto2 = AccessApprovalUtils.copyDboToDto(dbo);
    assertEquals(dto, dto2);
}
Also used : TermsOfUseAccessApproval(org.sagebionetworks.repo.model.TermsOfUseAccessApproval) DBOAccessApproval(org.sagebionetworks.repo.model.dbo.persistence.DBOAccessApproval) AccessApproval(org.sagebionetworks.repo.model.AccessApproval) ObjectSchema(org.sagebionetworks.schema.ObjectSchema) DBOAccessApproval(org.sagebionetworks.repo.model.dbo.persistence.DBOAccessApproval) JSONObjectAdapterImpl(org.sagebionetworks.schema.adapter.org.json.JSONObjectAdapterImpl) Test(org.junit.Test)

Example 7 with JSONObjectAdapterImpl

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

the class SchemaSerializationUtilsTest method testAnnotationsRoundtrip.

@Test
public void testAnnotationsRoundtrip() throws Exception {
    UserProfile dto = new UserProfile();
    dto.setOwnerId("101");
    dto.setFirstName("foo");
    dto.setLastName("bar");
    dto.setRStudioUrl("http://rstudio.com");
    dto.setDisplayName("foo bar");
    dto.setEtag("0");
    String jsonString = (String) UserProfile.class.getField(JSONEntity.EFFECTIVE_SCHEMA).get(null);
    ObjectSchema schema = new ObjectSchema(new JSONObjectAdapterImpl(jsonString));
    byte[] na = SchemaSerializationUtils.mapDtoFieldsToAnnotations(dto, schema);
    UserProfile dto2 = new UserProfile();
    SchemaSerializationUtils.mapAnnotationsToDtoFields(na, dto2, schema);
    assertEquals(dto, dto2);
}
Also used : UserProfile(org.sagebionetworks.repo.model.UserProfile) ObjectSchema(org.sagebionetworks.schema.ObjectSchema) JSONObjectAdapterImpl(org.sagebionetworks.schema.adapter.org.json.JSONObjectAdapterImpl) Test(org.junit.Test)

Example 8 with JSONObjectAdapterImpl

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

the class DBOUserGroupDAOImplTest method testGetMigrationObjectData.

@Test
public void testGetMigrationObjectData() throws Exception {
    boolean foundPublic = false;
    UserGroup ug = userGroupDAO.findGroup("PUBLIC", false);
    assertNotNull(ug);
    boolean foundUser = false;
    UserGroup oner = new UserGroup();
    oner.setName("oner");
    oner.setIsIndividual(true);
    String onerId = userGroupDAO.create(oner);
    groupsToDelete.add(onerId);
    UserProfile up = new UserProfile();
    up.setOwnerId(onerId);
    up.setEtag("some tag");
    String jsonString = (String) UserProfile.class.getField(JSONEntity.EFFECTIVE_SCHEMA).get(null);
    ObjectSchema schema = new ObjectSchema(new JSONObjectAdapterImpl(jsonString));
    // this will be deleted via cascade when the user-group is deleted
    String upId = userProfileDAO.create(up, schema);
    QueryResults<MigratableObjectData> migrationData = userGroupDAO.getMigrationObjectData(0, 10000, true);
    assert (migrationData.getTotalNumberOfResults() > 1);
    assertEquals(migrationData.getTotalNumberOfResults(), migrationData.getResults().size());
    for (MigratableObjectData od : migrationData.getResults()) {
        MigratableObjectDescriptor obj = od.getId();
        assertNotNull(obj.getId());
        assertEquals(MigratableObjectType.PRINCIPAL, obj.getType());
        assertNotNull(od.getEtag());
        // Groups are not dependent on any other migratable object
        assertTrue(od.getDependencies().isEmpty());
        if (obj.getId().equals(ug.getId())) {
            foundPublic = true;
            // multiuser groups have no real etags
            assertEquals(DBOUserGroupDAOImpl.DEFAULT_ETAG, od.getEtag());
        }
        if (obj.getId().equals(onerId)) {
            foundUser = true;
        }
    }
    assertTrue(foundPublic);
    assertTrue(foundUser);
    // make sure pagination works
    migrationData = userGroupDAO.getMigrationObjectData(0, 1, true);
    assertEquals(1, migrationData.getResults().size());
}
Also used : UserProfile(org.sagebionetworks.repo.model.UserProfile) ObjectSchema(org.sagebionetworks.schema.ObjectSchema) MigratableObjectData(org.sagebionetworks.repo.model.MigratableObjectData) MigratableObjectDescriptor(org.sagebionetworks.repo.model.MigratableObjectDescriptor) JSONObjectAdapterImpl(org.sagebionetworks.schema.adapter.org.json.JSONObjectAdapterImpl) UserGroup(org.sagebionetworks.repo.model.UserGroup) Test(org.junit.Test)

Example 9 with JSONObjectAdapterImpl

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

the class EntityObjectMapper method writeValue.

/**
 * Write the passed entity to the writer
 * @param w
 * @param entity
 * @throws JSONObjectAdapterException
 * @throws IOException
 */
public void writeValue(Writer w, JSONEntity entity) {
    JSONObjectAdapter adapter;
    try {
        adapter = entity.writeToJSONObject(new JSONObjectAdapterImpl());
        w.write(adapter.toJSONString());
    } catch (JSONObjectAdapterException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : JSONObjectAdapter(org.sagebionetworks.schema.adapter.JSONObjectAdapter) JSONObjectAdapterException(org.sagebionetworks.schema.adapter.JSONObjectAdapterException) IOException(java.io.IOException) JSONObjectAdapterImpl(org.sagebionetworks.schema.adapter.org.json.JSONObjectAdapterImpl)

Example 10 with JSONObjectAdapterImpl

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

the class JSONEntityHttpMessageConverterTest method testCreateEntityFromeAdapterBadJSON.

/**
 * This test was added for PLFM-1280.
 * @throws JSONObjectAdapterException
 */
@Test(expected = JSONObjectAdapterException.class)
public void testCreateEntityFromeAdapterBadJSON() throws JSONObjectAdapterException {
    // Test a vaild entity type with a field that does not exist on that type.
    JSONObjectAdapter adapter = new JSONObjectAdapterImpl();
    adapter.put("entityType", ExampleEntity.class.getName());
    adapter.put("notAField", "shoudld not exist");
    JSONEntityHttpMessageConverter.createEntityFromeAdapter(adapter);
}
Also used : JSONObjectAdapter(org.sagebionetworks.schema.adapter.JSONObjectAdapter) ExampleEntity(org.sagebionetworks.repo.model.ExampleEntity) JSONObjectAdapterImpl(org.sagebionetworks.schema.adapter.org.json.JSONObjectAdapterImpl) 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