ReactomeClient { public ImmutableMap<String, String> getPathwayNames(Collection<String> stableIds) { ImmutableMap.Builder<String, String> mappedStableIdsBuilder = ImmutableMap.builder(); for (List<String> partition : getPartitions(stableIds)) { mappedStableIdsBuilder.putAll(fetchPathwayNames(partition)); } return getMappedIds(stableIds, mappedStableIdsBuilder); } @Inject  ReactomeClient(RestTemplate restTemplate); Optional<String> getPathwayName(String reactomeId); Optional<String> getPlantPathwayName(String reactomeId); ImmutableMap<String, String> getPathwayNames(Collection<String> stableIds); ImmutableMap<String, String> getPlantPathwayNames(Collection<String> stableIds);  }
@Test public void returnEmptyForAnEmptyCollection() { assertThat(subject.getPathwayNames(Collections.emptySet()).entrySet(), hasSize(0)); }
@Test public void skipUnfoundStableIds() { when(restTemplateMock.postForObject(eq(ReactomeClient.URL), anyString(), eq(String.class))) .thenReturn("[" + "{\"stId\":\"R-FOO-111\", \"displayName\":\"Bar signal\"}, " + "{\"stId\":\"R-FOO-333\", \"displayName\":\"Bar regulator\"}]"); assertThat( subject.getPathwayNames(ImmutableSet.of("R-FOO-333", "R-FOO-111", "R-FOO-666")).entrySet(), hasSize(2)); }
@Test public void returnEmptyIfSomethingGoesWrong() { when(restTemplateMock.postForObject(eq(ReactomeClient.URL), anyString(), eq(String.class))) .thenThrow(new RestClientException("Timeout!")); assertThat(subject.getPathwayNames(ImmutableSet.of("R-FOO-123")).entrySet(), hasSize(0)); }
@Test public void returnEmptyIfInvalidResponse() { when(restTemplateMock.postForObject(eq(ReactomeClient.URL), anyString(), eq(String.class))) .thenReturn("[{\"stId\":\"R-FOO-111\", \"displayName\":\"Bar signal\"}"); assertThat(subject.getPathwayNames(ImmutableSet.of("R-FOO-111")).entrySet(), hasSize(0)); }
@Test public void returnEmptyIfEmptyResponse() { when(restTemplateMock.postForObject(eq(ReactomeClient.URL), anyString(), eq(String.class))).thenReturn(""); assertThat(subject.getPathwayNames(ImmutableSet.of("R-FOO-123")).entrySet(), hasSize(0)); }
@Test public void fetchPathways() { ImmutableSet<String> pathwayIds = ImmutableSet.of("R-FOO-111", "R-FOO-222", "R-FOO-333"); when(restTemplateMock.postForObject( eq(ReactomeClient.URL), eq(pathwayIds.stream().collect(Collectors.joining(","))), eq(String.class))) .thenReturn("[" + "{\"stId\":\"R-FOO-111\", \"displayName\":\"Bar signal\"}, " + "{\"stId\":\"R-FOO-222\", \"displayName\":\"Bar receptor\"}, " + "{\"stId\":\"R-FOO-333\", \"displayName\":\"Bar regulator\"}]"); Map<String, String> result = subject.getPathwayNames(pathwayIds); assertThat(result.keySet(), hasSize(3)); assertThat(result, hasEntry(is("R-FOO-111"), is("Bar signal"))); assertThat(result, hasEntry(is("R-FOO-222"), is("Bar receptor"))); assertThat(result, hasEntry(is("R-FOO-333"), is("Bar regulator"))); }
@Test public void makesRequestsInBatchesOfQueryMaxSize() { when(restTemplateMock.postForObject(eq(ReactomeClient.URL), anyString(), eq(String.class))).thenReturn(""); subject.getPathwayNames(SET_OF_45_STABLE_IDS); int numberOfCalls = SET_OF_45_STABLE_IDS.size() / ReactomeClient.QUERY_MAX_SIZE + 1; ArgumentCaptor<String> postData = ArgumentCaptor.forClass(String.class); verify(restTemplateMock, times(numberOfCalls)) .postForObject(eq(ReactomeClient.URL), postData.capture(), eq(String.class)); for (int i = 0; i < numberOfCalls - 1; i++) { assertThat(postData.getAllValues().get(i).split(",").length, is(ReactomeClient.QUERY_MAX_SIZE)); } assertThat( postData.getAllValues().get(numberOfCalls - 1).split(",").length, is(SET_OF_45_STABLE_IDS.size() % ReactomeClient.QUERY_MAX_SIZE)); }