Skip to content

Commit

Permalink
Merge pull request #1672 from hmcts/AM-2937_Remove_IDAM_Dependency_In…
Browse files Browse the repository at this point in the history
…t_Tests

Am 2937 Preferable remove idam dependency int tests
  • Loading branch information
mikebrownccd authored Dec 11, 2023
2 parents a7c5eff + 6191ea1 commit 1a489fa
Show file tree
Hide file tree
Showing 9 changed files with 262 additions and 54 deletions.
21 changes: 21 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,20 @@ sourceSets {
}
}

idea {
module {
// config to allow Intelij to mark test source and resource files correctly to help linting tools
testSources.from(java.sourceSets.functionalTest.java.srcDirs)
testSources.from(java.sourceSets.integrationTest.java.srcDirs)
testSources.from(java.sourceSets.smokeTest.java.srcDirs)
testSources.from(java.sourceSets.contractTest.java.srcDirs)
testResources.from(java.sourceSets.functionalTest.resources.srcDirs)
testResources.from(java.sourceSets.integrationTest.resources.srcDirs)
testResources.from(java.sourceSets.smokeTest.resources.srcDirs)
testResources.from(java.sourceSets.contractTest.resources.srcDirs)
}
}

tasks.withType(JavaCompile) {
options.compilerArgs << "-Xlint:unchecked" << "-Werror"
}
Expand Down Expand Up @@ -298,6 +312,13 @@ repositories {
}
}

tasks.named('integration') {
useJUnitPlatform()

testLogging {
exceptionFormat = 'full'
}
}

dependencies {
implementation(group: 'org.springframework.boot', name:'spring-boot-starter-web', version: versions.springBoot) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package uk.gov.hmcts.reform.orgrolemapping.config;

import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
Expand Down Expand Up @@ -33,7 +33,7 @@ public class SwaggerPublisher extends BaseTest {
@Inject
private WebApplicationContext wac;

@Before
@BeforeEach
public void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,34 @@

import com.azure.messaging.servicebus.ServiceBusSenderClient;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.github.tomakehurst.wiremock.WireMockServer;
import com.microsoft.azure.servicebus.SubscriptionClient;
import com.opentable.db.postgres.embedded.EmbeddedPostgres;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.jdbc.datasource.SingleConnectionDataSource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.lang.NonNull;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.security.oauth2.client.registration.ReactiveClientRegistrationRepository;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.support.TestPropertySourceUtils;
import uk.gov.hmcts.reform.orgrolemapping.controller.utils.WiremockFixtures;


import javax.annotation.PreDestroy;
Expand All @@ -26,9 +40,17 @@
import java.sql.SQLException;
import java.util.Properties;

@RunWith(SpringRunner.class)
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;

@ContextConfiguration(initializers = {BaseTest.WireMockServerInitializer.class})
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("itest")
@EnableConfigurationProperties
public abstract class BaseTest {

public static final WireMockServer WIRE_MOCK_SERVER = new WireMockServer(options().dynamicPort());

protected static final ObjectMapper mapper = new ObjectMapper();

@MockBean
Expand All @@ -51,10 +73,20 @@ public abstract class BaseTest {
@MockBean(name = "getSubscriptionClient1")
private SubscriptionClient getSubscriptionClient1;

@BeforeClass
public static void init() {
@MockBean(name = "clientRegistrationRepository")
private ClientRegistrationRepository getClientRegistrationRepository;

@MockBean(name = "reactiveClientRegistrationRepository")
private ReactiveClientRegistrationRepository getReactiveClientRegistrationRepository;

static {
if (!WIRE_MOCK_SERVER.isRunning()) {
WIRE_MOCK_SERVER.start();
}

mapper.registerModule(new JavaTimeModule());
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
// Force re-initialisation of base types for each test suite
}

Expand Down Expand Up @@ -88,4 +120,31 @@ public void contextDestroyed() throws SQLException {
}
}
}

public static class WireMockServerInitializer
implements ApplicationContextInitializer<ConfigurableApplicationContext> {

private final WiremockFixtures wiremockFixtures = new WiremockFixtures();

@Override
public void initialize(@NonNull ConfigurableApplicationContext applicationContext) {

TestPropertySourceUtils.addInlinedPropertiesToEnvironment(
applicationContext,
"wiremock.server.port=" + WIRE_MOCK_SERVER.port()
);

try {
wiremockFixtures.stubIdamConfig();
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}

applicationContext.addApplicationListener((ApplicationListener<ContextClosedEvent>) event -> {
if (WIRE_MOCK_SERVER.isRunning()) {
WIRE_MOCK_SERVER.shutdown();
}
});
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package uk.gov.hmcts.reform.orgrolemapping.controller;

import org.jetbrains.annotations.NotNull;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -30,6 +30,7 @@
import uk.gov.hmcts.reform.orgrolemapping.apihelper.Constants;
import uk.gov.hmcts.reform.orgrolemapping.controller.advice.exception.UnauthorizedServiceException;
import uk.gov.hmcts.reform.orgrolemapping.controller.utils.MockUtils;
import uk.gov.hmcts.reform.orgrolemapping.controller.utils.WiremockFixtures;
import uk.gov.hmcts.reform.orgrolemapping.data.RefreshJobEntity;
import uk.gov.hmcts.reform.orgrolemapping.domain.model.Appointment;
import uk.gov.hmcts.reform.orgrolemapping.domain.model.CaseWorkerProfilesResponse;
Expand Down Expand Up @@ -63,10 +64,10 @@
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
Expand All @@ -90,6 +91,8 @@ public class RefreshControllerIntegrationTest extends BaseTest {

private static final Logger logger = LoggerFactory.getLogger(RefreshControllerIntegrationTest.class);

private final WiremockFixtures wiremockFixtures = new WiremockFixtures();

private static final String REFRESH_JOB_RECORDS_QUERY = "SELECT job_id, status, user_ids, linked_job_id,"
+ " comments, log FROM refresh_jobs where job_id=?";
private static final String AUTHORISED_SERVICE = "am_role_assignment_refresh_batch";
Expand Down Expand Up @@ -140,18 +143,18 @@ public class RefreshControllerIntegrationTest extends BaseTest {
StandardCharsets.UTF_8
);

@Before
@BeforeEach
public void setUp() throws Exception {
template = new JdbcTemplate(ds);
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
MockitoAnnotations.openMocks(this);
doReturn(authentication).when(securityContext).getAuthentication();
SecurityContextHolder.setContext(securityContext);
doReturn(true).when(featureConditionEvaluation).preHandle(any(),any(),any());
MockUtils.setSecurityAuthorities(authentication, MockUtils.ROLE_CASEWORKER);
wiremockFixtures.resetRequests();
}

@Ignore("Intermittent AM-2919")
@Disabled("Intermittent AM-2919")
@Test
@Sql(executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD, scripts = {"classpath:sql/insert_refresh_jobs.sql"})
public void shouldProcessRefreshRoleAssignmentsWithJobIdToComplete() throws Exception {
Expand Down Expand Up @@ -180,7 +183,7 @@ public void shouldProcessRefreshRoleAssignmentsWithJobIdToComplete() throws Exce
assertNotNull(refreshJob.getLog());
}

@Ignore("Intermittent AM-2919")
@Disabled("Intermittent AM-2919")
@Test
@Sql(executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD, scripts = {"classpath:sql/insert_refresh_jobs.sql"})
public void shouldProcessRefreshRoleAssignmentsWithJobIdToAborted() throws Exception {
Expand All @@ -207,7 +210,7 @@ public void shouldProcessRefreshRoleAssignmentsWithJobIdToAborted() throws Excep
assertThat(refreshJob.getLog(),containsString(String.join(",", refreshJob.getUserIds())));
}

@Ignore("Intermittent AM-2919")
@Disabled("Intermittent AM-2919")
@Test
@Sql(executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD, scripts = {"classpath:sql/insert_refresh_jobs.sql"})
public void shouldProcessRefreshRoleAssignmentsWithJobIdToAborted_status422() throws Exception {
Expand All @@ -234,7 +237,7 @@ public void shouldProcessRefreshRoleAssignmentsWithJobIdToAborted_status422() th
assertThat(refreshJob.getLog(),containsString(String.join(",", refreshJob.getUserIds())));
}

@Ignore("Intermittent AM-2919")
@Disabled("Intermittent AM-2919")
@Test
@Sql(executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD, scripts = {"classpath:sql/insert_refresh_jobs.sql"})
public void shouldProcessRefreshRoleAssignmentsWithJobIdToPartialComplete() throws Exception {
Expand All @@ -261,7 +264,7 @@ public void shouldProcessRefreshRoleAssignmentsWithJobIdToPartialComplete() thro
assertThat(refreshJob.getLog(), containsString(String.join(",", refreshJob.getUserIds())));
}

@Ignore("Intermittent AM-2919")
@Disabled("Intermittent AM-2919")
@Test
@Sql(executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD, scripts = {"classpath:sql/insert_refresh_jobs.sql"})
public void shouldProcessRefreshRoleAssignmentsWithJobIdToPartialComplete_status422() throws Exception {
Expand All @@ -288,7 +291,7 @@ public void shouldProcessRefreshRoleAssignmentsWithJobIdToPartialComplete_status
assertThat(refreshJob.getLog(), containsString(String.join(",", refreshJob.getUserIds())));
}

@Ignore("Intermittent AM-2919")
@Disabled("Intermittent AM-2919")
@Test
@Sql(executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD, scripts = {"classpath:sql/insert_refresh_jobs.sql"})
public void shouldProcessRefreshRoleAssignmentsWithFailedUsersToComplete() throws Exception {
Expand Down Expand Up @@ -373,7 +376,7 @@ public void shouldFailProcessRefreshRoleAssignmentsWithOutJobID() throws Excepti
.andReturn();
}

@Ignore("Intermittent AM-2919")
@Disabled("Intermittent AM-2919")
@Test
public void shouldFailProcessRefreshRoleAssignmentsWithInvalidServiceToken() throws Exception {
logger.info("Refresh request rejected with invalid service token");
Expand All @@ -391,7 +394,7 @@ public void shouldFailProcessRefreshRoleAssignmentsWithInvalidServiceToken() thr
assertThat(result.getResolvedException().getMessage(), equalTo(UNAUTHORIZED_SERVICE));
}

@Ignore("Intermittent AM-2919")
@Disabled("Intermittent AM-2919")
@Test
@Sql(executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD, scripts = {"classpath:sql/insert_refresh_jobs.sql"})
public void shouldProcessRefreshRoleAssignmentsWithJobIdToComplete_retryFail() throws Exception {
Expand All @@ -416,7 +419,7 @@ public void shouldProcessRefreshRoleAssignmentsWithJobIdToComplete_retryFail() t
assertEquals("NEW", refreshJob.getStatus());// failed process should change the status to IN-PROGRESS
}

@Ignore("Intermittent AM-2919")
@Disabled("Intermittent AM-2919")
@Test
@Sql(executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD, scripts = {"classpath:sql/insert_refresh_jobs.sql"})
public void shouldProcessRefreshRoleAssignmentsWithJobIdToComplete_CRDRetry() throws Exception {
Expand Down
Loading

0 comments on commit 1a489fa

Please sign in to comment.