GraphBackedMetadataRepository implements MetadataRepository { @Override @GraphTransaction public CreateUpdateEntitiesResult createEntities(ITypedReferenceableInstance... entities) throws RepositoryException, EntityExistsException { if (LOG.isDebugEnabled()) { LOG.debug("adding entities={}", entities); } try { TypedInstanceToGraphMapper instanceToGraphMapper = new TypedInstanceToGraphMapper(graphToInstanceMapper, deleteHandler); instanceToGraphMapper.mapTypedInstanceToGraph(TypedInstanceToGraphMapper.Operation.CREATE, entities); List<String> createdGuids = RequestContext.get().getCreatedEntityIds(); CreateUpdateEntitiesResult result = new CreateUpdateEntitiesResult(); EntityResult entityResult = new EntityResult(createdGuids, null, null); GuidMapping mapping = instanceToGraphMapper.createGuidMapping(); result.setEntityResult(entityResult); result.setGuidMapping(mapping); return result; } catch (EntityExistsException e) { throw e; } catch (AtlasException e) { throw new RepositoryException(e); } } @Inject  GraphBackedMetadataRepository(DeleteHandler deleteHandler, AtlasGraph atlasGraph); GraphToTypedInstanceMapper getGraphToInstanceMapper(); @Override String getTypeAttributeName(); @Override String getStateAttributeName(); @Override String getSuperTypeAttributeName(); String getIdAttributeName(); @Override String getVersionAttributeName(); @Override String getTraitLabel(IDataType<?> dataType, String traitName); @Override String getFieldNameInVertex(IDataType<?> dataType, AttributeInfo aInfo); String getFieldNameInVertex(IDataType<?> dataType, String attrName); @Override String getEdgeLabel(IDataType<?> dataType, AttributeInfo aInfo); @Override @GraphTransaction CreateUpdateEntitiesResult createEntities(ITypedReferenceableInstance... entities); @Override @GraphTransaction ITypedReferenceableInstance getEntityDefinition(String guid); @Override @GraphTransaction List<ITypedReferenceableInstance> getEntityDefinitions(String... guids); @Override @GraphTransaction ITypedReferenceableInstance getEntityDefinition(String entityType, String attribute, Object value); @Override @GraphTransaction List<String> getEntityList(String entityType); @Override @GraphTransaction List<String> getTraitNames(String guid); @Override @GraphTransaction void addTrait(List<String> entityGuids, ITypedStruct traitInstance); @Override @GraphTransaction void addTrait(String guid, ITypedStruct traitInstance); @Override @GraphTransaction void deleteTrait(String guid, String traitNameToBeDeleted); @Override @GraphTransaction CreateUpdateEntitiesResult updateEntities(ITypedReferenceableInstance... entitiesUpdated); @Override @GraphTransaction CreateUpdateEntitiesResult updatePartial(ITypedReferenceableInstance entity); @Override @GraphTransaction EntityResult deleteEntities(List<String> guids); AtlasGraph getGraph();  }
@Test public void testSubmitEntity() throws Exception { ITypedReferenceableInstance hrDept = TestUtils.createDeptEg1(typeSystem); List<String> guids = repositoryService.createEntities(hrDept).getCreatedEntities(); Assert.assertNotNull(guids); Assert.assertEquals(guids.size(), 5); guid = guids.get(4); Assert.assertNotNull(guid); }
@Test public void testCreateEntityWithOneNestingLevel() throws AtlasException { List<Referenceable> toValidate = new ArrayList<>(); Referenceable dept = new Referenceable(TestUtils.DEPARTMENT_TYPE); toValidate.add(dept); dept.set(TestUtils.NAME, "test1"); Referenceable mike = new Referenceable(TestUtils.PERSON_TYPE); toValidate.add(mike); mike.set(TestUtils.NAME, "Mike"); mike.set(TestUtils.DEPARTMENT_ATTR, dept); Referenceable mark = new Referenceable(TestUtils.PERSON_TYPE); toValidate.add(mark); mark.set(TestUtils.NAME, "Mark"); mark.set(TestUtils.DEPARTMENT_ATTR, dept); dept.set(TestUtils.EMPLOYEES_ATTR, ImmutableList.of(mike, mark)); Map<String,Referenceable> positions = new HashMap<>(); final String JANITOR = "janitor"; final String RECEPTIONIST = "receptionist"; positions.put(JANITOR, mike); positions.put(RECEPTIONIST, mark); dept.set(TestUtils.POSITIONS_ATTR, positions); ClassType deptType = TypeSystem.getInstance().getDataType(ClassType.class, TestUtils.DEPARTMENT_TYPE); ITypedReferenceableInstance deptInstance = deptType.convert(dept, Multiplicity.REQUIRED); CreateUpdateEntitiesResult result = repositoryService.createEntities(deptInstance); validateGuidMapping(toValidate, result); }
@Test public void testCreateEntityWithTwoNestingLevels() throws AtlasException { List<Referenceable> toVerify = new ArrayList<>(); Referenceable dept = new Referenceable(TestUtils.DEPARTMENT_TYPE); toVerify.add(dept); dept.set(TestUtils.NAME, "test2"); Referenceable wallace = new Referenceable(TestUtils.PERSON_TYPE); toVerify.add(wallace); wallace.set(TestUtils.NAME, "Wallace"); wallace.set(TestUtils.DEPARTMENT_ATTR, dept); Referenceable wallaceComputer = new Referenceable(TestUtils.ASSET_TYPE); toVerify.add(wallaceComputer); wallaceComputer.set("name", "wallaceComputer"); wallace.set(TestUtils.ASSETS_ATTR, ImmutableList.of(wallaceComputer)); Referenceable jordan = new Referenceable(TestUtils.PERSON_TYPE); toVerify.add(jordan); jordan.set(TestUtils.NAME, "Jordan"); jordan.set(TestUtils.DEPARTMENT_ATTR, dept); Referenceable jordanComputer = new Referenceable(TestUtils.ASSET_TYPE); toVerify.add(jordanComputer); jordanComputer.set("name", "jordanComputer"); jordan.set(TestUtils.ASSETS_ATTR, ImmutableList.of(jordanComputer)); dept.set(TestUtils.EMPLOYEES_ATTR, ImmutableList.of(wallace, jordan)); Map<String,Referenceable> positions = new HashMap<>(); final String JANITOR = "janitor"; final String RECEPTIONIST = "receptionist"; positions.put(JANITOR, wallace); positions.put(RECEPTIONIST, jordan); dept.set(TestUtils.POSITIONS_ATTR, positions); ClassType deptType = TypeSystem.getInstance().getDataType(ClassType.class, TestUtils.DEPARTMENT_TYPE); ITypedReferenceableInstance deptInstance = deptType.convert(dept, Multiplicity.REQUIRED); CreateUpdateEntitiesResult result = repositoryService.createEntities(deptInstance); validateGuidMapping(toVerify, result); }
@Test public void testCreateEntityWithThreeNestingLevels() throws AtlasException { List<Referenceable> toVerify = new ArrayList<>(); Referenceable dept = new Referenceable(TestUtils.DEPARTMENT_TYPE); toVerify.add(dept); dept.set(TestUtils.NAME, "test3"); Referenceable barry = new Referenceable(TestUtils.PERSON_TYPE); toVerify.add(barry); barry.set(TestUtils.NAME, "barry"); barry.set(TestUtils.DEPARTMENT_ATTR, dept); Referenceable barryComputer = new Referenceable(TestUtils.ASSET_TYPE); toVerify.add(barryComputer); barryComputer.set("name", "barryComputer"); barry.set(TestUtils.ASSETS_ATTR, ImmutableList.of(barryComputer)); Referenceable barryHardDrive = new Referenceable(TestUtils.ASSET_TYPE); toVerify.add(barryHardDrive); barryHardDrive.set("name", "barryHardDrive"); Referenceable barryCpuFan = new Referenceable(TestUtils.ASSET_TYPE); toVerify.add(barryCpuFan); barryCpuFan.set("name", "barryCpuFan"); Referenceable barryVideoCard = new Referenceable(TestUtils.ASSET_TYPE); toVerify.add(barryVideoCard); barryVideoCard.set("name", "barryVideoCard"); barryComputer.set("childAssets", ImmutableList.of(barryHardDrive, barryVideoCard, barryCpuFan)); Referenceable jacob = new Referenceable(TestUtils.PERSON_TYPE); toVerify.add(jacob); jacob.set(TestUtils.NAME, "jacob"); jacob.set(TestUtils.DEPARTMENT_ATTR, dept); Referenceable jacobComputer = new Referenceable(TestUtils.ASSET_TYPE); toVerify.add(jacobComputer); jacobComputer.set("name", "jacobComputer"); jacob.set(TestUtils.ASSETS_ATTR, ImmutableList.of(jacobComputer)); Referenceable jacobHardDrive = new Referenceable(TestUtils.ASSET_TYPE); toVerify.add(jacobHardDrive); jacobHardDrive.set("name", "jacobHardDrive"); Referenceable jacobCpuFan = new Referenceable(TestUtils.ASSET_TYPE); toVerify.add(jacobCpuFan); jacobCpuFan.set("name", "jacobCpuFan"); Referenceable jacobVideoCard = new Referenceable(TestUtils.ASSET_TYPE); toVerify.add(jacobVideoCard); jacobVideoCard.set("name", "jacobVideoCard"); jacobComputer.set("childAssets", ImmutableList.of(jacobHardDrive, jacobVideoCard, jacobCpuFan)); dept.set(TestUtils.EMPLOYEES_ATTR, ImmutableList.of(barry, jacob)); Map<String,Referenceable> positions = new HashMap<>(); final String JANITOR = "janitor"; final String RECEPTIONIST = "receptionist"; positions.put(JANITOR, barry); positions.put(RECEPTIONIST, jacob); dept.set(TestUtils.POSITIONS_ATTR, positions); ClassType deptType = TypeSystem.getInstance().getDataType(ClassType.class, TestUtils.DEPARTMENT_TYPE); ITypedReferenceableInstance deptInstance = deptType.convert(dept, Multiplicity.REQUIRED); CreateUpdateEntitiesResult result = repositoryService.createEntities(deptInstance); assertEquals(result.getCreatedEntities().size(), toVerify.size()); validateGuidMapping(toVerify, result); }
@Test public void testCreateEntity() throws Exception { Referenceable databaseInstance = new Referenceable(TestUtils.DATABASE_TYPE); databaseInstance.set("name", TestUtils.DATABASE_NAME); databaseInstance.set("description", "foo database"); databaseInstance.set("created", new Date(TestUtils.TEST_DATE_IN_LONG)); databaseInstance.set("namespace", "colo:cluster:hive:db"); databaseInstance.set("cluster", "cluster-1"); databaseInstance.set("colo", "colo-1"); System.out.println("databaseInstance = " + databaseInstance); ClassType dbType = typeSystem.getDataType(ClassType.class, TestUtils.DATABASE_TYPE); ITypedReferenceableInstance db = dbType.convert(databaseInstance, Multiplicity.REQUIRED); System.out.println("db = " + db); ITypedReferenceableInstance table = createHiveTableInstance(databaseInstance); List<String> guids = createEntities(db, table); Assert.assertEquals(guids.size(), 7); System.out.println("added db = " + guids.get(0)); System.out.println("added table = " + guids.get(6)); }
@Test public void testUTFValues() throws Exception { Referenceable hrDept = new Referenceable("Department"); Referenceable john = new Referenceable("Person"); john.set("name", randomUTF()); john.set("department", hrDept); hrDept.set("name", randomUTF()); hrDept.set("employees", ImmutableList.of(john)); ClassType deptType = typeSystem.getDataType(ClassType.class, "Department"); ITypedReferenceableInstance hrDept2 = deptType.convert(hrDept, Multiplicity.REQUIRED); List<String> guids = repositoryService.createEntities(hrDept2).getCreatedEntities(); Assert.assertNotNull(guids); Assert.assertEquals(guids.size(), 2); Assert.assertNotNull(guids.get(0)); Assert.assertNotNull(guids.get(1)); }