@Before public void setUp() { cfg = new HashMap<String, Object>(TestHelper.getDefaultTestSettings()); cfg.put(OgmProperties.DATABASE, "snafu"); cfg.put(OgmProperties.USERNAME, "foo"); cfg.put(OgmProperties.PASSWORD, "bar"); cfg.put("hibernate.ogm.mongodb.driver.serverSelectionTimeout", "1000"); }
private int expectedAssociationNumber() { if (EnumSet.of(NEO4J_EMBEDDED, NEO4J_REMOTE).contains(TestHelper.getCurrentDialectType())) { // In Neo4j relationships are bidirectional return 1; } else { return 2; } }
private Configuration createConfiguration(Class<?>[] entityTypes) { if (entityTypes == null || entityTypes.length == 0) { throw new IllegalArgumentException("Define at least a single annotated entity"); } OgmConfiguration configuration = TestHelper.getDefaultTestConfiguration(entityTypes); invokeTestConfigurationMethod(configuration); return configuration; }
@Test public void associationStorageSetOnPropertyLevelViaApiTakesPrecedenceOverAnnotation() throws Exception { Map<String, Object> settings = new HashMap<String, Object>(); TestHelper.configureOptionsFor(settings, getDocumentDatastoreConfiguration()) .entity(AnnotatedCloud.class) .property("backupSnowFlakes", ElementType.METHOD) .associationStorage(AssociationStorageType.IN_ENTITY); sessions = TestHelper.getDefaultTestSessionFactory( settings, AnnotatedCloud.class, Cloud.class, SnowFlake.class); createCloudWithTwoProducedAndOneBackupSnowflake(); assertThat(associationDocumentCount()).isEqualTo(0); assertThat(inEntityAssociationCount()).isEqualTo(2); }
@Test @SkipByDatastoreProvider(DatastoreProviderType.FONGO) public void testDefaultAuthenticationMechanism() throws Exception { LeakingMongoDBDatastoreProvider provider = new LeakingMongoDBDatastoreProvider(); cfg.put(OgmProperties.DATASTORE_PROVIDER, provider); // will start the service TestHelper.getDefaultTestStandardServiceRegistry(cfg).getService(DatastoreProvider.class); assertThat(provider.leakingClient.getCredentialsList().get(0).getMechanism()).isEqualTo(null); }
@Test @SkipByDatastoreProvider(DatastoreProviderType.FONGO) public void testConnectionErrorWrappedInHibernateException() throws Exception { cfg.put(OgmProperties.HOST, NON_EXISTENT_IP); error.expect(ServiceException.class); error.expectMessage("OGM000071"); // nested exception error.expectCause(hasMessage(containsString("OGM001214"))); // will start the service TestHelper.getDefaultTestStandardServiceRegistry(cfg).getService(DatastoreProvider.class); }
@After public void removeCloudAndSnowflakes() { Session session = sessions.openSession(); Transaction transaction = session.beginTransaction(); if (cloud != null) { Cloud cloudToDelete = (Cloud) session.get(Cloud.class, cloud.getId()); for (SnowFlake current : cloudToDelete.getProducedSnowFlakes()) { session.delete(current); } for (SnowFlake current : cloudToDelete.getBackupSnowFlakes()) { session.delete(current); } session.delete(cloudToDelete); } transaction.commit(); session.close(); assertThat(TestHelper.getNumberOfEntities(sessions)).isEqualTo(0); assertThat(TestHelper.getNumberOfAssociations(sessions)).isEqualTo(0); }
@Test @SkipByDatastoreProvider(DatastoreProviderType.FONGO) public void testNotRecognizedAuthenticationMechanism() throws Exception { cfg.put(MongoDBProperties.AUTHENTICATION_MECHANISM, "alhdfoiehfnl"); error.expect(ServiceException.class); error.expectMessage("OGM000072"); // nested exception error.expectCause(hasMessage(containsString("OGM000051"))); // will start the service TestHelper.getDefaultTestStandardServiceRegistry(cfg).getService(DatastoreProvider.class); }
@Test public void associationStorageSetToAssociationDocumentOnGlobalLevel() throws Exception { Map<String, Object> settings = new HashMap<String, Object>(); TestHelper.configureOptionsFor(settings, getDocumentDatastoreConfiguration()) .associationStorage(AssociationStorageType.ASSOCIATION_DOCUMENT); setupSessionFactory(settings); createCloudWithTwoProducedSnowflakes(); assertThat(associationDocumentCount()).isEqualTo(1); assertThat(inEntityAssociationCount()).isEqualTo(0); }
@Test @SkipByDatastoreProvider(DatastoreProviderType.FONGO) public void testSCRAMSHA1AuthenticationMechanism() throws Exception { LeakingMongoDBDatastoreProvider provider = new LeakingMongoDBDatastoreProvider(); cfg.put( MongoDBProperties.AUTHENTICATION_MECHANISM, AuthenticationMechanismType.SCRAM_SHA_1.name()); cfg.put(OgmProperties.DATASTORE_PROVIDER, provider); // will start the service TestHelper.getDefaultTestStandardServiceRegistry(cfg).getService(DatastoreProvider.class); assertThat(provider.leakingClient.getCredentialsList().get(0).getMechanism()) .isEqualTo(MongoCredential.SCRAM_SHA_1_MECHANISM); }
@Test @SkipByDatastoreProvider(DatastoreProviderType.FONGO) public void testAuthentication() throws Exception { error.expect(ServiceException.class); error.expectMessage("OGM000071"); // the timeout exception thrown by the driver will actually contain some information about the // authentication // error. Obviously quite fragile. Might change error.expectCause(hasMessage(containsString("Exception authenticating"))); StandardServiceRegistry serviceRegistry = TestHelper.getDefaultTestStandardServiceRegistry(cfg); // will start the service serviceRegistry.getService(DatastoreProvider.class); }
@Test @SkipByDatastoreProvider(DatastoreProviderType.FONGO) public void testConnectionTimeout() { cfg.put(OgmProperties.HOST, NON_EXISTENT_IP); cfg.put(OgmProperties.DATABASE, "ogm_test_database"); error.expect(ServiceException.class); error.expectMessage("OGM000071"); // the timeout exception thrown by the driver will actually contain some information about the // authentication // error. Obviously quite fragile. Might change error.expectCause(hasMessage(containsString("Timed out"))); // will start the service TestHelper.getDefaultTestStandardServiceRegistry(cfg).getService(DatastoreProvider.class); }
@Override public void run(RunNotifier notifier) { if (isTestScopedSessionFactoryRequired()) { testScopedSessionFactory = buildSessionFactory(); injectSessionFactory(null, testScopedFactoryFields, testScopedSessionFactory); } try { super.run(notifier); } finally { if (testScopedSessionFactory != null) { cleanUpPendingTransactionIfRequired(); TestHelper.dropSchemaAndDatabase(testScopedSessionFactory); testScopedSessionFactory.close(); } } }
@Test @SkipByGridDialect( value = {GridDialectType.REDIS_HASH}, comment = "Only Redis JSON supports in-entity association storage") public void associationStorageSetToInEntityOnGlobalLevel() throws Exception { Map<String, Object> settings = new HashMap<String, Object>(); TestHelper.configureOptionsFor(settings, getDocumentDatastoreConfiguration()) .associationStorage(AssociationStorageType.IN_ENTITY); setupSessionFactory(settings); createCloudWithTwoProducedSnowflakes(); assertThat(associationDocumentCount()).isEqualTo(0); assertThat(inEntityAssociationCount()).isEqualTo(1); }
@Test public void testJNDIReference() throws Exception { final EntityManagerFactory emf = Persistence.createEntityManagerFactory("ogm", TestHelper.getEnvironmentProperties()); SessionFactory factory = ((HibernateEntityManagerFactory) emf).getSessionFactory(); Reference reference = factory.getReference(); assertThat(reference.getClassName()).isEqualTo(OgmSessionFactoryImpl.class.getName()); assertThat(reference.getFactoryClassName()) .isEqualTo(OgmSessionFactoryObjectFactory.class.getName()); assertThat(reference.get(0)).isNotNull(); assertThat(reference.getFactoryClassLocation()).isNull(); OgmSessionFactoryObjectFactory objFactory = new OgmSessionFactoryObjectFactory(); SessionFactory factoryFromRegistry = (SessionFactory) objFactory.getObjectInstance(reference, null, null, null); assertThat(factoryFromRegistry.getClass()).isEqualTo(OgmSessionFactoryImpl.class); assertThat(factoryFromRegistry.getReference()).isEqualTo(factory.getReference()); emf.close(); }
@Test @SkipByGridDialect( value = {GridDialectType.REDIS_HASH}, comment = "Only Redis JSON supports in-entity association storage") public void associationStorageSetOnPropertyLevelTakesPrecedenceOverEntityLevel() throws Exception { Map<String, Object> settings = new HashMap<String, Object>(); TestHelper.configureOptionsFor(settings, getDocumentDatastoreConfiguration()) .entity(Cloud.class) .associationStorage(AssociationStorageType.IN_ENTITY) .property("backupSnowFlakes", ElementType.METHOD) .associationStorage(AssociationStorageType.ASSOCIATION_DOCUMENT); setupSessionFactory(settings); createCloudWithTwoProducedAndOneBackupSnowflake(); assertThat(associationDocumentCount()).isEqualTo(1); assertThat(inEntityAssociationCount()).isEqualTo(1); }
@Test public void testWrappedFromEntityManagerAPI() throws Exception { final EntityManagerFactory emf = Persistence.createEntityManagerFactory("ogm", TestHelper.getEnvironmentProperties()); assertThat(HibernateEntityManagerFactory.class.isAssignableFrom(emf.getClass())).isTrue(); SessionFactory factory = ((HibernateEntityManagerFactory) emf).getSessionFactory(); assertThat(factory.getClass()).isEqualTo(OgmSessionFactoryImpl.class); Session s = factory.openSession(); assertThat(s.getClass()).isEqualTo(OgmSessionImpl.class); assertThat(s.getSessionFactory().getClass()).isEqualTo(OgmSessionFactoryImpl.class); s.close(); EntityManager em = emf.createEntityManager(); assertThat(em.unwrap(Session.class) instanceof OgmSession); assertThat(em.getDelegate().getClass()).isEqualTo(OgmSessionImpl.class); em.close(); emf.close(); }
/** * Provides a default {@link OgmConfiguration} for tests, using the given set of annotated entity * types. * * @param entityTypes the entity types for which to build a configuration * @return a default configuration based on the given types */ public static OgmConfiguration getDefaultTestConfiguration(Class<?>... entityTypes) { OgmConfiguration configuration = new OgmConfiguration(); // by default use the new id generator scheme... configuration.setProperty(Configuration.USE_NEW_ID_GENERATOR_MAPPINGS, "true"); for (Map.Entry<String, String> entry : TestHelper.getEnvironmentProperties().entrySet()) { configuration.setProperty(entry.getKey(), entry.getValue()); } configuration.setProperty(Environment.HBM2DDL_AUTO, "none"); // volatile indexes for Hibernate Search (if used) configuration.setProperty("hibernate.search.default.directory_provider", "ram"); // disable warnings about unspecified Lucene version configuration.setProperty("hibernate.search.lucene_version", "LUCENE_35"); for (Class<?> aClass : entityTypes) { configuration.addAnnotatedClass(aClass); } return configuration; }
private void setupSessionFactory() { sessions = TestHelper.getDefaultTestSessionFactory( AnnotatedCloud.class, PolarCloud.class, SnowFlake.class); }
private void setupSessionFactory(Map<String, Object> settings) { sessions = TestHelper.getDefaultTestSessionFactory(settings, Cloud.class, SnowFlake.class); }
@Before public void setUp() { emf = Persistence.createEntityManagerFactory("ogm", TestHelper.getDefaultTestSettings()); em = emf.createEntityManager(); }