@Override public void doSetup() throws Exception { Map<String, Object> data = new HashMap<String, Object>(); data.put( "name", "rootentity" ); rootEntity = app.getEntityManager().create( "root", data ); }
/** * Invoke when an entity fails to write correctly */ public void entityFailed( final String message ) { entitiesFailed.incrementAndGet(); FailedImportEntity failedImportEntity = new FailedImportEntity(); failedImportEntity.setErrorMessage( message ); try { EntityManager entityManager = emf.getEntityManager(emf.getManagementAppId()); failedImportEntity = entityManager.create( failedImportEntity ); entityManager.createConnection( fileImport, ERRORS_CONNECTION_NAME, failedImportEntity ); } catch ( Exception e ) { throw new PersistenceException( "Unable to save failed entity import message", e ); } maybeFlush(); }
@Override public void doWrite(EntityManager em, FileImport fileImport, FileImportTracker tracker) { try { if (logger.isTraceEnabled()) { logger.trace("Writing imported entity {}:{} into app {}", entityType, entityUuid, em.getApplication().getUuid()); } em.create(entityUuid, entityType, properties); tracker.entityWritten(); } catch (Exception e) { logger.error("Error writing entity. From file:{}", fileImport.getFileName(), e); tracker.entityFailed(e.getMessage() + " From file: " + fileImport.getFileName()); } } }
/** * Invoked when a connection cannot be written */ public void connectionFailed( final String message ) { connectionsFailed.incrementAndGet(); FailedImportConnection failedImportConnection = new FailedImportConnection(); failedImportConnection.setErrorMessage( message ); try { EntityManager entityManager = emf.getEntityManager(emf.getManagementAppId()); failedImportConnection = entityManager.create( failedImportConnection ); entityManager.createConnection( fileImport, ERRORS_CONNECTION_NAME, failedImportConnection ); } catch ( Exception e ) { throw new PersistenceException( "Unable to save failed entity import message", e ); } maybeFlush(); }
@Override public Entity create( String type ) throws Exception { Entity entity = em.create( type, properties ); clear(); return entity; }
@Override public Entity writeEntity( Map<String, Object> entity ) throws Exception { return app.getEntityManager().create( "test", entity ); }
private void initMgmtAppInternal() { EntityManager em = getEntityManager(getManagementAppId()); indexService.queueInitializeApplicationIndex(CpNamingUtils.getApplicationScope(getManagementAppId())); try { if ( em.getApplication() == null ) { logger.info("Creating management application"); Map mgmtAppProps = new HashMap<String, Object>(); mgmtAppProps.put(PROPERTY_NAME, CassandraService.MANAGEMENT_APPLICATION); em.create( getManagementAppId(), TYPE_APPLICATION, mgmtAppProps); em.getApplication(); } } catch (Exception ex) { throw new RuntimeException("Fatal error creating management application", ex); } }
@Override public JobData createJob( String jobName, long fireTime, JobData jobData ) { Assert.notNull( jobName, "jobName is required" ); Assert.notNull( jobData, "jobData is required" ); try { jobData.setJobName( jobName ); JobData job = getEm().create( jobData ); JobStat stat = getEm().create( new JobStat( jobName, job.getUuid() ) ); scheduleJob( jobName, fireTime, job.getUuid(), stat.getUuid() ); return job; } catch ( Exception e ) { throw new JobRuntimeException( e ); } }
private Entity createEntitywithLocation( final EntityManager em,String name ,Double lat,Double lon) throws Exception { Map<String, Object> properties = new LinkedHashMap<String, Object>() {{ put("name", name); put("location", new LinkedHashMap<String, Object>() {{ put("latitude", lat); put("longitude", lon); }}); }}; return em.create("collars", properties); }
private User setupAppUser( UUID appId, String username, String email, boolean activated ) throws Exception { Mailbox.clearAll(); EntityManager em = setup.getEmf().getEntityManager( appId ); Map<String, Object> userProps = new LinkedHashMap<String, Object>(); userProps.put( "username", username ); userProps.put( "email", email ); userProps.put( "activated", activated ); User user = em.create( User.ENTITY_TYPE, User.class, userProps ); setup.getEntityIndex().refresh(app.getId()); return user; } }
@Test public void testDeprecatedGet() throws Exception { logger.info( "EntityManagerIT.testDeprecatedGet" ); EntityManager em = app.getEntityManager(); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put( "name", "XR-51B" ); properties.put( "fuel", "Nutrinox" ); Entity user = em.create( "robot", properties ); assertNotNull( user ); app.waitForQueueDrainAndRefreshIndex(); assertNotNull( em.get( user.getUuid() ) ); }
/** * Tests creating a large entity, then loading it, modifying it, saving it, then loading it again */ @Test public void testLargeEntityCrud() throws Exception { LOG.debug( "testLargeEntityCrud" ); SetConfigTestBypass.setValueByPass( serializationFig, "getMaxEntitySize", 641834 + "" ); final URL resource = this.getClass().getClassLoader().getResource( TEST_DATA_FILE ); final byte[] fileData = Files.readAllBytes( Paths.get( resource.toURI() ) ); final String fileAsString = new String( fileData, Charset.forName( "UTF-8" ) ); final Map<String, Object> json = ( Map<String, Object> ) JsonUtils.parse( fileAsString ); final EntityManager em = app.getEntityManager(); final Entity createReturned = em.create( "test", json ); final Entity loadReturnedRef = em.get( createReturned ); assertEquals( "Entities should be equal", createReturned, loadReturnedRef ); final Entity loadReturnedId = em.get( createReturned.getUuid() ); assertEquals( "Entities should be equal", createReturned, loadReturnedId ); }
@Test public void stringWithSpaces() throws Exception { EntityManager em = app.getEntityManager(); assertNotNull( em ); logger.debug( "stringWithSpaces" ); Map<String, Object> props = new HashMap<String, Object>(); props.put( "myString", "My simple string" ); Entity saved = em.create( "test", props ); app.waitForQueueDrainAndRefreshIndex(); Query query = Query.fromQL( "myString = 'My simple string'" ); Results results = em.searchCollection( em.getApplicationRef(), "tests", query ); Entity entity = results.getEntitiesMap().get( saved.getUuid() ); assertNotNull( entity ); }
@Test public void testCorrectType() throws Exception { EntityManager em = app.getEntityManager(); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put( "name", "testuser" ); properties.put( "username", "testuser" ); properties.put( "email", "test@foo.bar" ); Entity created = em.create( "user", properties ); Entity returned = em.get( new SimpleEntityRef( "user", created.getUuid() ) ); assertNotNull( created ); assertNotNull( returned ); assertTrue( created instanceof User ); assertTrue( returned instanceof User ); assertEquals( created, returned ); }
@Test( expected = DuplicateUniquePropertyExistsException.class ) public void duplicateNameTest() throws Exception { logger.debug( "duplicateNameTest" ); EntityManager em = app.getEntityManager(); assertNotNull( em ); DynamicEntity restaurant = new DynamicEntity(); restaurant.setName( "4peaks" ); Entity createdRestaurant = em.create( "restaurant", restaurant.getProperties() ); assertNotNull( createdRestaurant ); //we create 2 entities, otherwise this test will pass when it shouldn't DynamicEntity restaurant2 = new DynamicEntity(); restaurant2.setName( "4peaks" ); em.create( "restaurant", restaurant2.getProperties() ); } }
@Test public void testGetTokenForPrincipalUser() throws Exception { // create a user Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put( "username", "edanuff" ); properties.put( "email", "ed@anuff.com" ); Entity user = setup.getEmf().getEntityManager( applicationId ).create( "user", properties ); assertNotNull( user ); String token = ( ( ManagementServiceImpl ) setup.getMgmtSvc() ) .getTokenForPrincipal( TokenCategory.ACCESS, null, setup.getEmf().getManagementAppId(), AuthPrincipalType.APPLICATION_USER, user.getUuid(), 0 ); assertNotNull( token ); }
@Test( expected = DuplicateUniquePropertyExistsException.class ) public void duplicateIdentifierTest() throws Exception { logger.debug( "duplicateIdentifierTest" ); EntityManager em = app.getEntityManager(); assertNotNull( em ); User user = new User(); user.setUsername( "foobar" ); user.setEmail( "foobar@usergrid.org" ); Entity createUser = em.create( user ); assertNotNull( createUser ); //we create 2 entities, otherwise this test will pass when it shouldn't User user2 = new User(); user2.setUsername( "foobar" ); user2.setEmail( "foobar@usergrid.org" ); em.create( user2 ); }
@Test public void testImmutableForcedPropChange() throws Exception { logger.info( "EntityDaoTest.testProperties" ); EntityManager em = app.getEntityManager(); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put( "name", "one" ); Entity saved = em.create( "thing", properties ); Entity thingOne = em.get( new SimpleEntityRef( "thing", saved.getUuid() ) ); assertNotNull( "entity should not be null", thingOne ); assertEquals( "one", thingOne.getProperty( "name" ).toString() ); em.setProperty( thingOne, "name", "two", true ); Entity thingTwo = em.get( new SimpleEntityRef( "thing", saved.getUuid() ) ); assertEquals( "two", thingTwo.getProperty( "name" ) ); }
@Test public void testFilteringOfDuplicateEdges() throws Exception { logger.info( "EntityManagerIT.testFilteringOfDuplicateEdges" ); EntityManager em = app.getEntityManager(); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put( "name", "fluffy1" ); Entity entity = em.create( "fluffy", properties ); EntityRef appRef = new SimpleEntityRef("application", app.getId()); EntityRef entityRef = new SimpleEntityRef(entity.getType(), entity.getUuid()); assertNotNull( entity ); // create duplicate edges em.addToCollection(appRef, "fluffies", entityRef); em.addToCollection(appRef, "fluffies", entityRef); //app.waitForQueueDrainAndRefreshIndex(); Results results = em.getCollection(appRef, "fluffies", null, 10, Level.ALL_PROPERTIES, true); // we should be filtering duplicate edges so only assert 1 result back and not the # of edges assertEquals(1, results.getEntities().size()); }
private User createUser() throws Exception { String rando = RandomStringUtils.randomAlphanumeric( 10 ); String username = "user_" + rando; String email = username + "@example.com"; Map<String, Object> properties = new HashMap<String, Object>() {{ put( "username", username ); put( "email", email ); }}; EntityManager em = setup.getEmf().getEntityManager( setup.getEmf().getManagementAppId() ); Entity entity = em.create( "user", properties ); return em.get( new SimpleEntityRef( User.ENTITY_TYPE, entity.getUuid() ), User.class ); }