Skip to content

Commit

Permalink
adding proper mock
Browse files Browse the repository at this point in the history
  • Loading branch information
truhacevkir committed Dec 5, 2024
1 parent 5668796 commit 53ba48a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package no.unit.nva.clients;

import com.fasterxml.jackson.annotation.JsonProperty;
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,
public record GetCustomerResponse(@JsonProperty("id") 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 @@ -11,20 +11,26 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.io.IOException;
import java.net.URI;
import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandler;
import java.net.http.HttpResponse.BodyHandlers;
import java.nio.charset.StandardCharsets;
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;
import nva.commons.apigateway.exceptions.NotFoundException;
import nva.commons.core.Environment;
import nva.commons.core.paths.UriWrapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
Expand Down Expand Up @@ -178,8 +184,14 @@ void shouldThrowNotFoundWhenHttpClientNotFound() throws IOException, Interrupted
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 request = HttpRequest.newBuilder()
.GET()
.uri(createFetchCustomerByCristinIdUri(customerCristinId))
.build();

when(okResponseWithBody.body()).thenReturn(expectedCustomer.toJsonString());
when(okResponseWithBody.statusCode()).thenReturn(200);
when(httpClient.send(request, BodyHandlers.ofString(StandardCharsets.UTF_8))).thenReturn(okResponseWithBody);

var actual = authorizedIdentityServiceClient.getCustomerByCristinId(customerCristinId);

Expand All @@ -188,8 +200,28 @@ void shouldReturnCustomerWhenRequested() throws NotFoundException, IOException,

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

var request = HttpRequest.newBuilder()
.GET()
.uri(createFetchCustomerByCristinIdUri(topLevelOrgCristinId))
.build();

when(okResponseWithBody.body()).thenReturn(null);
when(okResponseWithBody.statusCode()).thenReturn(404);
when(httpClient.send(request, BodyHandlers.ofString(StandardCharsets.UTF_8))).thenReturn(okResponseWithBody);

assertThrows(NotFoundException.class, () -> authorizedIdentityServiceClient.getCustomerByCristinId(
topLevelOrgCristinId));
}

private static URI createFetchCustomerByCristinIdUri(URI customerCristinId) {
var fetchCustomerUri = UriWrapper.fromHost(new Environment().readEnv("API_HOST"))
.addChild("customer")
.addChild("cristinId")
.getUri();
return URI.create(fetchCustomerUri + "/" + URLEncoder.encode(customerCristinId.toString(),
StandardCharsets.UTF_8));
}

private GetCustomerResponse createCustomer(URI customerCristinId) {
Expand Down

0 comments on commit 53ba48a

Please sign in to comment.