Skip to content

Commit

Permalink
SHIBUI-522: Polymorphic Jackson model refactoring wip
Browse files Browse the repository at this point in the history
  • Loading branch information
dima767 committed May 31, 2018
1 parent 6e5950f commit 8c07929
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,34 @@

import com.fasterxml.jackson.annotation.JsonIgnore;
import edu.internet2.tier.shibboleth.admin.ui.domain.Attribute;
import edu.internet2.tier.shibboleth.admin.util.MDDCConstants;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.opensaml.core.xml.XMLObject;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.OrderColumn;
import javax.persistence.PostLoad;
import javax.persistence.Transient;
import java.util.ArrayList;
import java.util.List;

import static edu.internet2.tier.shibboleth.admin.util.ModelRepresentationConversions.getAttributeReleaseListFromAttributeList;

@Entity
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@Getter
@Setter
@ToString
public class EntityAttributesFilter extends MetadataFilter {

@OneToOne(cascade = CascadeType.ALL)
private EntityAttributesFilterTarget entityAttributesFilterTarget;

Expand All @@ -23,27 +38,11 @@ public class EntityAttributesFilter extends MetadataFilter {
@JsonIgnore
private List<Attribute> attributes = new ArrayList<>();

public EntityAttributesFilterTarget getEntityAttributesFilterTarget() {
return entityAttributesFilterTarget;
}

public void setEntityAttributesFilterTarget(EntityAttributesFilterTarget entityAttributesFilterTarget) {
this.entityAttributesFilterTarget = entityAttributesFilterTarget;
}

public List<Attribute> getAttributes() {
return attributes;
}

public void setAttributes(List<Attribute> attributes) {
this.attributes = attributes;
}
@Transient
private List<String> attributeRelease = new ArrayList<>();

@Override
public String toString() {
return "EntityAttributesFilter{" +
"entityAttributesFilterTarget=" + entityAttributesFilterTarget +
"\n, attributes=" + attributes +
"\n}";
@PostLoad
public void intoTransientRepresentation() {
this.attributeRelease = getAttributeReleaseListFromAttributeList(this.attributes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@
import java.util.List;
import java.util.stream.Collectors;

import static edu.internet2.tier.shibboleth.admin.util.ModelRepresentationConversions.getBooleanValueOfAttribute;
import static edu.internet2.tier.shibboleth.admin.util.ModelRepresentationConversions.getStringListOfAttributeValues;
import static edu.internet2.tier.shibboleth.admin.util.ModelRepresentationConversions.getStringListValueOfAttribute;

/**
* Default implementation of {@link EntityDescriptorService}
*
Expand Down Expand Up @@ -536,18 +540,7 @@ public EntityDescriptorRepresentation createRepresentationFromDescriptor(org.ope

@Override
public List<String> getAttributeReleaseListFromAttributeList(List<Attribute> attributeList) {
List<Attribute> releaseAttributes = attributeList.stream()
.filter(attribute -> attribute.getName().equals(MDDCConstants.RELEASE_ATTRIBUTES))
.collect(Collectors.toList());

if (releaseAttributes.size() != 1) {
// TODO: What do we do if there is more than one?
}
if (releaseAttributes.size() == 0) {
return new ArrayList<>();
} else {
return getStringListOfAttributeValues(releaseAttributes.get(0).getAttributeValues());
}
return getAttributeReleaseListFromAttributeList(attributeList);
}

@Override
Expand Down Expand Up @@ -597,25 +590,7 @@ public RelyingPartyOverridesRepresentation getRelyingPartyOverridesRepresentatio
return relyingPartyOverridesRepresentation;
}

private boolean getBooleanValueOfAttribute(Attribute attribute) {
return ((XSBoolean) attribute.getAttributeValues().get(0)).getValue().getValue();
}

private List<String> getStringListValueOfAttribute(Attribute attribute) {
return getStringListOfAttributeValues(attribute.getAttributeValues());
}

private List<String> getStringListOfAttributeValues(List<XMLObject> attributeValues) {
List<String> stringAttributeValues = new ArrayList<>();
for (XMLObject attributeValue : attributeValues) {
if (attributeValue instanceof org.opensaml.core.xml.schema.XSString) {
stringAttributeValues.add(((org.opensaml.core.xml.schema.XSString) attributeValue).getValue());
} else if (attributeValue instanceof org.opensaml.core.xml.schema.XSAny) {
stringAttributeValues.add(((org.opensaml.core.xml.schema.XSAny) attributeValue).getTextContent());
}
}
return stringAttributeValues;
}

@Override
public void updateDescriptorFromRepresentation(org.opensaml.saml.saml2.metadata.EntityDescriptor entityDescriptor, EntityDescriptorRepresentation representation) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package edu.internet2.tier.shibboleth.admin.util;

import edu.internet2.tier.shibboleth.admin.ui.domain.Attribute;
import edu.internet2.tier.shibboleth.admin.ui.domain.XSBoolean;
import org.opensaml.core.xml.XMLObject;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* Utility class to deal with model conversions related functionality
*/
public abstract class ModelRepresentationConversions {

public static List<String> getStringListOfAttributeValues(List<XMLObject> attributeValues) {
List<String> stringAttributeValues = new ArrayList<>();
for (XMLObject attributeValue : attributeValues) {
if (attributeValue instanceof org.opensaml.core.xml.schema.XSString) {
stringAttributeValues.add(((org.opensaml.core.xml.schema.XSString) attributeValue).getValue());
} else if (attributeValue instanceof org.opensaml.core.xml.schema.XSAny) {
stringAttributeValues.add(((org.opensaml.core.xml.schema.XSAny) attributeValue).getTextContent());
}
}
return stringAttributeValues;
}

public static List<String> getAttributeReleaseListFromAttributeList(List<Attribute> attributeList) {
List<Attribute> releaseAttributes = attributeList.stream()
.filter(attribute -> attribute.getName().equals(MDDCConstants.RELEASE_ATTRIBUTES))
.collect(Collectors.toList());

if (releaseAttributes.size() != 1) {
// TODO: What do we do if there is more than one?
}
if (releaseAttributes.size() == 0) {
return new ArrayList<>();
} else {
return getStringListOfAttributeValues(releaseAttributes.get(0).getAttributeValues());
}
}

public static boolean getBooleanValueOfAttribute(Attribute attribute) {
return ((XSBoolean) attribute.getAttributeValues().get(0)).getValue().getValue();
}

public static List<String> getStringListValueOfAttribute(Attribute attribute) {
return getStringListOfAttributeValues(attribute.getAttributeValues());
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@ package edu.internet2.tier.shibboleth.admin.ui.domain

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.SerializationFeature
import edu.internet2.tier.shibboleth.admin.ui.domain.filters.EntityAttributesFilter
import edu.internet2.tier.shibboleth.admin.ui.domain.filters.EntityAttributesFilterTarget
import edu.internet2.tier.shibboleth.admin.ui.domain.filters.EntityRoleWhiteListFilter
import edu.internet2.tier.shibboleth.admin.ui.domain.filters.MetadataFilter
import edu.internet2.tier.shibboleth.admin.ui.opensaml.OpenSamlObjects
import edu.internet2.tier.shibboleth.admin.ui.util.TestObjectGenerator
import edu.internet2.tier.shibboleth.admin.util.AttributeUtility
import spock.lang.Specification

import static edu.internet2.tier.shibboleth.admin.ui.domain.filters.EntityAttributesFilterTarget.EntityAttributesFilterTargetType.ENTITY

class PolymorphicFiltersJacksonHandlingTests extends Specification {

ObjectMapper mapper

AttributeUtility attributeUtility

TestObjectGenerator testObjectGenerator

def setup() {
mapper = new ObjectMapper()
mapper.enable(SerializationFeature.INDENT_OUTPUT)
Expand All @@ -29,6 +28,8 @@ class PolymorphicFiltersJacksonHandlingTests extends Specification {
}
it
}

testObjectGenerator = new TestObjectGenerator(attributeUtility)
}

def "Correct polymorphic serialization of EntityRoleWhiteListFilter"() {
Expand Down Expand Up @@ -68,16 +69,9 @@ class PolymorphicFiltersJacksonHandlingTests extends Specification {

def "Correct polymorphic serialization of EntityAttributesFilter"() {
given:
def filter = new EntityAttributesFilter().with {
it.name = 'EntityAttributesFilter'
it.entityAttributesFilterTarget = new EntityAttributesFilterTarget().with {
it.entityAttributesFilterTargetType = ENTITY
it.value = ['value1']
it
}
it.attributes = [attributeUtility.createAttributeWithBooleanValue('myattr', 'myattrFriendy', true)]
it
}
def filter = testObjectGenerator.buildEntityAttributesFilter()

filter.intoTransientRepresentation()

when:
def json = mapper.writeValueAsString(filter)
Expand Down

0 comments on commit 8c07929

Please sign in to comment.