Skip to content

Commit 0966a60

Browse files
Merge pull request #571 from BIBSYSDEV/update-nva-commons
Update nva commons
2 parents e7a7c64 + 811118f commit 0966a60

File tree

77 files changed

+594
-513
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+594
-513
lines changed

build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ allprojects {
66
apply plugin: 'nva-publication-api.java-library-conventions'
77
}
88

9+
wrapper {
10+
distributionType = Wrapper.DistributionType.ALL
11+
gradleVersion = "7.2"
12+
}
13+
14+
915
subprojects {
1016
// For using DynamoDB locally
1117
configurations.testImplementation.canBeResolved = true

buildSrc/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ repositories {
88

99
wrapper {
1010
distributionType = Wrapper.DistributionType.ALL
11-
gradleVersion = "7.0.2"
11+
gradleVersion = "7.2"
1212
}
1313

1414
dependencies {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package no.unit.nva.cristin;
2+
3+
import com.fasterxml.jackson.databind.DeserializationFeature;
4+
import com.fasterxml.jackson.databind.MapperFeature;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import nva.commons.core.JsonUtils;
7+
8+
public final class CristinImportConfig {
9+
10+
public static final ObjectMapper eventHandlerObjectMapper = JsonUtils.dtoObjectMapper;
11+
//ObjectMapper that allows us to keep track of the Cristin fields that we have not yet done something with them.
12+
public static final ObjectMapper cristinEntryMapper = objectMapperFailingOnUnknown();
13+
public static final ObjectMapper singleLineObjectMapper = JsonUtils.singleLineObjectMapper;
14+
15+
private CristinImportConfig() {
16+
17+
}
18+
19+
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);
23+
}
24+
}

cristin-import/src/main/java/no/unit/nva/cristin/lambda/CristinEntryEventConsumer.java

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package no.unit.nva.cristin.lambda;
22

3+
import static no.unit.nva.cristin.CristinImportConfig.eventHandlerObjectMapper;
34
import static no.unit.nva.cristin.lambda.constants.HardcodedValues.HARDCODED_PUBLICATIONS_OWNER;
45
import static no.unit.nva.publication.s3imports.ApplicationConstants.MAX_SLEEP_TIME;
56
import static no.unit.nva.publication.s3imports.FileImportUtils.timestampToString;
6-
import static nva.commons.core.JsonUtils.objectMapperWithEmpty;
77
import static nva.commons.core.attempt.Try.attempt;
88
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
99
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
@@ -75,24 +75,24 @@ protected Publication processInput(FileContentsEvent<JsonNode> input,
7575
Context context) {
7676
validateEvent(event);
7777
return attempt(() -> parseCristinObject(event))
78-
.map(CristinObject::toPublication)
79-
.flatMap(this::persistInDatabase)
80-
.orElseThrow(fail -> handleSavingError(fail, event));
78+
.map(CristinObject::toPublication)
79+
.flatMap(this::persistInDatabase)
80+
.orElseThrow(fail -> handleSavingError(fail, event));
8181
}
8282

8383
@JacocoGenerated
8484
private static S3Client defaultS3Client() {
8585
return S3Client.builder()
86-
.httpClient(UrlConnectionHttpClient.create())
87-
.build();
86+
.httpClient(UrlConnectionHttpClient.create())
87+
.build();
8888
}
8989

9090
@JacocoGenerated
9191
private static AmazonDynamoDB defaultDynamoDbClient() {
9292
return AmazonDynamoDBClientBuilder
93-
.standard()
94-
.withRegion(ApplicationConstants.AWS_REGION.id())
95-
.build();
93+
.standard()
94+
.withRegion(ApplicationConstants.AWS_REGION.id())
95+
.build();
9696
}
9797

9898
private CristinObject parseCristinObject(AwsEventBridgeEvent<FileContentsEvent<JsonNode>> event) {
@@ -102,17 +102,15 @@ private CristinObject parseCristinObject(AwsEventBridgeEvent<FileContentsEvent<J
102102
}
103103

104104
private Identifiable parseIdentifiableObject(AwsEventBridgeEvent<FileContentsEvent<JsonNode>> event) {
105-
106105
return attempt(() -> event.getDetail().getContents())
107-
.map(jsonNode ->
108-
objectMapperWithEmpty.convertValue(jsonNode, Identifiable.class))
109-
.orElseThrow();
106+
.map(jsonNode -> eventHandlerObjectMapper.convertValue(jsonNode, Identifiable.class))
107+
.orElseThrow();
110108
}
111109

112110
private CristinObject jsonNodeToCristinObject(AwsEventBridgeEvent<FileContentsEvent<JsonNode>> event) {
113111
return attempt(() -> event.getDetail().getContents())
114-
.map(CristinObject::fromJson)
115-
.orElseThrow();
112+
.map(CristinObject::fromJson)
113+
.orElseThrow();
116114
}
117115

118116
private void validateEvent(AwsEventBridgeEvent<FileContentsEvent<JsonNode>> event) {
@@ -190,24 +188,24 @@ private UriWrapper constructErrorFileUri(AwsEventBridgeEvent<FileContentsEvent<J
190188
Instant timestamp = event.getDetail().getTimestamp();
191189
UriWrapper bucket = fileUri.getHost();
192190
return bucket
193-
.addChild(ERRORS_FOLDER)
194-
.addChild(timestampToString(timestamp))
195-
.addChild(exception.getClass().getSimpleName())
196-
.addChild(fileUri.getPath())
197-
.addChild(createErrorReportFilename(event));
191+
.addChild(ERRORS_FOLDER)
192+
.addChild(timestampToString(timestamp))
193+
.addChild(exception.getClass().getSimpleName())
194+
.addChild(fileUri.getPath())
195+
.addChild(createErrorReportFilename(event));
198196
}
199197

200198
private String createErrorReportFilename(AwsEventBridgeEvent<FileContentsEvent<JsonNode>> event) {
201199
return extractCristinObjectId(event)
202-
.map(idString -> idString + JSON)
203-
.orElseGet(this::unknownCristinIdReportFilename);
200+
.map(idString -> idString + JSON)
201+
.orElseGet(this::unknownCristinIdReportFilename);
204202
}
205203

206204
private Optional<String> extractCristinObjectId(AwsEventBridgeEvent<FileContentsEvent<JsonNode>> event) {
207205
return attempt(() -> parseIdentifiableObject(event))
208-
.map(Identifiable::getId)
209-
.toOptional()
210-
.map(Objects::toString);
206+
.map(Identifiable::getId)
207+
.toOptional()
208+
.map(Objects::toString);
211209
}
212210

213211
private String unknownCristinIdReportFilename() {

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
package no.unit.nva.cristin.mapper;
22

33
import static java.util.Objects.nonNull;
4-
import static nva.commons.core.JsonUtils.objectMapperWithEmpty;
4+
import static no.unit.nva.cristin.CristinImportConfig.cristinEntryMapper;
55
import static nva.commons.core.attempt.Try.attempt;
66
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
77
import com.fasterxml.jackson.annotation.JsonProperty;
8-
import com.fasterxml.jackson.databind.DeserializationFeature;
98
import com.fasterxml.jackson.databind.JsonNode;
10-
import com.fasterxml.jackson.databind.MapperFeature;
11-
import com.fasterxml.jackson.databind.ObjectMapper;
129
import java.time.LocalDate;
1310
import java.util.List;
1411
import lombok.AccessLevel;
@@ -41,9 +38,7 @@ public class CristinObject implements JsonSerializable {
4138
public static final String MAIN_CATEGORY_FIELD = "varbeidhovedkatkode";
4239
public static final String SECONDARY_CATEGORY_FIELD = "varbeidunderkatkode";
4340
public static final String IDENTIFIER_ORIGIN = "Cristin";
44-
private static final ObjectMapper OBJECT_MAPPER_FAIL_ON_UNKNOWN =
45-
objectMapperWithEmpty.copy().configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true)
46-
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
41+
4742
public static final String BOOK_OR_REPORT_METADATA = "type_bok_rapport";
4843

4944
@JsonProperty("id")
@@ -100,6 +95,6 @@ public boolean isPeerReviewed() {
10095
}
10196

10297
public static CristinObject fromJson(JsonNode json) {
103-
return attempt(() -> OBJECT_MAPPER_FAIL_ON_UNKNOWN.convertValue(json, CristinObject.class)).orElseThrow();
98+
return attempt(() -> cristinEntryMapper.convertValue(json, CristinObject.class)).orElseThrow();
10499
}
105100
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package no.unit.nva.cristin;
22

3-
import static nva.commons.core.JsonUtils.objectMapperWithEmpty;
3+
import static no.unit.nva.cristin.CristinImportConfig.eventHandlerObjectMapper;
44
import static nva.commons.core.attempt.Try.attempt;
55
import com.fasterxml.jackson.databind.type.CollectionType;
66
import java.io.BufferedReader;
@@ -19,7 +19,7 @@ public class AbstractCristinImportTest extends ResourcesDynamoDbLocalTest {
1919

2020
public static final Integer NUMBER_OF_LINES_IN_RESOURCES_FILE = 100;
2121
public static final CollectionType CRISTIN_OBJECTS_LIST_JAVATYPE =
22-
objectMapperWithEmpty.getTypeFactory().constructCollectionType(List.class, CristinObject.class);
22+
eventHandlerObjectMapper.getTypeFactory().constructCollectionType(List.class, CristinObject.class);
2323
public static final String TESTING_DATA_INITIALIZATION_ERROR =
2424
"Set the field testingData before calling this method";
2525
public static final String INVALID_DATA_ERROR_MESSAGE = "The 'testingData' field does not contain valid data";
@@ -51,14 +51,14 @@ private Stream<CristinObject> readJsonArray() {
5151
}
5252

5353
private List<CristinObject> parseCristinObjectsArray(String jsonString) {
54-
return attempt(() -> objectMapperWithEmpty.<List<CristinObject>>readValue(jsonString,
55-
CRISTIN_OBJECTS_LIST_JAVATYPE))
54+
return attempt(() -> eventHandlerObjectMapper.<List<CristinObject>>readValue(jsonString,
55+
CRISTIN_OBJECTS_LIST_JAVATYPE))
5656
.orElseThrow();
5757
}
5858

5959
private Stream<CristinObject> readSeriesOfJsonObjects() {
6060
return newContentReader().lines()
61-
.map(attempt(line -> objectMapperWithEmpty.readValue(line, CristinObject.class)))
61+
.map(attempt(line -> eventHandlerObjectMapper.readValue(line, CristinObject.class)))
6262
.map(attempt -> attempt.orElseThrow(this::handleError));
6363
}
6464

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package no.unit.nva.cristin;
22

3+
import static no.unit.nva.cristin.CristinImportConfig.eventHandlerObjectMapper;
4+
import static no.unit.nva.cristin.CristinImportConfig.singleLineObjectMapper;
35
import static no.unit.nva.cristin.mapper.CristinObject.MAIN_CATEGORY_FIELD;
46
import static no.unit.nva.cristin.mapper.CristinObject.PUBLICATION_OWNER_FIELD;
57
import static no.unit.nva.cristin.mapper.CristinObject.SECONDARY_CATEGORY_FIELD;
@@ -14,8 +16,6 @@
1416
import static org.hamcrest.MatcherAssert.assertThat;
1517
import com.fasterxml.jackson.core.JsonProcessingException;
1618
import com.fasterxml.jackson.databind.JsonNode;
17-
import com.fasterxml.jackson.databind.ObjectMapper;
18-
import com.fasterxml.jackson.databind.SerializationFeature;
1919
import com.fasterxml.jackson.databind.node.ObjectNode;
2020
import com.github.javafaker.Faker;
2121
import java.net.URI;
@@ -47,7 +47,6 @@
4747
import no.unit.nva.cristin.mapper.CristinTitle;
4848
import no.unit.nva.events.models.AwsEventBridgeEvent;
4949
import no.unit.nva.publication.s3imports.FileContentsEvent;
50-
import nva.commons.core.JsonUtils;
5150
import org.apache.commons.lang3.RandomStringUtils;
5251

5352
public final class CristinDataGenerator {
@@ -56,8 +55,6 @@ public final class CristinDataGenerator {
5655
public static final Random RANDOM = new Random(System.currentTimeMillis());
5756
public static final Faker FAKER = Faker.instance();
5857
public static final int FIRST_TITLE = 0;
59-
public static final ObjectMapper OBJECT_MAPPER = JsonUtils.objectMapperSingleLine.configure(
60-
SerializationFeature.INDENT_OUTPUT, false);
6158
public static final int USE_WHOLE_ARRAY = -1;
6259
public static final int NUMBER_OF_KNOWN_SECONDARY_CATEGORIES = 1;
6360
public static final String ID_FIELD = "id";
@@ -548,9 +545,8 @@ private static CristinJournalPublicationJournal randomCristinJournalPublicationJ
548545
.build();
549546
}
550547

551-
private static String toJsonString(CristinObject c) {
552-
return attempt(() -> OBJECT_MAPPER.writeValueAsString(c))
553-
.orElseThrow();
548+
private static String toJsonString(CristinObject cristinObject) {
549+
return attempt(() -> singleLineObjectMapper.writeValueAsString(cristinObject)).orElseThrow();
554550
}
555551

556552
private static ObjectNode cristinObjectAsObjectNode(CristinObject cristinObject) throws JsonProcessingException {
@@ -560,13 +556,13 @@ private static ObjectNode cristinObjectAsObjectNode(CristinObject cristinObject)
560556
BOOK_OR_REPORT_PART_METADATA, HRCS_CATEGORIES_AND_ACTIVITIES, CRISTIN_MODIFIED_DATE,
561557
LECTURE_OR_POSTER_METADATA)));
562558

563-
return (ObjectNode) JsonUtils.objectMapperNoEmpty.readTree(cristinObject.toJsonString());
559+
return (ObjectNode) eventHandlerObjectMapper.readTree(cristinObject.toJsonString());
564560
}
565561

566562
private static <T> JsonNode convertToJsonNode(T inputData) {
567563
return inputData instanceof JsonNode
568564
? (JsonNode) inputData
569-
: JsonUtils.objectMapperNoEmpty.convertValue(inputData, JsonNode.class);
565+
: eventHandlerObjectMapper.convertValue(inputData, JsonNode.class);
570566
}
571567

572568
private static CristinSecondaryCategory randomSecondaryCategory() {

0 commit comments

Comments
 (0)