@Test public void testFlatMapMaxConcurrent() { final int m = 4; final AtomicInteger subscriptionCount = new AtomicInteger(); Observable<Integer> source = Observable.range(1, 10) .flatMap( new Func1<Integer, Observable<Integer>>() { @Override public Observable<Integer> call(Integer t1) { return compose(Observable.range(t1 * 10, 2), subscriptionCount, m) .subscribeOn(Schedulers.computation()); } }, m); TestSubscriber<Integer> ts = new TestSubscriber<Integer>(); source.subscribe(ts); ts.awaitTerminalEvent(); ts.assertNoErrors(); Set<Integer> expected = new HashSet<Integer>( Arrays.asList( 10, 11, 20, 21, 30, 31, 40, 41, 50, 51, 60, 61, 70, 71, 80, 81, 90, 91, 100, 101)); Assert.assertEquals(expected.size(), ts.getOnNextEvents().size()); Assert.assertTrue(expected.containsAll(ts.getOnNextEvents())); }
@Test public void testInit_NonEmptyCollection() throws Exception { // Mock ConnectionManager connectionManager = mock(ConnectionManager.class); Connection connection = mock(Connection.class); Connection connection1 = mock(Connection.class); Statement statement = mock(Statement.class); PreparedStatement preparedStatement = mock(PreparedStatement.class); when(connection1.prepareStatement("INSERT OR IGNORE INTO " + TABLE_NAME + " values(?, ?);")) .thenReturn(preparedStatement); when(connectionManager.getConnection(any(SQLiteIndex.class))) .thenReturn(connection) .thenReturn(connection1); when(connectionManager.isApplyUpdateForIndexEnabled(any(SQLiteIndex.class))).thenReturn(true); when(connection.createStatement()).thenReturn(statement); when(preparedStatement.executeBatch()).thenReturn(new int[] {2}); // The objects to add Set<Car> initWithObjects = new HashSet<Car>(2); initWithObjects.add( new Car(1, "Ford", "Focus", Car.Color.BLUE, 5, 9000.50, Arrays.asList("abs", "gps"))); initWithObjects.add( new Car(2, "Honda", "Civic", Car.Color.RED, 5, 5000.00, Arrays.asList("airbags"))); SQLiteIndex<String, Car, Integer> carFeaturesOffHeapIndex = new SQLiteIndex<String, Car, Integer>( Car.FEATURES, OBJECT_TO_ID, ID_TO_OBJECT, connectionManager); carFeaturesOffHeapIndex.init(initWithObjects, new QueryOptions()); // Verify verify(statement, times(1)) .executeUpdate( "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (objectKey INTEGER, value TEXT, PRIMARY KEY (objectKey, value)) WITHOUT ROWID;"); verify(statement, times(1)) .executeUpdate( "CREATE INDEX IF NOT EXISTS " + INDEX_NAME + " ON " + TABLE_NAME + " (value);"); verify(statement, times(2)).close(); verify(connection, times(1)).close(); verify(preparedStatement, times(2)).setObject(1, 1); verify(preparedStatement, times(1)).setObject(1, 2); verify(preparedStatement, times(1)).setObject(2, "abs"); verify(preparedStatement, times(1)).setObject(2, "gps"); verify(preparedStatement, times(1)).setObject(2, "airbags"); verify(preparedStatement, times(3)).addBatch(); verify(preparedStatement, times(1)).executeBatch(); verify(preparedStatement, times(1)).close(); verify(connection1, times(1)).close(); }
@Test public void userServiceReturnsExpectedData() throws Exception { setContext( "<ldap-user-service id='ldapUDS' user-search-filter='(uid={0})' group-search-filter='member={0}' /><ldap-server ldif='classpath:test-server.ldif'/>"); UserDetailsService uds = (UserDetailsService) appCtx.getBean("ldapUDS"); UserDetails ben = uds.loadUserByUsername("ben"); Set<String> authorities = AuthorityUtils.authorityListToSet(ben.getAuthorities()); assertEquals(3, authorities.size()); assertTrue(authorities.contains("ROLE_DEVELOPERS")); }
@Test public void testNotifyObjectsRemoved() throws Exception { // Mock ConnectionManager connectionManager = mock(ConnectionManager.class); Connection connection = mock(Connection.class); Connection connection1 = mock(Connection.class); Statement statement = mock(Statement.class); PreparedStatement preparedStatement = mock(PreparedStatement.class); // Behaviour when(connectionManager.getConnection(any(SQLiteIndex.class))) .thenReturn(connection) .thenReturn(connection1); when(connectionManager.isApplyUpdateForIndexEnabled(any(SQLiteIndex.class))).thenReturn(true); when(connection.createStatement()).thenReturn(statement); when(connection1.prepareStatement("DELETE FROM " + TABLE_NAME + " WHERE objectKey = ?;")) .thenReturn(preparedStatement); when(preparedStatement.executeBatch()).thenReturn(new int[] {1}); // The objects to add Set<Car> removedObjects = new HashSet<Car>(2); removedObjects.add( new Car(1, "Ford", "Focus", Car.Color.BLUE, 5, 9000.50, Arrays.asList("abs", "gps"))); removedObjects.add( new Car(2, "Honda", "Civic", Car.Color.RED, 5, 5000.00, Arrays.asList("airbags"))); @SuppressWarnings({"unchecked", "unused"}) SQLiteIndex<String, Car, Integer> carFeaturesOffHeapIndex = new SQLiteIndex<String, Car, Integer>( Car.FEATURES, OBJECT_TO_ID, ID_TO_OBJECT, connectionManager); carFeaturesOffHeapIndex.removeAll(removedObjects, new QueryOptions()); // Verify verify(statement, times(1)) .executeUpdate( "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (objectKey INTEGER, value TEXT, PRIMARY KEY (objectKey, value)) WITHOUT ROWID;"); verify(statement, times(1)) .executeUpdate( "CREATE INDEX IF NOT EXISTS " + INDEX_NAME + " ON " + TABLE_NAME + " (value);"); verify(connection, times(1)).close(); verify(preparedStatement, times(1)).setObject(1, 1); verify(preparedStatement, times(1)).setObject(1, 2); verify(preparedStatement, times(2)).addBatch(); verify(preparedStatement, times(1)).executeBatch(); verify(connection1, times(1)).close(); }
@Test public void shouldGetRightsForAUserOnHomeFacilityAndProgram() throws Exception { Long userId = 1L; Facility facility = new Facility(2L); Program program = new Program(3L); List<Right> expected = asList(CREATE_REQUISITION); when(facilityService.getHomeFacility(userId)).thenReturn(facility); when(roleRightsRepository.getRightsForUserOnHomeFacilityAndProgram(userId, program)) .thenReturn(expected); Set<Right> result = roleRightsService.getRightsForUserAndFacilityProgram(userId, facility, program); assertThat(result.containsAll(expected), is(true)); verify(roleRightsRepository).getRightsForUserOnHomeFacilityAndProgram(userId, program); }
@Test(timeout = 30000) public void flatMapRangeMixedAsyncLoop() { for (int i = 0; i < 2000; i++) { if (i % 10 == 0) { System.out.println("flatMapRangeAsyncLoop > " + i); } TestSubscriber<Integer> ts = new TestSubscriber<Integer>(); Observable.range(0, 1000) .flatMap( new Func1<Integer, Observable<Integer>>() { final Random rnd = new Random(); @Override public Observable<Integer> call(Integer t) { Observable<Integer> r = Observable.just(t); if (rnd.nextBoolean()) { r = r.asObservable(); } return r; } }) .observeOn(Schedulers.computation()) .subscribe(ts); ts.awaitTerminalEvent(2500, TimeUnit.MILLISECONDS); if (ts.getOnCompletedEvents().isEmpty()) { System.out.println(ts.getOnNextEvents().size()); } ts.assertTerminalEvent(); ts.assertNoErrors(); List<Integer> list = ts.getOnNextEvents(); if (list.size() < 1000) { Set<Integer> set = new HashSet<Integer>(list); for (int j = 0; j < 1000; j++) { if (!set.contains(j)) { System.out.println(j + " missing"); } } } assertEquals(1000, list.size()); } }
@Test public void shouldGetRightsForAUserOnSupervisedFacilityAndProgram() throws Exception { Long userId = 1L; Facility facility = new Facility(2L); Program program = new Program(3L); List<Right> expected = asList(CREATE_REQUISITION); SupervisoryNode supervisoryNode = new SupervisoryNode(4L); List<SupervisoryNode> supervisoryNodes = asList(supervisoryNode); when(supervisoryNodeService.getFor(facility, program)).thenReturn(supervisoryNode); when(supervisoryNodeService.getAllParentSupervisoryNodesInHierarchy(supervisoryNode)) .thenReturn(supervisoryNodes); when(roleRightsRepository.getRightsForUserOnSupervisoryNodeAndProgram( userId, supervisoryNodes, program)) .thenReturn(expected); Set<Right> result = roleRightsService.getRightsForUserAndFacilityProgram(userId, facility, program); verify(roleRightsRepository) .getRightsForUserOnSupervisoryNodeAndProgram(userId, supervisoryNodes, program); assertThat(result.containsAll(expected), is(true)); }