VersionService { @RequestMapping(value = "/version", method = GET) @ApiOperation(value = "Get the version of the service", produces = MediaType.APPLICATION_JSON_VALUE) @Timed @PublicAPI public Version version() { String buildId = manifestInfoProviders .stream() .map(ManifestInfoProvider::getManifestInfo) .map(ManifestInfo::getBuildId) .collect(joining("-")); final Optional<String> uniqueVersion = manifestInfoProviders .stream() .map(ManifestInfoProvider::getManifestInfo) .map(ManifestInfo::getVersionId) .filter(versionId -> !StringUtils.equals(UNDEFINED_VERSION_ID, versionId)) .reduce((s, s2) -> { if (StringUtils.equals(s, s2)) { return s; } return s + '-' + s2; }); String serviceUnifiedVersionId = uniqueVersion.orElse(UNDEFINED_VERSION_ID); return new Version(serviceUnifiedVersionId, buildId); }  @RequestMapping(value = "/version", method = GET) @ApiOperation(value = "Get the version of the service", produces = MediaType.APPLICATION_JSON_VALUE) @Timed @PublicAPI Version version();  }
@Test public void shouldAggregateBuildId() throws Exception { final ManifestInfoProvider provider1 = mock(ManifestInfoProvider.class); final ManifestInfoProvider provider2 = mock(ManifestInfoProvider.class); when(provider1.getManifestInfo()).thenReturn(new ManifestInfo("v1", "1234")); when(provider2.getManifestInfo()).thenReturn(new ManifestInfo("v1", "5678")); ReflectionTestUtils.setField(versionService, "manifestInfoProviders", asList(provider1, provider2)); final Version version = versionService.version(); assertEquals("v1", version.getVersionId()); assertEquals("1234-5678", version.getBuildId()); }
@Test public void shouldAggregateSameBuildId() throws Exception { final ManifestInfoProvider provider1 = mock(ManifestInfoProvider.class); final ManifestInfoProvider provider2 = mock(ManifestInfoProvider.class); when(provider1.getManifestInfo()).thenReturn(new ManifestInfo("v1", "1234")); when(provider2.getManifestInfo()).thenReturn(new ManifestInfo("v1", "1234")); ReflectionTestUtils.setField(versionService, "manifestInfoProviders", asList(provider1, provider2)); final Version version = versionService.version(); assertEquals("v1", version.getVersionId()); assertEquals("1234-1234", version.getBuildId()); }
@Test public void shouldAggregateVersionId() throws Exception { final ManifestInfoProvider provider1 = mock(ManifestInfoProvider.class); final ManifestInfoProvider provider2 = mock(ManifestInfoProvider.class); when(provider1.getManifestInfo()).thenReturn(new ManifestInfo("v1", "1234")); when(provider2.getManifestInfo()).thenReturn(new ManifestInfo("v2", "1234")); ReflectionTestUtils.setField(versionService, "manifestInfoProviders", asList(provider1, provider2)); final Version version = versionService.version(); assertEquals("v1-v2", version.getVersionId()); assertEquals("1234-1234", version.getBuildId()); }
@Test public void shouldShouldSkipMissingVersionId() throws Exception { final ManifestInfoProvider provider1 = mock(ManifestInfoProvider.class); final ManifestInfoProvider provider2 = mock(ManifestInfoProvider.class); when(provider1.getManifestInfo()).thenReturn(new ManifestInfo("v1", "1234")); when(provider2.getManifestInfo()).thenReturn(new ManifestInfo("N/A", "1234")); ReflectionTestUtils.setField(versionService, "manifestInfoProviders", asList(provider1, provider2)); final Version version = versionService.version(); assertEquals("v1", version.getVersionId()); assertEquals("1234-1234", version.getBuildId()); }