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

Fix duplicate key exception for class path catalog loader. #42

Merged
merged 1 commit into from
Aug 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
}

}
}