Skip to content
This repository has been archived by the owner on May 26, 2020. It is now read-only.

Commit

Permalink
Merge c3b78e5 into bc76883
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinYSpasov committed Aug 9, 2018
2 parents bc76883 + c3b78e5 commit 0242df5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.function.BinaryOperator;

import com.fasterxml.jackson.databind.ObjectMapper;

Expand Down Expand Up @@ -43,8 +44,9 @@ public ClasspathCatalogLoader(final ObjectMapper objectMapper,
* @return All found {@link Catalog}s mapped by their URIs
*/
public Map<URI, Catalog> getCatalogs() {
final BinaryOperator<Catalog> duplicateKeyMapper = (c1, c2) -> c1;
return listAllCatalogsFromClasspath().stream()
.collect(toMap(urlConverter::toUri, this::loadCatalog));
.collect(toMap(urlConverter::toUri, this::loadCatalog, duplicateKeyMapper));
}

private Catalog loadCatalog(final URL catalogUrl) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package uk.gov.justice.schema.catalog;

import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import uk.gov.justice.schema.catalog.domain.Catalog;
Expand Down Expand Up @@ -49,7 +51,7 @@ public void shouldThrowExceptionIfLoadingFileThrowsIOException() throws Exceptio
final URL url = new URL("file://src/code/my-file.txt");
final URI uri = url.toURI();

when(classpathResourceLoader.getResources( "META-INF/schema_catalog.json")).thenReturn(singletonList(url));
when(classpathResourceLoader.getResources("META-INF/schema_catalog.json")).thenReturn(singletonList(url));
when(urlConverter.toUri(url)).thenReturn(uri);
when(objectMapper.readValue(url, Catalog.class)).thenThrow(ioException);

Expand Down Expand Up @@ -78,4 +80,22 @@ public void shouldThrowExceptionIfLoadingResourcesThrowsIOException() throws Exc

}
}

@Test
public void shouldNotThrowExceptionIfDuplicateKeyAdded() throws Exception {

final URL url = new URL("file://src/code/my-file.txt");
final URI uri = url.toURI();

when(classpathResourceLoader.getResources("META-INF/schema_catalog.json")).thenReturn(asList(url, url));
when(urlConverter.toUri(url)).thenReturn(uri);
when(objectMapper.readValue(url, Catalog.class)).thenReturn(mock(Catalog.class));

try {
classpathCatalogLoader.getCatalogs();
} catch (final Exception notExpected) {
fail("Should not have thrown any exception");
}

}
}

0 comments on commit 0242df5

Please sign in to comment.