Skip to content

Commit f54df16

Browse files
AMP-437 Added validation in controller
1 parent 482ebd4 commit f54df16

3 files changed

Lines changed: 19 additions & 2 deletions

File tree

src/main/java/uk/gov/hmcts/cp/subscription/services/SubscriptionService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public ClientSubscription updateClientSubscription(final ClientSubscriptionReque
7070
return clientSubscriptionMapper.toDto(updatedClient, request.getEventTypes(), null);
7171
}
7272

73+
@Transactional(readOnly = true)
7374
public ClientSubscription getClientSubscription(final UUID clientId, final UUID subscriptionId) {
7475
log.info("getClientSubscription clientId:{} subscriptionId:{}", clientId, subscriptionId);
7576
final ClientEntity client = subscriptionValidationService.validateAndFetchClient(clientId, subscriptionId);
@@ -86,6 +87,7 @@ public void deleteClientSubscription(final UUID clientId, final UUID subscriptio
8687
clientRepository.delete(client);
8788
}
8889

90+
@Transactional(readOnly = true)
8991
public boolean hasAccess(final UUID subscriptionId,
9092
final String eventType) {
9193
return clientEventRepository.countByClientSubscriptionAndEventName(subscriptionId, eventType) > 0;

src/main/java/uk/gov/hmcts/cp/subscription/services/SubscriptionValidationService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public void validateClientDoesNotExist(final UUID clientId) {
3434
}
3535

3636
public void validateClientExists(final UUID clientId) {
37-
if (clientRepository.existsById(clientId)) {
38-
new EntityNotFoundException("Client not found for the provided clientId");
37+
if (!clientRepository.existsById(clientId)) {
38+
throw new EntityNotFoundException("Client not found for the provided clientId");
3939
}
4040
}
4141

src/test/java/uk/gov/hmcts/cp/subscription/unit/services/SubscriptionServiceValidationTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import static org.assertj.core.api.Assertions.assertThat;
2525
import static org.assertj.core.api.Assertions.assertThatThrownBy;
26+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
2627
import static org.mockito.Mockito.verify;
2728
import static org.mockito.Mockito.when;
2829

@@ -70,6 +71,20 @@ void validateClientDoesNotExist_should_throw_conflict_when_client_already_exists
7071
});
7172
}
7273

74+
@Test
75+
void validateClientExists_should_pass_when_client_exists() {
76+
when(clientRepository.existsById(clientId)).thenReturn(true);
77+
assertDoesNotThrow(() -> validationService.validateClientExists(clientId));
78+
}
79+
80+
@Test
81+
void validateClientExists_should_throw_when_client_not_found() {
82+
when(clientRepository.existsById(clientId)).thenReturn(false);
83+
assertThatThrownBy(() -> validationService.validateClientExists(clientId))
84+
.isInstanceOf(EntityNotFoundException.class)
85+
.hasMessage("Client not found for the provided clientId");
86+
}
87+
7388
@Test
7489
void validateAndFetchClient_should_return_client_when_found() {
7590
when(clientRepository.findByClientIdAndSubscriptionId(clientId, subscriptionId)).thenReturn(Optional.of(clientEntity));

0 commit comments

Comments
 (0)