FeatureRepositoryCustomImpl implements FeatureRepositoryCustom { @Override public Feature save(String idApplication, Feature feature) throws MongoGeospatialException { if(!mongoTemplate.collectionExists(collectionName+idApplication)){ DBCollection dbCollection = mongoTemplate.getCollection(collectionName+idApplication); DBObject obj = new BasicDBObject(); obj.put("geometry", "2dsphere"); dbCollection.ensureIndex(obj); } try{ mongoTemplate.save(feature, collectionName+idApplication); } catch(UncategorizedMongoDbException uncategorizedMongoDbException){ throw new MongoGeospatialException(); } return feature; }  @Override Feature save(String idApplication, Feature feature); @Override void delete(String idApplication, String idFeature); @Override Feature findByApplicationIdentifierAndId(String idApplication, String idFeature); @Override Collection<Feature> searchIDAppAndQuery(String idApplication,String query,Integer pageIndex, Integer pageSize); @Override Collection<Feature> searchIDAppAndQuery(String idApplication,String query,Integer pageIndex, Integer pageSize, String orderField, String order);  }
@Test public void saveIndexAndFeatureByAppIdTest() throws Exception{ String idApplication = "1"; Feature feature=Mockito.mock(Feature.class); DBCollection dbCollection=Mockito.mock(DBCollection.class); BasicDBObject obj=Mockito.mock(BasicDBObject.class); Mockito.when(mongoTemplate.collectionExists(collectionName+idApplication)).thenReturn(false); Mockito.when(mongoTemplate.getCollection(collectionName+idApplication)).thenReturn(dbCollection); PowerMockito.whenNew(BasicDBObject.class).withNoArguments().thenReturn(obj); Mockito.when(obj.put("geometry", "2dsphere")).thenReturn(dbCollection); featureRepository.save(idApplication, feature); Mockito.verify(mongoTemplate).collectionExists(collectionName+idApplication); Mockito.verify(mongoTemplate).save(feature,collectionName+idApplication); }
@Test public void saveFeatureByAppIdTest() throws MongoGeospatialException{ String idApplication = "1"; Feature feature=Mockito.mock(Feature.class); Mockito.when(mongoTemplate.collectionExists(collectionName+idApplication)).thenReturn(true); featureRepository.save(idApplication, feature); Mockito.verify(mongoTemplate).collectionExists(collectionName+idApplication); Mockito.verify(mongoTemplate).save(feature,collectionName+idApplication); }
@Test(expected=MongoGeospatialException.class) public void shouldThrowErrorWhenGeospatialIndexNotCreated() throws Exception{ String idApplication = "1"; Feature feature=Mockito.mock(Feature.class); DBCollection dbCollection=Mockito.mock(DBCollection.class); BasicDBObject obj=Mockito.mock(BasicDBObject.class); Mockito.when(mongoTemplate.collectionExists(collectionName+idApplication)).thenReturn(false); Mockito.when(mongoTemplate.getCollection(collectionName+idApplication)).thenReturn(dbCollection); PowerMockito.whenNew(BasicDBObject.class).withNoArguments().thenReturn(obj); Mockito.doThrow(UncategorizedMongoDbException.class).when(mongoTemplate).save(feature,collectionName+idApplication); featureRepository.save(idApplication, feature); Mockito.verify(mongoTemplate).collectionExists(collectionName+idApplication); Mockito.verify(mongoTemplate).save(feature,collectionName+idApplication); }