Skip to content
Snippets Groups Projects
Commit 83dc9e01 authored by Martin Vogel's avatar Martin Vogel
Browse files

test: add client tests (planning#2246)

parent 1718c389
No related branches found
No related tags found
1 merge request!436planning#2267: Destination API Client
package dev.fitko.fitconnect.client;
import dev.fitko.fitconnect.api.domain.model.destination.CreateDestination;
import dev.fitko.fitconnect.api.domain.model.destination.Destination;
import dev.fitko.fitconnect.api.domain.model.destination.Destinations;
import dev.fitko.fitconnect.api.domain.model.destination.StatusEnum;
import dev.fitko.fitconnect.api.domain.model.jwk.ApiJwk;
import dev.fitko.fitconnect.api.domain.model.jwk.ApiJwks;
import dev.fitko.fitconnect.api.domain.validation.ValidationResult;
import dev.fitko.fitconnect.api.exceptions.client.FitConnectDestinationException;
import dev.fitko.fitconnect.api.exceptions.internal.RestApiException;
import dev.fitko.fitconnect.api.services.destination.DestinationService;
import dev.fitko.fitconnect.client.util.DestinationValidator;
import dev.fitko.fitconnect.tools.keygen.TestKeyBuilder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.Set;
import java.util.UUID;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
class DestinationClientTest {
private DestinationClient client;
private DestinationService apiMock;
private DestinationValidator validatorMock;
@BeforeEach
void setUp() {
apiMock = mock(DestinationService.class);
validatorMock = mock(DestinationValidator.class);
client = new DestinationClient(apiMock, validatorMock);
}
@Test
void getDestination() {
// Given
var destinationId = UUID.randomUUID();
var expectedDestination = new Destination();
expectedDestination.setDestinationId(destinationId);
when(apiMock.getDestination(destinationId)).thenReturn(expectedDestination);
// When
final Destination destination = client.getDestination(destinationId);
// Then
assertNotNull(destination);
assertThat(destination, is(expectedDestination));
}
@Test
void getDestinationFailed() {
// Given
var destinationId = UUID.randomUUID();
when(apiMock.getDestination(destinationId)).thenThrow(new RestApiException("request failed"));
// When
final FitConnectDestinationException exception = assertThrows(FitConnectDestinationException.class, () -> client.getDestination(destinationId));
// Then
assertNotNull(exception);
assertThat(exception.getMessage(), containsString("request failed"));
}
@Test
void createDestination() {
// Given
var destination = CreateDestination.builder()
.name("Test")
.status(StatusEnum.DRAFT)
.build();
var expectedDestination = new Destination();
expectedDestination.setDestinationId(UUID.randomUUID());
expectedDestination.setName("Test");
expectedDestination.setStatus(StatusEnum.DRAFT);
when(apiMock.createDestination(destination)).thenReturn(expectedDestination);
when(validatorMock.validateNewDestination(destination)).thenReturn(ValidationResult.ok());
// When
final Destination createdDestination = client.createDestination(destination);
// Then
assertNotNull(createdDestination);
assertThat(createdDestination.getName(), is(expectedDestination.getName()));
assertThat(createdDestination.getStatus(), is(expectedDestination.getStatus()));
assertThat(createdDestination.getDestinationId(), is(expectedDestination.getDestinationId()));
}
@Test
void createInvalidDestination() {
// Given
var destination = CreateDestination.builder()
.name("Test")
.status(StatusEnum.DRAFT)
.build();
when(validatorMock.validateNewDestination(destination)).thenReturn(ValidationResult.error("validation failed"));
// When
var exception = assertThrows(FitConnectDestinationException.class, () -> client.createDestination(destination));
// Then
assertNotNull(exception);
assertThat(exception.getCause().getMessage(), containsString("validation failed"));
}
@Test
void updateDestination() {
// Given
var destination = new Destination();
destination.setName("destination to be updated");
when(apiMock.updateDestination(destination)).thenReturn(destination);
when(validatorMock.validateDestination(destination)).thenReturn(ValidationResult.ok());
// When
final Destination updatedDestination = client.updateDestination(destination);
// Then
assertNotNull(updatedDestination);
assertThat(updatedDestination.getName(), is(destination.getName()));
}
@Test
void updateInvalidDestination() {
// Given
var invalidDestination = new Destination();
when(validatorMock.validateDestination(invalidDestination)).thenReturn(ValidationResult.error("destination invalid"));
// When
var exception = assertThrows(FitConnectDestinationException.class, () -> client.updateDestination(invalidDestination));
// Then
assertNotNull(exception);
assertThat(exception.getCause().getMessage(), containsString("destination invalid"));
}
@Test
void listDestinations() {
// Given
var destinationOne = new Destination();
destinationOne.setDestinationId(UUID.randomUUID());
var destinationTwo = new Destination();
destinationTwo.setDestinationId(UUID.randomUUID());
var expectedDestinations = new Destinations();
expectedDestinations.setDestinations(Set.of(destinationOne, destinationTwo));
expectedDestinations.setOffset(0);
expectedDestinations.setCount(2);
expectedDestinations.setTotalCount(10);
when(apiMock.listDestinations(anyInt(), anyInt())).thenReturn(expectedDestinations);
// When
final Destinations destinations = client.listDestinations(0, 10);
// Then
assertNotNull(destinations);
assertThat(destinations.getCount(), is(2));
assertThat(destinations.getTotalCount(), is(10));
assertThat(destinations.getOffset(), is(0));
assertThat(destinations.getDestinations().size(), is(2));
}
@Test
void deleteDestination() {
// Given
final UUID destinationId = UUID.randomUUID();
// When
assertDoesNotThrow(() -> client.deleteDestination(destinationId));
// Then
verify(apiMock, times(1)).deleteDestination(destinationId);
}
@Test
void deleteDestinationFailed() {
// Given
final UUID destinationId = UUID.randomUUID();
doThrow(new RestApiException("destination not found")).when(apiMock).deleteDestination(destinationId);
// When
var exception = assertThrows(FitConnectDestinationException.class, () -> client.deleteDestination(destinationId));
// Then
assertNotNull(exception);
assertThat(exception.getMessage(), containsString("destination not found"));
}
@Test
void getKeyForDestination() {
// Given
var destinationId = UUID.randomUUID();
final ApiJwk expectedKey = TestKeyBuilder.generateEncryptionKeyPair(1024).getPublicApiJwk();
when(apiMock.getKey(destinationId, expectedKey.getKid())).thenReturn(expectedKey);
// When
final ApiJwk apiJwk = client.getKeyForDestination(destinationId, expectedKey.getKid());
// Then
assertNotNull(apiJwk);
assertThat(apiJwk, is(expectedKey));
}
@Test
void getKeysForDestination() {
// Given
var destinationId = UUID.randomUUID();
final ApiJwk encryptionKey = TestKeyBuilder.generateEncryptionKeyPair(1024).getPublicApiJwk();
final ApiJwk signatureKey = TestKeyBuilder.generateSignatureKeyPair(1024).getPublicApiJwk();
var expectedKeys = new ApiJwks();
expectedKeys.setOffset(0);
expectedKeys.setCount(2);
expectedKeys.setTotalCount(10);
expectedKeys.setKeys(Set.of(encryptionKey, signatureKey));
when(apiMock.listKeys(destinationId, 0, 2)).thenReturn(expectedKeys);
// When
final ApiJwks apiJwks = client.getKeysForDestination(destinationId, 0, 2);
// Then
assertNotNull(apiJwks);
assertThat(apiJwks, is(expectedKeys));
}
@Test
void addKeyToDestination() {
// Given
var destinationId = UUID.randomUUID();
final ApiJwk encryptionKey = TestKeyBuilder.generateEncryptionKeyPair(1024).getPublicApiJwk();
// When
assertDoesNotThrow(() -> client.addKeyToDestination(destinationId, encryptionKey));
// Then
verify(apiMock, times(1)).addKey(destinationId, encryptionKey);
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment