Search in sources :

Example 1 with AdapterFactoryImpl

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

the class SearchDocumentDriverImplAutowireTest method testAnnotations.

/**
 * All non-blob annotations belong in the free text annotations field, some
 * are _also_ facets in the search index
 *
 * @throws Exception
 */
@Test
public void testAnnotations() throws Exception {
    Node node = new Node();
    node.setId("5678");
    node.setParentId("1234");
    node.setETag("0");
    node.setNodeType(EntityType.dataset.name());
    node.setDescription("Test annotations");
    Long nonexistantPrincipalId = 42L;
    node.setCreatedByPrincipalId(nonexistantPrincipalId);
    node.setCreatedOn(new Date());
    node.setModifiedByPrincipalId(nonexistantPrincipalId);
    node.setModifiedOn(new Date());
    node.setVersionLabel("versionLabel");
    NodeRevisionBackup rev = new NodeRevisionBackup();
    NamedAnnotations named = new NamedAnnotations();
    Annotations primaryAnnos = named.getAdditionalAnnotations();
    primaryAnnos.addAnnotation("species", "Dragon");
    primaryAnnos.addAnnotation("numSamples", 999L);
    Annotations additionalAnnos = named.getAdditionalAnnotations();
    additionalAnnos.addAnnotation("Species", "Unicorn");
    additionalAnnos.addAnnotation("stringKey", "a multi-word annotation gets underscores so we can exact-match find it");
    additionalAnnos.addAnnotation("longKey", 10L);
    additionalAnnos.addAnnotation("number_of_samples", "42");
    additionalAnnos.addAnnotation("Tissue_Tumor", "ear lobe");
    additionalAnnos.addAnnotation("platform", "synapse");
    Date dateValue = new Date();
    additionalAnnos.addAnnotation("dateKey", dateValue);
    additionalAnnos.addAnnotation("blobKey", new String("bytes").getBytes());
    rev.setNamedAnnotations(named);
    Set<Reference> references = new HashSet<Reference>();
    Map<String, Set<Reference>> referenceMap = new HashMap<String, Set<Reference>>();
    referenceMap.put("tooMany", references);
    node.setReferences(referenceMap);
    for (int i = 0; i <= SearchDocumentDriverImpl.FIELD_VALUE_SIZE_LIMIT + 10; i++) {
        Reference ref = new Reference();
        ref.setTargetId("123" + i);
        ref.setTargetVersionNumber(1L);
        references.add(ref);
    }
    Set<ACCESS_TYPE> rwAccessType = new HashSet<ACCESS_TYPE>();
    rwAccessType.add(ACCESS_TYPE.READ);
    rwAccessType.add(ACCESS_TYPE.UPDATE);
    ResourceAccess rwResourceAccess = new ResourceAccess();
    // readWriteTest@sagebase.org
    rwResourceAccess.setPrincipalId(123L);
    rwResourceAccess.setAccessType(rwAccessType);
    Set<ACCESS_TYPE> roAccessType = new HashSet<ACCESS_TYPE>();
    roAccessType.add(ACCESS_TYPE.READ);
    ResourceAccess roResourceAccess = new ResourceAccess();
    // readOnlyTest@sagebase.org
    roResourceAccess.setPrincipalId(456L);
    roResourceAccess.setAccessType(roAccessType);
    Set<ResourceAccess> resourceAccesses = new HashSet<ResourceAccess>();
    resourceAccesses.add(rwResourceAccess);
    resourceAccesses.add(roResourceAccess);
    AccessControlList acl = new AccessControlList();
    acl.setResourceAccess(resourceAccesses);
    EntityPath fakeEntityPath = createFakeEntityPath();
    AdapterFactoryImpl adapterFactoryImpl = new AdapterFactoryImpl();
    JSONObjectAdapter adapter = adapterFactoryImpl.createNew();
    fakeEntityPath.writeToJSONObject(adapter);
    String fakeEntityPathJSONString = adapter.toJSONString();
    Document document = searchDocumentDriver.formulateSearchDocument(node, rev, acl, fakeEntityPath);
    assertEquals(DocumentTypeNames.add, document.getType());
    assertEquals("en", document.getLang());
    assertEquals(node.getId(), document.getId());
    assertTrue(0 < document.getVersion());
    DocumentFields fields = document.getFields();
    // Check entity property fields
    assertEquals(node.getId(), fields.getId());
    assertEquals(node.getETag(), fields.getEtag());
    assertEquals(node.getParentId(), fields.getParent_id());
    assertEquals(node.getName(), fields.getName());
    assertEquals("study", fields.getNode_type());
    assertEquals(node.getDescription(), fields.getDescription());
    // since the Principal doesn't exist, the 'created by' display name defaults to the principal ID
    assertEquals("" + nonexistantPrincipalId, fields.getCreated_by());
    assertEquals(new Long(node.getCreatedOn().getTime() / 1000), fields.getCreated_on());
    // since the Principal doesn't exist, the 'modified by' display name defaults to the principal ID
    assertEquals("" + nonexistantPrincipalId, fields.getModified_by());
    assertEquals(new Long(node.getModifiedOn().getTime() / 1000), fields.getModified_on());
    // Check boost field
    assertTrue(fields.getBoost().contains(node.getId()));
    assertTrue(fields.getBoost().contains(node.getName()));
    // Check the faceted fields
    assertEquals(2, fields.getNum_samples().size());
    assertEquals(new Long(42), fields.getNum_samples().get(0));
    assertEquals(new Long(999), fields.getNum_samples().get(1));
    assertEquals(2, fields.getSpecies().size());
    assertEquals("Dragon", fields.getSpecies().get(0));
    assertEquals("Unicorn", fields.getSpecies().get(1));
    assertEquals("ear lobe", fields.getTissue().get(0));
    assertEquals("synapse", fields.getPlatform().get(0));
    // Check ACL fields
    assertEquals(2, fields.getAcl().size());
    assertEquals(1, fields.getUpdate_acl().size());
    // Make sure our references were trimmed
    assertTrue(10 < fields.getReferences().size());
    assertEquals(SearchDocumentDriverImpl.FIELD_VALUE_SIZE_LIMIT, fields.getReferences().size());
}
Also used : AccessControlList(org.sagebionetworks.repo.model.AccessControlList) ResourceAccess(org.sagebionetworks.repo.model.ResourceAccess) DocumentFields(org.sagebionetworks.repo.model.search.DocumentFields) EntityPath(org.sagebionetworks.repo.model.EntityPath) AdapterFactoryImpl(org.sagebionetworks.schema.adapter.org.json.AdapterFactoryImpl) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) Reference(org.sagebionetworks.repo.model.Reference) JSONObjectAdapter(org.sagebionetworks.schema.adapter.JSONObjectAdapter) Node(org.sagebionetworks.repo.model.Node) NodeRevisionBackup(org.sagebionetworks.repo.model.NodeRevisionBackup) Document(org.sagebionetworks.repo.model.search.Document) Date(java.util.Date) NamedAnnotations(org.sagebionetworks.repo.model.NamedAnnotations) Annotations(org.sagebionetworks.repo.model.Annotations) ACCESS_TYPE(org.sagebionetworks.repo.model.ACCESS_TYPE) NamedAnnotations(org.sagebionetworks.repo.model.NamedAnnotations) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Date (java.util.Date)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 Test (org.junit.Test)1 ACCESS_TYPE (org.sagebionetworks.repo.model.ACCESS_TYPE)1 AccessControlList (org.sagebionetworks.repo.model.AccessControlList)1 Annotations (org.sagebionetworks.repo.model.Annotations)1 EntityPath (org.sagebionetworks.repo.model.EntityPath)1 NamedAnnotations (org.sagebionetworks.repo.model.NamedAnnotations)1 Node (org.sagebionetworks.repo.model.Node)1 NodeRevisionBackup (org.sagebionetworks.repo.model.NodeRevisionBackup)1 Reference (org.sagebionetworks.repo.model.Reference)1 ResourceAccess (org.sagebionetworks.repo.model.ResourceAccess)1 Document (org.sagebionetworks.repo.model.search.Document)1 DocumentFields (org.sagebionetworks.repo.model.search.DocumentFields)1 JSONObjectAdapter (org.sagebionetworks.schema.adapter.JSONObjectAdapter)1 AdapterFactoryImpl (org.sagebionetworks.schema.adapter.org.json.AdapterFactoryImpl)1