Skip to content

Commit

Permalink
GMS-177 Code smell fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-szrnka committed Dec 10, 2024
1 parent 4be559b commit b9901ea
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
public class SecretController extends AbstractClientController<SecretService> {

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
Expand Down Expand Up @@ -78,6 +78,6 @@ public ResponseEntity<String> rotateSecret(@PathVariable(ID) Long id) {
@PostMapping("/validate_value_length")
@PreAuthorize(ROLE_USER)
public ResponseEntity<BooleanValueDto> validateValueLength(@RequestBody SecretValueDto dto) {
return new ResponseEntity<>(secretValueSizeValidatorService.validateValueLength(dto), HttpStatus.OK);
return new ResponseEntity<>(secretLengthValidatorService.validateValueLength(dto), HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
@Slf4j
@Service
@RequiredArgsConstructor
public class SecretValueSizeValidatorService {
public class SecretLengthValidatorService {

private final KeystoreRepository keystoreRepository;
private final KeystoreAliasRepository keystoreAliasRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@
class SecretControllerTest extends AbstractClientControllerTest<SecretService, SecretController> {

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
Expand Down Expand Up @@ -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<BooleanValueDto> response = controller.validateValueLength(SecretValueDto.builder().build());
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -83,31 +81,36 @@ 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
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
Expand All @@ -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
Expand All @@ -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());
}
}

0 comments on commit b9901ea

Please sign in to comment.