Skip to content

Commit

Permalink
Added test class for abstract validator #3028
Browse files Browse the repository at this point in the history
  • Loading branch information
lorriborri committed May 22, 2024
1 parent 195bf0c commit 4a7dea5
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.mercedesbenz.sechub.wrapper.prepare.modules;

import static org.junit.jupiter.api.Assertions.*;

import java.util.regex.Pattern;

import org.junit.jupiter.api.Test;

class AbstractInputValidatorTest {

TestInputValidator validatorToTest;

@Test
void constructor_throws_exception_when_type_is_null() {
/* prepare */
Pattern locationPattern = Pattern.compile(".*");
Pattern usernamePattern = Pattern.compile(".*");
Pattern passwordPattern = Pattern.compile(".*");

/* execute */
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
validatorToTest = new TestInputValidator(null, locationPattern, usernamePattern, passwordPattern);
});

/* test */
assertEquals("Type must not be null or empty.", exception.getMessage());
}

@Test
void constructor_throws_exception_when_type_is_empty() {
/* prepare */
Pattern locationPattern = Pattern.compile(".*");
Pattern usernamePattern = Pattern.compile(".*");
Pattern passwordPattern = Pattern.compile(".*");

/* execute */
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
validatorToTest = new TestInputValidator("", locationPattern, usernamePattern, passwordPattern);
});

/* test */
assertEquals("Type must not be null or empty.", exception.getMessage());
}

@Test
void constructor_throws_exception_when_locationPattern_is_null() {
/* prepare */
Pattern usernamePattern = Pattern.compile(".*");
Pattern passwordPattern = Pattern.compile(".*");

/* execute */
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
validatorToTest = new TestInputValidator("type", null, usernamePattern, passwordPattern);
});

/* test */
assertEquals("Pattern must not be null.", exception.getMessage());
}

@Test
void constructor_throws_exception_when_usernamePattern_is_null() {
/* prepare */
Pattern locationPattern = Pattern.compile(".*");
Pattern passwordPattern = Pattern.compile(".*");

/* execute */
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
validatorToTest = new TestInputValidator("type", locationPattern, null, passwordPattern);
});

/* test */
assertEquals("Pattern must not be null.", exception.getMessage());
}

@Test
void constructor_throws_exception_when_passwordPattern_is_null() {
/* prepare */
Pattern locationPattern = Pattern.compile(".*");
Pattern usernamePattern = Pattern.compile(".*");

/* execute */
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
validatorToTest = new TestInputValidator("type", locationPattern, usernamePattern, null);
});

/* test */
assertEquals("Pattern must not be null.", exception.getMessage());
}

@Test
void constructor_creates_instance_when_all_parameters_are_valid() {
/* prepare */
Pattern locationPattern = Pattern.compile(".*");
Pattern usernamePattern = Pattern.compile(".*");
Pattern passwordPattern = Pattern.compile(".*");

/* execute */
validatorToTest = new TestInputValidator("type", locationPattern, usernamePattern, passwordPattern);

/* test */
assertNotNull(validatorToTest);
}

private class TestInputValidator extends AbstractInputValidator {
public TestInputValidator(String type, Pattern locationPattern, Pattern usernamePattern, Pattern passwordPattern) {
super(type, locationPattern, usernamePattern, passwordPattern);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void beforeEach() {
}

@Test
void when_inputvalidator_throws_InputValidatorException_prepare_return_false() throws IOException, PrepareWrapperInputValidatorException {
void when_inputValidator_throws_InputValidatorException_prepare_return_false() throws IOException, PrepareWrapperInputValidatorException {
/* prepare */
PrepareWrapperContext context = createContext();
doThrow(new PrepareWrapperInputValidatorException("test", InputValidatorExitcode.LOCATION_NOT_MATCHING_PATTERN)).when(gitInputValidator)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void beforeEach() throws IOException, InterruptedException {

@ParameterizedTest
@ValueSource(strings = { "https://host.xz/path/to/repo/.git", "http://myrepo/here/.git", "example.org.git" })
void when_cloneRepository_is_executed_the_processAdapterFactory_starts_JGit_clone(String location) throws IOException {
void when_cloneRepository_is_executed_the_processAdapterFactory_starts_JGit_clone(String location) {
/* prepare */
Map<String, SealedObject> credentialMap = new HashMap<>();
credentialMap.put(PDS_PREPARE_CREDENTIAL_USERNAME, CryptoAccess.CRYPTO_STRING.seal("user"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import com.mercedesbenz.sechub.commons.model.SecHubRemoteDataConfiguration;
import com.mercedesbenz.sechub.test.TestFileWriter;
import com.mercedesbenz.sechub.wrapper.prepare.cli.PrepareWrapperEnvironment;
import com.mercedesbenz.sechub.wrapper.prepare.modules.InputValidatorExitcode;
import com.mercedesbenz.sechub.wrapper.prepare.modules.PrepareWrapperInputValidatorException;
import com.mercedesbenz.sechub.wrapper.prepare.prepare.PrepareWrapperContext;

class PrepareWrapperModuleSkopeoTest {
Expand All @@ -40,6 +42,30 @@ void beforeEach() {
moduleToTest.skopeo = skopeo;
}

@Test
void when_inputValidator_throws_InputValidatorException_prepare_return_false() throws IOException, PrepareWrapperInputValidatorException {
/* prepare */
PrepareWrapperContext context = createContext();
doThrow(new PrepareWrapperInputValidatorException("test", InputValidatorExitcode.LOCATION_NOT_MATCHING_PATTERN)).when(skopeoInputValidator)
.validate(context);

/* execute */
boolean result = moduleToTest.prepare(context);

/* test */
assertFalse(result);
}

@Test
void when_inputvalidator_throws_exception_prepare_throws_exception() throws PrepareWrapperInputValidatorException {
/* prepare */
PrepareWrapperContext context = createContext();
doThrow(new IllegalStateException("test")).when(skopeoInputValidator).validate(context);

/* execute + test */
assertThrows(IllegalStateException.class, () -> moduleToTest.prepare(context));
}

@Test
void prepare_successful_when_user_credentials_are_configured_correctly() throws IOException {
/* prepare */
Expand Down Expand Up @@ -151,4 +177,11 @@ void isDownloadSuccessful_returns_false_when_no_tar_file_in_directory() throws I
/* test */
assertFalse(result);
}

private PrepareWrapperContext createContext() {
PrepareWrapperEnvironment environment = mock(PrepareWrapperEnvironment.class);
when(environment.getPdsPrepareUploadFolderDirectory()).thenReturn("test-upload-folder");
return new PrepareWrapperContext(createFromJSON("{}"), environment);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class WrapperSkopeoTest {

WrapperSkopeo wrapperToTest;
PDSProcessAdapterFactory processAdapterFactory;

ProcessAdapter processAdapter;

@BeforeEach
Expand Down

0 comments on commit 4a7dea5

Please sign in to comment.