Skip to content

Commit

Permalink
[ADD] knowledge tag serialization tests and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ostumpf committed Jan 21, 2015
1 parent 3481bf0 commit 62d4c92
Show file tree
Hide file tree
Showing 16 changed files with 160 additions and 35 deletions.
3 changes: 1 addition & 2 deletions jdeeco-core/model/RuntimeModel.ecore
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="SecurityRole">
<eStructuralFeatures xsi:type="ecore:EReference" name="consistsOf" upperBound="-1"
eType="#//SecurityRole"/>
eType="#//SecurityRole" containment="true"/>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="roleName" lowerBound="1"
eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
<eStructuralFeatures xsi:type="ecore:EReference" name="arguments" upperBound="-1"
Expand All @@ -144,4 +144,3 @@
<eClassifiers xsi:type="ecore:EClass" name="LocalKnowledgeTag" eSuperTypes="#//SecurityTag"/>
<eClassifiers xsi:type="ecore:EClass" name="SecurityTag"/>
</ecore:EPackage>

1 change: 0 additions & 1 deletion jdeeco-core/model/RuntimeModel.ecorediag
Original file line number Diff line number Diff line change
Expand Up @@ -1036,4 +1036,3 @@
<targetAnchor xmi:type="notation:IdentityAnchor" xmi:id="_7o8mtZz_EeS5f646i7SPww" id="(0.7941176470588235,0.7906976744186046)"/>
</edges>
</notation:Diagram>

Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,17 @@ public interface SecurityRole extends EObject {
EList<SecurityRoleArgument> getArguments();

/**
* Returns the value of the '<em><b>Consists Of</b></em>' reference list.
* Returns the value of the '<em><b>Consists Of</b></em>' containment reference list.
* The list contents are of type {@link cz.cuni.mff.d3s.deeco.model.runtime.api.SecurityRole}.
* <!-- begin-user-doc -->
* <p>
* If the meaning of the '<em>Consists Of</em>' reference list isn't clear,
* there really should be more of a description here...
* </p>
* <!-- end-user-doc -->
* @return the value of the '<em>Consists Of</em>' reference list.
* @return the value of the '<em>Consists Of</em>' containment reference list.
* @see cz.cuni.mff.d3s.deeco.model.runtime.meta.RuntimeMetadataPackage#getSecurityRole_ConsistsOf()
* @model
* @model containment="true"
* @generated
*/
EList<SecurityRole> getConsistsOf();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
import cz.cuni.mff.d3s.deeco.model.runtime.api.AbsoluteSecurityRoleArgument;
import cz.cuni.mff.d3s.deeco.model.runtime.impl.AbsoluteSecurityRoleArgumentImpl;

/**
*
* @author Ondřej Štumpf
*
*/
public class AbsoluteSecurityRoleArgumentExt extends AbsoluteSecurityRoleArgumentImpl implements Serializable {

private static final long serialVersionUID = 4072094973100189590L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
import cz.cuni.mff.d3s.deeco.model.runtime.api.BlankSecurityRoleArgument;
import cz.cuni.mff.d3s.deeco.model.runtime.impl.BlankSecurityRoleArgumentImpl;

/**
*
* @author Ondřej Štumpf
*
*/
public class BlankSecurityRoleArgumentExt extends BlankSecurityRoleArgumentImpl implements Serializable {

private static final long serialVersionUID = 1559162721313167821L;
Expand Down Expand Up @@ -69,7 +74,7 @@ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundE
res.load(stream, null);
BlankSecurityRoleArgument argument = (BlankSecurityRoleArgument) res.getContents().get(0);

name = argument.getName();
setName(argument.getName());
} catch (Exception e) {
throw e;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ public int hashCode() {

@Override
public String toString() {
return String.format("Knowledge Security Tag [role=%s]", requiredRole.toString());
return String.format("Knowledge Security Tag [role=%s]", requiredRole);
}

private void writeObject(ObjectOutputStream out) throws IOException {
XMLResource res = new XMLResourceImpl();
res.getContents().add(this);
XMLResource res = new XMLResourceImpl();
res.getContents().add(this);
StringWriter sw = new StringWriter();
res.save(sw, null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
import cz.cuni.mff.d3s.deeco.model.runtime.api.PathSecurityRoleArgument;
import cz.cuni.mff.d3s.deeco.model.runtime.impl.PathSecurityRoleArgumentImpl;

/**
*
* @author Ondřej Štumpf
*
*/
public class PathSecurityRoleArgumentExt extends PathSecurityRoleArgumentImpl implements Serializable {

private static final long serialVersionUID = 5680133272326377414L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,6 @@ public AbsoluteSecurityRoleArgument createAbsoluteSecurityRoleArgument() {
return new AbsoluteSecurityRoleArgumentExt();
}


// TODO: We might have also toString() method implemented for the KnowledgePath
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.io.StringWriter;
import java.util.List;
import java.util.stream.Collectors;

import org.eclipse.emf.ecore.xmi.XMLResource;
Expand All @@ -15,6 +16,11 @@
import cz.cuni.mff.d3s.deeco.model.runtime.api.SecurityRole;
import cz.cuni.mff.d3s.deeco.model.runtime.impl.SecurityRoleImpl;

/**
*
* @author Ondřej Štumpf
*
*/
public class SecurityRoleExt extends SecurityRoleImpl implements Serializable {

private static final long serialVersionUID = -4748717283540770244L;
Expand All @@ -34,22 +40,40 @@ public boolean equals(Object obj) {
return false;
} else if (!roleName.equals(other.roleName))
return false;

if (arguments == null) {
if (other.arguments != null)
if (consistsOf == null) {
if (other.consistsOf != null)
return false;
} else if (!arguments.equals(other.arguments))
} else if (!listsEqual(consistsOf,other.consistsOf))
return false;

if (consistsOf == null) {
if (other.consistsOf != null)
if (arguments == null) {
if (other.arguments != null)
return false;
} else if (!consistsOf.equals(other.consistsOf))
} else if (!listsEqual(arguments, other.arguments))
return false;

return true;
}

/**
* Manually calls equals() for each element of the list (cannot use basic equals() on the lists since the inheritance in Ecore)
* @param list
* @param other
* @return
*/
private <T> boolean listsEqual(List<T> list, List<T> other) {
if (list.size() != other.size()) {
return false;
} else {
boolean equal = true;
for (int i=0; i< list.size(); i++) {
equal = equal && list.get(i).equals(other.get(i));
}
return equal;
}
}

@Override
public int hashCode() {
final int prime = 17;
Expand All @@ -63,13 +87,13 @@ public int hashCode() {
@Override
public String toString() {
return String.format("Security Role [name=%s,args=%s,consistsOf=%s]", roleName,
arguments.stream().map(arg -> arg.toString()).collect(Collectors.joining(",")),
consistsOf.stream().map(p -> p.toString()).collect(Collectors.joining(",")));
getArguments().stream().map(arg -> arg.toString()).collect(Collectors.joining(",")),
getConsistsOf().stream().map(p -> p.toString()).collect(Collectors.joining(",")));
}

private void writeObject(ObjectOutputStream out) throws IOException {
XMLResource res = new XMLResourceImpl();
res.getContents().add(this);
XMLResource res = new XMLResourceImpl();
res.getContents().add(this);
StringWriter sw = new StringWriter();
res.save(sw, null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1371,7 +1371,7 @@ public void initializePackageContents() {
initEReference(getKnowledgeSecurityTag_RequiredRole(), this.getSecurityRole(), null, "requiredRole", null, 1, 1, KnowledgeSecurityTag.class, !IS_TRANSIENT, !IS_VOLATILE, IS_CHANGEABLE, IS_COMPOSITE, !IS_RESOLVE_PROXIES, !IS_UNSETTABLE, IS_UNIQUE, !IS_DERIVED, IS_ORDERED);

initEClass(securityRoleEClass, SecurityRole.class, "SecurityRole", !IS_ABSTRACT, !IS_INTERFACE, IS_GENERATED_INSTANCE_CLASS);
initEReference(getSecurityRole_ConsistsOf(), this.getSecurityRole(), null, "consistsOf", null, 0, -1, SecurityRole.class, !IS_TRANSIENT, !IS_VOLATILE, IS_CHANGEABLE, !IS_COMPOSITE, IS_RESOLVE_PROXIES, !IS_UNSETTABLE, IS_UNIQUE, !IS_DERIVED, IS_ORDERED);
initEReference(getSecurityRole_ConsistsOf(), this.getSecurityRole(), null, "consistsOf", null, 0, -1, SecurityRole.class, !IS_TRANSIENT, !IS_VOLATILE, IS_CHANGEABLE, IS_COMPOSITE, !IS_RESOLVE_PROXIES, !IS_UNSETTABLE, IS_UNIQUE, !IS_DERIVED, IS_ORDERED);
initEAttribute(getSecurityRole_RoleName(), ecorePackage.getEString(), "roleName", null, 1, 1, SecurityRole.class, !IS_TRANSIENT, !IS_VOLATILE, IS_CHANGEABLE, !IS_UNSETTABLE, !IS_ID, IS_UNIQUE, !IS_DERIVED, IS_ORDERED);
initEReference(getSecurityRole_Arguments(), this.getSecurityRoleArgument(), null, "arguments", null, 0, -1, SecurityRole.class, !IS_TRANSIENT, !IS_VOLATILE, IS_CHANGEABLE, IS_COMPOSITE, !IS_RESOLVE_PROXIES, !IS_UNSETTABLE, IS_UNIQUE, !IS_DERIVED, IS_ORDERED);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.impl.MinimalEObjectImpl;
import org.eclipse.emf.ecore.util.EObjectContainmentEList;
import org.eclipse.emf.ecore.util.EObjectResolvingEList;
import org.eclipse.emf.ecore.util.InternalEList;

/**
Expand All @@ -34,7 +33,7 @@
*/
public class SecurityRoleImpl extends MinimalEObjectImpl.Container implements SecurityRole {
/**
* The cached value of the '{@link #getConsistsOf() <em>Consists Of</em>}' reference list.
* The cached value of the '{@link #getConsistsOf() <em>Consists Of</em>}' containment reference list.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @see #getConsistsOf()
Expand Down Expand Up @@ -132,7 +131,7 @@ public EList<SecurityRoleArgument> getArguments() {
*/
public EList<SecurityRole> getConsistsOf() {
if (consistsOf == null) {
consistsOf = new EObjectResolvingEList<SecurityRole>(SecurityRole.class, this, RuntimeMetadataPackage.SECURITY_ROLE__CONSISTS_OF);
consistsOf = new EObjectContainmentEList<SecurityRole>(SecurityRole.class, this, RuntimeMetadataPackage.SECURITY_ROLE__CONSISTS_OF);
}
return consistsOf;
}
Expand All @@ -145,6 +144,8 @@ public EList<SecurityRole> getConsistsOf() {
@Override
public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
switch (featureID) {
case RuntimeMetadataPackage.SECURITY_ROLE__CONSISTS_OF:
return ((InternalEList<?>)getConsistsOf()).basicRemove(otherEnd, msgs);
case RuntimeMetadataPackage.SECURITY_ROLE__ARGUMENTS:
return ((InternalEList<?>)getArguments()).basicRemove(otherEnd, msgs);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1099,7 +1099,7 @@ public interface RuntimeMetadataPackage extends EPackage {
int SECURITY_ROLE = 21;

/**
* The feature id for the '<em><b>Consists Of</b></em>' reference list.
* The feature id for the '<em><b>Consists Of</b></em>' containment reference list.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
Expand Down Expand Up @@ -2263,10 +2263,10 @@ public interface RuntimeMetadataPackage extends EPackage {
EEnum getParameterKind();

/**
* Returns the meta object for the reference list '{@link cz.cuni.mff.d3s.deeco.model.runtime.api.SecurityRole#getConsistsOf <em>Consists Of</em>}'.
* Returns the meta object for the containment reference list '{@link cz.cuni.mff.d3s.deeco.model.runtime.api.SecurityRole#getConsistsOf <em>Consists Of</em>}'.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @return the meta object for the reference list '<em>Consists Of</em>'.
* @return the meta object for the containment reference list '<em>Consists Of</em>'.
* @see cz.cuni.mff.d3s.deeco.model.runtime.api.SecurityRole#getConsistsOf()
* @see #getSecurityRole()
* @generated
Expand Down Expand Up @@ -3020,7 +3020,7 @@ interface Literals {
EEnum PARAMETER_KIND = eINSTANCE.getParameterKind();

/**
* The meta object literal for the '<em><b>Consists Of</b></em>' reference list feature.
* The meta object literal for the '<em><b>Consists Of</b></em>' containment reference list feature.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import cz.cuni.mff.d3s.deeco.model.runtime.api.KnowledgeSecurityTag;
import cz.cuni.mff.d3s.deeco.model.runtime.api.PathNodeField;
import cz.cuni.mff.d3s.deeco.model.runtime.api.SecurityRole;
import cz.cuni.mff.d3s.deeco.model.runtime.custom.RuntimeMetadataFactoryExt;
import cz.cuni.mff.d3s.deeco.network.KnowledgeData;
import cz.cuni.mff.d3s.deeco.network.KnowledgeMetaData;
import cz.cuni.mff.d3s.deeco.network.KnowledgeSecurityAnnotation;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package cz.cuni.mff.d3s.deeco.runtime.custom;

import static org.junit.Assert.*;

import java.io.IOException;

import org.junit.Before;
import org.junit.Test;

import cz.cuni.mff.d3s.deeco.model.runtime.RuntimeModelHelper;
import cz.cuni.mff.d3s.deeco.model.runtime.api.AbsoluteSecurityRoleArgument;
import cz.cuni.mff.d3s.deeco.model.runtime.api.BlankSecurityRoleArgument;
import cz.cuni.mff.d3s.deeco.model.runtime.api.KnowledgeSecurityTag;
import cz.cuni.mff.d3s.deeco.model.runtime.api.PathSecurityRoleArgument;
import cz.cuni.mff.d3s.deeco.model.runtime.api.SecurityRole;
import cz.cuni.mff.d3s.deeco.model.runtime.custom.RuntimeMetadataFactoryExt;
import cz.cuni.mff.d3s.deeco.model.runtime.meta.RuntimeMetadataFactory;
import cz.cuni.mff.d3s.deeco.network.Serializer;

/**
*
* @author Ondřej Štumpf
*
*/
public class KnowledgeSecurityTagExtTest {

private RuntimeMetadataFactory factory;

@Before
public void setUp() {
factory = RuntimeMetadataFactoryExt.eINSTANCE;
}

@Test
public void serializationTest() throws IOException, ClassNotFoundException {
// given a tag is prepared
KnowledgeSecurityTag originalTag = createTag();

// when the tag is serialized
byte[] serialization = Serializer.serialize(originalTag);

// when the tag is deserialized
KnowledgeSecurityTag deserializedTag = (KnowledgeSecurityTag)Serializer.deserialize(serialization);

// then the objects are equal
assertEquals(originalTag.toString(), deserializedTag.toString());
assertEquals(originalTag, deserializedTag);
assertNotSame(originalTag, deserializedTag);
}

private KnowledgeSecurityTag createTag() {
KnowledgeSecurityTag tag = factory.createKnowledgeSecurityTag();
SecurityRole role = factory.createSecurityRole();
role.setRoleName("role");

AbsoluteSecurityRoleArgument absoluteArgument = factory.createAbsoluteSecurityRoleArgument();
absoluteArgument.setName("absolute");
absoluteArgument.setValue(123);

PathSecurityRoleArgument pathArgument = factory.createPathSecurityRoleArgument();
pathArgument.setName("absolute");
pathArgument.setKnowledgePath(RuntimeModelHelper.createKnowledgePath("level1", "level2"));

BlankSecurityRoleArgument blankArgument = factory.createBlankSecurityRoleArgument();
blankArgument.setName("blank");

role.getArguments().add(absoluteArgument);
role.getArguments().add(pathArgument);
role.getArguments().add(blankArgument);

SecurityRole parentRole = factory.createSecurityRole();
parentRole.setRoleName("parentRole");

AbsoluteSecurityRoleArgument parentAbsoluteArgument = factory.createAbsoluteSecurityRoleArgument();
parentAbsoluteArgument.setName("absoluteParent");
parentAbsoluteArgument.setValue(1234);

parentRole.getArguments().add(parentAbsoluteArgument);

role.getConsistsOf().add(parentRole);
tag.setRequiredRole(role);
return tag;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,8 @@ public void decryptValueSet_SecurityTest() throws InvalidKeyException, NoSuchAlg
assertNotSame(securitySet, decryptedData.getSecuritySet());

List<KnowledgeSecurityTag> tags = (List<KnowledgeSecurityTag>) decryptedData.getSecuritySet().getValue(RuntimeModelHelper.createKnowledgePath("secured"));
assertEquals(role.getRoleName(), tags.get(0).getRequiredRole().getRoleName());
assertEquals(role.getArguments().size(), tags.get(0).getRequiredRole().getArguments().size());
assertEquals(role.getConsistsOf().size(), tags.get(0).getRequiredRole().getConsistsOf().size());
assertEquals(tag, tags.get(0));
assertNotSame(tag, tags.get(0));
}

@Test
Expand Down
Loading

0 comments on commit 62d4c92

Please sign in to comment.