Skip to content

Commit

Permalink
Merge pull request #272 from europeana/EA-3954_new_profiles_definitions
Browse files Browse the repository at this point in the history
Ea 3954 new profiles definitions
  • Loading branch information
gsergiu authored Nov 14, 2024
2 parents 501508a + 3ed946d commit f36be22
Show file tree
Hide file tree
Showing 12 changed files with 249 additions and 213 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package eu.europeana.set.client.connection;

import java.io.IOException;

import org.apache.commons.lang3.StringUtils;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.springframework.http.HttpStatus;
Expand All @@ -18,157 +18,156 @@
*/
public class UserSetApiConnection extends BaseApiConnection {

String regularUserAuthorizationValue = null;

/**
* Create a new connection to the UserSet Service (REST API).
*
* @param apiKey API Key required to access the API
*/
public UserSetApiConnection(String setServiceUri, String apiKey) {
super(setServiceUri, apiKey);
initConfigurations();
String regularUserAuthorizationValue = null;

/**
* Create a new connection to the UserSet Service (REST API).
*
* @param apiKey API Key required to access the API
*/
public UserSetApiConnection(String setServiceUri, String apiKey) {
super(setServiceUri, apiKey);
initConfigurations();
}

public UserSetApiConnection() {
this(ClientConfiguration.getInstance().getServiceUri(),
ClientConfiguration.getInstance().getApiKey());
initConfigurations();
}

private void initConfigurations() {
// regularUserAuthorizationValue =
// ClientConfiguration.getInstance().getAuthorizationHeaderValue();
regularUserAuthorizationValue = getOauthToken();
}

public String getOauthToken() {
try {

String ACCESS_TOKEN = "access_token";
String oauthUri = ClientConfiguration.getInstance().getOauthServiceUri();
String oauthParams = ClientConfiguration.getInstance().getOauthRequestParams();
HttpConnection connection = new HttpConnection();
ResponseEntity<String> response;
response = connection.post(oauthUri, oauthParams, "application/x-www-form-urlencoded");

if (HttpStatus.OK == response.getStatusCode()) {
String body = response.getBody();
JSONObject json = new JSONObject(body);
if (json.has(ACCESS_TOKEN)) {
return "Bearer " + json.getString(ACCESS_TOKEN);
} else {
throw new TechnicalRuntimeException(
"Cannot extract authentication token from reponse:" + body);
}
} else {
throw new TechnicalRuntimeException("Error occured when calling oath service! " + response);
}
} catch (IOException | JSONException e) {
throw new TechnicalRuntimeException("Cannot retrieve authentication token!", e);
}

public UserSetApiConnection() {
this(ClientConfiguration.getInstance().getServiceUri(), ClientConfiguration.getInstance().getApiKey());
initConfigurations();
}

/**
* This method creates UserSet object from Json string. Example HTTP request for tag object:
* http://localhost:8080/set/?profile=minimal
*
* @param set The UserSet body
* @param profile
* @return response entity that comprises response body, headers and status code.
* @throws IOException
*/
public ResponseEntity<String> createUserSet(String set, String profile) throws IOException {

StringBuilder urlBuilder = getUserSetServiceUri();
if (StringUtils.isNotEmpty(profile)) {
urlBuilder.append(WebUserSetFields.PAR_CHAR);
urlBuilder.append(CommonApiConstants.QUERY_PARAM_PROFILE)
.append(WebUserSetFields.EQUALS_PARAMETER).append(profile);
}

private void initConfigurations() {
// regularUserAuthorizationValue = ClientConfiguration.getInstance().getAuthorizationHeaderValue();
regularUserAuthorizationValue = getOauthToken();
}

public String getOauthToken() {
try {

String ACCESS_TOKEN = "access_token";
String oauthUri = ClientConfiguration.getInstance().getOauthServiceUri();
String oauthParams = ClientConfiguration.getInstance().getOauthRequestParams();
HttpConnection connection = new HttpConnection();
ResponseEntity<String> response;
response = connection.post(oauthUri, oauthParams, "application/x-www-form-urlencoded");

if (HttpStatus.OK == response.getStatusCode()) {
String body = response.getBody();
JSONObject json = new JSONObject(body);
if (json.has(ACCESS_TOKEN)) {
return "Bearer " + json.getString(ACCESS_TOKEN);
} else {
throw new TechnicalRuntimeException("Cannot extract authentication token from reponse:" + body);
}
} else {
throw new TechnicalRuntimeException(
"Error occured when calling oath service! " + response);
}
} catch (IOException | JSONException e) {
throw new TechnicalRuntimeException("Cannot retrieve authentication token!", e);
}
String resUrl = urlBuilder.toString();

}
logger.trace("Ivoking create set: {} ", resUrl);

/**
* This method creates UserSet object from Json string. Example HTTP request for
* tag object: http://localhost:8080/set/?profile=minimal
*
* @param set The UserSet body
* @param profile
* @return response entity that comprises response body, headers and status
* code.
* @throws IOException
* Execute Europeana API request
*/
public ResponseEntity<String> createUserSet(String set, String profile) throws IOException {

StringBuilder urlBuilder = getUserSetServiceUri();
urlBuilder.append(WebUserSetFields.PAR_CHAR);
urlBuilder.append(CommonApiConstants.QUERY_PARAM_PROFILE).append(WebUserSetFields.EQUALS_PARAMETER)
.append(profile);

String resUrl = urlBuilder.toString();

logger.trace("Ivoking create set: {} ", resUrl);

/**
* Execute Europeana API request
*/
return postURL(resUrl, set, regularUserAuthorizationValue);
return postURL(resUrl, set, regularUserAuthorizationValue);
}

/**
* This method retrieves UserSet object. Example HTTP request for tag object:
* http://localhost:8080/set/{identifier}.jsonld?profile=minimal where identifier is: 496
*
* @param identifier
* @param profile
* @return response entity that comprises response body, headers and status code.
* @throws IOException
*/
public ResponseEntity<String> getUserSet(String identifier, String profile) throws IOException {

StringBuilder urlBuilder = getUserSetServiceUri();
urlBuilder.append(identifier).append(WebUserSetFields.JSON_LD_REST);
if (StringUtils.isNotEmpty(profile)) {
urlBuilder.append(WebUserSetFields.PAR_CHAR);
urlBuilder.append(CommonApiConstants.QUERY_PARAM_PROFILE)
.append(WebUserSetFields.EQUALS_PARAMETER).append(profile);
}

/**
* This method retrieves UserSet object. Example HTTP request for tag object:
* http://localhost:8080/set/{identifier}.jsonld?profile=minimal where
* identifier is: 496
*
* @param identifier
* @param profile
* @return response entity that comprises response body, headers and status
* code.
* @throws IOException
* Execute Europeana API request
*/
public ResponseEntity<String> getUserSet(String identifier, String profile) throws IOException {

StringBuilder urlBuilder = getUserSetServiceUri();
urlBuilder.append(identifier).append(WebUserSetFields.JSON_LD_REST);
urlBuilder.append(WebUserSetFields.PAR_CHAR);
urlBuilder.append(CommonApiConstants.QUERY_PARAM_PROFILE).append(WebUserSetFields.EQUALS_PARAMETER)
.append(profile);

/**
* Execute Europeana API request
*/
return getURL(urlBuilder.toString(), regularUserAuthorizationValue);
return getURL(urlBuilder.toString(), regularUserAuthorizationValue);
}

/**
* This method updates UserSet object by the passed Json update string. Example HTTP request:
* http://localhost:8080/set/{identifier}.jsonld?profile=standard where identifier is: 496 and the
* update JSON string is: { "title": {"en":"Sport"},"description": {"en":"Best sport"} }
*
* @param identifier The identifier that comprise set ID
* @param updateUserSet The update UserSet body in JSON format
* @param profile
* @return response entity that comprises response body, headers and status code.
* @throws IOException
*/
public ResponseEntity<String> updateUserSet(String identifier, String updateUserSet,
String profile) throws IOException {

StringBuilder urlBuilder = getUserSetServiceUri();
urlBuilder.append(identifier).append(WebUserSetFields.JSON_LD_REST);
if (StringUtils.isNotEmpty(profile)) {
urlBuilder.append(WebUserSetFields.PAR_CHAR);
urlBuilder.append(CommonApiConstants.QUERY_PARAM_PROFILE)
.append(WebUserSetFields.EQUALS_PARAMETER).append(profile);
}

/**
* This method updates UserSet object by the passed Json update string. Example
* HTTP request: http://localhost:8080/set/{identifier}.jsonld?profile=standard
* where identifier is: 496 and the update JSON string is: { "title":
* {"en":"Sport"},"description": {"en":"Best sport"} }
*
* @param identifier The identifier that comprise set ID
* @param updateUserSet The update UserSet body in JSON format
* @param profile
* @return response entity that comprises response body, headers and status
* code.
* @throws IOException
* Execute Europeana API request
*/
public ResponseEntity<String> updateUserSet(String identifier, String updateUserSet, String profile)
throws IOException {

StringBuilder urlBuilder = getUserSetServiceUri();
urlBuilder.append(identifier).append(WebUserSetFields.JSON_LD_REST);
urlBuilder.append(WebUserSetFields.PAR_CHAR);
urlBuilder.append(CommonApiConstants.QUERY_PARAM_PROFILE).append(WebUserSetFields.EQUALS_PARAMETER)
.append(profile);

/**
* Execute Europeana API request
*/
return putURL(urlBuilder.toString(), updateUserSet, regularUserAuthorizationValue);
}
return putURL(urlBuilder.toString(), updateUserSet, regularUserAuthorizationValue);
}

/**
* This method deletes UserSet object by the passed identifier. Example HTTP request:
* http://localhost:8080/set/{identifier}.jsonld?profile=minimal where identifier is: 494
*
* @param identifier The identifier that comprise set ID
* @return response entity that comprises response headers and status code.
* @throws IOException
*/
public ResponseEntity<String> deleteUserSet(String identifier) throws IOException {

StringBuilder urlBuilder = getUserSetServiceUri();
urlBuilder.append(identifier).append(WebUserSetFields.JSON_LD_REST);

/**
* This method deletes UserSet object by the passed identifier. Example HTTP
* request: http://localhost:8080/set/{identifier}.jsonld?profile=minimal where
* identifier is: 494
*
* @param identifier The identifier that comprise set ID
* @return response entity that comprises response headers and status code.
* @throws IOException
* Execute Europeana API request
*/
public ResponseEntity<String> deleteUserSet(String identifier) throws IOException {

StringBuilder urlBuilder = getUserSetServiceUri();
urlBuilder.append(identifier).append(WebUserSetFields.JSON_LD_REST);
urlBuilder.append(WebUserSetFields.PAR_CHAR);
urlBuilder.append(CommonApiConstants.QUERY_PARAM_PROFILE).append(WebUserSetFields.EQUALS_PARAMETER)
.append(CommonApiConstants.PROFILE_MINIMAL);

/**
* Execute Europeana API request
*/
return deleteURL(urlBuilder.toString(), regularUserAuthorizationValue);
}
return deleteURL(urlBuilder.toString(), regularUserAuthorizationValue);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import eu.europeana.set.client.web.WebUserSetApi;
import eu.europeana.set.client.web.WebUserSetApiImpl;
import eu.europeana.set.definitions.model.UserSet;
import eu.europeana.set.definitions.model.vocabulary.SetResourceProfile;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class BaseWebUserSetProtocol {
Expand Down Expand Up @@ -84,7 +83,7 @@ protected void deleteUserSet(String identifier) {
}

protected ResponseEntity<String> getUserSet(UserSet set) {
return getApiClient().getUserSet(set.getIdentifier(), SetResourceProfile.META.getProfileParamValue());
return getApiClient().getUserSet(set.getIdentifier(), null);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import eu.europeana.set.definitions.model.vocabulary.SetResourceProfile;


/**
Expand Down Expand Up @@ -46,7 +45,7 @@ public class WebUserSetProtocolExceptionsTest extends BaseWebUserSetProtocol {
public void createWebsetUserSetWithoutBody() throws IOException {

ResponseEntity<String> response = getApiClient().createUserSet(
null, SetResourceProfile.META.getProfileParamValue());
null, null);

assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
}
Expand All @@ -55,15 +54,15 @@ public void createWebsetUserSetWithoutBody() throws IOException {
@Test
public void createWebUserSetWithCorruptedBody() {
ResponseEntity<String> response = getApiClient().createUserSet(
CORRUPTED_JSON, SetResourceProfile.META.getProfileParamValue());
CORRUPTED_JSON, null);

assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
}

@Test
public void getWebUserSetWithWrongIdentifier() {
ResponseEntity<String> response = getApiClient().getUserSet(
WRONG_GENERATED_IDENTIFIER, SetResourceProfile.META.getProfileParamValue());
WRONG_GENERATED_IDENTIFIER, null);
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
}

Expand All @@ -74,7 +73,7 @@ public void updateWebsetUserSetWithWrongIdentifierNumber() throws IOException {
ResponseEntity<String> response = getApiClient().updateUserSet(
WRONG_GENERATED_IDENTIFIER
, requestBody
, SetResourceProfile.META.getProfileParamValue());
, null);
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
}

Expand All @@ -85,7 +84,7 @@ public void updateWebUserSetWithWrongIdentifier() throws IOException {
ResponseEntity<String> response = getApiClient().updateUserSet(
WRONG_GENERATED_IDENTIFIER
, requestBody
, SetResourceProfile.META.getProfileParamValue());
, null);
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
}

Expand Down
Loading

0 comments on commit f36be22

Please sign in to comment.