Skip to content

Commit

Permalink
[SHIBUI-1220]
Browse files Browse the repository at this point in the history
Refactored getValueFromXSStringOrXSAny to getValueFromXMLObject which
now supports Any, String, and Boolean. Includes tests.
  • Loading branch information
Bill Smith committed Feb 12, 2019
1 parent caabfd7 commit a098276
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ public EntityDescriptorRepresentation createRepresentationFromDescriptor(org.ope
if (jpaAttribute.getAttributeValues().size() != 1) {
throw new RuntimeException("Multiple/No values detected where one is expected!");
}
attributeValues = getValueFromXSStringOrXSAny(jpaAttribute.getAttributeValues().get(0));
attributeValues = getValueFromXMLObject(jpaAttribute.getAttributeValues().get(0));
break;
case INTEGER:
if (jpaAttribute.getAttributeValues().size() != 1) {
Expand All @@ -536,7 +536,7 @@ public EntityDescriptorRepresentation createRepresentationFromDescriptor(org.ope
}
if (overrideProperty.getPersistType() != null &&
!overrideProperty.getPersistType().equals(overrideProperty.getDisplayType())) {
attributeValues = getValueFromXSStringOrXSAny(jpaAttribute.getAttributeValues().get(0));
attributeValues = getValueFromXMLObject(jpaAttribute.getAttributeValues().get(0));
} else {
attributeValues = Boolean.valueOf(((XSBoolean) jpaAttribute.getAttributeValues()
.get(0)).getStoredValue());
Expand All @@ -545,7 +545,7 @@ public EntityDescriptorRepresentation createRepresentationFromDescriptor(org.ope
case SET:
case LIST:
attributeValues = jpaAttribute.getAttributeValues().stream()
.map(attributeValue -> getValueFromXSStringOrXSAny(attributeValue))
.map(attributeValue -> getValueFromXMLObject(attributeValue))
.collect(Collectors.toList());
}
relyingPartyOverrides.put(((RelyingPartyOverrideProperty) override.get()).getName(), attributeValues);
Expand All @@ -559,11 +559,17 @@ public EntityDescriptorRepresentation createRepresentationFromDescriptor(org.ope
return representation;
}

private String getValueFromXSStringOrXSAny(XMLObject xmlObject) {
if (xmlObject instanceof XSAny) {
return ((XSAny)xmlObject).getTextContent();
} else {
return ((XSString)xmlObject).getValue();
private String getValueFromXMLObject(XMLObject xmlObject) {
String objectType = xmlObject.getClass().getSimpleName();
switch (objectType) {
case "XSAny":
return ((XSAny)xmlObject).getTextContent();
case "XSString":
return ((XSString)xmlObject).getValue();
case "XSBoolean":
return ((XSBoolean)xmlObject).getStoredValue();
default:
throw new RuntimeException(String.format("Unsupported XML Object type [%s]", objectType));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import edu.internet2.tier.shibboleth.admin.ui.configuration.CoreShibUiConfigurat
import edu.internet2.tier.shibboleth.admin.ui.configuration.CustomPropertiesConfiguration
import edu.internet2.tier.shibboleth.admin.ui.domain.EntityDescriptor
import edu.internet2.tier.shibboleth.admin.ui.domain.XSAny
import edu.internet2.tier.shibboleth.admin.ui.domain.XSAnyBuilder
import edu.internet2.tier.shibboleth.admin.ui.domain.XSBoolean
import edu.internet2.tier.shibboleth.admin.ui.domain.XSBooleanBuilder
import edu.internet2.tier.shibboleth.admin.ui.domain.XSStringBuilder
import edu.internet2.tier.shibboleth.admin.ui.domain.frontend.AssertionConsumerServiceRepresentation
import edu.internet2.tier.shibboleth.admin.ui.domain.frontend.ContactRepresentation
import edu.internet2.tier.shibboleth.admin.ui.domain.frontend.EntityDescriptorRepresentation
Expand Down Expand Up @@ -786,6 +789,59 @@ class JPAEntityDescriptorServiceImplTests extends Specification {
expectedVersion == actualVersion
}

def "SHIBUI-1220 getValueFromXMLObject handles XSAny"() {
given:
def builder = new XSAnyBuilder()
def xsAny = builder.buildObject('namespace', 'localname', 'prefix')
def expectedTextContent = 'expectedTextContent'
xsAny.setTextContent(expectedTextContent)

when:
def result = service.getValueFromXMLObject(xsAny)

then:
result == expectedTextContent
}

def "SHIBUI-1220 getValueFromXMLObject handles XSString"() {
given:
def builder = new XSStringBuilder()
def xsString = builder.buildObject('namespace', 'localname', 'prefix')
def expectedValue = 'expectedValue'
xsString.setValue(expectedValue)

when:
def result = service.getValueFromXMLObject(xsString)

then:
result == expectedValue
}

def "SHIBUI-1220 getValueFromXMLObject handles XSBoolean"() {
given:
def builder = new XSBooleanBuilder()
def xsBoolean = builder.buildObject('namespace', 'localname', 'prefix')
def expectedValue = 'true'
xsBoolean.setStoredValue(expectedValue)

when:
def result = service.getValueFromXMLObject(xsBoolean)

then:
result == expectedValue
}

def "SHIBUI-1220 getValueFromXMLObject throws RuntimeException for unhandled object type"() {
given:
def unhandledObject = new Object()

when:
service.getValueFromXMLObject(unhandledObject)

then:
thrown RuntimeException
}

EntityDescriptor generateRandomEntityDescriptor() {
EntityDescriptor ed = new EntityDescriptor()

Expand Down

0 comments on commit a098276

Please sign in to comment.