Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into
Browse files Browse the repository at this point in the history
EA-3723-fulltext-translations
  • Loading branch information
gsergiu committed Mar 4, 2024
2 parents c9f341e + 18f4c74 commit 539a58c
Show file tree
Hide file tree
Showing 33 changed files with 831 additions and 439 deletions.
6 changes: 0 additions & 6 deletions annotation-dereferenciation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,6 @@
<version>${version.log4j2}</version>
</dependency>

<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>${version.httpClient}</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import org.springframework.stereotype.Service;

import eu.europeana.annotation.config.AnnotationConfiguration;
import eu.europeana.annotation.connection.HttpConnection;
import eu.europeana.annotation.definitions.exception.AnnotationDereferenciationException;
import eu.europeana.annotation.utils.HttpConnection;

/**
* This class supports requests to Metis API and XSLT conversion of response.
Expand Down Expand Up @@ -102,7 +102,7 @@ public Map<String, String> dereferenceOne(String uri, String language) throws An

try {
UriBuilder uriBuilder = UriBuilder.fromPath(baseUrl).queryParam(PARAM_URI, uri);
streamResponse = httpConnection.getURLContent(uriBuilder.build().toString());
streamResponse = httpConnection.getURLContentAsStream(uriBuilder.build().toString());
if(streamResponse==null) {
throw new AnnotationDereferenciationException("MetisDereferenciationClient returns null.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -35,10 +33,8 @@
import org.springframework.web.context.WebApplicationContext;
import org.testcontainers.containers.output.ToStringConsumer;
import org.testcontainers.containers.output.WaitingConsumer;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import eu.europeana.annotation.AnnotationBasePackageMapper;
import eu.europeana.annotation.config.AnnotationConfiguration;
import eu.europeana.annotation.config.AnnotationConfigurationImpl;
Expand Down Expand Up @@ -72,18 +68,31 @@
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class AbstractIntegrationTest extends AnnotationTestsConstants {

public static final String TOKEN_REGULAR = "regular-userid:publisher-username:user";
public static final String TOKEN_PUBLISHER = "publisher-userid:publisher-username:publisher";
public static final String TOKEN_ADMIN = "admin-userid:admin-username:admin";
public static final String ORG_ID_SAZ = "saz-org-id";

//FAKE Tokens format: user_id:user_name:role:apikey(optional):affiliation(optional)
public static final String TOKEN_REGULAR = "regular-userid:regular-username:user:regular-user-apikey";
public static final String TOKEN_PUBLISHER = "publisher-userid:publisher-username:publisher:publisher-apikey";

public static final String TOKEN_PROVIDER_SAZ = "provider-saz-userid:saz-username:user:saz-apikey:"+ ORG_ID_SAZ;
public static final String TOKEN_ADMIN = "admin-userid:admin-username:admin:admin-apikey";
static String regularUserAuthorizationValue = OAuthUtils.TYPE_BEARER + " " + TOKEN_REGULAR;
static String publisherUserAuthorizationValue = OAuthUtils.TYPE_BEARER + " " + TOKEN_PUBLISHER;
static String providerSazUserAuthorizationValue = OAuthUtils.TYPE_BEARER + " " + TOKEN_PROVIDER_SAZ;
static String adminUserAuthorizationValue = OAuthUtils.TYPE_BEARER + " " + TOKEN_ADMIN;


public static final String SEARCH_API_FOUND = "{\"apikey\":\"testapikey\",\"success\":true,\"requestNumber\":999,\"itemsCount\":0,\"totalResults\":1,\"items\":[]}";
public static final String SEARCH_API_NOT_FOUND = "{\"apikey\":\"testapikey\",\"success\":true,\"requestNumber\":999,\"itemsCount\":0,\"totalResults\":0,\"items\":[]}";

public static final String KEY_SAZ_ITEM1 = buildAffiliationSetKey(ORG_ID_SAZ, "item1");
public static final Set<String> SEARCH_API_AFFILIATION_CHECK = Set.of(KEY_SAZ_ITEM1);

private static List<Long> createdAnnotations = new ArrayList<Long>();
protected static List<Long> createdModerationRecords = new ArrayList<Long>();

protected static MockMvc mockMvc;
private static MockWebServer mockMetis;
private static MockWebServer mockMetisAndSearch;

@Autowired
protected WebApplicationContext webApplicationContext;
Expand All @@ -109,6 +118,12 @@ public class AbstractIntegrationTest extends AnnotationTestsConstants {

private static final MongoContainer MONGO_CONTAINER;
private static final SolrContainer SOLR_CONTAINER;

static String buildAffiliationSetKey(String organization, String id) {
String key = organization+":"+id;
return key;
}


static {
MONGO_CONTAINER = new MongoContainer("anno-it")
Expand All @@ -121,15 +136,16 @@ public class AbstractIntegrationTest extends AnnotationTestsConstants {

SOLR_CONTAINER.start();

mockMetis = new MockWebServer();
mockMetis.setDispatcher(setupMetisDispatcher());
mockMetisAndSearch = new MockWebServer();
mockMetisAndSearch.setDispatcher(setupMetisDispatcher());
try {
mockMetis.start();
mockMetisAndSearch.start();
} catch (IOException e) {
throw new RuntimeException(e);
}

}


@BeforeAll
protected void initAppConextAndOauthTokens() throws Exception {
Expand All @@ -138,6 +154,7 @@ protected void initAppConextAndOauthTokens() throws Exception {
// initialize mockMvc
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();

//disable OAuth.
disableOauth();
}

Expand All @@ -147,17 +164,32 @@ private static Dispatcher setupMetisDispatcher() {
@Override
public MockResponse dispatch(@NotNull RecordedRequest request) throws InterruptedException {
String responseBody=null;
String uri = request.getRequestUrl().queryParameter("uri");
String query = request.getRequestUrl().queryParameter("query");

try {
if(request.getBody()!=null) {
if(request.getBody()!=null && request.getBody().size() > 0) {
responseBody = AnnotationTestUtils.loadFile(
AnnotationTestUtils.METIS_RESPONSE_MAP.getOrDefault(
AnnotationTestsConstants.DEREFERENCE_MANY, AnnotationTestsConstants.EMPTY_METIS_RESPONSE));
}
else {
String uri = Objects.requireNonNull(request.getRequestUrl()).queryParameter("uri");
else if(uri != null){
//handle METIS deref request
responseBody = AnnotationTestUtils.loadFile(
AnnotationTestUtils.METIS_RESPONSE_MAP.getOrDefault(
uri, AnnotationTestsConstants.EMPTY_METIS_RESPONSE));
} else if(query != null){
//handle SEARCH API request
String itemId = StringUtils.substringAfterLast(query.replaceAll("\"", ""), '/');
//currently we have only one qf, but we might have more
String qf = request.getRequestUrl().queryParameter("qf");
assertNotNull(qf);
String organization = StringUtils.substringAfter(qf.replaceAll("\"", ""), ':');

String key = buildAffiliationSetKey(organization, itemId);

responseBody = SEARCH_API_AFFILIATION_CHECK.contains(key) ?
SEARCH_API_FOUND : SEARCH_API_NOT_FOUND;
}
return new MockResponse().setResponseCode(200).setBody(responseBody);
} catch (Exception e) {
Expand Down Expand Up @@ -201,29 +233,35 @@ static void setProperties(DynamicPropertyRegistry registry) {
registry.add("solr.annotation.url", SOLR_CONTAINER::getConnectionUrl);
registry.add(
"metis.baseUrl",
() -> String.format("http://%s:%s", mockMetis.getHostName(), mockMetis.getPort()));
() -> String.format("http://%s:%s", mockMetisAndSearch.getHostName(), mockMetisAndSearch.getPort()));
registry.add(
"searchApi.baseUrl",
() -> String.format("http://%s:%s?wskey=testapikey", mockMetisAndSearch.getHostName(), mockMetisAndSearch.getPort()));
}

protected String getAuthorizationHeaderValue(String user) {
if (USER_ADMIN.equals(user)) {
if (adminUserAuthorizationValue != null) {
return adminUserAuthorizationValue;
} else {
return "";
}
} else if(TOKEN_PUBLISHER.equals(user)) {
if (publisherUserAuthorizationValue != null) {
return publisherUserAuthorizationValue;
} else {
return "";
}
} else {
if (regularUserAuthorizationValue != null) {
return regularUserAuthorizationValue;
} else {
return "";
}
String authorization;
if(user == null) {
//allow tests to use the default user without explicitly stating it
user = USER_REGULAR;
}
switch (user) {
case USER_ADMIN:
authorization = adminUserAuthorizationValue;
break;
case USER_PUBLISHER: //TO CHECK why TOKEN_PUBLISHER was used before
authorization = publisherUserAuthorizationValue;
break;
case USER_PROVIDER_SAZ:
authorization = providerSazUserAuthorizationValue;
break;
case USER_REGULAR:
default:
authorization = regularUserAuthorizationValue;
break;
}

return authorization;
}

protected Logger log = LogManager.getLogger(getClass());
Expand Down
Loading

0 comments on commit 539a58c

Please sign in to comment.