diff --git a/src/main/java/com/uid2/shared/model/Service.java b/src/main/java/com/uid2/shared/model/Service.java index 2eeabf29..edce1c6a 100644 --- a/src/main/java/com/uid2/shared/model/Service.java +++ b/src/main/java/com/uid2/shared/model/Service.java @@ -16,17 +16,23 @@ public class Service { private String linkIdRegex; private String name; private Set roles; + private boolean disabled; public Service(int serviceId, int siteId, String name, Set roles) { - this(serviceId, siteId, name, roles, null); + this(serviceId, siteId, name, roles, null, false); } public Service(int serviceId, int siteId, String name, Set roles, String linkIdRegex) { + this(serviceId, siteId, name, roles, linkIdRegex, false); + } + + public Service(int serviceId, int siteId, String name, Set roles, String linkIdRegex, boolean disabled) { this.serviceId = serviceId; this.siteId = siteId; this.name = name; this.roles = Objects.requireNonNullElseGet(roles, HashSet::new); this.linkIdRegex = linkIdRegex; + this.disabled = disabled; } public int getServiceId() { @@ -65,6 +71,14 @@ public void setRoles(Set roles) { this.roles = Objects.requireNonNullElseGet(roles, HashSet::new); } + public boolean isDisabled() { + return disabled; + } + + public void setDisabled(boolean disabled) { + this.disabled = disabled; + } + @Override public String toString() { return "Service{" + @@ -73,6 +87,7 @@ public String toString() { ", name='" + name + '\'' + ", roles=" + roles + ", linkIdRegex=" + linkIdRegex + + ", disabled=" + disabled + '}'; } @@ -84,6 +99,7 @@ public boolean equals(Object o) { return serviceId == other.serviceId && siteId == other.siteId + && disabled == other.disabled && Objects.equals(name, other.name) && Objects.equals(roles, other.roles) && Objects.equals(linkIdRegex, other.linkIdRegex); @@ -91,6 +107,6 @@ public boolean equals(Object o) { @Override public int hashCode() { - return Objects.hash(serviceId, siteId, name, roles.hashCode(), linkIdRegex); + return Objects.hash(serviceId, siteId, name, roles.hashCode(), linkIdRegex, disabled); } } diff --git a/src/main/java/com/uid2/shared/model/ServiceLink.java b/src/main/java/com/uid2/shared/model/ServiceLink.java index 0f0d57ec..68125349 100644 --- a/src/main/java/com/uid2/shared/model/ServiceLink.java +++ b/src/main/java/com/uid2/shared/model/ServiceLink.java @@ -20,6 +20,11 @@ public class ServiceLink { private String linkId; private String name; private Set roles; + private boolean disabled; + + public ServiceLink(String linkId, int serviceId, int siteId, String name, Set roles) { + this(linkId, serviceId, siteId, name, roles, false); + } @JsonCreator public ServiceLink( @@ -27,12 +32,14 @@ public ServiceLink( @JsonProperty("service_id") int serviceId, @JsonProperty("site_id") int siteId, @JsonProperty("name") String name, - @JsonProperty("roles") Set roles) { + @JsonProperty("roles") Set roles, + @JsonProperty("disabled") boolean disabled) { this.linkId = linkId; this.serviceId = serviceId; this.siteId = siteId; this.name = name; this.roles = Objects.requireNonNullElseGet(roles, HashSet::new); + this.disabled = disabled; } public String getLinkId() { @@ -67,6 +74,14 @@ public void setRoles(Set roles) { this.roles = Objects.requireNonNullElseGet(roles, HashSet::new); } + public boolean isDisabled() { + return disabled; + } + + public void setDisabled(boolean disabled) { + this.disabled = disabled; + } + @Override public String toString() { return "ServiceLink{" + @@ -75,6 +90,7 @@ public String toString() { ", linkId='" + linkId + '\'' + ", name='" + name + '\'' + ", roles=" + roles + + ", disabled=" + disabled + '}'; } @@ -83,12 +99,16 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; ServiceLink serviceLink = (ServiceLink) o; - return siteId == serviceLink.siteId && serviceId == serviceLink.serviceId && linkId.equals(serviceLink.linkId) - && name.equals(serviceLink.name) && roles.equals(serviceLink.roles); + return siteId == serviceLink.siteId + && serviceId == serviceLink.serviceId + && linkId.equals(serviceLink.linkId) + && name.equals(serviceLink.name) + && roles.equals(serviceLink.roles) + && disabled == serviceLink.disabled; } @Override public int hashCode() { - return Objects.hash(siteId, serviceId, linkId, name, roles.hashCode()); + return Objects.hash(siteId, serviceId, linkId, name, roles.hashCode(), disabled); } } diff --git a/src/main/java/com/uid2/shared/store/parser/ServiceParser.java b/src/main/java/com/uid2/shared/store/parser/ServiceParser.java index edd24519..be02ad4d 100644 --- a/src/main/java/com/uid2/shared/store/parser/ServiceParser.java +++ b/src/main/java/com/uid2/shared/store/parser/ServiceParser.java @@ -23,6 +23,7 @@ public ParsingResult> deserialize(InputStream inputStream) int siteId = serviceSpec.getInteger("site_id"); String name = serviceSpec.getString("name"); String linkIdRegex = serviceSpec.getString("link_id_regex"); + boolean disabled = serviceSpec.getBoolean("disabled", false); JsonArray rolesSpec = serviceSpec.getJsonArray("roles"); HashSet roles = new HashSet<>(); @@ -30,7 +31,7 @@ public ParsingResult> deserialize(InputStream inputStream) roles.add(Enum.valueOf(Role.class, rolesSpec.getString(j))); } - Service service = new Service(serviceId, siteId, name, roles, linkIdRegex); + Service service = new Service(serviceId, siteId, name, roles, linkIdRegex, disabled); serviceMap.put(serviceId, service); } diff --git a/src/test/java/com/uid2/shared/store/RotatingServiceLinkStoreTest.java b/src/test/java/com/uid2/shared/store/RotatingServiceLinkStoreTest.java index d1385f71..157ba010 100644 --- a/src/test/java/com/uid2/shared/store/RotatingServiceLinkStoreTest.java +++ b/src/test/java/com/uid2/shared/store/RotatingServiceLinkStoreTest.java @@ -41,13 +41,32 @@ private JsonObject makeMetadata(String location) { return metadata; } - private ServiceLink addServiceLink(JsonArray content, String linkId, int serviceId, int siteId, String name, Set roles) { - ServiceLink link = new ServiceLink(linkId, serviceId, siteId, name, roles); + private ServiceLink addServiceLink(JsonArray content, String linkId, int serviceId, int siteId, String name, Set roles, boolean disabled) { + ServiceLink link = new ServiceLink(linkId, serviceId, siteId, name, roles, disabled); JsonObject jo = JsonObject.mapFrom(link); content.add(jo); return link; } + @Test + public void allConstructors(){ + ServiceLink link = new ServiceLink("abc123", 1, 123, "Test Service 1", Set.of()); + assertEquals("abc123", link.getLinkId()); + assertEquals(1, link.getServiceId()); + assertEquals(123, link.getSiteId()); + assertEquals("Test Service 1", link.getName()); + assertEquals(Set.of(), link.getRoles()); + assertEquals(false, link.isDisabled()); + + link = new ServiceLink("abc123", 1, 123, "Test Service 1", Set.of(), true); + assertEquals("abc123", link.getLinkId()); + assertEquals(1, link.getServiceId()); + assertEquals(123, link.getSiteId()); + assertEquals("Test Service 1", link.getName()); + assertEquals(Set.of(), link.getRoles()); + assertEquals(true, link.isDisabled()); + } + @Test public void loadContent_emptyArray_loadsZeroServiceLinks() throws Exception { JsonArray content = new JsonArray(); @@ -62,10 +81,10 @@ public void loadContent_emptyArray_loadsZeroServiceLinks() throws Exception { @Test public void loadContent_multipleServiceLinksStored_loadsAllServiceLinks() throws Exception { JsonArray content = new JsonArray(); - ServiceLink l1 = addServiceLink(content, "abc123", 1, 123, "Test Service 1", Set.of()); - ServiceLink l2 = addServiceLink(content, "abc123", 2, 123, "test1", Set.of(Role.MAPPER)); - ServiceLink l3 = addServiceLink(content, "ghi789", 1, 123, "Test Service 1", Set.of(Role.MAPPER, Role.SHARER)); - ServiceLink l4 = addServiceLink(content, "jkl1011", 3, 124, "test2", null); + ServiceLink l1 = addServiceLink(content, "abc123", 1, 123, "Test Service 1", Set.of(), false); + ServiceLink l2 = addServiceLink(content, "abc123", 2, 123, "test1", Set.of(Role.MAPPER), true); + ServiceLink l3 = addServiceLink(content, "ghi789", 1, 123, "Test Service 1", Set.of(Role.MAPPER, Role.SHARER), false); + ServiceLink l4 = addServiceLink(content, "jkl1011", 3, 124, "test2", null, true); when(cloudStorage.download("locationPath")).thenReturn(makeInputStream(content)); final long count = serviceLinkStore.loadContent(makeMetadata("locationPath")); @@ -77,9 +96,9 @@ public void loadContent_multipleServiceLinksStored_loadsAllServiceLinks() throws @Test public void getServiceLink_multipleServiceLinksStored_findsCorrectServiceLink() throws Exception { JsonArray content = new JsonArray(); - ServiceLink l1 = addServiceLink(content, "abc123", 1, 123, "Test Service 1", Set.of()); - ServiceLink l2 = addServiceLink(content, "abc123", 2, 123, "test1", Set.of(Role.MAPPER)); - ServiceLink l3 = addServiceLink(content, "ghi789", 1, 123, "Test Service 1", Set.of(Role.MAPPER, Role.SHARER)); + ServiceLink l1 = addServiceLink(content, "abc123", 1, 123, "Test Service 1", Set.of(), false); + ServiceLink l2 = addServiceLink(content, "abc123", 2, 123, "test1", Set.of(Role.MAPPER), true); + ServiceLink l3 = addServiceLink(content, "ghi789", 1, 123, "Test Service 1", Set.of(Role.MAPPER, Role.SHARER), false); when(cloudStorage.download("locationPath")).thenReturn(makeInputStream(content)); @@ -98,7 +117,7 @@ public void getServiceLink_multipleServiceLinksStored_findsCorrectServiceLink() @Test public void createService_nullRole_createsServiceLinkWithEmptySetOfRoles() throws Exception { JsonArray content = new JsonArray(); - ServiceLink sl = addServiceLink(content, "jkl1011", 3, 124, "Test Service", null); + ServiceLink sl = addServiceLink(content, "jkl1011", 3, 124, "Test Service", null, false); when(cloudStorage.download("locationPath")).thenReturn(makeInputStream(content)); diff --git a/src/test/java/com/uid2/shared/store/RotatingServiceStoreTest.java b/src/test/java/com/uid2/shared/store/RotatingServiceStoreTest.java index f59c4621..82747ad4 100644 --- a/src/test/java/com/uid2/shared/store/RotatingServiceStoreTest.java +++ b/src/test/java/com/uid2/shared/store/RotatingServiceStoreTest.java @@ -40,13 +40,40 @@ private JsonObject makeMetadata(String location) { return metadata; } - private Service addService(JsonArray content, int serviceId, int siteId, String name, Set roles, String linkIdRegex) { - Service service = new Service(serviceId, siteId, name, roles, linkIdRegex); + private Service addService(JsonArray content, int serviceId, int siteId, String name, Set roles, String linkIdRegex, boolean disabled) { + Service service = new Service(serviceId, siteId, name, roles, linkIdRegex, disabled); JsonObject jo = JsonObject.mapFrom(service); content.add(jo); return service; } + @Test + public void allConstructors(){ + Service service = new Service(1, 123, "Test Service 1", Set.of()); + assertEquals(1, service.getServiceId()); + assertEquals(123, service.getSiteId()); + assertEquals("Test Service 1", service.getName()); + assertEquals(Set.of(), service.getRoles()); + assertEquals(null, service.getLinkIdRegex()); + assertEquals(false, service.isDisabled()); + + service = new Service(1, 123, "Test Service 1", Set.of(), "nonnull"); + assertEquals(1, service.getServiceId()); + assertEquals(123, service.getSiteId()); + assertEquals("Test Service 1", service.getName()); + assertEquals(Set.of(), service.getRoles()); + assertEquals("nonnull", service.getLinkIdRegex()); + assertEquals(false, service.isDisabled()); + + service = new Service(1, 123, "Test Service 1", Set.of(), "nonnull", true); + assertEquals(1, service.getServiceId()); + assertEquals(123, service.getSiteId()); + assertEquals("Test Service 1", service.getName()); + assertEquals(Set.of(), service.getRoles()); + assertEquals("nonnull", service.getLinkIdRegex()); + assertEquals(true, service.isDisabled()); + } + @Test public void loadContentEmptyArray() throws Exception { JsonArray content = new JsonArray(); @@ -59,10 +86,10 @@ public void loadContentEmptyArray() throws Exception { @Test public void loadContentMultipleServices() throws Exception { JsonArray content = new JsonArray(); - Service s1 = addService(content, 1, 123, "Test Service 1", Set.of(), null); - Service s2 = addService(content, 2, 123, "test1", Set.of(Role.GENERATOR), "regexA"); - Service s3 = addService(content, 3, 124, "Test Service 1", Set.of(Role.GENERATOR, Role.SHARING_PORTAL), null); - Service s4 = addService(content, 4, 125, "test2", Set.of(Role.MAINTAINER), "regexB"); + Service s1 = addService(content, 1, 123, "Test Service 1", Set.of(), null, false); + Service s2 = addService(content, 2, 123, "test1", Set.of(Role.GENERATOR), "regexA", true); + Service s3 = addService(content, 3, 124, "Test Service 1", Set.of(Role.GENERATOR, Role.SHARING_PORTAL), null, false); + Service s4 = addService(content, 4, 125, "test2", Set.of(Role.MAINTAINER), "regexB", true); when(cloudStorage.download("locationPath")).thenReturn(makeInputStream(content)); final long count = serviceStore.loadContent(makeMetadata("locationPath"));