This repository was archived by the owner on Oct 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Allow only identifiable Organizations #342
Open
brinxmat
wants to merge
4
commits into
main
Choose a base branch
from
force-organizations-specification-removing-empty-organizations
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 33 additions & 42 deletions
75
nva-datamodel-java/src/main/java/no/unit/nva/model/Organization.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } | ||
| } |
29 changes: 29 additions & 0 deletions
29
nva-datamodel-java/src/test/java/no/unit/nva/MigrationTestingOrganizationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package no.unit.nva; | ||
|
|
||
| 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)); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How will returning
nullbehave when we are parsing a list ofOrganizations?