Search in sources :

Example 61 with MvcResult

use of org.springframework.test.web.servlet.MvcResult in project CzechIdMng by bcvsolutions.

the class ExtendExpirationFilterTest method testSuccessfulTokenExtension.

@Test
public void testSuccessfulTokenExtension() throws Exception {
    IdmJwtAuthenticationDto authDto = AuthenticationTestUtils.getAuthDto(identityService.getByUsername(TEST_ADMIN_USERNAME), Lists.newArrayList(IdmAuthorityUtils.getAdminAuthority()));
    String token = getAuthToken(authDto);
    sleep();
    MvcResult result = getMockMvc().perform(get(AuthenticationTestUtils.getSelfPath(TEST_ADMIN_USERNAME)).header(JwtAuthenticationMapper.AUTHENTICATION_TOKEN_NAME, token).contentType(HAL_CONTENT_TYPE)).andExpect(status().isOk()).andExpect(content().contentType(HAL_CONTENT_TYPE)).andExpect(jsonPath("$.username", equalTo(TEST_ADMIN_USERNAME))).andReturn();
    IdmJwtAuthenticationDto extendedDto = getIdmJwtDto(result);
    checkSuccessfulTokenExtension(authDto, extendedDto);
}
Also used : IdmJwtAuthenticationDto(eu.bcvsolutions.idm.core.security.api.dto.IdmJwtAuthenticationDto) MvcResult(org.springframework.test.web.servlet.MvcResult) Test(org.junit.Test) AbstractRestTest(eu.bcvsolutions.idm.test.api.AbstractRestTest)

Example 62 with MvcResult

use of org.springframework.test.web.servlet.MvcResult in project CzechIdMng by bcvsolutions.

the class DefaultRecaptchaRestTest method getMockHttpServletResponse.

private MockHttpServletResponse getMockHttpServletResponse(String jsonContent) throws Exception {
    MockMvc mvc = getMockMvc();
    ResultActions actions = mvc.perform(MockMvcRequestBuilders.post(BaseDtoController.BASE_PATH + RecaptchaController.URL_PATH).with(authentication(getAuthentication())).contentType(MediaTypes.HAL_JSON).content(jsonContent));
    MvcResult res = actions.andReturn();
    return res.getResponse();
}
Also used : ResultActions(org.springframework.test.web.servlet.ResultActions) MvcResult(org.springframework.test.web.servlet.MvcResult) MockMvc(org.springframework.test.web.servlet.MockMvc)

Example 63 with MvcResult

use of org.springframework.test.web.servlet.MvcResult in project CzechIdMng by bcvsolutions.

the class AbstractSwaggerTest method convertSwagger.

/**
 * Converts module's swagger endpoint to json
 *
 * @see ModuleDescriptor#getId()
 * @param moduleId
 * @throws Exception
 */
public void convertSwagger(String moduleId) throws Exception {
    MvcResult mvcResult = getMockMvc().perform(get(String.format("%s?group=%s", path, moduleId)).accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andReturn();
    MockHttpServletResponse response = mvcResult.getResponse();
    String swaggerJson = response.getContentAsString();
    Files.createDirectories(Paths.get(outputDir));
    try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(outputDir, filename), StandardCharsets.UTF_8)) {
        writer.write(swaggerJson);
    }
}
Also used : MvcResult(org.springframework.test.web.servlet.MvcResult) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) BufferedWriter(java.io.BufferedWriter)

Example 64 with MvcResult

use of org.springframework.test.web.servlet.MvcResult in project Saturn by vipshop.

the class AlarmRestApiControllerTest method testRaiseAlarmFailWithUnExpectedException.

@Test
public void testRaiseAlarmFailWithUnExpectedException() throws Exception {
    String customErrMsg = "some exception throws";
    willThrow(new RuntimeException(customErrMsg)).given(restApiService).raiseAlarm(any(String.class), any(String.class), any(String.class), any(Integer.class), any(AlarmInfo.class));
    AlarmEntity alarmEntity = new AlarmEntity("job", "exec", "name", "title", "CRITICAL");
    MvcResult result = mvc.perform(post("/rest/v1/mydomain/alarms/raise").contentType(MediaType.APPLICATION_JSON).content(alarmEntity.toJSON())).andExpect(status().isInternalServerError()).andReturn();
    assertEquals("error message not equal", customErrMsg, fetchErrorMessage(result));
}
Also used : AlarmInfo(com.vip.saturn.job.integrate.entity.AlarmInfo) MvcResult(org.springframework.test.web.servlet.MvcResult) Test(org.junit.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest) AbstractSaturnConsoleTest(com.vip.saturn.job.console.AbstractSaturnConsoleTest)

Example 65 with MvcResult

use of org.springframework.test.web.servlet.MvcResult in project Saturn by vipshop.

the class AlarmRestApiControllerTest method testRaiseAlarmFailAsMissingMandatoryField.

@Test
public void testRaiseAlarmFailAsMissingMandatoryField() throws Exception {
    // missing jobName
    AlarmEntity alarmEntity = new AlarmEntity(null, "exec", "name", "title", "CRITICAL");
    MvcResult result = mvc.perform(post("/rest/v1/mydomain/alarms/raise").contentType(MediaType.APPLICATION_JSON).content(alarmEntity.toJSON())).andExpect(status().isBadRequest()).andReturn();
    assertEquals("error message not equal", "Invalid request. Missing parameter: {jobName}", fetchErrorMessage(result));
    // missing executorname
    alarmEntity = new AlarmEntity("job1", null, "name", "title", "CRITICAL");
    result = mvc.perform(post("/rest/v1/mydomain/alarms/raise").contentType(MediaType.APPLICATION_JSON).content(alarmEntity.toJSON())).andExpect(status().isBadRequest()).andReturn();
    assertEquals("error message not equal", "Invalid request. Missing parameter: {executorName}", fetchErrorMessage(result));
    // missing name
    alarmEntity = new AlarmEntity("job1", "exec", null, "title", "CRITICAL");
    result = mvc.perform(post("/rest/v1/mydomain/alarms/raise").contentType(MediaType.APPLICATION_JSON).content(alarmEntity.toJSON())).andExpect(status().isBadRequest()).andReturn();
    assertEquals("error message not equal", "Invalid request. Missing parameter: {name}", fetchErrorMessage(result));
    // missing title
    alarmEntity = new AlarmEntity("job1", "exec", "name", null, "CRITICAL");
    result = mvc.perform(post("/rest/v1/mydomain/alarms/raise").contentType(MediaType.APPLICATION_JSON).content(alarmEntity.toJSON())).andExpect(status().isBadRequest()).andReturn();
    assertEquals("error message not equal", "Invalid request. Missing parameter: {title}", fetchErrorMessage(result));
    // missing level
    alarmEntity = new AlarmEntity("job1", "exec", "name", "title", null);
    result = mvc.perform(post("/rest/v1/mydomain/alarms/raise").contentType(MediaType.APPLICATION_JSON).content(alarmEntity.toJSON())).andExpect(status().isBadRequest()).andReturn();
    assertEquals("error message not equal", "Invalid request. Missing parameter: {level}", fetchErrorMessage(result));
}
Also used : MvcResult(org.springframework.test.web.servlet.MvcResult) Test(org.junit.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest) AbstractSaturnConsoleTest(com.vip.saturn.job.console.AbstractSaturnConsoleTest)

Aggregations

MvcResult (org.springframework.test.web.servlet.MvcResult)536 Test (org.junit.Test)300 Test (org.junit.jupiter.api.Test)143 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)132 ResultMatcher (org.springframework.test.web.servlet.ResultMatcher)69 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)38 MockHttpSession (org.springframework.mock.web.MockHttpSession)35 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)34 WebMvcTest (org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)31 DatabaseSetup (com.github.springtestdbunit.annotation.DatabaseSetup)26 ExpectedDatabase (com.github.springtestdbunit.annotation.ExpectedDatabase)26 RequestBuilder (org.springframework.test.web.servlet.RequestBuilder)24 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)22 Map (java.util.Map)22 ResultActions (org.springframework.test.web.servlet.ResultActions)19 Cookie (javax.servlet.http.Cookie)18 MockMvc (org.springframework.test.web.servlet.MockMvc)17 HttpSession (jakarta.servlet.http.HttpSession)16 JsonNode (com.fasterxml.jackson.databind.JsonNode)15 AbstractSaturnConsoleTest (com.vip.saturn.job.console.AbstractSaturnConsoleTest)15