diff --git a/Jenkinsfile_CNP b/Jenkinsfile_CNP index 68934fbf..28fe8377 100644 --- a/Jenkinsfile_CNP +++ b/Jenkinsfile_CNP @@ -2,11 +2,15 @@ @Library("Infrastructure") +import uk.gov.hmcts.contino.GradleBuilder + def type = "java" def product = "pip" def component = "account-management" def kv = product + '-ss-kv' +GradleBuilder builder = new GradleBuilder(this, product) + def setupTestSecrets() { def bootstap_env = env.ENV == "prod" || env.ENV == "demo" || env.ENV == "sbox" ? env.ENV : "stg" azureKeyVault( @@ -18,7 +22,8 @@ def setupTestSecrets() { secret('app-pip-publication-services-scope', 'PUBLICATION_SERVICES_AZ_API'), secret('app-pip-subscription-management-scope', 'SUBSCRIPTION_MANAGEMENT_AZ_API'), secret('app-tenant', 'TENANT_ID'), - + secret('app-pip-subscription-management-id', 'CLIENT_ID_FT'), + secret('app-pip-subscription-management-pwd', 'CLIENT_SECRET_FT'), ]) { env.APP_URI = "${APP_URI}" env.CLIENT_ID = "${CLIENT_ID}" @@ -26,6 +31,8 @@ def setupTestSecrets() { env.PUBLICATION_SERVICES_AZ_API = "${PUBLICATION_SERVICES_AZ_API}" env.SUBSCRIPTION_MANAGEMENT_AZ_API = "${SUBSCRIPTION_MANAGEMENT_AZ_API}" env.TENANT_ID = "${TENANT_ID}" + env.CLIENT_ID_FT = "${CLIENT_ID_FT}" + env.CLIENT_SECRET_FT = "${CLIENT_SECRET_FT}" } } @@ -62,4 +69,7 @@ withPipeline(type, product, component) { disableLegacyDeployment() enableApiGatewayTest() + afterAlways('test') { + builder.gradle('integration') + } } diff --git a/README.md b/README.md index 7f90e611..92e2cd31 100644 --- a/README.md +++ b/README.md @@ -196,20 +196,23 @@ Below is a table of currently used environment variables for starting the servic Secrets required for getting tests to run correctly can be found in the below table: -| Variable | Description | -|:-------------------------------|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| APP_URI | Uniform Resource Identifier - the location where the application expects to receive bearer tokens after a successful authentication process. The application then validates received bearer tokens using the AUD parameter in the token. | -| CLIENT_ID | Unique ID for the application within Azure AD. Used to identify the application during authentication. | -| CLIENT_SECRET | Secret key for authentication requests to the service. | -| PUBLICATION_SERVICES_AZ_API | Used as part of the `scope` parameter when requesting a token from Azure. Used for service-to-service communication with the pip-publication-services service. | -| SUBSCRIPTION_MANAGEMENT_AZ_API | Used as part of the `scope` parameter when requesting a token from Azure. Used for service-to-service communication with the pip-subscription-management service. | -| TENANT_ID | Directory unique ID assigned to our Azure AD tenant. Represents the organisation that owns and manages the Azure AD instance. | +| Variable | Description | +|:-------------------------------|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| APP_URI | Uniform Resource Identifier - the location where the application expects to receive bearer tokens after a successful authentication process. The application then validates received bearer tokens using the AUD parameter in the token. | +| CLIENT_ID | Unique ID for the application within Azure AD. Used to identify the application during authentication. | +| CLIENT_SECRET | Secret key for authentication requests to the service. | +| PUBLICATION_SERVICES_AZ_API | Used as part of the `scope` parameter when requesting a token from Azure. Used for service-to-service communication with the pip-publication-services service. | +| SUBSCRIPTION_MANAGEMENT_AZ_API | Used as part of the `scope` parameter when requesting a token from Azure. Used for service-to-service communication with the pip-subscription-management service. | +| TENANT_ID | Directory unique ID assigned to our Azure AD tenant. Represents the organisation that owns and manages the Azure AD instance. | +| CLIENT_ID_FT | Client ID of external service used for authentication with account-management application in the functional tests. | +| CLIENT_SECRET_FT | Client secret of external service.used for authentication with account-management application in the functional tests. | #### Application.yaml files The service can also be adapted using the yaml files found in the following locations: - [src/main/resources/application.yaml](./src/main/resources/application.yaml) for changes to the behaviour of the service itself. - [src/main/resources/application-dev.yaml](./src/main/resources/application-dev.yaml) for changes to the behaviour of the service when running locally. - [src/test/resources/application-test.yaml](./src/test/resources/application-test.yaml) for changes to other test types (e.g. unit tests). +- [src/integrationTest/resources/application-integration.yaml](./src/integrationTest/resources/application-integration.yaml) for changes to the application when it's running integration tests. - [src/integrationTest/resources/application-functional.yaml](./src/functionalTest/resources/application-functional.yaml) for changes to the application when its running functional tests. ### Fortify @@ -315,19 +318,27 @@ We use a few automated tools to ensure quality and security within the service. ## Test Suite -This microservice is comprehensively tested using both unit and functional tests. +This microservice is comprehensively tested using unit, integration and functional tests. ### Unit tests Unit tests can be run on demand using `./gradlew test`. +### Integration tests + +Integration tests can be run on demand using `./gradlew integration`. + +For our integration tests, we are using Square's [MockWebServer](https://github.com/square/okhttp/tree/master/mockwebserver) library. This allows us to test the full HTTP stack for our service-to-service interactions. + +The mock server interacts with external CaTH services on staging. + ### Functional tests Functional tests can be run using `./gradlew functional` -For our functional tests, we are using Square's [MockWebServer](https://github.com/square/okhttp/tree/master/mockwebserver) library. This allows us to test the full HTTP stack for our service-to-service interactions. +Functional testing is performed on the stood-up account-management instance on the dev pod (during pull request) or on staging (when running on the master branch). -The functional tests also call out to Data Management in staging to retrieve publications. +This account-management instance interacts with external CaTH services on staging. ## Contributing We are happy to accept third-party contributions. See [.github/CONTRIBUTING.md](./.github/CONTRIBUTING.md) for more details. diff --git a/build.gradle b/build.gradle index 39e73668..dcfdd32a 100644 --- a/build.gradle +++ b/build.gradle @@ -48,6 +48,15 @@ sourceSets { resources.srcDir file('src/functionalTest/resources') } + integrationTest { + java { + compileClasspath += main.output + runtimeClasspath += main.output + srcDir file('src/integrationTest/java') + } + resources.srcDir file('src/integrationTest/resources') + } + smokeTest { java { compileClasspath += main.output @@ -77,6 +86,9 @@ configurations { functionalTestImplementation.extendsFrom testImplementation functionalTestRuntimeOnly.extendsFrom runtimeOnly + integrationTestImplementation.extendsFrom testImplementation + integrationTestRuntimeOnly.extendsFrom runtimeOnly + smokeTestImplementation.extendsFrom testImplementation smokeTestRuntimeOnly.extendsFrom runtimeOnly @@ -113,6 +125,13 @@ task functional(type: Test) { classpath = sourceSets.functionalTest.runtimeClasspath } +task integration(type: Test) { + description = "Runs integration tests" + group = "Verification" + testClassesDirs = sourceSets.integrationTest.output.classesDirs + classpath = sourceSets.integrationTest.runtimeClasspath +} + task smoke(type: Test) { description = "Runs Smoke Tests" testClassesDirs = sourceSets.smokeTest.output.classesDirs @@ -127,7 +146,7 @@ checkstyle { pmd { toolVersion = "7.6.0" - sourceSets = [sourceSets.main, sourceSets.test, sourceSets.functionalTest, sourceSets.smokeTest] + sourceSets = [sourceSets.main, sourceSets.test, sourceSets.functionalTest, sourceSets.integrationTest, sourceSets.smokeTest] reportsDir = file("$project.buildDir/reports/pmd") ruleSetFiles = files("config/pmd/ruleset.xml") } @@ -137,7 +156,7 @@ jacoco { } jacocoTestReport { - executionData(test) + executionData(test, integration) reports { xml.required = true csv.required = false @@ -246,6 +265,8 @@ dependencies { testImplementation group: 'org.apiguardian', name: 'apiguardian-api', version: '1.1.2' testImplementation group: 'org.mockito', name: 'mockito-inline', version: '5.2.0' testImplementation group: 'com.github.hmcts', name: 'fortify-client', version: '1.4.4', classifier: 'all' + + functionalTestImplementation group: 'io.rest-assured', name: 'rest-assured' } task fortifyScan(type: JavaExec) { @@ -272,6 +293,10 @@ rootProject.tasks.named("processFunctionalTestResources") { duplicatesStrategy = 'include' } +rootProject.tasks.named("processIntegrationTestResources") { + duplicatesStrategy = 'include' +} + wrapper { distributionType = Wrapper.DistributionType.ALL } diff --git a/charts/pip-account-management/Chart.yaml b/charts/pip-account-management/Chart.yaml index 83ec29f5..51ab8568 100644 --- a/charts/pip-account-management/Chart.yaml +++ b/charts/pip-account-management/Chart.yaml @@ -2,7 +2,7 @@ name: pip-account-management apiVersion: v2 appVersion: "1.0" home: https://github.com/hmcts/pip-account-management -version: 0.0.45 +version: 0.0.46 description: Publishing & Information Account Management maintainers: - name: HMCTS PIP Team @@ -10,4 +10,7 @@ dependencies: - name: java version: 5.2.1 repository: 'https://hmctspublic.azurecr.io/helm/v1/repo/' - + - name: blobstorage + version: 2.0.2 + repository: https://hmctspublic.azurecr.io/helm/v1/repo/ + condition: blobstorage.enabled diff --git a/charts/pip-account-management/values.dev.template.yaml b/charts/pip-account-management/values.dev.template.yaml index 1fd20aba..221fd2f7 100644 --- a/charts/pip-account-management/values.dev.template.yaml +++ b/charts/pip-account-management/values.dev.template.yaml @@ -13,6 +13,21 @@ java: DB_USER: "{{ .Values.postgresql.auth.username}}" DB_PASS: "{{ .Values.postgresql.auth.password}}" DB_PORT: "5432" - #disableKeyVaults: true + SPRING_PROFILES_ACTIVE: blobStorageDev postgresql: enabled: true + secrets: + STORAGE_ACCOUNT_NAME: + secretRef: storage-account-{{ .Release.Name }}-blobstorage + key: storage_account_name + STORAGE_ACCOUNT_URL: + secretRef: storage-secret-{{ .Release.Name }}-blobstorage + key: blobEndpoint + STORAGE_ACCOUNT_KEY: + secretRef: storage-secret-{{ .Release.Name }}-blobstorage + key: accessKey +blobstorage: + resourceGroup: pip-aso-dev-rg + setup: + containers: + - files diff --git a/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/MediaApplicationCreationTest.java b/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/MediaApplicationCreationTest.java new file mode 100644 index 00000000..64dd8ec3 --- /dev/null +++ b/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/MediaApplicationCreationTest.java @@ -0,0 +1,52 @@ +package uk.gov.hmcts.reform.pip.account.management.controllers; + +import io.restassured.response.Response; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.core.io.ClassPathResource; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import uk.gov.hmcts.reform.pip.account.management.utils.FunctionalTestBase; +import uk.gov.hmcts.reform.pip.account.management.utils.OAuthClient; + +import java.util.Map; +import java.util.concurrent.ThreadLocalRandom; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.springframework.http.HttpHeaders.AUTHORIZATION; +import static org.springframework.http.HttpStatus.OK; + +@ExtendWith(SpringExtension.class) +@ActiveProfiles(profiles = "functional") +@SpringBootTest(classes = {OAuthClient.class}) +class MediaApplicationCreationTest extends FunctionalTestBase { + + private static final String TEST_NAME = "E2E Account Management Test Name"; + private static final String TEST_EMPLOYER = "E2E Account Management Test Employer"; + private static final String TEST_EMAIL_PREFIX = String.format( + "pip-am-test-email-%s", ThreadLocalRandom.current().nextInt(1000, 9999)); + + private static final String TEST_EMAIL = String.format(TEST_EMAIL_PREFIX + "@justice.gov.uk"); + private static final String STATUS = "PENDING"; + + private static final String TESTING_SUPPORT_APPLICATION_URL = "/testing-support/application/"; + private static final String MEDIA_APPLICATION_URL = "/application"; + private static final String BEARER = "Bearer "; + private static final String MOCK_FILE = "files/test-image.png"; + + @AfterAll + public void teardown() { + doDeleteRequest(TESTING_SUPPORT_APPLICATION_URL + TEST_EMAIL, Map.of(AUTHORIZATION, BEARER + accessToken), ""); + } + + @Test + void shouldBeAbleToCreateAMediaApplication() throws Exception { + final Response response = + doPostMultipartForApplication(MEDIA_APPLICATION_URL, Map.of(AUTHORIZATION, BEARER + accessToken), + new ClassPathResource(MOCK_FILE).getFile(), TEST_NAME, TEST_EMAIL, TEST_EMPLOYER, STATUS); + + assertThat(response.getStatusCode()).isEqualTo(OK.value()); + } +} diff --git a/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/utils/AuthException.java b/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/utils/AuthException.java new file mode 100644 index 00000000..215ddd0b --- /dev/null +++ b/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/utils/AuthException.java @@ -0,0 +1,9 @@ +package uk.gov.hmcts.reform.pip.account.management.utils; + +public class AuthException extends RuntimeException { + private static final long serialVersionUID = -6991745899622330407L; + + public AuthException(String error) { + super(error); + } +} diff --git a/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/utils/FunctionalTestBase.java b/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/utils/FunctionalTestBase.java new file mode 100644 index 00000000..ae512b35 --- /dev/null +++ b/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/utils/FunctionalTestBase.java @@ -0,0 +1,94 @@ +package uk.gov.hmcts.reform.pip.account.management.utils; + +import io.restassured.RestAssured; +import io.restassured.response.Response; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.TestInstance; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.util.CollectionUtils; +import uk.gov.hmcts.reform.pip.account.management.Application; + +import java.io.File; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import static io.restassured.RestAssured.given; +import static org.springframework.http.HttpHeaders.CONTENT_TYPE; + +@SpringBootTest(classes = {Application.class, OAuthClient.class}, + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +public class FunctionalTestBase { + + protected static final String CONTENT_TYPE_VALUE = "application/json"; + + @Autowired + private OAuthClient authClient; + + protected String accessToken; + + @Value("${test-url}") + private String testUrl; + + @BeforeAll + void setUp() { + RestAssured.baseURI = testUrl; + accessToken = authClient.generateAccessToken(); + } + + protected Response doGetRequest(final String path, final Map additionalHeaders) { + return given() + .relaxedHTTPSValidation() + .headers(getRequestHeaders(additionalHeaders)) + .when() + .get(path) + .thenReturn(); + } + + protected Response doPostRequest(final String path, final Map additionalHeaders, + final String body) { + return given() + .relaxedHTTPSValidation() + .headers(getRequestHeaders(additionalHeaders)) + .body(body) + .when() + .post(path) + .thenReturn(); + } + + protected Response doPostMultipartForApplication(final String path, final Map additionalHeaders, + final File file, String name, String email, String employer, String status) { + return given() + .relaxedHTTPSValidation() + .headers(additionalHeaders) + .multiPart("file", file) + .multiPart("fullName", name) + .multiPart("email", email) + .multiPart("employer", employer) + .multiPart("status", status) + .when() + .post(path) + .thenReturn(); + } + + protected Response doDeleteRequest(final String path, final Map additionalHeaders, + final String body) { + return given() + .relaxedHTTPSValidation() + .headers(getRequestHeaders(additionalHeaders)) + .body(body) + .when() + .delete(path) + .thenReturn(); + } + + private static Map getRequestHeaders(final Map additionalHeaders) { + final Map headers = new ConcurrentHashMap<>(Map.of(CONTENT_TYPE, CONTENT_TYPE_VALUE)); + if (!CollectionUtils.isEmpty(additionalHeaders)) { + headers.putAll(additionalHeaders); + } + return headers; + } +} diff --git a/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/utils/OAuthClient.java b/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/utils/OAuthClient.java new file mode 100644 index 00000000..88a02446 --- /dev/null +++ b/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/utils/OAuthClient.java @@ -0,0 +1,42 @@ +package uk.gov.hmcts.reform.pip.account.management.utils; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import static io.restassured.RestAssured.given; + +@Component +public class OAuthClient { + + @Value("${CLIENT_ID_FT}") + private String clientId; + + @Value("${CLIENT_SECRET_FT}") + private String clientSecret; + + @Value("${TENANT_ID}") + private String tenantId; + + @Value("${APP_URI}") + private String scope; + + public String generateAccessToken() { + String token = given() + .relaxedHTTPSValidation() + .header("content-type", "application/x-www-form-urlencoded") + .formParam("client_id", clientId) + .formParam("scope", scope + "/.default") + .formParam("client_secret", clientSecret) + .formParam("grant_type", "client_credentials") + .baseUri("https://login.microsoftonline.com/" + tenantId + "/oauth2/v2.0/token") + .post() + .body() + .jsonPath() + .get("access_token"); + + if (token == null) { + throw new AuthException("Unable to generate access token for the API"); + } + return token; + } +} diff --git a/src/functionalTest/resources/application-functional.yaml b/src/functionalTest/resources/application-functional.yaml index 02b5b63f..acee636b 100644 --- a/src/functionalTest/resources/application-functional.yaml +++ b/src/functionalTest/resources/application-functional.yaml @@ -1,10 +1 @@ -spring: - flyway: - enabled: true - jpa: - hibernate: - # Set to update for functional tests, so it creates the environment in H2 - ddl-auto: update - -admin: - max-system-admin: ${MAX_SYSTEM_ADMIN_ACCOUNTS:2} +test-url: ${TEST_URL:http://localhost:6969} diff --git a/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/config/AzureBlobConfigurationTestConfiguration.java b/src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/config/AzureBlobConfigurationTestConfiguration.java similarity index 96% rename from src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/config/AzureBlobConfigurationTestConfiguration.java rename to src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/config/AzureBlobConfigurationTestConfiguration.java index 0a2d317a..e14f17ff 100644 --- a/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/config/AzureBlobConfigurationTestConfiguration.java +++ b/src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/config/AzureBlobConfigurationTestConfiguration.java @@ -12,7 +12,7 @@ * Mock class for testing to mock out external calls to Azure. */ @Configuration -@Profile({"test", "functional"}) +@Profile({"integration"}) public class AzureBlobConfigurationTestConfiguration { @Mock diff --git a/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/config/AzureConfigurationClientTestConfiguration.java b/src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/config/AzureConfigurationClientTestConfiguration.java similarity index 98% rename from src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/config/AzureConfigurationClientTestConfiguration.java rename to src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/config/AzureConfigurationClientTestConfiguration.java index 495e793a..b0ec80d7 100644 --- a/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/config/AzureConfigurationClientTestConfiguration.java +++ b/src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/config/AzureConfigurationClientTestConfiguration.java @@ -9,7 +9,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; -@Profile("functional") +@Profile("integration") @Configuration public class AzureConfigurationClientTestConfiguration { diff --git a/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/config/SwaggerPublisherTest.java b/src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/config/SwaggerPublisherTest.java similarity index 97% rename from src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/config/SwaggerPublisherTest.java rename to src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/config/SwaggerPublisherTest.java index c4702a5a..c182a9c8 100644 --- a/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/config/SwaggerPublisherTest.java +++ b/src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/config/SwaggerPublisherTest.java @@ -25,7 +25,7 @@ @SpringBootTest(classes = {AzureConfigurationClientTestConfiguration.class, Application.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @AutoConfigureMockMvc -@ActiveProfiles(profiles = "functional") +@ActiveProfiles(profiles = "integration") @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS) @AutoConfigureEmbeddedDatabase(type = AutoConfigureEmbeddedDatabase.DatabaseType.POSTGRES) class SwaggerPublisherTest { diff --git a/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/AccountTest.java b/src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/AccountTest.java similarity index 99% rename from src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/AccountTest.java rename to src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/AccountTest.java index 5d180d8a..e395df12 100644 --- a/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/AccountTest.java +++ b/src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/AccountTest.java @@ -57,7 +57,7 @@ @SpringBootTest(classes = {AzureConfigurationClientTestConfiguration.class, Application.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @AutoConfigureMockMvc -@ActiveProfiles(profiles = "functional") +@ActiveProfiles(profiles = "integration") @AutoConfigureEmbeddedDatabase(type = AutoConfigureEmbeddedDatabase.DatabaseType.POSTGRES) @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) @WithMockUser(username = "admin", authorities = {"APPROLE_api.request.admin"}) diff --git a/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/AuditTest.java b/src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/AuditTest.java similarity index 99% rename from src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/AuditTest.java rename to src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/AuditTest.java index 9f4d804f..ebb80f64 100644 --- a/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/AuditTest.java +++ b/src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/AuditTest.java @@ -33,7 +33,7 @@ @SpringBootTest(classes = {AzureConfigurationClientTestConfiguration.class, Application.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @AutoConfigureMockMvc -@ActiveProfiles(profiles = "functional") +@ActiveProfiles(profiles = "integration") @AutoConfigureEmbeddedDatabase(type = AutoConfigureEmbeddedDatabase.DatabaseType.POSTGRES) @WithMockUser(username = "admin", authorities = {"APPROLE_api.request.admin"}) @SuppressWarnings({"PMD.TooManyMethods", "PMD.ExcessiveImports", "PMD.JUnitTestsShouldIncludeAssert"}) diff --git a/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/AzureAccountTest.java b/src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/AzureAccountTest.java similarity index 99% rename from src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/AzureAccountTest.java rename to src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/AzureAccountTest.java index b22b2668..c791beaf 100644 --- a/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/AzureAccountTest.java +++ b/src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/AzureAccountTest.java @@ -52,7 +52,7 @@ @SpringBootTest(classes = {AzureConfigurationClientTestConfiguration.class, Application.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @AutoConfigureMockMvc -@ActiveProfiles(profiles = "functional") +@ActiveProfiles(profiles = "integration") @AutoConfigureEmbeddedDatabase(type = AutoConfigureEmbeddedDatabase.DatabaseType.POSTGRES) @WithMockUser(username = "admin", authorities = {"APPROLE_api.request.admin"}) @SuppressWarnings({"PMD.TooManyMethods", "PMD.ExcessiveImports"}) diff --git a/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/BulkAccountCreationTest.java b/src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/BulkAccountCreationTest.java similarity index 99% rename from src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/BulkAccountCreationTest.java rename to src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/BulkAccountCreationTest.java index f9261563..c270dff8 100644 --- a/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/BulkAccountCreationTest.java +++ b/src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/BulkAccountCreationTest.java @@ -40,7 +40,7 @@ @SpringBootTest(classes = {AzureConfigurationClientTestConfiguration.class, Application.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @AutoConfigureMockMvc -@ActiveProfiles(profiles = "functional") +@ActiveProfiles(profiles = "integration") @AutoConfigureEmbeddedDatabase(type = AutoConfigureEmbeddedDatabase.DatabaseType.POSTGRES) @WithMockUser(username = "admin", authorities = {"APPROLE_api.request.admin"}) class BulkAccountCreationTest { diff --git a/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/CustomAccountRetrievalTest.java b/src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/CustomAccountRetrievalTest.java similarity index 99% rename from src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/CustomAccountRetrievalTest.java rename to src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/CustomAccountRetrievalTest.java index 1df6fcbd..0a56a663 100644 --- a/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/CustomAccountRetrievalTest.java +++ b/src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/CustomAccountRetrievalTest.java @@ -37,7 +37,7 @@ @SpringBootTest(classes = {AzureConfigurationClientTestConfiguration.class, Application.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @AutoConfigureMockMvc -@ActiveProfiles(profiles = "functional") +@ActiveProfiles(profiles = "integration") @AutoConfigureEmbeddedDatabase(type = AutoConfigureEmbeddedDatabase.DatabaseType.POSTGRES) @WithMockUser(username = "admin", authorities = {"APPROLE_api.request.admin"}) @SuppressWarnings({"PMD.TooManyMethods","PMD.JUnitTestsShouldIncludeAssert"}) diff --git a/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/GetWelcomeTest.java b/src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/GetWelcomeTest.java similarity index 97% rename from src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/GetWelcomeTest.java rename to src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/GetWelcomeTest.java index 241a0e53..913b966c 100644 --- a/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/GetWelcomeTest.java +++ b/src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/GetWelcomeTest.java @@ -19,7 +19,7 @@ @SpringBootTest(classes = {AzureConfigurationClientTestConfiguration.class, Application.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @AutoConfigureMockMvc -@ActiveProfiles(profiles = "functional") +@ActiveProfiles(profiles = "integration") @AutoConfigureEmbeddedDatabase(type = AutoConfigureEmbeddedDatabase.DatabaseType.POSTGRES) class GetWelcomeTest { diff --git a/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/InactiveAccountManagementTest.java b/src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/InactiveAccountManagementTest.java similarity index 99% rename from src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/InactiveAccountManagementTest.java rename to src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/InactiveAccountManagementTest.java index 8b582ad8..e2cbde4e 100644 --- a/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/InactiveAccountManagementTest.java +++ b/src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/InactiveAccountManagementTest.java @@ -21,7 +21,7 @@ @SpringBootTest(classes = {AzureConfigurationClientTestConfiguration.class, Application.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @AutoConfigureMockMvc -@ActiveProfiles(profiles = "functional") +@ActiveProfiles(profiles = "integration") @AutoConfigureEmbeddedDatabase(type = AutoConfigureEmbeddedDatabase.DatabaseType.POSTGRES) @WithMockUser(username = "admin", authorities = {"APPROLE_api.request.admin"}) @SuppressWarnings({"PMD.TooManyMethods", "PMD.JUnitTestsShouldIncludeAssert"}) diff --git a/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/MediaApplicationTest.java b/src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/MediaApplicationTest.java similarity index 99% rename from src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/MediaApplicationTest.java rename to src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/MediaApplicationTest.java index 5b14c398..a1c5cd79 100644 --- a/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/MediaApplicationTest.java +++ b/src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/MediaApplicationTest.java @@ -47,7 +47,7 @@ @SpringBootTest(classes = {AzureConfigurationClientTestConfiguration.class, Application.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @AutoConfigureMockMvc -@ActiveProfiles(profiles = "functional") +@ActiveProfiles(profiles = "integration") @AutoConfigureEmbeddedDatabase(type = AutoConfigureEmbeddedDatabase.DatabaseType.POSTGRES) @WithMockUser(username = "admin", authorities = {"APPROLE_api.request.admin"}) diff --git a/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/SensitivityTest.java b/src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/SensitivityTest.java similarity index 99% rename from src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/SensitivityTest.java rename to src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/SensitivityTest.java index aee8a2e9..b9c011c3 100644 --- a/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/SensitivityTest.java +++ b/src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/SensitivityTest.java @@ -37,7 +37,7 @@ @SpringBootTest(classes = {AzureConfigurationClientTestConfiguration.class, Application.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @AutoConfigureMockMvc -@ActiveProfiles(profiles = "functional") +@ActiveProfiles(profiles = "integration") @AutoConfigureEmbeddedDatabase(type = AutoConfigureEmbeddedDatabase.DatabaseType.POSTGRES) @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) @WithMockUser(username = "admin", authorities = { "APPROLE_api.request.admin" }) diff --git a/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/SystemAdminAccountTest.java b/src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/SystemAdminAccountTest.java similarity index 99% rename from src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/SystemAdminAccountTest.java rename to src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/SystemAdminAccountTest.java index fbf96694..8658c73d 100644 --- a/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/SystemAdminAccountTest.java +++ b/src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/SystemAdminAccountTest.java @@ -31,7 +31,7 @@ @SpringBootTest(classes = {AzureConfigurationClientTestConfiguration.class, Application.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @AutoConfigureMockMvc -@ActiveProfiles(profiles = "functional") +@ActiveProfiles(profiles = "integration") @AutoConfigureEmbeddedDatabase(type = AutoConfigureEmbeddedDatabase.DatabaseType.POSTGRES) @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) @WithMockUser(username = "admin", authorities = {"APPROLE_api.request.admin"}) diff --git a/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/SystemAdminB2CAccountTest.java b/src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/SystemAdminB2CAccountTest.java similarity index 99% rename from src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/SystemAdminB2CAccountTest.java rename to src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/SystemAdminB2CAccountTest.java index c2ad4bf0..21089fb0 100644 --- a/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/SystemAdminB2CAccountTest.java +++ b/src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/SystemAdminB2CAccountTest.java @@ -43,7 +43,7 @@ @SpringBootTest(classes = {AzureConfigurationClientTestConfiguration.class, Application.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @AutoConfigureMockMvc -@ActiveProfiles(profiles = "functional") +@ActiveProfiles(profiles = "integration") @AutoConfigureEmbeddedDatabase(type = AutoConfigureEmbeddedDatabase.DatabaseType.POSTGRES) @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) @WithMockUser(username = "admin", authorities = {"APPROLE_api.request.admin"}) diff --git a/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/TestingSupportApiTest.java b/src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/TestingSupportApiTest.java similarity index 99% rename from src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/TestingSupportApiTest.java rename to src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/TestingSupportApiTest.java index 801f9415..8cd7d306 100644 --- a/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/TestingSupportApiTest.java +++ b/src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/TestingSupportApiTest.java @@ -54,7 +54,7 @@ @SpringBootTest(classes = {AzureConfigurationClientTestConfiguration.class, Application.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @AutoConfigureMockMvc -@ActiveProfiles(profiles = "functional") +@ActiveProfiles(profiles = "integration") @AutoConfigureEmbeddedDatabase(type = AutoConfigureEmbeddedDatabase.DatabaseType.POSTGRES) @WithMockUser(username = "admin", authorities = {"APPROLE_api.request.admin"}) diff --git a/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/ViewTest.java b/src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/ViewTest.java similarity index 98% rename from src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/ViewTest.java rename to src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/ViewTest.java index 5b290f02..80e5dc14 100644 --- a/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/ViewTest.java +++ b/src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/ViewTest.java @@ -19,7 +19,7 @@ @SpringBootTest(classes = {AzureConfigurationClientTestConfiguration.class, Application.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @AutoConfigureMockMvc -@ActiveProfiles(profiles = "functional") +@ActiveProfiles(profiles = "integration") @AutoConfigureEmbeddedDatabase(type = AutoConfigureEmbeddedDatabase.DatabaseType.POSTGRES) @SuppressWarnings({"PMD.JUnitTestsShouldIncludeAssert"}) class ViewTest { diff --git a/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/model/CustomPageImpl.java b/src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/model/CustomPageImpl.java similarity index 100% rename from src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/model/CustomPageImpl.java rename to src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/model/CustomPageImpl.java diff --git a/src/functionalTest/resources/add-admin-users.sql b/src/integrationTest/resources/add-admin-users.sql similarity index 100% rename from src/functionalTest/resources/add-admin-users.sql rename to src/integrationTest/resources/add-admin-users.sql diff --git a/src/integrationTest/resources/application-integration.yaml b/src/integrationTest/resources/application-integration.yaml new file mode 100644 index 00000000..02b5b63f --- /dev/null +++ b/src/integrationTest/resources/application-integration.yaml @@ -0,0 +1,10 @@ +spring: + flyway: + enabled: true + jpa: + hibernate: + # Set to update for functional tests, so it creates the environment in H2 + ddl-auto: update + +admin: + max-system-admin: ${MAX_SYSTEM_ADMIN_ACCOUNTS:2} diff --git a/src/functionalTest/resources/csv/invalidCsv.txt b/src/integrationTest/resources/csv/invalidCsv.txt similarity index 100% rename from src/functionalTest/resources/csv/invalidCsv.txt rename to src/integrationTest/resources/csv/invalidCsv.txt diff --git a/src/functionalTest/resources/csv/invalidEmail.csv b/src/integrationTest/resources/csv/invalidEmail.csv similarity index 100% rename from src/functionalTest/resources/csv/invalidEmail.csv rename to src/integrationTest/resources/csv/invalidEmail.csv diff --git a/src/functionalTest/resources/csv/mediaEmailOnly.csv b/src/integrationTest/resources/csv/mediaEmailOnly.csv similarity index 100% rename from src/functionalTest/resources/csv/mediaEmailOnly.csv rename to src/integrationTest/resources/csv/mediaEmailOnly.csv diff --git a/src/functionalTest/resources/csv/valid.csv b/src/integrationTest/resources/csv/valid.csv similarity index 100% rename from src/functionalTest/resources/csv/valid.csv rename to src/integrationTest/resources/csv/valid.csv diff --git a/src/main/java/uk/gov/hmcts/reform/pip/account/management/config/AzureBlobConfiguration.java b/src/main/java/uk/gov/hmcts/reform/pip/account/management/config/AzureBlobConfiguration.java index 672939b2..c0e5c916 100644 --- a/src/main/java/uk/gov/hmcts/reform/pip/account/management/config/AzureBlobConfiguration.java +++ b/src/main/java/uk/gov/hmcts/reform/pip/account/management/config/AzureBlobConfiguration.java @@ -4,15 +4,24 @@ import com.azure.identity.DefaultAzureCredentialBuilder; import com.azure.storage.blob.BlobContainerClient; import com.azure.storage.blob.BlobContainerClientBuilder; +import com.azure.storage.common.StorageSharedKeyCredential; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; +import org.springframework.core.env.Environment; + +import java.util.Arrays; @Configuration -@Profile("!test & !functional") +@Profile("!test & !integration & !functional") public class AzureBlobConfiguration { private static final String BLOB_ENDPOINT = "https://%s.blob.core.windows.net/"; + private static final String DEV_PROFILE = "blobStorageDev"; + + @Autowired + private Environment env; @Value("${spring.cloud.azure.active-directory.profile.tenant-id}") private String tenantId; @@ -22,11 +31,23 @@ public class AzureBlobConfiguration { @Bean public BlobContainerClient blobContainerClient(AzureBlobConfigurationProperties - azureBlobConfigurationProperties) { - if (managedIdentityClientId.isEmpty()) { + configurationProperties) { + if (Arrays.stream(env.getActiveProfiles()) + .anyMatch(DEV_PROFILE::equals)) { + StorageSharedKeyCredential storageCredential = new StorageSharedKeyCredential( + configurationProperties.getStorageAccountName(), + configurationProperties.getStorageAccountKey() + ); + + return new BlobContainerClientBuilder() + .endpoint(configurationProperties.getStorageAccountUrl()) + .credential(storageCredential) + .containerName(configurationProperties.getContainerName()) + .buildClient(); + } else if (managedIdentityClientId.isEmpty()) { return new BlobContainerClientBuilder() - .connectionString(azureBlobConfigurationProperties.getConnectionString()) - .containerName(azureBlobConfigurationProperties.getContainerName()) + .connectionString(configurationProperties.getConnectionString()) + .containerName(configurationProperties.getContainerName()) .buildClient(); } @@ -36,9 +57,9 @@ public BlobContainerClient blobContainerClient(AzureBlobConfigurationProperties .build(); return new BlobContainerClientBuilder() - .endpoint(String.format(BLOB_ENDPOINT, azureBlobConfigurationProperties.getStorageAccountName())) + .endpoint(String.format(BLOB_ENDPOINT, configurationProperties.getStorageAccountName())) .credential(defaultCredential) - .containerName(azureBlobConfigurationProperties.getContainerName()) + .containerName(configurationProperties.getContainerName()) .buildClient(); } diff --git a/src/main/java/uk/gov/hmcts/reform/pip/account/management/config/AzureBlobConfigurationProperties.java b/src/main/java/uk/gov/hmcts/reform/pip/account/management/config/AzureBlobConfigurationProperties.java index 8d08bb7d..fd49d079 100644 --- a/src/main/java/uk/gov/hmcts/reform/pip/account/management/config/AzureBlobConfigurationProperties.java +++ b/src/main/java/uk/gov/hmcts/reform/pip/account/management/config/AzureBlobConfigurationProperties.java @@ -24,6 +24,16 @@ public class AzureBlobConfigurationProperties { */ private String storageAccountName; + /** + * The url of the storage account containing the blob (only required when running in the dev pod). + */ + private String storageAccountUrl; + + /** + * The access key of the storage account containing the blob (only required when running in the dev pod). + */ + private String storageAccountKey; + /** * The name of the container to connect to. */ diff --git a/src/main/java/uk/gov/hmcts/reform/pip/account/management/config/AzureConfigurationClient.java b/src/main/java/uk/gov/hmcts/reform/pip/account/management/config/AzureConfigurationClient.java index 5c67bbca..228a3f4e 100644 --- a/src/main/java/uk/gov/hmcts/reform/pip/account/management/config/AzureConfigurationClient.java +++ b/src/main/java/uk/gov/hmcts/reform/pip/account/management/config/AzureConfigurationClient.java @@ -10,7 +10,7 @@ /** * Configuration class used to initialise beans to talk to Azure graph. */ -@Profile("!test & !functional") +@Profile("!test & !integration & !functional") @Configuration public class AzureConfigurationClient { diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 4aaa2007..456a3b2f 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -95,6 +95,8 @@ azure: blob: connection-string: ${CONNECTION_STRING} storage-account-name: ${STORAGE_ACCOUNT_NAME} + storage-account-url: ${STORAGE_ACCOUNT_URL:} + storage-account-key: ${STORAGE_ACCOUNT_KEY:} container-name: files managed-identity: client-id: ${MANAGED_IDENTITY_CLIENT_ID:} diff --git a/src/test/java/uk/gov/hmcts/reform/pip/account/management/config/AzureBlobConfigurationTest.java b/src/test/java/uk/gov/hmcts/reform/pip/account/management/config/AzureBlobConfigurationTest.java index 94b0663e..edec30ee 100644 --- a/src/test/java/uk/gov/hmcts/reform/pip/account/management/config/AzureBlobConfigurationTest.java +++ b/src/test/java/uk/gov/hmcts/reform/pip/account/management/config/AzureBlobConfigurationTest.java @@ -8,6 +8,7 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.core.env.Environment; import org.springframework.test.util.ReflectionTestUtils; import static org.mockito.Mockito.when; @@ -17,11 +18,16 @@ */ @ExtendWith(MockitoExtension.class) class AzureBlobConfigurationTest { + private static final String[] BLOB_STORAGE_DEV_PROFILE = {"blobStorageDev"}; private static final String TENANT_ID = "123"; private static final String MANAGED_IDENTITY_CLIENT_ID = "456"; private static final String LOCAL_STORAGE_ACCOUNT_NAME = "testAccount"; private static final String LOCAL_BLOB_ENDPOINT = "http://127.0.0.1:10000/" + LOCAL_STORAGE_ACCOUNT_NAME; private static final String AZURE_STORAGE_ACCOUNT_NAME = "azureAccount"; + private static final String AZURE_STORAGE_ACCOUNT_KEY = "12345"; + private static final String AZURE_STORAGE_ACCOUNT_URL = "https://" + + AZURE_STORAGE_ACCOUNT_NAME + + ".blob.core.windows.net"; private static final String AZURE_BLOB_ENDPOINT = "https://" + AZURE_STORAGE_ACCOUNT_NAME + ".blob.core.windows.net"; @@ -34,18 +40,45 @@ class AzureBlobConfigurationTest { @Mock private AzureBlobConfigurationProperties blobConfigProperties; + @Mock + private Environment env; + @InjectMocks private AzureBlobConfiguration azureBlobConfiguration; @BeforeEach void setup() { + when(env.getActiveProfiles()).thenReturn(new String[1]); ReflectionTestUtils.setField(azureBlobConfiguration, "tenantId", TENANT_ID); when(blobConfigProperties.getContainerName()).thenReturn(CONTAINER_NAME); } + @Test + void testCreationOfAzureBlobClientWithDevProfile() { + when(env.getActiveProfiles()).thenReturn(BLOB_STORAGE_DEV_PROFILE); + when(blobConfigProperties.getStorageAccountName()).thenReturn(AZURE_STORAGE_ACCOUNT_NAME); + when(blobConfigProperties.getStorageAccountUrl()).thenReturn(AZURE_STORAGE_ACCOUNT_URL); + when(blobConfigProperties.getStorageAccountKey()).thenReturn(AZURE_STORAGE_ACCOUNT_KEY); + when(blobConfigProperties.getContainerName()).thenReturn(CONTAINER_NAME); + + BlobContainerClient blobContainerClient = azureBlobConfiguration.blobContainerClient( + blobConfigProperties + ); + + SoftAssertions softly = new SoftAssertions(); + + softly.assertThat(blobContainerClient).isNotNull(); + softly.assertThat(blobContainerClient.getAccountUrl()).isEqualTo(AZURE_STORAGE_ACCOUNT_URL); + softly.assertThat(blobContainerClient.getAccountName()).isEqualTo(AZURE_STORAGE_ACCOUNT_NAME); + softly.assertThat(blobContainerClient.getBlobContainerName()).isEqualTo(CONTAINER_NAME); + + softly.assertAll(); + } + @Test void testCreationOfAzureBlobClientWithManagedIdentity() { ReflectionTestUtils.setField(azureBlobConfiguration, "managedIdentityClientId", MANAGED_IDENTITY_CLIENT_ID); + when(env.getActiveProfiles()).thenReturn(new String[1]); when(blobConfigProperties.getStorageAccountName()).thenReturn(AZURE_STORAGE_ACCOUNT_NAME); BlobContainerClient blobContainerClient = azureBlobConfiguration.blobContainerClient(blobConfigProperties); @@ -63,6 +96,7 @@ void testCreationOfAzureBlobClientWithManagedIdentity() { @Test void testCreationOfAzureBlobClientWithoutManagedIdentity() { ReflectionTestUtils.setField(azureBlobConfiguration, "managedIdentityClientId", ""); + when(env.getActiveProfiles()).thenReturn(new String[1]); when(blobConfigProperties.getConnectionString()).thenReturn(CONNECTION_STRING); BlobContainerClient blobContainerClient = azureBlobConfiguration.blobContainerClient(blobConfigProperties);