Skip to content

Commit

Permalink
Make the inlined shape test more robust (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
berezovskyi committed Sep 16, 2021
1 parent 07bec14 commit 40d98e7
Showing 1 changed file with 51 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Set;

import javax.xml.datatype.DatatypeConfigurationException;
Expand All @@ -29,11 +30,11 @@ public class ShapeFactoryTest {
private static ShapeWithWorkingReferences createShapeInstanceWithWorkingReferences(String id) {
ShapeWithWorkingReferences r = new ShapeWithWorkingReferences(URI.create("http://test.adaptor.net/r/" + id));
r.setIdentifier(id);

LocalShape local = new LocalShape();
local.setIdentifier("local" + id);
r.setReferencesAsLocal(local);

ReferencedShape ref = new ReferencedShape();
ref.setAbout(URI.create("http://test.adaptor.net/ref/" + id));
r.setReferencesAsResource(new Link(ref.getAbout()));
Expand All @@ -43,44 +44,44 @@ private static ShapeWithWorkingReferences createShapeInstanceWithWorkingReferenc
inline.setIdentifier("inline" + id);
r.setInlines(inline);


InlinedShape i1 = new InlinedShape();
i1.setAbout(URI.create("http://test.adaptor.net/inlineMany_1_" + id));
i1.setIdentifier("inlineMany_1_" + id);
r.addInlinesMany(i1);

InlinedShape i2 = new InlinedShape();
i2.setAbout(URI.create("http://test.adaptor.net/inlineMany_2_" + id));
i2.setIdentifier("inlineMany_2_" + id);
r.addInlinesMany(i2);

return r;
}

@Test
public void validShapeTest() throws OslcCoreApplicationException, URISyntaxException {
ResourceShape shape = ResourceShapeFactory.createResourceShape("http://localhost:8080",
OslcConstants.PATH_RESOURCE_SHAPES,
"http://test.net/ns/test#",
ResourceShape shape = ResourceShapeFactory.createResourceShape("http://localhost:8080",
OslcConstants.PATH_RESOURCE_SHAPES,
"http://test.net/ns/test#",
ShapeWithWorkingReferences.class);
assertNotNull(shape);
}


@Test(expected = OslcCoreInvalidRepresentationException.class)
public void invalidShapeWithInlinedResourceTest() throws OslcCoreApplicationException, URISyntaxException {
ResourceShape shape = ResourceShapeFactory.createResourceShape("http://localhost:8080",
OslcConstants.PATH_RESOURCE_SHAPES,
"http://test.net/ns/test#",
ResourceShape shape = ResourceShapeFactory.createResourceShape("http://localhost:8080",
OslcConstants.PATH_RESOURCE_SHAPES,
"http://test.net/ns/test#",
ShapeWithWrongReferenceToInline.class);
assertNotNull(shape);
}

@Test(expected = OslcCoreInvalidValueTypeException.class)
public void invalidShapeWithReferencedResourceTest() throws OslcCoreApplicationException, URISyntaxException {
ResourceShape shape = ResourceShapeFactory.createResourceShape("http://localhost:8080",
OslcConstants.PATH_RESOURCE_SHAPES,
"http://test.net/ns/test#",
ResourceShape shape = ResourceShapeFactory.createResourceShape("http://localhost:8080",
OslcConstants.PATH_RESOURCE_SHAPES,
"http://test.net/ns/test#",
ShapeWithWrongReferenceToResource.class);
assertNotNull(shape);
}
Expand All @@ -91,39 +92,46 @@ public void validInstanceMarshalsAndUnmarshalsAsModelsTest() throws NoSuchMethod
final Model sourceModel = JenaModelHelper.createJenaModel(new Object[]{sourceResource});
ShapeWithWorkingReferences resultingResource = JenaModelHelper.unmarshalSingle(sourceModel, ShapeWithWorkingReferences.class);
final Model resultingModel = JenaModelHelper.createJenaModel(new Object[]{resultingResource});

assertThat(sourceModel).isomorphicWith(resultingModel);
}

@Test
public void validInstanceMarshalsAndUnmarshalsAsResourcesTest2() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, DatatypeConfigurationException, OslcCoreApplicationException, InstantiationException, SecurityException, URISyntaxException {
ShapeWithWorkingReferences sourceResource = createShapeInstanceWithWorkingReferences("1");
final Model sourceModel = JenaModelHelper.createJenaModel(new Object[]{sourceResource});
ShapeWithWorkingReferences resultingResource = JenaModelHelper.unmarshalSingle(sourceModel, ShapeWithWorkingReferences.class);
final Model resultingModel = JenaModelHelper.createJenaModel(new Object[]{resultingResource});

//Assert that the identifier of the inlined resource remains intact.
assertTrue(sourceResource.getInlines().getIdentifier().equals(resultingResource.getInlines().getIdentifier()));

//Assert that the URL of the inlined resource remains intact.
assertTrue(sourceResource.getInlines().getAbout().equals(resultingResource.getInlines().getAbout()));


//Assert that inlined resources, with multiple cardinality, remains intact.
InlinedShape s1 = (InlinedShape) sourceResource.getInlinesMany().toArray()[0];
InlinedShape r1 = (InlinedShape) resultingResource.getInlinesMany().toArray()[0];
assertTrue(s1.getIdentifier().equals(r1.getIdentifier()));

//Assert that the identifier of the Local resource remains intact.
assertTrue(sourceResource.getReferencesAsLocal().getIdentifier().equals(resultingResource.getReferencesAsLocal().getIdentifier()));

//Assert that the URL of the local resource is null.
assertNull(sourceResource.getReferencesAsLocal().getAbout());
assertNull(resultingResource.getReferencesAsLocal().getAbout());

//Assert that the URL of the referenced resource remains intact.
assertNotNull(resultingResource.getReferencesAsResource().getValue());
assertTrue(sourceResource.getReferencesAsResource().getValue().equals(resultingResource.getReferencesAsResource().getValue()));
ShapeWithWorkingReferences sourceResource = createShapeInstanceWithWorkingReferences("1");
final Model sourceModel = JenaModelHelper.createJenaModel(new Object[]{sourceResource});
ShapeWithWorkingReferences resultingResource = JenaModelHelper.unmarshalSingle(sourceModel, ShapeWithWorkingReferences.class);
final Model resultingModel = JenaModelHelper.createJenaModel(new Object[]{resultingResource});

//Assert that the identifier of the inlined resource remains intact.
assertTrue(sourceResource.getInlines().getIdentifier().equals(resultingResource.getInlines().getIdentifier()));

//Assert that the URL of the inlined resource remains intact.
assertTrue(sourceResource.getInlines().getAbout().equals(resultingResource.getInlines().getAbout()));


//Assert that inlined resources, with multiple cardinality, remains intact.
InlinedShape[] sourcesInlines = sourceResource.getInlinesMany().toArray(new InlinedShape[]{});
InlinedShape[] resultInlines = resultingResource.getInlinesMany().toArray(new InlinedShape[]{});
for (InlinedShape sourcesInline : sourcesInlines) {
assertTrue("Shape "+sourcesInline.getIdentifier()+" missing in the resulting inlined shapes",
containsResource(resultInlines, sourcesInline));
}

//Assert that the identifier of the Local resource remains intact.
assertTrue(sourceResource.getReferencesAsLocal().getIdentifier().equals(resultingResource.getReferencesAsLocal().getIdentifier()));

//Assert that the URL of the local resource is null.
assertNull(sourceResource.getReferencesAsLocal().getAbout());
assertNull(resultingResource.getReferencesAsLocal().getAbout());

//Assert that the URL of the referenced resource remains intact.
assertNotNull(resultingResource.getReferencesAsResource().getValue());
assertTrue(sourceResource.getReferencesAsResource().getValue().equals(resultingResource.getReferencesAsResource().getValue()));
}


private boolean containsResource(InlinedShape[] resultInlines, InlinedShape s1) {
return Arrays.stream(resultInlines).anyMatch(inlinedShape -> inlinedShape.getIdentifier().equals(s1.getIdentifier()));
}

}

0 comments on commit 40d98e7

Please sign in to comment.