From b9901ea38ee7e197c41d5528bae0bd03ab8e6923 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Szrnka?= Date: Tue, 10 Dec 2024 10:13:47 +0100 Subject: [PATCH] GMS-177 Code smell fixes --- .../functions/secret/SecretController.java | 8 +-- ...java => SecretLengthValidatorService.java} | 2 +- .../secret/SecretControllerTest.java | 10 ++-- ... => SecretLengthValidatorServiceTest.java} | 51 ++++++++++++------- 4 files changed, 42 insertions(+), 29 deletions(-) rename code/gms-backend/src/main/java/io/github/gms/functions/secret/{SecretValueSizeValidatorService.java => SecretLengthValidatorService.java} (98%) rename code/gms-backend/src/test/java/io/github/gms/functions/secret/{SecretValueSizeValidatorServiceTest.java => SecretLengthValidatorServiceTest.java} (66%) diff --git a/code/gms-backend/src/main/java/io/github/gms/functions/secret/SecretController.java b/code/gms-backend/src/main/java/io/github/gms/functions/secret/SecretController.java index 0cc1c5e2..4810cb01 100644 --- a/code/gms-backend/src/main/java/io/github/gms/functions/secret/SecretController.java +++ b/code/gms-backend/src/main/java/io/github/gms/functions/secret/SecretController.java @@ -29,12 +29,12 @@ public class SecretController extends AbstractClientController { private final SecretRotationService secretRotationService; - private final SecretValueSizeValidatorService secretValueSizeValidatorService; + private final SecretLengthValidatorService secretLengthValidatorService; - public SecretController(SecretService service, SecretRotationService secretRotationService, SecretValueSizeValidatorService secretValueSizeValidatorService) { + public SecretController(SecretService service, SecretRotationService secretRotationService, SecretLengthValidatorService secretLengthValidatorService) { super(service); this.secretRotationService = secretRotationService; - this.secretValueSizeValidatorService = secretValueSizeValidatorService; + this.secretLengthValidatorService = secretLengthValidatorService; } @PostMapping @@ -78,6 +78,6 @@ public ResponseEntity rotateSecret(@PathVariable(ID) Long id) { @PostMapping("/validate_value_length") @PreAuthorize(ROLE_USER) public ResponseEntity validateValueLength(@RequestBody SecretValueDto dto) { - return new ResponseEntity<>(secretValueSizeValidatorService.validateValueLength(dto), HttpStatus.OK); + return new ResponseEntity<>(secretLengthValidatorService.validateValueLength(dto), HttpStatus.OK); } } diff --git a/code/gms-backend/src/main/java/io/github/gms/functions/secret/SecretValueSizeValidatorService.java b/code/gms-backend/src/main/java/io/github/gms/functions/secret/SecretLengthValidatorService.java similarity index 98% rename from code/gms-backend/src/main/java/io/github/gms/functions/secret/SecretValueSizeValidatorService.java rename to code/gms-backend/src/main/java/io/github/gms/functions/secret/SecretLengthValidatorService.java index 017b92db..98297b1a 100644 --- a/code/gms-backend/src/main/java/io/github/gms/functions/secret/SecretValueSizeValidatorService.java +++ b/code/gms-backend/src/main/java/io/github/gms/functions/secret/SecretLengthValidatorService.java @@ -25,7 +25,7 @@ @Slf4j @Service @RequiredArgsConstructor -public class SecretValueSizeValidatorService { +public class SecretLengthValidatorService { private final KeystoreRepository keystoreRepository; private final KeystoreAliasRepository keystoreAliasRepository; diff --git a/code/gms-backend/src/test/java/io/github/gms/functions/secret/SecretControllerTest.java b/code/gms-backend/src/test/java/io/github/gms/functions/secret/SecretControllerTest.java index d57cd814..32f92a11 100644 --- a/code/gms-backend/src/test/java/io/github/gms/functions/secret/SecretControllerTest.java +++ b/code/gms-backend/src/test/java/io/github/gms/functions/secret/SecretControllerTest.java @@ -27,14 +27,14 @@ class SecretControllerTest extends AbstractClientControllerTest { private SecretRotationService secretRotationService; - private SecretValueSizeValidatorService secretValueSizeValidatorService; + private SecretLengthValidatorService secretLengthValidatorService; @BeforeEach void setup() { service = Mockito.mock(SecretService.class); secretRotationService = mock(SecretRotationService.class); - secretValueSizeValidatorService = mock(SecretValueSizeValidatorService.class); - controller = new SecretController(service, secretRotationService, secretValueSizeValidatorService); + secretLengthValidatorService = mock(SecretLengthValidatorService.class); + controller = new SecretController(service, secretRotationService, secretLengthValidatorService); } @Test @@ -146,7 +146,7 @@ void rotateSecret_whenInputProvided_thenReturnOk() { @Test void validateValueLength_whenInputProvided_thenReturnOk() { // arrange - when(secretValueSizeValidatorService.validateValueLength(any())).thenReturn(new BooleanValueDto(true)); + when(secretLengthValidatorService.validateValueLength(any())).thenReturn(new BooleanValueDto(true)); // act ResponseEntity response = controller.validateValueLength(SecretValueDto.builder().build()); @@ -156,6 +156,6 @@ void validateValueLength_whenInputProvided_thenReturnOk() { assertNotNull(response.getBody()); assertTrue(response.getBody().getValue()); assertEquals(200, response.getStatusCode().value()); - verify(secretValueSizeValidatorService).validateValueLength(any()); + verify(secretLengthValidatorService).validateValueLength(any()); } } \ No newline at end of file diff --git a/code/gms-backend/src/test/java/io/github/gms/functions/secret/SecretValueSizeValidatorServiceTest.java b/code/gms-backend/src/test/java/io/github/gms/functions/secret/SecretLengthValidatorServiceTest.java similarity index 66% rename from code/gms-backend/src/test/java/io/github/gms/functions/secret/SecretValueSizeValidatorServiceTest.java rename to code/gms-backend/src/test/java/io/github/gms/functions/secret/SecretLengthValidatorServiceTest.java index 1d2f24c4..aac9e592 100644 --- a/code/gms-backend/src/test/java/io/github/gms/functions/secret/SecretValueSizeValidatorServiceTest.java +++ b/code/gms-backend/src/test/java/io/github/gms/functions/secret/SecretLengthValidatorServiceTest.java @@ -35,15 +35,13 @@ import static io.github.gms.util.TestUtils.createSecretValueDto; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; /** * @author Peter Szrnka * @since 1.0 */ -class SecretValueSizeValidatorServiceTest extends AbstractLoggingUnitTest { +class SecretLengthValidatorServiceTest extends AbstractLoggingUnitTest { @Mock private KeystoreRepository keystoreRepository; @@ -56,14 +54,14 @@ class SecretValueSizeValidatorServiceTest extends AbstractLoggingUnitTest { @Mock private ObjectMapper objectMapper; @InjectMocks - private SecretValueSizeValidatorService secretValueSizeValidatorService; + private SecretLengthValidatorService secretLengthValidatorService; @Override @BeforeEach public void setup() { super.setup(); - ReflectionTestUtils.setField(secretValueSizeValidatorService, "keystorePath", "keystorePath"); - addAppender(SecretValueSizeValidatorService.class); + ReflectionTestUtils.setField(secretLengthValidatorService, "keystorePath", "keystorePath"); + addAppender(SecretLengthValidatorService.class); } @Test @@ -72,7 +70,7 @@ void validateValueLength_whenKeystoreEntityNotFound_thenReturnFalse() { SecretValueDto dto = createSecretValueDto(1L, 1L); // act - BooleanValueDto response = secretValueSizeValidatorService.validateValueLength(dto); + BooleanValueDto response = secretLengthValidatorService.validateValueLength(dto); // assert assertNotNull(response); @@ -83,14 +81,16 @@ void validateValueLength_whenKeystoreEntityNotFound_thenReturnFalse() { void validateValueLength_whenKeystoreAliasEntityNotFound_thenReturnFalse() { // arrange SecretValueDto dto = createSecretValueDto(1L, 1L); - when(keystoreRepository.findById(eq(1L))).thenReturn(Optional.of(TestUtils.createKeystoreEntity())); + when(keystoreRepository.findById(1L)).thenReturn(Optional.of(TestUtils.createKeystoreEntity())); // act - BooleanValueDto response = secretValueSizeValidatorService.validateValueLength(dto); + BooleanValueDto response = secretLengthValidatorService.validateValueLength(dto); // assert assertNotNull(response); assertFalse(response.getValue()); + verify(keystoreRepository).findById(1L); + verify(keystoreAliasRepository).findById(1L); } @Test @@ -98,16 +98,19 @@ void validateValueLength_whenKeystoreFileNotFound_thenReturnFalse() throws CertificateException, NoSuchAlgorithmException, IOException, KeyStoreException { // arrange SecretValueDto dto = createSecretValueDto(1L, 1L); - when(keystoreRepository.findById(eq(1L))).thenReturn(Optional.of(TestUtils.createKeystoreEntity())); - when(keystoreAliasRepository.findById(eq(1L))).thenReturn(Optional.of(TestUtils.createKeystoreAliasEntity())); + when(keystoreRepository.findById(1L)).thenReturn(Optional.of(TestUtils.createKeystoreEntity())); + when(keystoreAliasRepository.findById(1L)).thenReturn(Optional.of(TestUtils.createKeystoreAliasEntity())); when(keystoreDataService.getKeyStore(any(GetKeystore.class))).thenThrow(new FileNotFoundException()); // act - BooleanValueDto response = secretValueSizeValidatorService.validateValueLength(dto); + BooleanValueDto response = secretLengthValidatorService.validateValueLength(dto); // assert assertNotNull(response); assertFalse(response.getValue()); + verify(keystoreRepository).findById(1L); + verify(keystoreAliasRepository).findById(1L); + verify(keystoreDataService).getKeyStore(any(GetKeystore.class)); } @Test @@ -116,18 +119,23 @@ void validateValueLength_whenEncryptionFailed_thenReturnFalse() NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException { // arrange SecretValueDto dto = createSecretValueDto(1L, 1L); - when(keystoreRepository.findById(eq(1L))).thenReturn(Optional.of(TestUtils.createKeystoreEntity())); - when(keystoreAliasRepository.findById(eq(1L))).thenReturn(Optional.of(TestUtils.createKeystoreAliasEntity())); + when(keystoreRepository.findById(1L)).thenReturn(Optional.of(TestUtils.createKeystoreEntity())); + when(keystoreAliasRepository.findById(1L)).thenReturn(Optional.of(TestUtils.createKeystoreAliasEntity())); when(keystoreDataService.getKeyStore(any(GetKeystore.class))).thenReturn(mock(KeyStore.class)); when(objectMapper.writeValueAsString(any())).thenReturn(IntStream.of(1, 1000).collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append).toString()); when(cryptoService.encrypt(any(), any())).thenThrow(new GmsException("Encryption failed", GMS_001)); // act - BooleanValueDto response = secretValueSizeValidatorService.validateValueLength(dto); + BooleanValueDto response = secretLengthValidatorService.validateValueLength(dto); // assert assertNotNull(response); assertFalse(response.getValue()); + verify(keystoreRepository).findById(1L); + verify(keystoreAliasRepository).findById(1L); + verify(keystoreDataService).getKeyStore(any(GetKeystore.class)); + verify(objectMapper).writeValueAsString(any()); + verify(cryptoService).encrypt(any(), any()); } @Test @@ -136,18 +144,23 @@ void validateValueLength_whenEncryptionSucceeded_thenReturnTrue() NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException { // arrange SecretValueDto dto = createSecretValueDto(1L, 1L); - when(keystoreRepository.findById(eq(1L))).thenReturn(Optional.of(TestUtils.createKeystoreEntity())); - when(keystoreAliasRepository.findById(eq(1L))).thenReturn(Optional.of(TestUtils.createKeystoreAliasEntity())); + when(keystoreRepository.findById(1L)).thenReturn(Optional.of(TestUtils.createKeystoreEntity())); + when(keystoreAliasRepository.findById(1L)).thenReturn(Optional.of(TestUtils.createKeystoreAliasEntity())); when(keystoreDataService.getKeyStore(any(GetKeystore.class))).thenReturn(mock(KeyStore.class)); when(objectMapper.writeValueAsString(any())).thenReturn(IntStream.of(1, 1000).collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append).toString()); when(cryptoService.encrypt(any(), any())).thenReturn("encryptedValue"); // act - BooleanValueDto response = secretValueSizeValidatorService.validateValueLength(dto); + BooleanValueDto response = secretLengthValidatorService.validateValueLength(dto); // assert assertNotNull(response); assertTrue(response.getValue()); assertLogContains(logAppender, "Encrypted secret value size: 14"); + verify(keystoreRepository).findById(1L); + verify(keystoreAliasRepository).findById(1L); + verify(keystoreDataService).getKeyStore(any(GetKeystore.class)); + verify(objectMapper).writeValueAsString(any()); + verify(cryptoService).encrypt(any(), any()); } }