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);
}
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();
}
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);
}
}
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));
}
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));
}
Aggregations