1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
//@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
@ContextConfiguration(initializers = {ApplicationControllerTest.Initializer.class})
public class ApplicationControllerTest extends InserDataForIntegrationTests {
@Autowired
private MockMvc mockMvc;
@Autowired
private AuthenticationManager authManager;
@Autowired
private ObjectMapper objectMapper;
@Autowired
private WebApplicationContext context;
@ClassRule
public static PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer("postgres")
.withDatabaseName("staffaccess")
.withUsername("sa")
.withPassword("sa");
static class Initializer
implements ApplicationContextInitializer<ConfigurableApplicationContext> {
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
TestPropertyValues.of(
"spring.datasource.url=" + postgreSQLContainer.getJdbcUrl(),
"spring.datasource.username=" + postgreSQLContainer.getUsername(),
"spring.datasource.password=" + postgreSQLContainer.getPassword()
).applyTo(configurableApplicationContext.getEnvironment());
}
}
@BeforeAll
public static void beforeAll() {
postgreSQLContainer.start();
}
@AfterAll
public static void afterAll() {
postgreSQLContainer.stop();
}
@Before
@Transactional
public void setup() {
mockMvc = MockMvcBuilders
.webAppContextSetup(context)
.apply(springSecurity())
.build();
UsernamePasswordAuthenticationToken authReq = new UsernamePasswordAuthenticationToken("username", "password");
Authentication auth = authManager.authenticate(authReq);
SecurityContextHolder.getContext().setAuthentication(auth);
insertData();
}
@Test
public void searchApplicationTest() throws Exception {
this.mockMvc
.perform(post("/api/searchApplication")
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.param("appowner", "true")
.content(objectMapper.writeValueAsString(Arrays.asList("appli")))
)
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(1)))
.andExpect(jsonPath("$[0].id").value(1))
.andExpect(jsonPath("$[0].name").value("application1"))
.andReturn();
}
@Test
public void createApplicationTest() throws Exception {
int appCount1 = this.applicationRepository.findAllCount();
int roleCount1 = this.roleRepository.findAllCount();
this.mockMvc
.perform(post("/api/createApplication")
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(new ApplicationDto("application2", Arrays.asList(new RoleDto("role2")), new EnvironmentDto(1, "DEV"), new EmployeeDto(null, null, null, "username"))))
)
.andExpect(status().isOk())
.andExpect(jsonPath("$.name").value("application2"))
.andExpect(jsonPath("$.directorOwner.account").value("username"))
.andExpect(jsonPath("$.directorOwner.account").value("username"))
.andReturn();
int appCount2 = this.applicationRepository.findAllCount();
int roleCount2 = this.roleRepository.findAllCount();
Assert.assertEquals(appCount2, appCount1 + 1);
Assert.assertEquals(roleCount2, roleCount1 + 1);
}
@Test
public void getAllEnvironmentsTest() throws Exception {
this.mockMvc
.perform(get("/api/environments")
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
)
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(1)))
.andExpect(jsonPath("$[0].name").value("DEV"))
.andReturn();
}
@Test
public void searchAppNodesTest() throws Exception {
this.mockMvc
.perform(get("/api/searchAppNodes")
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.param("search", "John Doe")
)
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(1)))
.andExpect(jsonPath("$[0].name").value("DEV"))
.andReturn();
}
} |
Partager