use of org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get in project CzechIdMng by bcvsolutions.
the class IdmIdentityControllerRestTest method testProfile.
@Test
public void testProfile() throws UnsupportedEncodingException, IOException, Exception {
IdmIdentityDto owner = getHelper().createIdentity((GuardedString) null);
//
// profile image
getMockMvc().perform(MockMvcRequestBuilders.get(getDetailUrl(owner.getId()) + "/profile").with(authentication(getAdminAuthentication()))).andExpect(status().isNoContent());
getMockMvc().perform(MockMvcRequestBuilders.get(getDetailUrl(owner.getId()) + "/profile/image").with(authentication(getAdminAuthentication()))).andExpect(status().isNoContent());
//
String fileName = "file.png";
String content = "some image";
String response = getMockMvc().perform(MockMvcRequestBuilders.multipart(getDetailUrl(owner.getId()) + "/profile/image").file(new MockMultipartFile("data", fileName, "image/png", IOUtils.toByteArray(IOUtils.toInputStream(content)))).param("fileName", fileName).with(authentication(getAdminAuthentication()))).andExpect(status().isOk()).andExpect(content().contentType(TestHelper.HAL_CONTENT_TYPE)).andReturn().getResponse().getContentAsString();
IdmProfileDto createdProfile = (IdmProfileDto) getMapper().readValue(response, IdmProfileDto.class);
//
Assert.assertNotNull(createdProfile);
Assert.assertNotNull(createdProfile.getId());
Assert.assertNotNull(createdProfile.getImage());
IdmAttachmentDto image = attachmentManager.get(createdProfile.getImage());
Assert.assertEquals(content.length(), image.getFilesize().intValue());
Assert.assertEquals(createdProfile.getId(), image.getOwnerId());
Assert.assertEquals(attachmentManager.getOwnerType(createdProfile), image.getOwnerType());
Assert.assertEquals(fileName, image.getName());
InputStream is = attachmentManager.getAttachmentData(image.getId());
try {
Assert.assertEquals(content, IOUtils.toString(is));
} finally {
IOUtils.closeQuietly(is);
}
//
// get profile
response = getMockMvc().perform(MockMvcRequestBuilders.get(getDetailUrl(owner.getId()) + "/profile").with(authentication(getAdminAuthentication()))).andExpect(status().isOk()).andExpect(content().contentType(TestHelper.HAL_CONTENT_TYPE)).andReturn().getResponse().getContentAsString();
createdProfile = (IdmProfileDto) getMapper().readValue(response, IdmProfileDto.class);
Assert.assertEquals(image.getId(), createdProfile.getImage());
//
// get profile image
response = getMockMvc().perform(MockMvcRequestBuilders.get(getDetailUrl(owner.getId()) + "/profile/image").with(authentication(getAdminAuthentication()))).andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
Assert.assertEquals(content, response);
//
// get profile permissions
response = getMockMvc().perform(get(getDetailUrl(owner.getId()) + "/profile/permissions").with(authentication(getAdminAuthentication())).contentType(TestHelper.HAL_CONTENT_TYPE)).andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
//
// convert embedded object to list of strings
List<String> permissions = getMapper().readValue(response, new TypeReference<List<String>>() {
});
Assert.assertNotNull(permissions);
Assert.assertFalse(permissions.isEmpty());
Assert.assertTrue(permissions.stream().anyMatch(p -> p.equals(IdmBasePermission.ADMIN.getName())));
//
// delete image
response = getMockMvc().perform(MockMvcRequestBuilders.delete(getDetailUrl(owner.getId()) + "/profile/image").with(authentication(getAdminAuthentication()))).andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
createdProfile = (IdmProfileDto) getMapper().readValue(response, IdmProfileDto.class);
Assert.assertNull(createdProfile.getImage());
//
// get profile without image
getMockMvc().perform(MockMvcRequestBuilders.get(getDetailUrl(owner.getId()) + "/profile/image").with(authentication(getAdminAuthentication()))).andExpect(status().isNoContent());
//
identityService.delete(owner);
//
// profile is deleted
getMockMvc().perform(MockMvcRequestBuilders.get(getDetailUrl(owner.getId()) + "/profile/image").with(authentication(getAdminAuthentication()))).andExpect(status().isNoContent());
// attachment is deleted
Assert.assertNull(attachmentManager.get(image));
Assert.assertNull(profileService.get(createdProfile));
}
Aggregations