Skip to content

Commit 384cf16

Browse files
Merge pull request #572 from BIBSYSDEV/NP-3276-EXCEPTION-FOR-MISSING-CONTRIBUTORS
Np 3276 exception for missing contributors
2 parents 519648f + 3df0258 commit 384cf16

File tree

5 files changed

+50
-10
lines changed

5 files changed

+50
-10
lines changed

cristin-import/src/main/java/no/unit/nva/cristin/CristinImportConfig.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ private CristinImportConfig() {
1717
}
1818

1919
private static ObjectMapper objectMapperFailingOnUnknown() {
20-
return JsonUtils.dtoObjectMapper.
21-
copy().configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true)
22-
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
20+
return JsonUtils.dtoObjectMapper
21+
.copy().configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true)
22+
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
2323
}
2424
}

cristin-import/src/main/java/no/unit/nva/cristin/mapper/CristinMapper.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import no.unit.nva.cristin.mapper.nva.NvaReportBuilder;
4040
import no.unit.nva.cristin.mapper.nva.exceptions.InvalidIsbnRuntimeException;
4141
import no.unit.nva.cristin.mapper.nva.exceptions.InvalidIssnRuntimeException;
42+
import no.unit.nva.cristin.mapper.nva.exceptions.MissingContributorsException;
4243
import no.unit.nva.model.AdditionalIdentifier;
4344
import no.unit.nva.model.Contributor;
4445
import no.unit.nva.model.EntityDescription;
@@ -164,11 +165,15 @@ private EntityDescription generateEntityDescription() {
164165
}
165166

166167
private List<Contributor> extractContributors() {
168+
if (isNull(cristinObject.getContributors())) {
169+
throw new MissingContributorsException();
170+
}
167171
return cristinObject.getContributors()
168-
.stream()
169-
.map(attempt(CristinContributor::toNvaContributor))
170-
.map(Try::orElseThrow)
171-
.collect(Collectors.toList());
172+
.stream()
173+
.map(attempt(CristinContributor::toNvaContributor))
174+
.map(Try::orElseThrow)
175+
.collect(Collectors.toList());
176+
172177
}
173178

174179
private List<URI> generateNvaHrcsCategoriesAndActivities() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package no.unit.nva.cristin.mapper.nva.exceptions;
2+
3+
public class MissingContributorsException extends RuntimeException {
4+
5+
public static final String MISSING_CONTRIBUTORS_ERROR_MESSAGE =
6+
"No contributors present. All publications must have contributors.";
7+
8+
public MissingContributorsException() {
9+
super(MISSING_CONTRIBUTORS_ERROR_MESSAGE);
10+
}
11+
}

cristin-import/src/test/java/no/unit/nva/cristin/CristinDataGenerator.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,12 @@ public static JsonNode objectWithInvalidIssn() throws JsonProcessingException {
263263
return cristinObjectAsObjectNode(cristinObject);
264264
}
265265

266+
public static JsonNode objectWithoutContributors() throws JsonProcessingException {
267+
CristinObject cristinObject = randomBook();
268+
cristinObject.setContributors(null);
269+
return eventHandlerObjectMapper.readTree(cristinObject.toJsonString());
270+
}
271+
266272
public static CristinObject newCristinObjectWithRoleCode(CristinContributorRoleCode roleCode) {
267273
return createObjectWithCristinContributorRoleCode(0, createContributors(roleCode));
268274
}
@@ -665,4 +671,5 @@ private static String randomDoiSuffix() {
665671
MAX_DOI_PREFIX_SUBPART_LENGTH))
666672
.collect(Collectors.joining(DOI_SUBPART_DELIMITER));
667673
}
674+
668675
}

cristin-import/src/test/java/no/unit/nva/cristin/lambda/CristinEntryEventConsumerTest.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import no.unit.nva.cristin.mapper.nva.exceptions.InvalidIssnRuntimeException;
5050
import no.unit.nva.cristin.mapper.nva.exceptions.UnsupportedMainCategoryException;
5151
import no.unit.nva.cristin.mapper.nva.exceptions.UnsupportedSecondaryCategoryException;
52+
import no.unit.nva.cristin.mapper.nva.exceptions.MissingContributorsException;
5253
import no.unit.nva.events.models.AwsEventBridgeEvent;
5354
import no.unit.nva.model.Publication;
5455
import no.unit.nva.model.PublicationStatus;
@@ -299,6 +300,21 @@ public void handlerThrowsInvalidIssnRuntimeExceptionWhenTheIssnIsInvalid() throw
299300
assertThat(cause, is(instanceOf(InvalidIssnRuntimeException.class)));
300301
}
301302

303+
@Test
304+
public void handlerThrowsMissingContributorsRuntimeExceptionWhenTheCristinObjectHasNoContributors()
305+
throws JsonProcessingException {
306+
JsonNode cristinObjectWithoutContributors = CristinDataGenerator.objectWithoutContributors();
307+
AwsEventBridgeEvent<FileContentsEvent<JsonNode>> awsEvent =
308+
CristinDataGenerator.toAwsEvent(cristinObjectWithoutContributors);
309+
InputStream inputStream = IoUtils.stringToStream(awsEvent.toJsonString());
310+
311+
Executable action = () -> handler.handleRequest(inputStream, outputStream, CONTEXT);
312+
313+
RuntimeException exception = assertThrows(RuntimeException.class, action);
314+
Throwable cause = exception.getCause();
315+
assertThat(cause, is(instanceOf(MissingContributorsException.class)));
316+
}
317+
302318
@Test
303319
public void handlerCreatesFileWithCustomNameWhenCristinIdIsNotFound() throws JsonProcessingException {
304320
JsonNode cristinObjectWithoutId = CristinDataGenerator.objectWithoutId();
@@ -451,9 +467,10 @@ private AwsEventBridgeEvent<FileContentsEvent<Identifiable>> parseEventAsIdentif
451467
throws JsonProcessingException {
452468
JavaType fileContentsType = eventHandlerObjectMapper.getTypeFactory()
453469
.constructParametricType(FileContentsEvent.class, Identifiable.class);
454-
JavaType eventType = eventHandlerObjectMapper.getTypeFactory().constructParametricType(AwsEventBridgeEvent.class,
455-
fileContentsType);
456-
AwsEventBridgeEvent<FileContentsEvent<Identifiable>> event = eventHandlerObjectMapper.readValue(input, eventType);
470+
JavaType eventType = eventHandlerObjectMapper.getTypeFactory()
471+
.constructParametricType(AwsEventBridgeEvent.class, fileContentsType);
472+
AwsEventBridgeEvent<FileContentsEvent<Identifiable>> event =
473+
eventHandlerObjectMapper.readValue(input, eventType);
457474
return event;
458475
}
459476

0 commit comments

Comments
 (0)