From 097fa15e9cd2f4c9f6c18fa04f5c7ba5ed88c660 Mon Sep 17 00:00:00 2001 From: Bill Smith Date: Thu, 10 May 2018 09:42:58 -0700 Subject: [PATCH] [SHIBUI-479] Added a small unit test for checking EntityDescriptor version. Added getters for the version onto the representation for a filter and entity descriptor. --- .../EntityDescriptorRepresentation.java | 4 ++ .../domain/frontend/FilterRepresentation.java | 4 ++ ...JPAEntityDescriptorServiceImplTests.groovy | 18 ++++++ .../admin/ui/util/TestObjectGenerator.groovy | 55 +++++++++++++++++-- 4 files changed, 75 insertions(+), 6 deletions(-) diff --git a/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/domain/frontend/EntityDescriptorRepresentation.java b/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/domain/frontend/EntityDescriptorRepresentation.java index c11a935f5..c684d458a 100644 --- a/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/domain/frontend/EntityDescriptorRepresentation.java +++ b/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/domain/frontend/EntityDescriptorRepresentation.java @@ -196,6 +196,10 @@ public void setAttributeRelease(List attributeRelease) { this.attributeRelease = attributeRelease; } + public int getVersion() { + return version; + } + public void setVersion(int version) { this.version = version; } diff --git a/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/domain/frontend/FilterRepresentation.java b/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/domain/frontend/FilterRepresentation.java index 09e1e9772..b9e2f1213 100644 --- a/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/domain/frontend/FilterRepresentation.java +++ b/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/domain/frontend/FilterRepresentation.java @@ -89,6 +89,10 @@ public void setModifiedDate(LocalDateTime modifiedDate) { this.modifiedDate = modifiedDate; } + public int getVersion() { + return version; + } + public void setVersion(int version) { this.version = version; } diff --git a/backend/src/test/groovy/edu/internet2/tier/shibboleth/admin/ui/service/JPAEntityDescriptorServiceImplTests.groovy b/backend/src/test/groovy/edu/internet2/tier/shibboleth/admin/ui/service/JPAEntityDescriptorServiceImplTests.groovy index 5e4a2d0a0..0a30fa8bf 100644 --- a/backend/src/test/groovy/edu/internet2/tier/shibboleth/admin/ui/service/JPAEntityDescriptorServiceImplTests.groovy +++ b/backend/src/test/groovy/edu/internet2/tier/shibboleth/admin/ui/service/JPAEntityDescriptorServiceImplTests.groovy @@ -3,6 +3,7 @@ package edu.internet2.tier.shibboleth.admin.ui.service import com.fasterxml.jackson.databind.ObjectMapper import edu.internet2.tier.shibboleth.admin.ui.domain.frontend.* import edu.internet2.tier.shibboleth.admin.ui.opensaml.OpenSamlObjects +import edu.internet2.tier.shibboleth.admin.ui.util.TestObjectGenerator import org.springframework.boot.test.json.JacksonTester import org.xmlunit.builder.DiffBuilder import org.xmlunit.builder.Input @@ -11,6 +12,9 @@ import org.xmlunit.diff.ElementSelectors import spock.lang.Specification class JPAEntityDescriptorServiceImplTests extends Specification { + + def testObjectGenerator + OpenSamlObjects openSamlObjects = new OpenSamlObjects().with { init() it @@ -22,6 +26,7 @@ class JPAEntityDescriptorServiceImplTests extends Specification { def setup() { JacksonTester.initFields(this, new ObjectMapper()) + testObjectGenerator = new TestObjectGenerator() } @@ -589,4 +594,17 @@ class JPAEntityDescriptorServiceImplTests extends Specification { assert descriptor.getSPSSODescriptor('').getKeyDescriptors().size() == 1 assert descriptor.getSPSSODescriptor('').getKeyDescriptors()[0].getUse() == null } + + def "createRepresentationFromDescriptor creates a representation containing a version that is a hash of the original object"() { + given: + def entityDescriptor = testObjectGenerator.buildEntityDescriptor() + def expectedVersion = entityDescriptor.hashCode() + + when: + def representation = service.createRepresentationFromDescriptor(entityDescriptor) + + then: + def actualVersion = representation.version + expectedVersion == actualVersion + } } diff --git a/backend/src/test/groovy/edu/internet2/tier/shibboleth/admin/ui/util/TestObjectGenerator.groovy b/backend/src/test/groovy/edu/internet2/tier/shibboleth/admin/ui/util/TestObjectGenerator.groovy index 94015e0e3..302bdac95 100644 --- a/backend/src/test/groovy/edu/internet2/tier/shibboleth/admin/ui/util/TestObjectGenerator.groovy +++ b/backend/src/test/groovy/edu/internet2/tier/shibboleth/admin/ui/util/TestObjectGenerator.groovy @@ -1,19 +1,20 @@ package edu.internet2.tier.shibboleth.admin.ui.util -import edu.internet2.tier.shibboleth.admin.ui.domain.AdditionalMetadataLocation import edu.internet2.tier.shibboleth.admin.ui.domain.Attribute +import edu.internet2.tier.shibboleth.admin.ui.domain.ContactPerson import edu.internet2.tier.shibboleth.admin.ui.domain.EntityAttributesFilter import edu.internet2.tier.shibboleth.admin.ui.domain.EntityAttributesFilterTarget import edu.internet2.tier.shibboleth.admin.ui.domain.EntityDescriptor +import edu.internet2.tier.shibboleth.admin.ui.domain.LocalizedName +import edu.internet2.tier.shibboleth.admin.ui.domain.OrganizationDisplayName +import edu.internet2.tier.shibboleth.admin.ui.domain.OrganizationName +import edu.internet2.tier.shibboleth.admin.ui.domain.OrganizationURL import edu.internet2.tier.shibboleth.admin.ui.domain.frontend.FilterRepresentation import edu.internet2.tier.shibboleth.admin.ui.domain.frontend.FilterTargetRepresentation import edu.internet2.tier.shibboleth.admin.ui.domain.frontend.RelyingPartyOverridesRepresentation import edu.internet2.tier.shibboleth.admin.util.AttributeUtility import edu.internet2.tier.shibboleth.admin.util.MDDCConstants import org.opensaml.saml.saml2.metadata.Organization -import org.w3c.dom.Element - -import java.time.LocalDateTime /** * @author Bill Smith (wsmith@unicon.net) @@ -146,7 +147,7 @@ class TestObjectGenerator { entityDescriptor.setServiceEnabled(generator.randomBoolean()) entityDescriptor.setResourceId(generator.randomId()) entityDescriptor.setOrganization(buildOrganization()) - //TODO Finish adding the rest of the setters here. + entityDescriptor.addContactPerson(buildContactPerson()) //TODO: Implement these if we ever start setting them elsewhere //entityDescriptor.setRoleDescriptors(buildRoleDescriptors()) @@ -165,8 +166,50 @@ class TestObjectGenerator { Organization buildOrganization() { Organization organization = new edu.internet2.tier.shibboleth.admin.ui.domain.Organization() - //TODO Fill this out based on what gets used in the Organization domain object + organization.setNamespaceURI(generator.randomString(20)) + organization.setElementLocalName(generator.randomString(20)) + organization.setNamespacePrefix(generator.randomString(5)) + + organization.setOrganizationNames(buildListOfTypeWithValues(OrganizationName.class, generator.randomInt(1, 10))) + organization.setOrganizationDisplayNames(buildListOfTypeWithValues(OrganizationDisplayName.class, generator.randomInt(1, 10))) + organization.setOrganizationURLs(buildListOfTypeWithValues(OrganizationURL.class, generator.randomInt(1, 10))) + + //TODO: Implement these if we ever start setting them elsewhere + //organization.setExtensions(buildExtensions()) return organization } + + ContactPerson buildContactPerson() { + ContactPerson contactPerson = new ContactPerson(); + + contactPerson.setNamespaceURI(generator.randomString(20)) + contactPerson.setElementLocalName(generator.randomString(20)) + contactPerson.setNamespacePrefix(generator.randomString(5)) + + return contactPerson + } + + /** + * This method takes a type and a size and builds a List of that size containing objects of that type. This is + * intended to be used with things that extend LocalizedName such as {@link OrganizationName}, {@link OrganizationDisplayName}, + * or with {@link OrganizationURL}s (really, a class that has a setValue() method). + * + * @param type the type of list to generate + * @param listSize the number of instances of that type to generate and add to the list + * @return a list of the specified size containing objects of the specified type + */ + private List buildListOfTypeWithValues(Class type, int listSize) { + List list = [] + listSize.times { + T newItemOfType = type.newInstance() + if (newItemOfType instanceof LocalizedName) { + newItemOfType.value = generator.randomString(10) + } else if (newItemOfType instanceof OrganizationURL) { + newItemOfType.value = generator.randomString(10) + } + list.add(newItemOfType) + } + return list + } }