Summary Table
Categories |
Total Count |
PII |
0 |
URL |
0 |
DNS |
1 |
EKL |
0 |
IP |
0 |
PORT |
0 |
VsID |
0 |
CF |
0 |
AI |
0 |
VPD |
0 |
PL |
0 |
Other |
0 |
File Content
package gov.va.med.ars.controller;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.mockito.Matchers.*; // any, verify, content,
//import static org.mockito.Matchers.any;
//import static org.mockito.Mockito.verify;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
import org.apache.http.protocol.HTTP;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SerializationFeature;
import gov.va.med.ars.exceptions.GenericException;
import gov.va.med.ars.file.FileOperations;
import gov.va.med.ars.filter.CORSFilter;
import gov.va.med.ars.model.request.AuditLogger;
import gov.va.med.ars.model.request.ExportAsPDF;
import gov.va.med.ars.service.IAuditLoggerService;
@RunWith(MockitoJUnitRunner.class)
public class FileOperationsControllerTest {
private String thisSrc = "src/test/java/gov/va/med/ars/controller/FileOperationsControllerTest.java"; // our own source should exist??
// Status expect <415>, HTTP Status 415 - Unsupported Media Type
//
private String[] testImages = {
"src/test/resources/images/bmp-400x400.bmp",
"src/test/resources/images/bmp-1420x1420.bmp",
"src/test/resources/images/gif-400x400.gif",
"src/test/resources/images/gif-1420x1420.gif",
"src/test/resources/images/jpg-400x400.jpg",
"src/test/resources/images/jpg-1420x1420.jpg",
"src/test/resources/images/png-400x400.png",
"src/test/resources/images/png-1420x1420.png",
"src/test/resources/images/tif-400x400.tif",
"src/test/resources/images/tif-1420x1420.tif",
};
private String vhaName = "VHANAME";
private MockMvc mockMvc;
@Mock
private IAuditLoggerService auditLoggerService;
@InjectMocks
private FileOperations fileOperationsController;
@Before
public void init() throws Exception {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders
.standaloneSetup(fileOperationsController)
.addFilters(new CORSFilter())
.build();
}
// viewAttachment
// exportAttachment
// exportAsPDF
@Test
public void testSuccessController_file_view_images() throws Exception {
// The inaptly named AuditLogger is the request for an attachment. But it also contains identification for auditing
// List<String> imgList = Arrays.asList(images);
// Stream<String> imgStream = imgList.stream();
// imgStream.forEach( (img) -> {
System.out.println("FileOperationsControllerTest:testSuccessController_file_view_images");
for( String img : testImages ) {
AuditLogger request = new AuditLogger(vhaName, "86753303.001", false); // doesn't matter
when(auditLoggerService.getPathForTheAttachment(any(AuditLogger.class))).thenReturn(img); // always return our image name
System.out.println("getPathForTheAttachment returns " + img);
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
String jsonRequest = ow.writeValueAsString(request);
System.out.println("jsonRequest=" + jsonRequest);
mockMvc.perform(
post("/api/v1/file").contentType(MediaType.APPLICATION_JSON).content(jsonRequest))
// .andDo(print())
.andExpect(status().isOk())
;
}
}
@Test
public void testSuccessController_file_view() throws Exception {
// The inaptly named AuditLogger is the request for an attachment. But it also contains identification for auditing
AuditLogger request = new AuditLogger(vhaName, "86753303.001", false);
when(auditLoggerService.getPathForTheAttachment(any(AuditLogger.class))).thenReturn(thisSrc);
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
String jsonRequest = ow.writeValueAsString(request);
mockMvc.perform(
post("/api/v1/file").contentType(MediaType.APPLICATION_JSON).content(jsonRequest))
// .andDo(print())
.andExpect(status().isOk())
;
}
@Test
public void testSuccessController_file_Download() throws Exception, AssertionError {
Boolean isDownload = true;
AuditLogger request = new AuditLogger(vhaName, "86753303.001", isDownload); // autoboxing inserts new Boolean(true)
when(auditLoggerService.getPathForTheAttachment(any(AuditLogger.class))).thenReturn(thisSrc);
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
String jsonRequest = ow.writeValueAsString(request);
mockMvc.perform(
post("/api/v1/file").contentType(MediaType.APPLICATION_JSON).content(jsonRequest))
// .andDo(print())
.andExpect(status().isOk())
;
}
// @Test(expected = Exception.class)
// @Test(expected = AssertionError.class)
@Test(expected = org.springframework.web.util.NestedServletException.class) // just so
public void testExceptionFileCannotBeAccessedController_view() throws Exception, AssertionError {
Boolean isDownload = false;
AuditLogger request=new AuditLogger(vhaName, "86753303.001", isDownload);
when(auditLoggerService.getPathForTheAttachment(request)).thenReturn(null);
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
String jsonRequest = ow.writeValueAsString(request);
mockMvc.perform(
post("/api/v1/file").contentType(MediaType.APPLICATION_JSON).content(jsonRequest))
// .andDo(print())
.andExpect(status().isOk())
;
}
// @Test(expected = Exception.class)
// @Test(expected = AssertionError.class)
@Test(expected = org.springframework.web.util.NestedServletException.class)
public void testExceptionFileCannotBeAccessedController_Download() throws Exception, AssertionError {
Boolean isDownload = false;
AuditLogger request=new AuditLogger(vhaName, "86753303.001", isDownload);
when(auditLoggerService.getPathForTheAttachment(request)).thenReturn(null);
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
String jsonRequest = ow.writeValueAsString(request);
mockMvc.perform(
post("/api/v1/file").contentType(MediaType.APPLICATION_JSON).content(jsonRequest))
// .andDo(print())
.andExpect(status().isOk())
;
}
@Test
public void testNOTFOUNDController_file_ExportAsPDF() throws Exception {
// List<String> imgList = Arrays.asList(images);
// Stream<String> imgStream = imgList.stream();
// imgStream.forEach( (img) -> {
String[] badboys = {"NOTFOUND.bmp", "uglydog.jpg"};
for( String img : badboys ) {
ExportAsPDF request = new ExportAsPDF(vhaName, "86753303.001");
when(auditLoggerService.getPathForExportAsPDF(any(ExportAsPDF.class))).thenReturn(img);
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
String jsonRequest = ow.writeValueAsString(request);
mockMvc.perform(
post("/api/v1/ExportAsPDF")
.contentType(MediaType.APPLICATION_JSON)
.content(jsonRequest))
// .andDo(print())
.andExpect(status().is(404))
;
}
}
@Test
public void testSuccessController_file_ExportAsPDF_images() throws Exception {
// List<String> imgList = Arrays.asList(images);
// Stream<String> imgStream = imgList.stream();
// imgStream.forEach( (img) -> {
System.err.println("BERNIE: " + "testSuccessController_file_ExportAsPDF_images"); // testing the test
for( String img : testImages ) {
ExportAsPDF request = new ExportAsPDF(vhaName, "86753303.001");
when(auditLoggerService.getPathForExportAsPDF(any(ExportAsPDF.class))).thenReturn(img);
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
String jsonRequest = ow.writeValueAsString(request);
System.err.println("jsonRequest = " + jsonRequest); // testing the test
mockMvc.perform(
post("/api/v1/exportAsPDF")
.contentType(MediaType.APPLICATION_JSON)
.content(jsonRequest))
// .andDo(print()) // way too verbose!
.andExpect(status().isOk())
// .andExpect(content().contentType(MediaType.APPLICATION_OCTET_STREAM))
.andExpect(content().contentType(MediaType.valueOf("application/pdf")))
;
}
}
@Test(expected = java.io.IOException.class) // We get an exception, instead of 415! Could be improved.
public void testSuccessController_file_ExportAsPDF_non_image() throws Exception {
ExportAsPDF request = new ExportAsPDF(vhaName, "86753303.001"); // ignored by mock getPathForExportAsPDF
when(auditLoggerService.getPathForExportAsPDF(any(ExportAsPDF.class))).thenReturn(thisSrc);
//file:/C:/Users/
DNS
/eclipse/trm-oxygen/ars_app/src/test/java/gov/va/med/ars/controller/FileOperationsControllerTest.java is not a recognized imageformat.
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
String jsonRequest = ow.writeValueAsString(request);
mockMvc.perform(
post("/api/v1/exportAsPDF").contentType(MediaType.APPLICATION_JSON).content(jsonRequest))
.andDo(print())
.andExpect(status().is(415))
// Status expected: <415>, HTTP Status 415 - Unsupported Media Type. Actually, we get an exception!
;
}
}