Skip to content
This repository was archived by the owner on Oct 31, 2024. It is now read-only.
Open
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 @@ -10,7 +10,7 @@ plugins {

group 'com.github.bibsysdev'

version '0.19.27'
version '0.19.28'

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,85 +1,76 @@
package no.unit.nva.model;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

import java.net.URI;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;

import nva.commons.core.JacocoGenerated;

import static java.util.Objects.isNull;

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
public class Organization implements Agent {

@JsonProperty("id")
private URI id;
@JsonProperty("labels")
private Map<String, String> labels;

public Organization() {
public static final String ID_FIELD = "id";
public static final String LABELS_FIELD = "labels";
@JsonProperty(ID_FIELD)
private final URI id;
@JsonProperty(LABELS_FIELD)
private final Map<String, String> labels;

@Deprecated
@JsonCreator
public static Organization fromJson(@JsonProperty(ID_FIELD) URI id,
@JsonProperty(LABELS_FIELD) Map<String, String> labels) {
if (isNull(id)) {
return null;
}
return new Organization(id, labels);
}

private Organization(Builder builder) {
setId(builder.id);
setLabels(builder.labels);
public Organization(@JsonProperty(ID_FIELD) URI id,
@JsonProperty(LABELS_FIELD) Map<String, String> labels) {
this.id = validateId(id);
this.labels = labels;
}

public URI getId() {
return id;
}

public void setId(URI id) {
this.id = id;
}

public Map<String, String> getLabels() {
return Objects.nonNull(labels) ? labels : Collections.emptyMap();
}

public void setLabels(Map<String, String> labels) {
this.labels = labels;
private URI validateId(URI candidate) {
if (isNull(candidate)) {
throw new IllegalArgumentException("The id of an Organization can not be null");
}
return candidate;
}

@JacocoGenerated
@Override
@JacocoGenerated
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
if (!(o instanceof Organization)) {
return false;
}
Organization that = (Organization) o;
return Objects.equals(getId(), that.getId())
&& Objects.equals(getLabels(), that.getLabels());
&& Objects.equals(getLabels(), that.getLabels());
}

@JacocoGenerated
@Override
@JacocoGenerated
public int hashCode() {
return Objects.hash(getId(), getLabels());
}

public static final class Builder {

private URI id;
private Map<String, String> labels;

public Builder() {
}

public Builder withId(URI id) {
this.id = id;
return this;
}

public Builder withLabels(Map<String, String> labels) {
this.labels = labels;
return this;
}

public Organization build() {
return new Organization(this);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package no.unit.nva;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How will returning null behave when we are parsing a list of Organizations?


import com.fasterxml.jackson.core.JsonProcessingException;
import no.unit.nva.commons.json.JsonUtils;
import no.unit.nva.model.Organization;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

@Deprecated
class MigrationTestingOrganizationTest {

@ParameterizedTest
@ValueSource(strings = {"{\"type\": \"Organization\"}",
"{\"type\": \"Organization\", \"labels\": {\"en\":\"A label\"}}"})
void shouldRemoveImperfectlyFormedOrganizations(String value) throws JsonProcessingException {
assertNull(JsonUtils.dtoObjectMapper.readValue(value, Organization.class));
}

@ParameterizedTest
@ValueSource(strings = {"{\"type\": \"Organization\", \"id\": \"https://example.org\", "
+ "\"labels\": {\"en\":\"A label\"}}",
"{\"type\": \"Organization\", \"id\": \"https://example.org\"}"})
void shouldRetainWellFormedOrganizations(String value) throws JsonProcessingException {
assertNotNull(JsonUtils.dtoObjectMapper.readValue(value, Organization.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,7 @@ private static List<Organization> randomOrganizations() {
}

private static Organization randomOrganization() {
return new Organization.Builder()
.withLabels(randomLabels())
.withId(randomUri())
.build();
return new Organization(randomUri(), randomLabels());
}

private static List<String> randomTags() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
package no.unit.nva.model.testing;

import static no.unit.nva.model.testing.RandomUtils.randomLabel;
import static no.unit.nva.model.testing.RandomUtils.randomLabels;
import static no.unit.nva.testutils.RandomDataGenerator.randomBoolean;
import static no.unit.nva.testutils.RandomDataGenerator.randomElement;
import static no.unit.nva.testutils.RandomDataGenerator.randomInstant;
import static no.unit.nva.testutils.RandomDataGenerator.randomIsbn13;
import static no.unit.nva.testutils.RandomDataGenerator.randomIssn;
import static no.unit.nva.testutils.RandomDataGenerator.randomString;
import static no.unit.nva.testutils.RandomDataGenerator.randomUri;
import static nva.commons.core.attempt.Try.attempt;
import java.net.URI;
import java.time.Instant;
import java.util.List;
import no.unit.nva.model.Agent;
import no.unit.nva.model.Organization;
import no.unit.nva.model.contexttypes.Artistic;
Expand Down Expand Up @@ -45,6 +32,21 @@
import no.unit.nva.model.time.Time;
import nva.commons.core.JacocoGenerated;

import java.net.URI;
import java.time.Instant;
import java.util.List;

import static no.unit.nva.model.testing.RandomUtils.randomLabel;
import static no.unit.nva.model.testing.RandomUtils.randomLabels;
import static no.unit.nva.testutils.RandomDataGenerator.randomBoolean;
import static no.unit.nva.testutils.RandomDataGenerator.randomElement;
import static no.unit.nva.testutils.RandomDataGenerator.randomInstant;
import static no.unit.nva.testutils.RandomDataGenerator.randomIsbn13;
import static no.unit.nva.testutils.RandomDataGenerator.randomIssn;
import static no.unit.nva.testutils.RandomDataGenerator.randomString;
import static no.unit.nva.testutils.RandomDataGenerator.randomUri;
import static nva.commons.core.attempt.Try.attempt;

@SuppressWarnings("PMD.CouplingBetweenObjects")
@JacocoGenerated
public class PublicationContextBuilder {
Expand Down Expand Up @@ -186,10 +188,7 @@ private static Period randomPeriod(Instant from) {
}

private static Agent randomAgent() {
return new Organization.Builder()
.withId(randomUri())
.withLabels(randomLabels())
.build();
return new Organization(randomUri(), randomLabels());
}

private static Report randomReport()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
import static no.unit.nva.testutils.RandomDataGenerator.randomInteger;
import static no.unit.nva.testutils.RandomDataGenerator.randomString;
import static org.hamcrest.MatcherAssert.assertThat;

import com.github.javafaker.Faker;

import java.net.URI;
import java.util.List;
import java.util.Set;

import no.unit.nva.identifiers.SortableIdentifier;
import no.unit.nva.model.AdditionalIdentifier;
import no.unit.nva.model.Approval;
Expand Down Expand Up @@ -114,11 +117,11 @@ private static MonetaryAmount randomMonetaryAmount() {

public static ResearchProject randomResearchProject() {
return new ResearchProject.Builder()
.withId(randomUri())
.withName(randomString())
.withApprovals(randomApprovals())
.withGrants(randomGrants())
.build();
.withId(randomUri())
.withName(randomString())
.withApprovals(randomApprovals())
.withGrants(randomGrants())
.build();
}

public static List<Grant> randomGrants() {
Expand All @@ -127,9 +130,9 @@ public static List<Grant> randomGrants() {

public static Grant randomGrant() {
return new Grant.Builder()
.withId(randomString())
.withSource(randomString())
.build();
.withId(randomString())
.withSource(randomString())
.build();
}

public static List<Approval> randomApprovals() {
Expand All @@ -139,44 +142,41 @@ public static List<Approval> randomApprovals() {
public static Approval randomApproval() {

return new Approval.Builder()
.withApprovalStatus(randomElement(ApprovalStatus.values()))
.withDate(randomInstant())
.withApplicationCode(randomString())
.withApprovedBy(randomElement(ApprovalsBody.values()))
.build();
.withApprovalStatus(randomElement(ApprovalStatus.values()))
.withDate(randomInstant())
.withApplicationCode(randomString())
.withApprovedBy(randomElement(ApprovalsBody.values()))
.build();
}

public static AdditionalIdentifier randomAdditionalIdentifier() {
return new AdditionalIdentifier(randomString(), randomString());
}

public static Organization randomOrganization() {
return new Organization.Builder()
.withId(randomUri())
.withLabels(randomLabels())
.build();
return new Organization(randomUri(), randomLabels());
}

private static Publication buildRandomPublicationFromInstance(Class<?> publicationInstanceClass) {
return new Builder()
.withIdentifier(SortableIdentifier.next())
.withPublisher(randomOrganization())
.withSubjects(List.of(randomUri()))
.withStatus(randomElement(PublicationStatus.values()))
.withPublishedDate(randomInstant())
.withModifiedDate(randomInstant())
.withAdditionalIdentifiers(Set.of(randomAdditionalIdentifier()))
.withProjects(randomProjects())
.withFundings(randomFundings())
.withResourceOwner(randomResourceOwner())
.withLink(randomUri())
.withIndexedDate(randomInstant())
.withHandle(randomUri())
.withDoi(randomDoi())
.withCreatedDate(randomInstant())
.withEntityDescription(randomEntityDescription(publicationInstanceClass))
.withAssociatedArtifacts(AssociatedArtifactsGenerator.randomAssociatedArtifacts())
.build();
.withIdentifier(SortableIdentifier.next())
.withPublisher(randomOrganization())
.withSubjects(List.of(randomUri()))
.withStatus(randomElement(PublicationStatus.values()))
.withPublishedDate(randomInstant())
.withModifiedDate(randomInstant())
.withAdditionalIdentifiers(Set.of(randomAdditionalIdentifier()))
.withProjects(randomProjects())
.withFundings(randomFundings())
.withResourceOwner(randomResourceOwner())
.withLink(randomUri())
.withIndexedDate(randomInstant())
.withHandle(randomUri())
.withDoi(randomDoi())
.withCreatedDate(randomInstant())
.withEntityDescription(randomEntityDescription(publicationInstanceClass))
.withAssociatedArtifacts(AssociatedArtifactsGenerator.randomAssociatedArtifacts())
.build();
}

private static ResourceOwner randomResourceOwner() {
Expand Down