|
6 | 6 | import org.acme.model.domain.EligibilityCheck; |
7 | 7 | import org.acme.model.dto.EligibilityCheck.CreateCheckRequest; |
8 | 8 | import org.acme.persistence.EligibilityCheckRepository; |
| 9 | +import org.acme.persistence.DocumentAlreadyExistsException; |
9 | 10 | import org.acme.persistence.StorageService; |
10 | 11 | import org.acme.service.CustomCheckDmnTemplate; |
11 | 12 | import org.acme.service.DmnService; |
@@ -61,6 +62,10 @@ void setUp() throws Exception { |
61 | 62 | when(dmnService.extractInputSchema(anyString(), any(), anyString())) |
62 | 63 | .thenReturn(new ObjectMapper().createObjectNode()); |
63 | 64 | when(repository.saveNewPublishedCustomCheck(any())).thenReturn("published-check-1"); |
| 65 | + when(repository.getWorkingId(any())).thenAnswer(invocation -> { |
| 66 | + EligibilityCheck check = invocation.getArgument(0); |
| 67 | + return "W-" + check.getOwnerId() + "-" + check.getModule() + "-" + check.getName(); |
| 68 | + }); |
64 | 69 | } |
65 | 70 |
|
66 | 71 | @Test |
@@ -118,6 +123,66 @@ void createCustomCheckRemovesTheCheckWhenItsDmnCannotBeStored() throws Exception |
118 | 123 | verify(repository).deleteWorkingCustomCheck(checkId); |
119 | 124 | } |
120 | 125 |
|
| 126 | + @Test |
| 127 | + void createCustomCheckRejectsAnExistingActiveCheck() throws Exception { |
| 128 | + CreateCheckRequest request = createCheckRequest(); |
| 129 | + String checkId = "W-owner-1-income-incomeCheck"; |
| 130 | + EligibilityCheck existing = new EligibilityCheck( |
| 131 | + request.name(), request.module(), request.description(), List.of(), USER_ID); |
| 132 | + when(repository.getWorkingCustomCheck(USER_ID, checkId, true)).thenReturn(Optional.of(existing)); |
| 133 | + |
| 134 | + Response response = resource.createCustomCheck(identity, request); |
| 135 | + |
| 136 | + assertEquals(Response.Status.CONFLICT.getStatusCode(), response.getStatus()); |
| 137 | + assertEquals( |
| 138 | + "You already have a check named \"incomeCheck\" in module \"income\".", |
| 139 | + ((java.util.Map<?, ?>) response.getEntity()).get("error")); |
| 140 | + verify(repository, never()).saveNewWorkingCustomCheck(any()); |
| 141 | + verify(storageService, never()).writeStringToStorage(anyString(), anyString(), anyString()); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + void createCustomCheckExplainsAnArchivedCollision() throws Exception { |
| 146 | + CreateCheckRequest request = createCheckRequest(); |
| 147 | + String checkId = "W-owner-1-income-incomeCheck"; |
| 148 | + EligibilityCheck existing = new EligibilityCheck( |
| 149 | + request.name(), request.module(), request.description(), List.of(), USER_ID); |
| 150 | + existing.setIsArchived(true); |
| 151 | + when(repository.getWorkingCustomCheck(USER_ID, checkId, true)).thenReturn(Optional.of(existing)); |
| 152 | + |
| 153 | + Response response = resource.createCustomCheck(identity, request); |
| 154 | + |
| 155 | + assertEquals(Response.Status.CONFLICT.getStatusCode(), response.getStatus()); |
| 156 | + assertEquals( |
| 157 | + "A check named \"incomeCheck\" in module \"income\" is archived. Restore it or choose a different name.", |
| 158 | + ((java.util.Map<?, ?>) response.getEntity()).get("error")); |
| 159 | + verify(repository, never()).saveNewWorkingCustomCheck(any()); |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + void createCustomCheckMapsAConcurrentCollisionToConflict() throws Exception { |
| 164 | + CreateCheckRequest request = createCheckRequest(); |
| 165 | + String checkId = "W-owner-1-income-incomeCheck"; |
| 166 | + when(repository.saveNewWorkingCustomCheck(any())) |
| 167 | + .thenThrow(new DocumentAlreadyExistsException(checkId, new RuntimeException())); |
| 168 | + |
| 169 | + Response response = resource.createCustomCheck(identity, request); |
| 170 | + |
| 171 | + assertEquals(Response.Status.CONFLICT.getStatusCode(), response.getStatus()); |
| 172 | + assertEquals( |
| 173 | + "You already have a check named \"incomeCheck\" in module \"income\".", |
| 174 | + ((java.util.Map<?, ?>) response.getEntity()).get("error")); |
| 175 | + verify(storageService, never()).writeStringToStorage(anyString(), anyString(), anyString()); |
| 176 | + } |
| 177 | + |
| 178 | + private CreateCheckRequest createCheckRequest() { |
| 179 | + return new CreateCheckRequest( |
| 180 | + "incomeCheck", |
| 181 | + "income", |
| 182 | + "Checks the applicant's income", |
| 183 | + List.of()); |
| 184 | + } |
| 185 | + |
121 | 186 | @Test |
122 | 187 | void firstPublishKeepsInitialVersion() throws Exception { |
123 | 188 | when(repository.getPublishedCheckVersions(workingCheck)).thenReturn(List.of()); |
|
0 commit comments