Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<!-- EM version with aggregator changes for testing -->
<version.entityManagementApi>1.7.6</version.entityManagementApi>
<version.entityManagementApi>1.7.9-SNAPSHOT</version.entityManagementApi>

<version-api-commons-sb3>0.1.3</version-api-commons-sb3>
<version-api-commons-sb3>0.1.7-SNAPSHOT</version-api-commons-sb3>
<jackson.version>2.18.3</jackson.version>
<junit.jupiter.version>5.7.2</junit.jupiter.version>
<commons.lang.version>3.17.0</commons.lang.version>
Expand Down Expand Up @@ -87,6 +87,12 @@
<version>${version-api-commons-sb3}</version>
</dependency>

<dependency>
<groupId>eu.europeana.api</groupId>
<artifactId>commons-sb3-definitions</artifactId>
<version>${version-api-commons-sb3}</version>
</dependency>

<dependency>
<groupId>eu.europeana.api</groupId>
<artifactId>entity-management-definitions</artifactId>
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/eu/europeana/entity/client/EntityApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* Entity Api Client
Expand Down Expand Up @@ -82,6 +83,12 @@ public List<Entity> enrichEntity(String text, String lang, String type, String r
return getMetadata(enrichResults);
}

@Override
public List<Entity> enrichEntity(String type, Map<String, String> textLangMap, int rows) throws EntityClientException {
List<String> enrichResults = getEntityClientApiConnection().retrieveEnrichment(type, textLangMap, rows);
return getMetadata(enrichResults);
}

@Override
public Entity getEntity(String entityId) throws EntityClientException {
return getEntityClientApiConnection().getEntityById(entityId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@
import org.apache.hc.core5.http.HttpHeaders;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.ProtocolException;
import org.apache.hc.core5.net.URIBuilder;
import org.apache.hc.core5.reactor.IOReactorConfig;


import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import static eu.europeana.entity.client.utils.EntityApiConstants.*;

Expand Down Expand Up @@ -140,6 +143,48 @@ public List<String> retrieveEnrichment(String text, String language, String type
return Collections.emptyList();
}

/**
* Entity Enrichment retrieval
* @param textLangMap text lang map for entity enrich
* @param type type of entity
* @param rows rows
* @return lis of entity ids
* @throws EntityClientException throws if there is an error in post request or reading the response
*/
public List<String> retrieveEnrichment(String type, Map<String, String> textLangMap, int rows) throws EntityClientException{
try {
URI enrichUrl = new URIBuilder(entityApiUri + PATH_SEPERATOR + ENRICH_PATH).build();
String jsonBody = EntityClientUtils.buildEnrichRequest(type, textLangMap, rows);
LOGGER.debug("{} ",jsonBody);

SimpleHttpResponse response = entityApiConnection.post(
enrichUrl.toString(),
jsonBody,
ContentType.APPLICATION_JSON.getMimeType(),
this.auth);

if (response.getCode() == HttpStatus.SC_OK) {
List<String> entities = EntityClientUtils.getEntityApiResults(response.getBodyText());
if (entities != null) {
LOGGER.debug("{} entities found for enrich text/lang={}, type={}", entities.size(), textLangMap, type);
return entities;
}
} else {
LOGGER.error("Error in enrichment response {}", response.getBodyText());
}
} catch (URISyntaxException e) {
throw new EntityClientException("Error creating enrich Urls " +e.getMessage(), HttpStatus.SC_INTERNAL_SERVER_ERROR, e);
} catch (ExecutionException | IOException e) {
throw new EntityClientException(ERROR_MESSAGE + e.getMessage(), HttpStatus.SC_INTERNAL_SERVER_ERROR, e);
} catch (InterruptedException e) { // the interrupted state is restored
LOGGER.warn(INTERRUPTED_MESSAGE, e);
/* Clean up whatever needs to be handled before interrupting */
Thread.currentThread().interrupt();
}
LOGGER.debug("No entity found for enrich text/lang: {}, type={}", textLangMap, type);
return Collections.emptyList();
}

/**
* Returns the Entity matching the entity id
* This method executes the entity Retrieval method of EM.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package eu.europeana.entity.client.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.node.ObjectNode;
import eu.europeana.api.commons_sb3.definitions.search.enrich.EnrichQuery;
import eu.europeana.api.commons_sb3.definitions.search.enrich.EnrichRequest;
import eu.europeana.entity.client.exception.EntityClientException;
import org.apache.commons.lang3.StringUtils;
import org.apache.hc.core5.http.HttpStatus;
Expand All @@ -16,6 +20,7 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import static eu.europeana.entity.client.utils.EntityApiConstants.*;

Expand All @@ -25,7 +30,7 @@
public class EntityClientUtils {

private static final Logger LOGGER = LogManager.getLogger(EntityClientUtils.class);
private static final ObjectMapper mapper = new ObjectMapper();
private static final ObjectMapper mapper = buildMapper();

private EntityClientUtils() {
// to hide implicit one
Expand Down Expand Up @@ -98,6 +103,21 @@ public static URI buildEntityEnrichUrl(String url, String text, String lang, Str
}
}

public static String buildEnrichRequest(String type, Map<String, String> textLangMap, int rows) throws EntityClientException {
if (textLangMap == null || textLangMap.isEmpty()) {
throw new EntityClientException("No values provided for enrichment request");
}
try {
List<EnrichQuery> query = new ArrayList<>();
for (Map.Entry<String, String> entry : textLangMap.entrySet()) {
query.add(new EnrichQuery(entry.getKey(), entry.getValue()));
}
return mapper.writeValueAsString(new EnrichRequest(type, query, rows));
} catch (JsonProcessingException e) {
throw new EntityClientException("Error creating enrich request " +e.getMessage(), HttpStatus.SC_INTERNAL_SERVER_ERROR, e);
}
}


/**
* Builds the Entity Api resolve url
Expand Down Expand Up @@ -168,4 +188,28 @@ public static List<String> getEntityApiResults(String json) throws JsonProcessin
}
return entities;
}

/**
* Extracts an error message from the given JSON response string.
*
* @param response the JSON response string to parse for an error message
* @return the extracted error message if available, or an empty string if the parsing fails
*/
public static String getErrorMessage(String response) {
try {
return mapper.readTree(response).get("message").asText();
} catch (JsonProcessingException e) {
LOGGER.error("Error while parsing the response", e);
}
return "";
}

private static ObjectMapper buildMapper() {
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
mapper.registerModule(module);
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.findAndRegisterModules();
return mapper;
}
}
9 changes: 9 additions & 0 deletions src/main/java/eu/europeana/entity/client/web/EntityApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import eu.europeana.entitymanagement.definitions.model.Entity;

import java.util.List;
import java.util.Map;

/**
* Entity client api interface
Expand All @@ -31,6 +32,14 @@ public interface EntityApi {
*/
public List<Entity> enrichEntity(String text, String lang, String type, String rows) throws EntityClientException;

/**
* This method returns entity enrichment depending on given text, types and language.
* @param type
* @param textLangMap
* @param rows
*/
public List<Entity> enrichEntity(String type, Map<String, String> textLangMap, int rows) throws EntityClientException;

/**
* Get Entity by EntityId
* @param entityId
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package eu.europeana.entity.client.web;

import eu.europeana.entity.client.EntityApiClient;
import eu.europeana.entity.client.config.EntityClientConfiguration;
import eu.europeana.entity.client.exception.EntityClientException;
import eu.europeana.entitymanagement.definitions.model.Entity;
import eu.europeana.entitymanagement.vocabulary.EntityTypes;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.*;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class EntityClientEnrichTest {

EntityApi apiClient ;

/**
* This constructor will create a Entity api client based on values present in properties file.
* @throws EntityClientException
*/
@BeforeAll
void setup() throws EntityClientException {
apiClient = new EntityApiClient(new EntityClientConfiguration());
}

/**
* Tests the behavior - when called with null values for the text
* and language map parameters.
*
* Expected behavior:
* - An {@link EntityClientException} is thrown.
* - The exception message is "No values provided for enrichment request".
*/
@Test
public void testEnrichment_1() {
EntityClientException exception = assertThrows(EntityClientException.class, () ->
apiClient.enrichEntity("place", null, 0));

assertEquals("No values provided for enrichment request", exception.getMessage());
}

/**
* Test scenario - rows invalid value for enrichment
* @throws EntityClientException
*/
@Test
public void testEnrichment_2() throws EntityClientException {
List<Entity> enrichments = apiClient.enrichEntity("place", Collections.singletonMap("paris", "en"), -1);
assertTrue(enrichments.isEmpty());

enrichments = apiClient.enrichEntity("place", Collections.singletonMap("paris", "en"), 60);
assertTrue(enrichments.isEmpty());
}

/**
* Test scenario - empty rows or zero rows
* Enrichments results should be present as the default value of rows is 10
* @throws EntityClientException
*/
@Test
public void testEnrichment_3() throws EntityClientException {
List<Entity> enrichments = apiClient.enrichEntity("place", Collections.singletonMap("paris", "en"), 0);
assertNotNull(enrichments);
assertEquals(EntityTypes.Place.getEntityType(), enrichments.get(0).getType());
}

/**
* Test scenario - empty lang in the map
* @throws EntityClientException
*/
@Test
public void testEnrichment_4() throws EntityClientException {
List<Entity> enrichments = apiClient.enrichEntity("place", Collections.singletonMap("paris", null), 0);
assertNotNull(enrichments);
assertEquals(EntityTypes.Place.getEntityType(), enrichments.get(0).getType());
assertTrue(StringUtils.contains(enrichments.get(0).getEntityId(), "place/41488"));

enrichments = apiClient.enrichEntity("agent", Collections.singletonMap("paris", ""), 0);
assertNotNull(enrichments);
assertEquals(EntityTypes.Agent.getEntityType(), enrichments.get(0).getType());
assertTrue(StringUtils.contains(enrichments.get(0).getEntityId(), "agent/52295"));
}

/**
* Test scenario - empty type
* @throws EntityClientException
*/
@Test
public void testEnrichment_5() throws EntityClientException {
List<Entity> enrichments = apiClient.enrichEntity("", Collections.singletonMap("paris", ""), 0);
assertNotNull(enrichments);
assertEquals(2, enrichments.size());

assertEquals(EntityTypes.Place.getEntityType(), enrichments.get(0).getType());
assertTrue(StringUtils.contains(enrichments.get(0).getEntityId(), "place/41488"));

assertEquals(EntityTypes.Agent.getEntityType(), enrichments.get(1).getType());
assertTrue(StringUtils.contains(enrichments.get(1).getEntityId(), "agent/52295"));
}

/**
* Test scenario - multiple values
* @throws EntityClientException
*/
@Test
public void testEnrichment_6() throws EntityClientException {
Map<String, String> map = new HashMap<>();
map.put("paris", "en");
map.put("India", null);
map.put("France", "fr");
List<Entity> enrichments = apiClient.enrichEntity("", map, 0);
assertNotNull(enrichments);
assertEquals(7, enrichments.size());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,9 @@ void testGetEntity_2() throws EntityClientException {
* @throws JsonProcessingException
*/
@Test
@Disabled ("testGetEntity_3 disabled because the temp fix shows Aggregator as organisation")
void testGetEntity_3() throws EntityClientException {
Organization entity = (Organization) apiClient.getEntity("http://data.europeana.eu/organization/4563");
assertNotNull(entity);
assertEquals(EntityTypes.Organization.getEntityType(), entity.getType());
assertEquals(EntityTypes.Aggregator.getEntityType(), entity.getType());
assertNotNull(entity.getAggregatesFrom());

Expand Down Expand Up @@ -124,7 +122,6 @@ void testResolveEntity_2() throws EntityClientException {
void testEnrichment_1() throws EntityClientException {
List<Entity> enrichments = apiClient.enrichEntity("CulturaItalia", null, "organization", null);
assertNotNull(enrichments);
assertFalse(enrichments.isEmpty());
}


Expand Down
Loading