Skip to content

Commit

Permalink
Merge pull request #247 from europeana/#238_bodies_junit_tests
Browse files Browse the repository at this point in the history
#238 EA-1916 implemented tests for search for different body types
  • Loading branch information
gsergiu authored Mar 27, 2020
2 parents 1d73aaa + cdf4b62 commit 550fc1d
Show file tree
Hide file tree
Showing 4 changed files with 798 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class BaseTaggingTest extends BaseWebAnnotationProtocolTest {
public static final String TAG_GEO_WRONG_LONG = "/tag/wrong/geotag_wrong_long.json";
public static final String TAG_STANDARD = "/tag/standard.json";
public static final String TAG_GEOTAG = "/tag/geotag.json";
public static final String LINK_STANDARD = "/link/standard.json";
public static final String LINK_SEMANTIC = "/link/edmIsSimilarTo.json";
public static final String SEMANTICTAG_SIMPLE_MINIMAL = "/semantictag/simple_minimal.json";
public static final String SEMANTICTAG_SIMPLE_STANDARD = "/semantictag/simple_standard.json";
public static final String SEMANTICTAG_SPECIFIC_MINIMAL = "/semantictag/specific_minimal.json";
Expand All @@ -33,6 +35,8 @@ public class BaseTaggingTest extends BaseWebAnnotationProtocolTest {
public static final String DEREFERENCED_SEMANTICTAG_TEST_ENTITY = "/semantictag/dereferenced_semantictag_viaf_test_entity.json";
public static final String DEREFERENCED_SEMANTICTAG_TEST_ENTITY_2 = "/semantictag/dereferenced_semantictag_viaf_test_entity2.json";
public static final String DEREFERENCED_SEMANTICTAG_TEST_ENTITY_3 = "/semantictag/dereferenced_semantictag_viaf_test_entity3.json";
public static final String DESCRIBING_WEB_RESOURCE = "/describing/web_resource.json";
public static final String TRANSCRIPTION_WITH_RIGHTS = "/transcription/transcription-with-rights.json";

protected Annotation createAndValidateTag(String inputFile) throws IOException, JsonParseException,
IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,354 @@
package eu.europeana.annotation.client.integration.webanno.tag;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.stanbol.commons.exception.JsonParseException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import eu.europeana.annotation.client.AnnotationSearchApiImpl;
import eu.europeana.annotation.definitions.model.Annotation;
import eu.europeana.annotation.definitions.model.body.GraphBody;
import eu.europeana.annotation.definitions.model.body.TagBody;
import eu.europeana.annotation.definitions.model.body.impl.EdmPlaceBody;
import eu.europeana.annotation.definitions.model.body.impl.FullTextResourceBody;
import eu.europeana.annotation.definitions.model.body.impl.SemanticTagBody;
import eu.europeana.annotation.definitions.model.body.impl.TextBody;
import eu.europeana.annotation.definitions.model.entity.impl.EdmPlace;
import eu.europeana.annotation.definitions.model.graph.Graph;
import eu.europeana.annotation.definitions.model.search.SearchProfiles;
import eu.europeana.annotation.definitions.model.search.result.AnnotationPage;
import eu.europeana.annotation.definitions.model.target.Target;
import eu.europeana.annotation.definitions.model.vocabulary.BodyInternalTypes;
import eu.europeana.annotation.definitions.model.vocabulary.MotivationTypes;

public class BodyTypesTest extends BaseTaggingTest {

static final String VALUE_TESTSET = "generator_uri: \"http://test.europeana.org/45e86248-1218-41fc-9643-689d30dbe651\"";
static final String VALUE_ID = "anno_id:";
static final String VALUE_TARGET = "target_uri:\"http://data.europeana.eu/item/09102/_UEDIN_214\"";
static final String VALUE_TARGET_TAG = "target_uri:\"http://data.europeana.eu/item/000002/_UEDIN_214\"";
static final String VALUE_TARGET_LINK = "target_uri:\"http://data.europeana.eu/item/123/xyz\"";
static final String VALUE_TARGET_LINK_SEMANTIC = "target_uri:\"http://data.europeana.eu/item/2059207/data_sounds_T471_5\"";
static final String VALUE_DESCRIBING_TARGET_SCOPE = "target_uri:\"http://data.europeana.eu/item/07931/diglit_uah_m1\"";
static final String VALUE_BODY_URI = "body_uri:\"http://www.geonames.org/2988507\"";
static final String VALUE_DESCRIBING_BODY_VALUE = "body_value:\"... this is the textual description of the item ...\"";
static final String VALUE_BODY_VALUE = "body_value:\"trombone\"";
static final String VALUE_BODY_LINK_RELATION = "link_relation:\"isSimilarTo\"";
static final String VALUE_BODY_LINK_RESOURCE_URI = "link_resource_uri:\"http://thesession.org/tunes/52\"";
static final String VALUE_BODY_SPECIFIC_RESOURCE = "body_uri:\"http://www.geonames.org/2988507\""; // source
static final String VALUE_BODY_FULL_TEXT_RESOURCE = "body_value:\"... complete transcribed text in HTML ...\"";

static final int TOTAL_BY_ID_FOUND = 1;

protected Logger log = LogManager.getLogger(getClass());

private AnnotationSearchApiImpl annotationSearchApi;


/**
* Create annotations data set before each test execution
*
* @throws IOException
*/
@BeforeEach
public void createAnnotationSearchApi() throws JsonParseException, IOException {
annotationSearchApi = new AnnotationSearchApiImpl();
}

@Test
public void createSemanticTag() throws IOException, JsonParseException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {

String requestBody = getJsonStringInput(SEMANTICTAG_SIMPLE_STANDARD);

Annotation inputAnno = parseTag(requestBody);

// create indexed tag
Annotation storedAnno = createTag(requestBody);

assertTrue(BodyInternalTypes.isSemanticTagBody(storedAnno.getBody().getInternalType()));
//validate the reflection of input in output!
validateOutputAgainstInput(storedAnno, inputAnno);

// search for indexed id and textual values
searchByBodyValue(VALUE_ID+"\""+storedAnno.getAnnotationId().getIdentifier()+"\"", TOTAL_BY_ID_FOUND);
searchByBodyValue(VALUE_BODY_URI, TOTAL_BY_ID_FOUND);
searchByBodyValue(VALUE_TARGET, TOTAL_BY_ID_FOUND);

// validate fields
assertTrue(storedAnno.getMotivation().equals(MotivationTypes.TAGGING.name().toLowerCase()));
assertEquals(storedAnno.getBody().getInternalType(), BodyInternalTypes.SEMANTIC_TAG.name());
TagBody tagBody = ((SemanticTagBody) storedAnno.getBody());
assertNotNull(tagBody.getHttpUri());
assertTrue(tagBody.getHttpUri().equals("http://www.geonames.org/2988507"));
assertNotNull(tagBody.getLanguage());
assertTrue(tagBody.getLanguage().equals("en"));
Target target = storedAnno.getTarget();
assertNotNull(target.getHttpUri());
assertTrue(target.getHttpUri().equals("http://data.europeana.eu/item/09102/_UEDIN_214"));

// remove tag
deleteAnnotation(storedAnno);
}

@Test
public void createGeoTag() throws IOException, JsonParseException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {

String requestBody = getJsonStringInput(TAG_GEOTAG);

Annotation inputAnno = parseTag(requestBody);

// create indexed tag
Annotation storedAnno = createTag(requestBody);

assertTrue(BodyInternalTypes.isGeoTagBody(storedAnno.getBody().getInternalType()));
//validate the reflection of input in output!
validateOutputAgainstInput(storedAnno, inputAnno);

// search for indexed id and textual values
searchByBodyValue(VALUE_ID+"\""+storedAnno.getAnnotationId().getIdentifier()+"\"", TOTAL_BY_ID_FOUND);
searchByBodyValue(VALUE_TARGET, TOTAL_BY_ID_FOUND);

// validate fields
assertTrue(storedAnno.getMotivation().equals(MotivationTypes.TAGGING.name().toLowerCase()));
assertEquals(storedAnno.getBody().getInternalType(), BodyInternalTypes.GEO_TAG.name());
EdmPlace placeBody = (EdmPlace) ((EdmPlaceBody) storedAnno.getBody()).getPlace();
assertNotNull(placeBody.getLatitude());
assertTrue(placeBody.getLatitude().equals("48.853415"));
assertNotNull(placeBody.getLongitude());
assertTrue(placeBody.getLongitude().equals("-102.348800"));
Target target = storedAnno.getTarget();
assertNotNull(target.getHttpUri());
assertTrue(target.getHttpUri().equals("http://data.europeana.eu/item/09102/_UEDIN_214"));

// remove tag
deleteAnnotation(storedAnno);
}

@Test
public void createTag() throws IOException, JsonParseException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {

String requestBody = getJsonStringInput(TAG_BODY_TEXT);

Annotation inputAnno = parseTag(requestBody);

// create indexed tag
Annotation storedAnno = createTag(requestBody);

assertTrue(BodyInternalTypes.isTagBody(storedAnno.getBody().getInternalType()));
//validate the reflection of input in output!
validateOutputAgainstInput(storedAnno, inputAnno);

// search for indexed id and textual values
searchByBodyValue(VALUE_ID+"\""+storedAnno.getAnnotationId().getIdentifier()+"\"", TOTAL_BY_ID_FOUND);
searchByBodyValue(VALUE_BODY_VALUE, TOTAL_BY_ID_FOUND);
searchByBodyValue(VALUE_TARGET_TAG, TOTAL_BY_ID_FOUND);

// validate fields
assertTrue(storedAnno.getMotivation().equals(MotivationTypes.TAGGING.name().toLowerCase()));
assertEquals(storedAnno.getBody().getInternalType(), BodyInternalTypes.TAG.name());
TagBody tagBody = ((TagBody) storedAnno.getBody());
assertNotNull(tagBody.getValue());
assertTrue(tagBody.getValue().equals("trombone"));
Target target = storedAnno.getTarget();
assertNotNull(target.getHttpUri());
assertTrue(target.getHttpUri().equals("http://data.europeana.eu/item/000002/_UEDIN_214"));

// remove tag
deleteAnnotation(storedAnno);
}

@Test
public void createTagText() throws IOException, JsonParseException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {

String requestBody = getJsonStringInput(DESCRIBING_WEB_RESOURCE);

// create indexed tag
Annotation storedAnno = createTag(requestBody);

assertTrue(BodyInternalTypes.isTextualBody(storedAnno.getBody().getInternalType()));

// search for indexed id and textual values
searchByBodyValue(VALUE_ID+"\""+storedAnno.getAnnotationId().getIdentifier()+"\"", TOTAL_BY_ID_FOUND);
searchByBodyValue(VALUE_DESCRIBING_BODY_VALUE, TOTAL_BY_ID_FOUND);
searchByBodyValue(VALUE_DESCRIBING_TARGET_SCOPE, TOTAL_BY_ID_FOUND);

// validate fields
assertTrue(storedAnno.getMotivation().equals(MotivationTypes.DESCRIBING.name().toLowerCase()));
assertEquals(storedAnno.getBody().getInternalType(), BodyInternalTypes.TEXT.name());
TextBody textBody = ((TextBody) storedAnno.getBody());
assertNotNull(textBody.getValue());
assertTrue(textBody.getValue().equals("... this is the textual description of the item ..."));
Target target = storedAnno.getTarget();
assertNotNull(target.getSource());
assertTrue(target.getSource().equals("http://www.europeana1914-1918.eu/attachments/2020601/20841.235882.full.jpg"));
assertNotNull(target.getScope());
assertTrue(target.getScope().equals("http://data.europeana.eu/item/07931/diglit_uah_m1"));

// remove tag
deleteAnnotation(storedAnno);
}

@Test
public void createLink() throws IOException, JsonParseException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {

String requestBody = getJsonStringInput(LINK_STANDARD);

// create indexed tag
Annotation storedAnno = createTag(requestBody);

// search for indexed id and textual values
searchByBodyValue(VALUE_ID+"\""+storedAnno.getAnnotationId().getIdentifier()+"\"", TOTAL_BY_ID_FOUND);
searchByBodyValue(VALUE_TARGET_LINK, TOTAL_BY_ID_FOUND);

// validate fields
assertTrue(storedAnno.getMotivation().equals(MotivationTypes.LINKING.name().toLowerCase()));
Target target = storedAnno.getTarget();
assertNotNull(target.getValues());
assertTrue(target.getValues().contains("http://data.europeana.eu/item/123/xyz"));

// remove tag
deleteAnnotation(storedAnno);
}

@Test
public void createSemanticLink() throws IOException, JsonParseException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {

String requestBody = getJsonStringInput(LINK_SEMANTIC);

// create indexed tag
Annotation storedAnno = createTag(requestBody);

// search for indexed id and textual values
searchByBodyValue(VALUE_ID+"\""+storedAnno.getAnnotationId().getIdentifier()+"\"", TOTAL_BY_ID_FOUND);
searchByBodyValue(VALUE_TARGET_LINK_SEMANTIC, TOTAL_BY_ID_FOUND);

// validate fields
assertTrue(storedAnno.getMotivation().equals(MotivationTypes.LINKING.name().toLowerCase()));
Target target = storedAnno.getTarget();
assertNotNull(target.getHttpUri());
assertTrue(target.getHttpUri().equals("http://data.europeana.eu/item/2059207/data_sounds_T471_5"));

// remove tag
deleteAnnotation(storedAnno);
}

@Test
public void createGraph() throws IOException, JsonParseException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {

String requestBody = getJsonStringInput(LINK_SEMANTIC);

// create indexed tag
Annotation storedAnno = createTag(requestBody);

// search for indexed id and textual values
searchByBodyValue(VALUE_ID+"\""+storedAnno.getAnnotationId().getIdentifier()+"\"", TOTAL_BY_ID_FOUND);
searchByBodyValue(VALUE_BODY_LINK_RESOURCE_URI, TOTAL_BY_ID_FOUND);
searchByBodyValue(VALUE_BODY_LINK_RELATION, TOTAL_BY_ID_FOUND);
searchByBodyValue(VALUE_TARGET_LINK_SEMANTIC, TOTAL_BY_ID_FOUND);

// validate fields
assertTrue(storedAnno.getMotivation().equals(MotivationTypes.LINKING.name().toLowerCase()));
assertEquals(storedAnno.getBody().getInternalType(), BodyInternalTypes.GRAPH.name());
Graph graphBody = ((GraphBody) storedAnno.getBody()).getGraph();
assertNotNull(graphBody.getNode());
assertTrue(graphBody.getNode().getHttpUri().equals("http://thesession.org/tunes/52"));
assertTrue(graphBody.getRelationName().equals("isSimilarTo"));
assertTrue(graphBody.getResourceUri().equals("http://data.europeana.eu/item/2059207/data_sounds_T471_5"));
Target target = storedAnno.getTarget();
assertNotNull(target.getHttpUri());
assertTrue(target.getHttpUri().equals("http://data.europeana.eu/item/2059207/data_sounds_T471_5"));

// remove tag
deleteAnnotation(storedAnno);
}

@Test
public void createSemanticTagSpecific() throws IOException, JsonParseException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {

String requestBody = getJsonStringInput(SEMANTICTAG_SPECIFIC_STANDARD);

Annotation inputAnno = parseTag(requestBody);

// create indexed tag
Annotation storedAnno = createTag(requestBody);

assertTrue(BodyInternalTypes.isSemanticTagBody(storedAnno.getBody().getInternalType()));
//validate the reflection of input in output!
validateOutputAgainstInput(storedAnno, inputAnno);

// search for indexed id and textual values
searchByBodyValue(VALUE_ID+"\""+storedAnno.getAnnotationId().getIdentifier()+"\"", TOTAL_BY_ID_FOUND);
searchByBodyValue(VALUE_BODY_SPECIFIC_RESOURCE, TOTAL_BY_ID_FOUND);
searchByBodyValue(VALUE_TARGET, TOTAL_BY_ID_FOUND);

// validate fields
assertTrue(storedAnno.getMotivation().equals(MotivationTypes.TAGGING.name().toLowerCase()));
assertEquals(storedAnno.getBody().getInternalType(), BodyInternalTypes.SEMANTIC_TAG.name());
TagBody tagBody = ((SemanticTagBody) storedAnno.getBody());
assertNotNull(tagBody.getHttpUri());
assertTrue(tagBody.getHttpUri().equals("http://sws.geonames.org/2988506"));
assertNotNull(tagBody.getLanguage());
assertTrue(tagBody.getLanguage().equals("en"));
Target target = storedAnno.getTarget();
assertNotNull(target.getHttpUri());
assertTrue(target.getHttpUri().equals("http://data.europeana.eu/item/09102/_UEDIN_214"));

// remove tag
deleteAnnotation(storedAnno);
}

@Test
public void createFullTextResourceTag() throws IOException, JsonParseException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {

String requestBody = getJsonStringInput(TRANSCRIPTION_WITH_RIGHTS);

Annotation inputAnno = parseTag(requestBody);

// create indexed tag
Annotation storedAnno = createTag(requestBody);

assertTrue(BodyInternalTypes.isFullTextResourceTagBody(storedAnno.getBody().getInternalType()));
//validate the reflection of input in output!
validateOutputAgainstInput(storedAnno, inputAnno);

// search for indexed id and textual values
searchByBodyValue(VALUE_ID+"\""+storedAnno.getAnnotationId().getIdentifier()+"\"", TOTAL_BY_ID_FOUND);
searchByBodyValue(VALUE_BODY_FULL_TEXT_RESOURCE, TOTAL_BY_ID_FOUND);
searchByBodyValue(VALUE_TARGET, TOTAL_BY_ID_FOUND);

// validate fields
assertTrue(storedAnno.getMotivation().equals(MotivationTypes.TRANSCRIBING.name().toLowerCase()));
assertEquals(storedAnno.getBody().getInternalType(), BodyInternalTypes.FULL_TEXT_RESOURCE.name());
FullTextResourceBody textBody = ((FullTextResourceBody) storedAnno.getBody());
assertNotNull(textBody.getValue());
assertTrue(textBody.getValue().equals("... complete transcribed text in HTML ..."));
Target target = storedAnno.getTarget();
assertNotNull(target.getSource());
assertTrue(target.getSource().equals("http://www.europeana1914-1918.eu/attachments/2020601/20841.235882.full.jpg"));
assertNotNull(target.getScope());
assertTrue(target.getScope().equals("http://data.europeana.eu/item/07931/diglit_uah_m1"));

// remove tag
deleteAnnotation(storedAnno);
}

/**
* Search annotations by textual body value for different body types
* @param bodyValue
* @param foundAnnotationsNumber
*/
private void searchByBodyValue(String bodyValue, int foundAnnotationsNumber) {
AnnotationPage annPg = annotationSearchApi.searchAnnotations(bodyValue, SearchProfiles.MINIMAL, null);
assertNotNull(annPg, "AnnotationPage must not be null");
assertTrue(foundAnnotationsNumber <= annPg.getTotalInCollection());
}

}
Loading

0 comments on commit 550fc1d

Please sign in to comment.