Write tests for SigningRequestDocumentController.patchSigningRequestDocument
This commit is contained in:
6
pom.xml
6
pom.xml
@ -125,6 +125,12 @@
|
|||||||
<artifactId>postgresql</artifactId>
|
<artifactId>postgresql</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.instancio</groupId>
|
||||||
|
<artifactId>instancio-junit</artifactId>
|
||||||
|
<version>5.3.0</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -34,10 +34,8 @@ public class SigningRequestDocumentController implements SigningRequestDocumentA
|
|||||||
signingRequestDocumentService.updateSigningRequestDocument(patchedDocument);
|
signingRequestDocumentService.updateSigningRequestDocument(patchedDocument);
|
||||||
|
|
||||||
return ResponseEntity.noContent().build();
|
return ResponseEntity.noContent().build();
|
||||||
} catch (JsonPatchException e) {
|
} catch (JsonPatchException | IOException e) {
|
||||||
throw new IllegalArgumentException("Failed to apply patch", e);
|
throw new IllegalArgumentException("Failed to apply patch", e);
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException("Failed to apply patch", e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,115 @@
|
|||||||
|
package ch.dlmw.swisssignchallenge;
|
||||||
|
|
||||||
|
import ch.dlmw.swisssignchallenge.controllers.SigningRequestDocumentController;
|
||||||
|
import ch.dlmw.swisssignchallenge.entities.SigningRequestDocument;
|
||||||
|
import ch.dlmw.swisssignchallenge.services.SigningRequestDocumentService;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import org.instancio.Instancio;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.mockito.ArgumentCaptor;
|
||||||
|
import org.mockito.InjectMocks;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
import org.openapitools.model.PatchOperation;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.NoSuchElementException;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import static org.instancio.Select.field;
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
public class SigningRequestDocumentControllerTests {
|
||||||
|
@Mock
|
||||||
|
private SigningRequestDocumentService signingRequestDocumentService;
|
||||||
|
|
||||||
|
@InjectMocks
|
||||||
|
private SigningRequestDocumentController signingRequestDocumentController;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void setUp() {
|
||||||
|
var objectMapper = new ObjectMapper();
|
||||||
|
signingRequestDocumentController = new SigningRequestDocumentController(
|
||||||
|
signingRequestDocumentService,
|
||||||
|
null,
|
||||||
|
objectMapper
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenCorrectRequest_whenPatchSigningRequestDocument_thenPatchDocument() {
|
||||||
|
var id = UUID.randomUUID();
|
||||||
|
var patchOperations = List.of(
|
||||||
|
new PatchOperation("replace", "/confirmed", "true")
|
||||||
|
);
|
||||||
|
|
||||||
|
var returnedSigningRequestDocument = Instancio.of(SigningRequestDocument.class)
|
||||||
|
.set(field(SigningRequestDocument::isConfirmed), false)
|
||||||
|
.create();
|
||||||
|
when(signingRequestDocumentService.getSigningRequestDocument(id.toString()))
|
||||||
|
.thenReturn(returnedSigningRequestDocument);
|
||||||
|
|
||||||
|
signingRequestDocumentController.patchSigningRequestDocument(id.toString(), patchOperations);
|
||||||
|
|
||||||
|
var documentCaptor = ArgumentCaptor.forClass(SigningRequestDocument.class);
|
||||||
|
verify(signingRequestDocumentService).updateSigningRequestDocument(documentCaptor.capture());
|
||||||
|
|
||||||
|
assertTrue(documentCaptor.getValue().isConfirmed());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenNonExistingSigningRequestDocument_whenPatchSigningRequestDocument_thenThrowNoSuchElementException() {
|
||||||
|
var id = UUID.randomUUID();
|
||||||
|
var patchOperations = List.of(
|
||||||
|
new PatchOperation("replace", "/confirmed", "true")
|
||||||
|
);
|
||||||
|
|
||||||
|
when(signingRequestDocumentService.getSigningRequestDocument(id.toString()))
|
||||||
|
.thenThrow(NoSuchElementException.class);
|
||||||
|
|
||||||
|
assertThrows(NoSuchElementException.class, () -> {
|
||||||
|
signingRequestDocumentController.patchSigningRequestDocument(id.toString(), patchOperations);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInvalidPatchOperationPath_whenPatchSigningRequestDocument_thenThrowIllegalArgumentException() {
|
||||||
|
var id = UUID.randomUUID();
|
||||||
|
var patchOperations = List.of(
|
||||||
|
new PatchOperation("replace", "/invalid", "true")
|
||||||
|
);
|
||||||
|
|
||||||
|
var returnedSigningRequestDocument = Instancio.of(SigningRequestDocument.class)
|
||||||
|
.set(field(SigningRequestDocument::isConfirmed), false)
|
||||||
|
.create();
|
||||||
|
when(signingRequestDocumentService.getSigningRequestDocument(id.toString()))
|
||||||
|
.thenReturn(returnedSigningRequestDocument);
|
||||||
|
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> {
|
||||||
|
signingRequestDocumentController.patchSigningRequestDocument(id.toString(), patchOperations);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInvalidPatchOperationValue_whenPatchSigningRequestDocument_thenThrowIllegalArgumentException() {
|
||||||
|
var id = UUID.randomUUID();
|
||||||
|
var patchOperations = List.of(
|
||||||
|
new PatchOperation("replace", "/confirmed", "notabool")
|
||||||
|
);
|
||||||
|
|
||||||
|
var returnedSigningRequestDocument = Instancio.of(SigningRequestDocument.class)
|
||||||
|
.set(field(SigningRequestDocument::isConfirmed), false)
|
||||||
|
.create();
|
||||||
|
when(signingRequestDocumentService.getSigningRequestDocument(id.toString()))
|
||||||
|
.thenReturn(returnedSigningRequestDocument);
|
||||||
|
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> {
|
||||||
|
signingRequestDocumentController.patchSigningRequestDocument(id.toString(), patchOperations);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user