Skip to content

Commit

Permalink
fetching customer by cristin id
Browse files Browse the repository at this point in the history
  • Loading branch information
truhacevkir committed Dec 5, 2024
1 parent e657777 commit 5668796
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

group 'com.github.bibsysdev'
version = '1.41.2'
version = '1.41.3'

java {
sourceCompatibility = JavaVersion.VERSION_17
Expand Down
10 changes: 10 additions & 0 deletions clients/src/main/java/no/unit/nva/clients/GetCustomerResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package no.unit.nva.clients;

import java.net.URI;
import java.util.UUID;
import no.unit.nva.commons.json.JsonSerializable;

public record GetCustomerResponse(URI id, UUID identifier, String name, String displayName, String shortName,
URI cristinId) implements JsonSerializable {

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static nva.commons.core.attempt.Try.attempt;
import com.fasterxml.jackson.core.JsonProcessingException;
import java.net.URI;
import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
Expand All @@ -29,6 +30,8 @@ public class IdentityServiceClient {
private static final String API_PATH_USERS = "users";
private static final String AUTH_HOST = new Environment().readEnv("BACKEND_CLIENT_AUTH_URL");
private static final String API_HOST = new Environment().readEnv("API_HOST");
public static final String CUSTOMER_PATH_PARAM = "customer";
public static final String CRISTIN_ID_PATH_PARAM = "cristinId";
private final AuthorizedBackendClient authorizedClient;
private final HttpClient unauthorizedClient;

Expand Down Expand Up @@ -82,6 +85,25 @@ public GetUserResponse getUser(String userName) throws NotFoundException {
.orElseThrow(this::handleFailure);
}

public GetCustomerResponse getCustomerByCristinId(URI topLevelOrgCristinId) throws NotFoundException {
var request = HttpRequest.newBuilder()
.GET()
.uri(constructCustomerGetPath(topLevelOrgCristinId));
return attempt(getHttpResponseCallable(request))
.map(this::validateResponse)
.map(r -> mapResponse(GetCustomerResponse.class, r))
.orElseThrow(this::handleFailure);
}

private URI constructCustomerGetPath(URI topLevelOrgCristinId) {
var customerByCristinIdUri = UriWrapper.fromHost(API_HOST)
.addChild(CUSTOMER_PATH_PARAM)
.addChild(CRISTIN_ID_PATH_PARAM)
.getUri();
return URI.create(
customerByCristinIdUri + "/" + URLEncoder.encode(topLevelOrgCristinId.toString(), UTF_8));
}

@JacocoGenerated
private static CognitoCredentials fetchCredentials() {
var secretsReader = new SecretsReader(SecretsReader.defaultSecretsManagerClient());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandler;
import java.util.List;
import java.util.UUID;
import no.unit.nva.auth.CognitoCredentials;
import no.unit.nva.clients.GetUserResponse.Role;
import no.unit.nva.clients.GetUserResponse.ViewingScope;
Expand Down Expand Up @@ -173,6 +174,29 @@ void shouldThrowNotFoundWhenHttpClientNotFound() throws IOException, Interrupted
assertThrows(NotFoundException.class, action);
}

@Test
void shouldReturnCustomerWhenRequested() throws NotFoundException, IOException, InterruptedException {
var customerCristinId = randomUri();
var expectedCustomer = createCustomer(customerCristinId);
var mockedResponse = mockResponse(expectedCustomer.toJsonString());
when(httpClient.send(any(HttpRequest.class), any(BodyHandler.class))).thenReturn(mockedResponse);

var actual = authorizedIdentityServiceClient.getCustomerByCristinId(customerCristinId);

assertEquals(expectedCustomer, actual);
}

@Test
void shouldThrowNotFoundWhenCustomerNotFound() throws IOException, InterruptedException {
when(httpClient.send(any(HttpRequest.class), any(BodyHandler.class))).thenReturn(notFoundResponse);
assertThrows(NotFoundException.class, () -> authorizedIdentityServiceClient.getCustomerByCristinId(randomUri()));
}

private GetCustomerResponse createCustomer(URI customerCristinId) {
return new GetCustomerResponse(randomUri(), UUID.randomUUID(), randomString(), randomString(), randomString(),
customerCristinId);
}

private GetUserResponse createUser(String userName) {
return GetUserResponse.builder()
.withUsername(userName)
Expand Down

0 comments on commit 5668796

Please sign in to comment.