|
| 1 | +package org.codeforphilly.bdt.builder.persistence.impl; |
| 2 | + |
| 3 | +import jakarta.ws.rs.BadRequestException; |
| 4 | +import org.codeforphilly.bdt.builder.model.domain.Screener; |
| 5 | +import org.codeforphilly.bdt.builder.persistence.PublishedScreenerRepository; |
| 6 | +import org.codeforphilly.bdt.builder.persistence.StorageService; |
| 7 | +import org.junit.jupiter.api.BeforeEach; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 10 | +import org.mockito.InjectMocks; |
| 11 | +import org.mockito.Mock; |
| 12 | +import org.mockito.Spy; |
| 13 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 14 | + |
| 15 | +import java.io.IOException; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Map; |
| 18 | +import java.util.Optional; |
| 19 | + |
| 20 | +import static org.junit.jupiter.api.Assertions.*; |
| 21 | +import static org.mockito.Mockito.*; |
| 22 | + |
| 23 | +@ExtendWith(MockitoExtension.class) |
| 24 | +class ScreenerPublishingTest { |
| 25 | + @Mock StorageService storage; |
| 26 | + @Mock PublishedScreenerRepository published; |
| 27 | + @Spy @InjectMocks ScreenerRepositoryImpl repository; |
| 28 | + Screener screener; |
| 29 | + |
| 30 | + @BeforeEach |
| 31 | + void setup() { |
| 32 | + screener = Screener.create("owner", "Housing", null); |
| 33 | + screener.setId("working"); |
| 34 | + when(storage.getScreenerWorkingFormSchemaPath("working")).thenReturn("form/working/working.json"); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + void missingFormDoesNotCreateAPublicRecordOrMarkTheScreenerPublished() throws Exception { |
| 39 | + when(storage.readFormSchema("form/working/working.json")).thenReturn(Optional.empty()); |
| 40 | + var error = assertThrows(BadRequestException.class, () -> repository.publishScreener(screener)); |
| 41 | + assertEquals("Save the form in Form Editor before publishing.", error.getMessage()); |
| 42 | + assertNull(screener.getPublishedScreenerId()); |
| 43 | + assertNull(screener.getLastPublishDate()); |
| 44 | + verifyNoInteractions(published); |
| 45 | + verify(repository, never()).updateWorkingScreener(any()); |
| 46 | + verify(storage, never()).updatePublishedFormSchemaArtifact(anyString(), anyString()); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void failedStorageReadIsNotTreatedAsAnUnsavedForm() throws Exception { |
| 51 | + var failure = new IOException("Storage unavailable"); |
| 52 | + when(storage.readFormSchema("form/working/working.json")).thenThrow(failure); |
| 53 | + assertSame(failure, assertThrows(IOException.class, () -> repository.publishScreener(screener))); |
| 54 | + verifyNoInteractions(published); |
| 55 | + verify(repository, never()).updateWorkingScreener(any()); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void failedFormCopyDoesNotRefreshBenefitsOrAdvancePublicationMetadata() throws Exception { |
| 60 | + savedForm(); |
| 61 | + screener.setPublishedScreenerId("public"); |
| 62 | + screener.setLastPublishDate("previous publication"); |
| 63 | + doThrow(new IOException("Write failed")).when(storage).updatePublishedFormSchemaArtifact("working", "public"); |
| 64 | + assertThrows(IOException.class, () -> repository.publishScreener(screener)); |
| 65 | + assertEquals("previous publication", screener.getLastPublishDate()); |
| 66 | + verifyNoInteractions(published); |
| 67 | + verify(repository, never()).updateWorkingScreener(any()); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void publicationIsMarkedSuccessfulOnlyAfterArtifactsHaveBeenSaved() throws Exception { |
| 72 | + savedForm(); |
| 73 | + when(published.createPublishedScreener(screener)).thenReturn("public"); |
| 74 | + doNothing().when(repository).updateWorkingScreener(screener); |
| 75 | + repository.publishScreener(screener); |
| 76 | + var order = inOrder(storage, published, repository); |
| 77 | + order.verify(storage).readFormSchema("form/working/working.json"); |
| 78 | + order.verify(published).createPublishedScreener(screener); |
| 79 | + order.verify(storage).updatePublishedFormSchemaArtifact("working", "public"); |
| 80 | + order.verify(published).refreshPublishedScreenerbenefits("public", List.of()); |
| 81 | + order.verify(repository).updateWorkingScreener(screener); |
| 82 | + assertNotNull(screener.getLastPublishDate()); |
| 83 | + } |
| 84 | + |
| 85 | + private void savedForm() throws Exception { |
| 86 | + when(storage.readFormSchema("form/working/working.json")).thenReturn(Optional.of(Map.of("components", List.of()))); |
| 87 | + doReturn(List.of()).when(repository).getBenefitsInScreener(screener); |
| 88 | + } |
| 89 | +} |
0 commit comments