@Test public void foundHeroesShouldContainFrodoAndGandalf() { // given when(characterRepository.getFellowshipOfTheRing()).thenReturn(fellowshipOfTheRing); // when List<Character> heroes = tolkienService.getFellowshipOfTheRing(); // then assertThat(heroes, hasItems(new ReflectionEquals(new Character("Frodo", HOBBIT, LocalDate.of(2968, Month.SEPTEMBER, 22))), new ReflectionEquals(new Character("Gandalf", MAIAR, LocalDate.MIN)))); }
@Test public void pollFirstMultiple() { map.put(IDENTIFIER, blockDefinitionsWithBothNodes); assertThat(map.get(IDENTIFIER).size(), is(2)); Optional<BlockDefinition> definition1 = underTest.pollFirst(IDENTIFIER); assertThat(definition1.isPresent(), is(true)); assertThat(definition1.get(), new ReflectionEquals(blockDefinition1)); assertThat(map.get(IDENTIFIER).size(), is(1)); Optional<BlockDefinition> definition2 = underTest.pollFirst(IDENTIFIER); assertThat(definition2.isPresent(), is(true)); assertThat(definition2.get(), new ReflectionEquals(blockDefinition2)); assertThat(map.get(IDENTIFIER).size(), is(0)); }
@Test public void assertGetJobExecutionShardingContextWhenNotAssignShardingItem() { when(configService.getShardingTotalCount()).thenReturn(3); when(configService.isFailover()).thenReturn(false); when(configService.isMonitorExecution()).thenReturn(false); when(configService.getFetchDataCount()).thenReturn(10); JobExecutionMultipleShardingContext expected = new JobExecutionMultipleShardingContext(); expected.setJobName("testJob"); expected.setShardingTotalCount(3); expected.setFetchDataCount(10); assertThat(executionContextService.getJobExecutionShardingContext(Collections.<Integer>emptyList()), new ReflectionEquals(expected)); verify(configService).getShardingTotalCount(); verify(configService).isMonitorExecution(); verify(configService).getFetchDataCount(); }
@Test public void assertGetJobExecutionShardingContextWhenAssignShardingItems() { when(configService.getShardingTotalCount()).thenReturn(3); when(configService.isFailover()).thenReturn(false); when(configService.isMonitorExecution()).thenReturn(false); when(configService.getFetchDataCount()).thenReturn(10); Map<Integer, String> shardingItemParameters = new HashMap<Integer, String>(3); shardingItemParameters.put(0, "A"); shardingItemParameters.put(1, "B"); shardingItemParameters.put(2, "C"); when(configService.getShardingItemParameters()).thenReturn(shardingItemParameters); Map<Integer, String> offsets = new HashMap<Integer, String>(2); offsets.put(0, "offset0"); offsets.put(1, "offset1"); when(offsetService.getOffsets(Arrays.asList(0, 1))).thenReturn(offsets); JobExecutionMultipleShardingContext expected = new JobExecutionMultipleShardingContext(); expected.setJobName("testJob"); expected.setShardingTotalCount(3); expected.setFetchDataCount(10); expected.setShardingItems(Arrays.asList(0, 1)); expected.getShardingItemParameters().put(0, "A"); expected.getShardingItemParameters().put(1, "B"); expected.setOffsets(offsets); assertThat(executionContextService.getJobExecutionShardingContext(Arrays.asList(0, 1)), new ReflectionEquals(expected)); verify(configService).getShardingTotalCount(); verify(configService).isMonitorExecution(); verify(configService).getFetchDataCount(); verify(configService).getShardingItemParameters(); verify(offsetService).getOffsets(Arrays.asList(0, 1)); }
@Test public void assertGetJobExecutionShardingContextWhenHasRunningItems() { when(configService.getShardingTotalCount()).thenReturn(3); when(configService.isFailover()).thenReturn(true); when(configService.isMonitorExecution()).thenReturn(true); when(jobNodeStorage.isJobNodeExisted("execution/0/running")).thenReturn(false); when(jobNodeStorage.isJobNodeExisted("execution/1/running")).thenReturn(true); when(configService.getFetchDataCount()).thenReturn(10); Map<Integer, String> shardingItemParameters = new HashMap<Integer, String>(3); shardingItemParameters.put(0, "A"); shardingItemParameters.put(1, "B"); shardingItemParameters.put(2, "C"); when(configService.getShardingItemParameters()).thenReturn(shardingItemParameters); Map<Integer, String> offsets = new HashMap<Integer, String>(1); offsets.put(0, "offset0"); when(offsetService.getOffsets(Collections.singletonList(0))).thenReturn(offsets); JobExecutionMultipleShardingContext expected = new JobExecutionMultipleShardingContext(); expected.setJobName("testJob"); expected.setShardingTotalCount(3); expected.setFetchDataCount(10); expected.setShardingItems(Collections.singletonList(0)); expected.getShardingItemParameters().put(0, "A"); expected.setMonitorExecution(true); expected.setOffsets(offsets); assertThat(executionContextService.getJobExecutionShardingContext(Lists.newArrayList(0, 1)), new ReflectionEquals(expected)); verify(configService).getShardingTotalCount(); verify(configService).isMonitorExecution(); verify(jobNodeStorage).isJobNodeExisted("execution/0/running"); verify(jobNodeStorage).isJobNodeExisted("execution/1/running"); verify(configService).getFetchDataCount(); verify(configService).getShardingItemParameters(); verify(offsetService).getOffsets(Collections.singletonList(0)); }
@Test public void assertGetJobExecutionShardingContextWhenAssignShardingItems() { when(configService.getShardingTotalCount()).thenReturn(3); when(configService.isFailover()).thenReturn(false); when(configService.isMonitorExecution()).thenReturn(false); when(configService.getFetchDataCount()).thenReturn(10); Map<Integer, String> shardingItemParameters = new HashMap<>(3); shardingItemParameters.put(0, "A"); shardingItemParameters.put(1, "B"); shardingItemParameters.put(2, "C"); when(configService.getShardingItemParameters()).thenReturn(shardingItemParameters); Map<Integer, String> offsets = new HashMap<>(2); offsets.put(0, "offset0"); offsets.put(1, "offset1"); when(offsetService.getOffsets(Arrays.asList(0, 1))).thenReturn(offsets); JobExecutionMultipleShardingContext expected = new JobExecutionMultipleShardingContext(); expected.setJobName("testJob"); expected.setShardingTotalCount(3); expected.setFetchDataCount(10); expected.setShardingItems(Arrays.asList(0, 1)); expected.getShardingItemParameters().put(0, "A"); expected.getShardingItemParameters().put(1, "B"); expected.setOffsets(offsets); assertThat(executionContextService.getJobExecutionShardingContext(Arrays.asList(0, 1)), new ReflectionEquals(expected)); verify(configService).getShardingTotalCount(); verify(configService).isMonitorExecution(); verify(configService).getFetchDataCount(); verify(configService).getShardingItemParameters(); verify(offsetService).getOffsets(Arrays.asList(0, 1)); }
@Test public void assertGetJobExecutionShardingContextWhenHasRunningItems() { when(configService.getShardingTotalCount()).thenReturn(3); when(configService.isFailover()).thenReturn(true); when(configService.isMonitorExecution()).thenReturn(true); when(jobNodeStorage.isJobNodeExisted("execution/0/running")).thenReturn(false); when(jobNodeStorage.isJobNodeExisted("execution/1/running")).thenReturn(true); when(configService.getFetchDataCount()).thenReturn(10); Map<Integer, String> shardingItemParameters = new HashMap<>(3); shardingItemParameters.put(0, "A"); shardingItemParameters.put(1, "B"); shardingItemParameters.put(2, "C"); when(configService.getShardingItemParameters()).thenReturn(shardingItemParameters); Map<Integer, String> offsets = new HashMap<>(1); offsets.put(0, "offset0"); when(offsetService.getOffsets(Collections.singletonList(0))).thenReturn(offsets); JobExecutionMultipleShardingContext expected = new JobExecutionMultipleShardingContext(); expected.setJobName("testJob"); expected.setShardingTotalCount(3); expected.setFetchDataCount(10); expected.setShardingItems(Collections.singletonList(0)); expected.getShardingItemParameters().put(0, "A"); expected.setMonitorExecution(true); expected.setOffsets(offsets); assertThat(executionContextService.getJobExecutionShardingContext(Lists.newArrayList(0, 1)), new ReflectionEquals(expected)); verify(configService).getShardingTotalCount(); verify(configService).isMonitorExecution(); verify(jobNodeStorage).isJobNodeExisted("execution/0/running"); verify(jobNodeStorage).isJobNodeExisted("execution/1/running"); verify(configService).getFetchDataCount(); verify(configService).getShardingItemParameters(); verify(offsetService).getOffsets(Collections.singletonList(0)); }
@Test public void addLastMultiple() throws Exception { underTest.addLast(blockNode1, resourceReference1); underTest.addLast(blockNode2, resourceReference2); assertThat(map.get(IDENTIFIER).get(0), new ReflectionEquals(blockDefinition1)); assertThat(map.get(IDENTIFIER).get(1), new ReflectionEquals(blockDefinition2)); }
@Test public void addFirstMultiple() throws Exception { underTest.addFirst(blockNode1, resourceReference1); underTest.addFirst(blockNode2, resourceReference2); assertThat(map.get(IDENTIFIER).get(1), new ReflectionEquals(blockDefinition1)); assertThat(map.get(IDENTIFIER).get(0), new ReflectionEquals(blockDefinition2)); }
@Test public void get() { map.put(IDENTIFIER, blockDefinitionsWithNode); Optional<BlockDefinition> definitionA = underTest.get(IDENTIFIER); assertThat(definitionA.isPresent(), is(true)); assertThat(definitionA.get(), new ReflectionEquals(blockDefinition1)); Optional<BlockDefinition> definitionB = underTest.get(IDENTIFIER, 0); assertThat(definitionB.isPresent(), is(true)); assertThat(definitionB.get(), new ReflectionEquals(blockDefinition1)); }
@Test public void getMultiple() { map.put(IDENTIFIER, blockDefinitionsWithBothNodes); Optional<BlockDefinition> definition1 = underTest.get(IDENTIFIER, 0); assertThat(definition1.isPresent(), is(true)); assertThat(definition1.get(), new ReflectionEquals(blockDefinition1)); Optional<BlockDefinition> definition2 = underTest.get(IDENTIFIER, 1); assertThat(definition2.isPresent(), is(true)); assertThat(definition2.get(), new ReflectionEquals(blockDefinition2)); }
@Test public void testSaveResourceCredentials() throws Exception { final APIResourceCredentials credentials1 = new APIResourceCredentials(TestUtils.LOCAL_USER_ID, "tempToken1", "tempTokenSecret1"); final APIResourceCredentials credentials2 = new APIResourceCredentials(TestUtils.LOCAL_USER_ID, "tempToken2", "tempTokenSecret2"); this.fitbitApiCredentialsCacheOdb.saveResourceCredentials(TestUtils.LOCAL_USER_DETAIL, credentials1); assertTrue(new ReflectionEquals(credentials1).matches(this.fitbitApiCredentialsCacheOdb.getResourceCredentials(TestUtils.LOCAL_USER_DETAIL))); this.fitbitApiCredentialsCacheOdb.saveResourceCredentials(TestUtils.LOCAL_USER_DETAIL, credentials2); assertTrue(new ReflectionEquals(credentials2).matches(this.fitbitApiCredentialsCacheOdb.getResourceCredentials(TestUtils.LOCAL_USER_DETAIL))); }
/** * Without this method we would need to override equals() and .hashcode() for each object, which explodes code and makes it less maintainable */ public <T> void assertObjectEquals(final T expected, final T actual) { assertTrue("Expected:\n" + expected + "\nactual:\n" + actual, new ReflectionEquals(actual, (String[])null).matches(expected)); }
@Test public void addLast() throws Exception { underTest.addLast(blockNode1, resourceReference1); assertThat(map.get(IDENTIFIER).peek(), new ReflectionEquals(blockDefinition1)); }
@Test public void addLastFirst() throws Exception { underTest.addFirst(blockNode1, resourceReference1); assertThat(map.get(IDENTIFIER).peek(), new ReflectionEquals(blockDefinition1)); }
@Test public void testGetResourceCredentials() throws Exception { assertNull(this.fitbitApiCredentialsCacheOdb.getResourceCredentials(TestUtils.LOCAL_USER_DETAIL)); this.fitbitApiCredentialsCacheOdb.saveResourceCredentials(TestUtils.LOCAL_USER_DETAIL, this.apiResourceCredentials); assertTrue(new ReflectionEquals(this.apiResourceCredentials).matches(this.fitbitApiCredentialsCacheOdb.getResourceCredentials(TestUtils.LOCAL_USER_DETAIL))); }
@Test public void testGetResourceCredentialsByTempToken() throws Exception { this.fitbitApiCredentialsCacheOdb.saveResourceCredentials(TestUtils.LOCAL_USER_DETAIL, this.apiResourceCredentials); assertTrue(new ReflectionEquals(this.apiResourceCredentials).matches(this.fitbitApiCredentialsCacheOdb.getResourceCredentialsByTempToken("tempToken"))); }
/** * Object argument that is reflection-equal to the given value with support for excluding * selected fields from a class. * <p> * This matcher can be used when equals() is not implemented on compared objects. * Matcher uses java reflection API to compare fields of wanted and actual object. * <p> * Works similarly to EqualsBuilder.reflectionEquals(this, other, exlucdeFields) from * apache commons library. * <p> * <b>Warning</b> The equality check is shallow! * <p> * See examples in javadoc for {@link Matchers} class * * @param value * the given value. * @param excludeFields * fields to exclude, if field does not exist it is ignored. * @return <code>null</code>. */ public static <T> T refEq(T value, String... excludeFields) { return reportMatcher(new ReflectionEquals(value, excludeFields)).<T>returnNull(); }