Skip to content

Commit

Permalink
EA-4015 added serach user set client methods and classes
Browse files Browse the repository at this point in the history
  • Loading branch information
SrishtiSingh-eu committed Nov 21, 2024
1 parent c7137ae commit 8c5decf
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 76 deletions.
118 changes: 77 additions & 41 deletions set-client/src/main/java/eu/europeana/set/client/UserSetApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import eu.europeana.set.client.config.ClientConfiguration;
import eu.europeana.set.client.exception.SetApiClientException;
import eu.europeana.set.client.web.SearchUserSetApi;
import eu.europeana.set.client.web.WebUserSetApi;
import org.springframework.http.ResponseEntity;

Expand All @@ -16,61 +17,96 @@
*
*/

public class UserSetApiClient extends BaseUserSetApi implements WebUserSetApi {
public class UserSetApiClient extends BaseUserSetApi {

private final WebUserSetClient webUserSetClient;
private final SearchUserSetClient searchUserSetClient;

public UserSetApiClient(ClientConfiguration configuration) throws SetApiClientException {
super(configuration);
this.webUserSetClient = new WebUserSetClient();
this.searchUserSetClient = new SearchUserSetClient();
}

@Override
public ResponseEntity<String> createUserSet(String set, String profile) {
ResponseEntity<String> res;
try {
res = getApiConnection().createUserSet(set, profile);
} catch (IOException e) {
throw new TechnicalRuntimeException(
"Exception occured when invoking the UserSetJsonApi createUserSet method", e);
public WebUserSetApi getWebUserSetApi() {
return webUserSetClient;
}
return res;
}

@Override
public ResponseEntity<String> deleteUserSet(String identifier) {
ResponseEntity<String> res;
try {
res = getApiConnection().deleteUserSet(identifier);
} catch (IOException e) {
throw new TechnicalRuntimeException(
"Exception occured when invoking the UserSetJsonApi deleteUserSet method", e);
public SearchUserSetApi getSearchUserSetApi() {
return searchUserSetClient;
}

return res;
}
/**
* Web User Set Client class
*/
private class WebUserSetClient implements WebUserSetApi {
@Override
public ResponseEntity<String> createUserSet(String set, String profile) {
ResponseEntity<String> res;
try {
res = getApiConnection().createUserSet(set, profile);
} catch (IOException e) {
throw new TechnicalRuntimeException(
"Exception occured when invoking the UserSetJsonApi createUserSet method", e);
}
return res;
}

@Override
public ResponseEntity<String> deleteUserSet(String identifier) {
ResponseEntity<String> res;
try {
res = getApiConnection().deleteUserSet(identifier);
} catch (IOException e) {
throw new TechnicalRuntimeException(
"Exception occured when invoking the UserSetJsonApi deleteUserSet method", e);
}

return res;
}

@Override
public ResponseEntity<String> getUserSet(String identifier, String profile) {
ResponseEntity<String> res;
try {
res = getApiConnection().getUserSet(identifier, profile);
} catch (IOException e) {
throw new TechnicalRuntimeException("Exception occured when invoking the UserSetJsonApi getUserSet method",
e);
@Override
public ResponseEntity<String> getUserSet(String identifier, String profile) {
ResponseEntity<String> res;
try {
res = getApiConnection().getUserSet(identifier, profile);
} catch (IOException e) {
throw new TechnicalRuntimeException("Exception occured when invoking the UserSetJsonApi getUserSet method",
e);
}

return res;
}

@Override
public ResponseEntity<String> updateUserSet(String identifier, String set, String profile) {
ResponseEntity<String> res;
try {
res = getApiConnection().updateUserSet(identifier, set, profile);
} catch (IOException e) {
throw new TechnicalRuntimeException(
"Exception occured when invoking the UserSetJsonApi updateUserSet method", e);
}

return res;
}
}

return res;
}
public class SearchUserSetClient implements SearchUserSetApi {

@Override
public ResponseEntity<String> updateUserSet(String identifier, String set, String profile) {
ResponseEntity<String> res;
try {
res = getApiConnection().updateUserSet(identifier, set, profile);
} catch (IOException e) {
throw new TechnicalRuntimeException(
"Exception occured when invoking the UserSetJsonApi updateUserSet method", e);
@Override
public ResponseEntity<String> searchUserSet(String query, String[] qf,
String sort, int page, int pageSize, String facet, int facetLimit, String profile) {
ResponseEntity<String> res;
try {
res = getApiConnection().searchUserSet(query, qf, sort, page, pageSize, facet, facetLimit, profile);
} catch (IOException e) {
throw new TechnicalRuntimeException(
"Exception occured when invoking the UserSetSearch API searchUserSet method", e);
}
return res;
}
}

return res;
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package eu.europeana.set.client.connection;

import java.io.IOException;
import java.net.URI;
import java.util.function.Function;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.http.ResponseEntity;

import eu.europeana.set.common.http.HttpConnection;
import eu.europeana.set.definitions.model.vocabulary.WebUserSetFields;
import org.springframework.web.util.UriBuilder;

import static eu.europeana.set.definitions.model.vocabulary.WebUserSetFields.FACETS;
import static eu.europeana.set.definitions.model.vocabulary.WebUserSetFields.SEARCH_PATH;
import static eu.europeana.api.commons.definitions.vocabulary.CommonApiConstants.*;

public class BaseApiConnection {

Expand Down Expand Up @@ -128,6 +135,34 @@ public StringBuilder getUserSetServiceUri() {
return urlBuilder;
}

public static Function<UriBuilder, URI> buildSearchUrl(String query, String[] qf, String sort, int page,
int pageSize, String facet, int facetLimit,
String profile) {
return uriBuilder -> {
UriBuilder builder =
uriBuilder
.path(SEARCH_PATH)
//.queryParam(WSKEY, wskey)
.queryParam(QUERY_PARAM_QUERY, query)
.queryParam(QUERY_PARAM_PAGE, page)
.queryParam(QUERY_PARAM_PAGE_SIZE, pageSize);
if (qf != null) {
builder.queryParam(QUERY_PARAM_QF, qf);
}
if (sort != null) {
builder.queryParam(QUERY_PARAM_SORT, sort);
}
if (facet != null) {
builder.queryParam(QUERY_PARAM_FACET, facet);
builder.queryParam("facet.limit", facetLimit);
}
if (profile != null) {
builder.queryParam(QUERY_PARAM_PROFILE, profile);
}
return builder.build();
};
}

public String getApiKey() {
return apiKey;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ public ResponseEntity<String> createUserSet(String set, String profile) throws I
String resUrl = urlBuilder.toString();

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

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

Expand All @@ -69,10 +65,6 @@ public ResponseEntity<String> getUserSet(String identifier, String profile) thro
urlBuilder.append(CommonApiConstants.QUERY_PARAM_PROFILE)
.append(WebUserSetFields.EQUALS_PARAMETER).append(profile);
}

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

Expand All @@ -97,10 +89,6 @@ public ResponseEntity<String> updateUserSet(String identifier, String updateUser
urlBuilder.append(CommonApiConstants.QUERY_PARAM_PROFILE)
.append(WebUserSetFields.EQUALS_PARAMETER).append(profile);
}

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

Expand All @@ -116,13 +104,17 @@ public ResponseEntity<String> deleteUserSet(String identifier) throws IOExceptio

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

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

public ResponseEntity<String> searchUserSet(String query, String[] qf, String sort, int page,
int pageSize, String facet, int facetLimit,
String profile) throws IOException {

StringBuilder urlBuilder = getUserSetServiceUri().append(buildSearchUrl(query, qf, sort, page, pageSize, facet, facetLimit, profile));
System.out.println(urlBuilder.toString());
return getURL(urlBuilder.toString(), regularUserAuthorizationValue);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package eu.europeana.set.client.web;

import org.springframework.http.ResponseEntity;

public interface SearchUserSetApi {

/**
* This methods retrieves the search results from the db
* @param query
* @param qf
* @param sort
* @param page
* @param pageSize
* @param facet
* @param facetLimit
* @param profile
* @return
*/
public ResponseEntity<String> searchUserSet(String query, String[] qf, String sort, int page, int pageSize,
String facet, int facetLimit, String profile);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,13 @@ public class BaseWebUserSetProtocol {
String START = "{";
String END = "}";

private WebUserSetApi apiClient;
protected UserSetApiClient apiClient;

@BeforeEach
public void initObjects() throws SetApiClientException {
apiClient = new UserSetApiClient(new ClientConfiguration());
}

public WebUserSetApi getApiClient() {
return apiClient;
}

/**
* This method creates test set object
*
Expand All @@ -54,7 +50,7 @@ protected ResponseEntity<String> storeTestUserSet(String resource, String profil
/**
* store set
*/
ResponseEntity<String> storedResponse = getApiClient().createUserSet(requestBody, profile);
ResponseEntity<String> storedResponse = apiClient.getWebUserSetApi().createUserSet(requestBody, profile);
return storedResponse;
}

Expand All @@ -75,14 +71,13 @@ protected void deleteUserSet(UserSet set) throws SetApiClientException {
}

protected void deleteUserSet(String identifier) throws SetApiClientException {
WebUserSetApi webUserSetApi = new UserSetApiClient(new ClientConfiguration());
ResponseEntity<String> re = webUserSetApi.deleteUserSet(identifier);
ResponseEntity<String> re = apiClient.getWebUserSetApi().deleteUserSet(identifier);
assertEquals(HttpStatus.OK, re.getStatusCode());
log.trace("User set deleted: /" + identifier);
}

protected ResponseEntity<String> getUserSet(UserSet set) {
return getApiClient().getUserSet(set.getIdentifier(), null);
return apiClient.getWebUserSetApi().getUserSet(set.getIdentifier(), null);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class WebUserSetProtocolExceptionsTest extends BaseWebUserSetProtocol {
@Test
public void createWebsetUserSetWithoutBody() throws IOException {

ResponseEntity<String> response = getApiClient().createUserSet(
ResponseEntity<String> response = apiClient.getWebUserSetApi().createUserSet(
null, null);

assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
Expand All @@ -53,15 +53,15 @@ public void createWebsetUserSetWithoutBody() throws IOException {

@Test
public void createWebUserSetWithCorruptedBody() {
ResponseEntity<String> response = getApiClient().createUserSet(
ResponseEntity<String> response = apiClient.getWebUserSetApi().createUserSet(
CORRUPTED_JSON, null);

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

@Test
public void getWebUserSetWithWrongIdentifier() {
ResponseEntity<String> response = getApiClient().getUserSet(
ResponseEntity<String> response = apiClient.getWebUserSetApi().getUserSet(
WRONG_GENERATED_IDENTIFIER, null);
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
}
Expand All @@ -70,7 +70,7 @@ public void getWebUserSetWithWrongIdentifier() {
public void updateWebsetUserSetWithWrongIdentifierNumber() throws IOException {
String requestBody = getJsonStringInput(USER_SET_CONTENT);

ResponseEntity<String> response = getApiClient().updateUserSet(
ResponseEntity<String> response = apiClient.getWebUserSetApi().updateUserSet(
WRONG_GENERATED_IDENTIFIER
, requestBody
, null);
Expand All @@ -81,7 +81,7 @@ public void updateWebsetUserSetWithWrongIdentifierNumber() throws IOException {
public void updateWebUserSetWithWrongIdentifier() throws IOException {
String requestBody = getJsonStringInput(USER_SET_CONTENT);

ResponseEntity<String> response = getApiClient().updateUserSet(
ResponseEntity<String> response = apiClient.getWebUserSetApi().updateUserSet(
WRONG_GENERATED_IDENTIFIER
, requestBody
, null);
Expand Down
Loading

0 comments on commit 8c5decf

Please sign in to comment.