MapRepositoryCustomImpl implements MapRepositoryCustom { @Override public Collection<Feature> searchIDAppAndQuery(String appIdentifier, String query, Integer pageIndex, Integer pageSize) throws QueryException{ Query mongoQuery = createQuery(query,pageIndex, pageSize); Collection<Feature> features = mongoTemplate.find(mongoQuery, Feature.class, collectionPrefixMap + appIdentifier); return features; }  @Override List<Feature> searchByLocation(String appIdentifier,Double longitude, Double latitude, Integer floor); @Override Collection<Feature> searchIDAppAndQuery(String appIdentifier,
			String query, Integer pageIndex, Integer pageSize); @Override Collection<Feature> searchIDAppAndQuery(String appIdentifier,
			String query, Integer pageIndex, Integer pageSize,
			String orderField, String order);  }
@Test public void findByIDAppAndQueryTest() throws Exception{ String idApplication = "1"; String queryJSON = "{ geometry:{ $geoWithin:{ $centerSphere:[ [20.05,20.01] , 0.05]} } }"; Integer pageIndex=5; Integer pageSize= 20; int skipElementsValue = pageIndex*pageSize; Query querySkip=Mockito.mock(Query.class); Query queryLimit=Mockito.mock(Query.class); PowerMockito.whenNew(BasicQuery.class).withArguments(queryJSON).thenReturn(basicQuery); Mockito.when(basicQuery.skip(skipElementsValue)).thenReturn(querySkip); Mockito.when(querySkip.limit(pageSize)).thenReturn(queryLimit); Mockito.when(mongoTemplate.find(queryLimit, Feature.class, collectionMap+idApplication)).thenReturn(features); Collection<Feature> featuresResponse=mapRepositoryCustomImpl.searchIDAppAndQuery(idApplication, queryJSON, pageIndex, pageSize); Mockito.verify(mongoTemplate).find(queryLimit, Feature.class, collectionMap+idApplication); Assert.assertEquals("Features must be equals", featuresResponse, features); }
@SuppressWarnings("unchecked") @Test(expected=QueryException.class) public void findByIDAppAndQueryErrorQueryTesTest() throws Exception{ String idApplication = "1"; String queryJSON = "{ geometry:{ $geoWithin: $centerSphere:[ [20.05,20.01] , 0.05]} } }"; Integer pageIndex=5; Integer pageSize= 20; PowerMockito.whenNew(BasicQuery.class).withArguments(queryJSON).thenThrow(Exception.class); mapRepositoryCustomImpl.searchIDAppAndQuery(idApplication, queryJSON, pageIndex, pageSize); }
@Test public void findByIDAppAndQueryWithOrderTest() throws Exception{ String idApplication = "1"; String queryJSON = "{ geometry:{ $geoWithin:{ $centerSphere:[ [20.05,20.01] , 0.05]} } }"; Integer pageIndex=5; Integer pageSize= 20; int skipElementsValue = pageIndex*pageSize; String orderField = "_id"; String order = "ASC"; Query querySkip=Mockito.mock(Query.class); Query queryLimit=Mockito.mock(Query.class); Query query=Mockito.mock(Query.class); Sort sort = Mockito.mock(Sort.class); PowerMockito.whenNew(BasicQuery.class).withArguments(queryJSON).thenReturn(basicQuery); Mockito.when(basicQuery.skip(skipElementsValue)).thenReturn(querySkip); Mockito.when(querySkip.limit(pageSize)).thenReturn(queryLimit); PowerMockito.whenNew(Sort.class).withArguments(Sort.Direction.valueOf(order), orderField).thenReturn(sort); Mockito.when(queryLimit.with(sort)).thenReturn(query); Mockito.when(mongoTemplate.find(query, Feature.class, collectionMap+idApplication)).thenReturn(features); Collection<Feature> featuresResponse=mapRepositoryCustomImpl.searchIDAppAndQuery(idApplication, queryJSON, pageIndex, pageSize, orderField, order); Mockito.verify(mongoTemplate).find(query, Feature.class, collectionMap+idApplication); Assert.assertEquals("Features must be equals", featuresResponse, features); }
@SuppressWarnings("unchecked") @Test(expected=QueryException.class) public void findByIDAppAndQueryErrorQueryTest() throws Exception{ String idApplication = "1"; String queryJSON = "{ geometry:{ $geoWithin: $centerSphere:[ [20.05,20.01] , 0.05]} } }"; Integer pageIndex=5; Integer pageSize= 20; String orderField = "_id"; String order = "ASC"; PowerMockito.whenNew(BasicQuery.class).withArguments(queryJSON).thenThrow(Exception.class); mapRepositoryCustomImpl.searchIDAppAndQuery(idApplication, queryJSON, pageIndex, pageSize, orderField, order); }