Search in sources :

Example 21 with IAttachment

use of com.ibm.websphere.jaxrs20.multipart.IAttachment in project CSC480-22S by tenbergen.

the class ProfessorAssignmentResource method addRubricToPeerReview.

/**
 * File is uploaded as form-data and passed back as a List<IAttachment>
 * The attachment is processed in FileDao.FileFactory, which reads and
 * reconstructs the file through inputStream and outputStream respectively
 *
 * @param attachments  type List<IAttachment>: file(s) passed back as form-data
 * @param courseID     type String
 * @param assignmentID type int
 * @return Response
 */
@POST
@RolesAllowed("professor")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces({ MediaType.MULTIPART_FORM_DATA, "application/pdf" })
@Path("/courses/{courseID}/assignments/{assignmentID}/peer-review/rubric/upload")
public Response addRubricToPeerReview(List<IAttachment> attachments, @PathParam("courseID") String courseID, @PathParam("assignmentID") int assignmentID) throws Exception {
    for (IAttachment attachment : attachments) {
        if (attachment == null)
            continue;
        String fileName = attachment.getDataHandler().getName();
        if (!fileName.endsWith("pdf") && !fileName.endsWith("zip") && !fileName.endsWith("docx"))
            return Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE).build();
        new AssignmentInterface().writeRubricToPeerReviews(FileDAO.fileFactory(fileName, courseID, attachment, assignmentID));
    }
    return Response.status(Response.Status.OK).entity("Successfully added file to peer reviews.").build();
}
Also used : AssignmentInterface(edu.oswego.cs.rest.database.AssignmentInterface) IAttachment(com.ibm.websphere.jaxrs20.multipart.IAttachment) RolesAllowed(javax.annotation.security.RolesAllowed)

Example 22 with IAttachment

use of com.ibm.websphere.jaxrs20.multipart.IAttachment in project CSC480-22S by tenbergen.

the class ProfessorAssignmentResource method addTemplateToPeerReview.

/**
 * File is uploaded as form-data and passed back as a List<IAttachment>
 * The attachment is processed in FileDao.FileFactory, which reads and
 * reconstructs the file through inputStream and outputStream respectively
 *
 * @param attachments  type List<IAttachment>: file(s) passed back as form-data
 * @param courseID     type String
 * @param assignmentID type int
 * @return Response
 */
@POST
@RolesAllowed("professor")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces({ MediaType.MULTIPART_FORM_DATA, "application/pdf" })
@Path("/courses/{courseID}/assignments/{assignmentID}/peer-review/template/upload")
public Response addTemplateToPeerReview(List<IAttachment> attachments, @PathParam("courseID") String courseID, @PathParam("assignmentID") int assignmentID) throws Exception {
    for (IAttachment attachment : attachments) {
        if (attachment == null)
            continue;
        String fileName = attachment.getDataHandler().getName();
        if (!fileName.endsWith("pdf") && !fileName.endsWith("zip") && !fileName.endsWith("docx"))
            return Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE).build();
        new AssignmentInterface().writeTemplateToPeerReviews(FileDAO.fileFactory(fileName, courseID, attachment, assignmentID));
    }
    return Response.status(Response.Status.OK).entity("Successfully added file to peer reviews.").build();
}
Also used : AssignmentInterface(edu.oswego.cs.rest.database.AssignmentInterface) IAttachment(com.ibm.websphere.jaxrs20.multipart.IAttachment) RolesAllowed(javax.annotation.security.RolesAllowed)

Example 23 with IAttachment

use of com.ibm.websphere.jaxrs20.multipart.IAttachment in project CSC480-22S by tenbergen.

the class CourseManagerTests method testMassAddStudents.

// For whatever reason, AttachmentBuilder.newBuilder is recognized at compiler time but not at run time...?
// gives a NoClassDefFoundError if you try to run this test. I explored different dependency versions to fix this,
// ... but no dice. Figured this was becoming more trouble than it was worth, so I switched tactics a bit.
// For this reason, any tests that involve a file upload/download are only ever done in Postman.
@Test
public void testMassAddStudents() throws FileNotFoundException {
    targetUrl = "/courses/course/student/mass-add/";
    List<IAttachment> attachments = new ArrayList<>();
    File testFile = new File("src/test/testRosters/test.csv");
    attachments.add(AttachmentBuilder.newBuilder("studentRoster").inputStream(new FileInputStream(testFile)).build());
    Response r = client.target(baseUrl + targetUrl).request().post(Entity.entity(attachments, MediaType.MULTIPART_FORM_DATA));
    Assertions.assertEquals(Response.Status.OK, Response.Status.fromStatusCode(r.getStatus()), "Student was not added properly.");
}
Also used : Response(javax.ws.rs.core.Response) ArrayList(java.util.ArrayList) IAttachment(com.ibm.websphere.jaxrs20.multipart.IAttachment) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 24 with IAttachment

use of com.ibm.websphere.jaxrs20.multipart.IAttachment in project quality-measure-and-cohort-service by Alvearie.

the class CohortEngineRestHandlerTest method testCohortBadCQLDefinition.

@PrepareForTest({ Response.class, FHIRRestUtils.class })
@Test
public void testCohortBadCQLDefinition() throws Exception {
    prepMocks();
    mockResponseClasses();
    mockFhirResourceRetrieval("/metadata", getCapabilityStatement());
    Patient patient = getPatient("123", AdministrativeGender.FEMALE, 40);
    mockFhirResourceRetrieval(patient);
    PowerMockito.mockStatic(ServiceBaseUtility.class);
    PowerMockito.mockStatic(FHIRRestUtils.class);
    PowerMockito.mockStatic(DefaultFhirClientBuilder.class);
    PowerMockito.when(ServiceBaseUtility.apiSetup(VERSION, logger, MethodNames.EVALUATE_COHORT.getName())).thenReturn(null);
    PowerMockito.whenNew(DefaultFhirClientBuilder.class).withArguments(Mockito.any()).thenReturn(mockDefaultFhirClientBuilder);
    // Create the ZIP part of the request
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (ZipOutputStream zos = new ZipOutputStream(baos)) {
        File tempFile = new File("src/test/resources/cql/basic/Test-bad-1.0.0.cql");
        ZipEntry entry = new ZipEntry("cql/Test-bad-1.0.0.cql");
        zos.putNextEntry(entry);
        byte[] x = new byte[(int) tempFile.length()];
        new FileInputStream(tempFile).read(x);
        zos.write(x);
        zos.closeEntry();
    }
    ByteArrayInputStream zipIs = new ByteArrayInputStream(baos.toByteArray());
    IAttachment measurePart = mockAttachment(zipIs);
    CohortEvaluation requestData = new CohortEvaluation();
    FhirServerConfig serverConfig = getFhirServerConfig();
    requestData.setDataServerConfig(serverConfig);
    requestData.setTerminologyServerConfig(serverConfig);
    requestData.setDefineToRun("Female");
    requestData.setEntrypoint("Test-bad-1.0.0.cql");
    requestData.setPatientIds("123");
    requestData.setLoggingLevel(CqlDebug.TRACE);
    ObjectMapper om = new ObjectMapper();
    String json = om.writeValueAsString(requestData);
    ByteArrayInputStream jsonIs = new ByteArrayInputStream(json.getBytes());
    IAttachment request = mockAttachment(jsonIs);
    // Assemble them together into a reasonable facsimile of the real request
    IMultipartBody body = getFhirConfigFileBody();
    when(body.getAttachment(CohortEngineRestHandler.CQL_DEFINITION)).thenReturn(measurePart);
    when(body.getAttachment(CohortEngineRestHandler.REQUEST_DATA_PART)).thenReturn(request);
    when(measurePart.getDataHandler().getName()).thenReturn("Test-bad_1.0.0_cql.zip");
    when(measurePart.getHeader("Content-Disposition")).thenReturn("Test-bad_1.0.0_cql.zip");
    Response loadResponse = restHandler.evaluateCohort(mockRequestContext, null, body);
    assertNotNull(loadResponse);
    PowerMockito.verifyStatic(Response.class);
    Response.status(500);
    PowerMockito.verifyStatic(ServiceBaseUtility.class);
    ServiceBaseUtility.apiSetup(Mockito.isNull(), Mockito.any(), Mockito.anyString());
    // verifyStatic must be called before each verification
    PowerMockito.verifyStatic(ServiceBaseUtility.class);
    ServiceBaseUtility.apiCleanup(Mockito.any(), Mockito.eq(MethodNames.EVALUATE_COHORT.getName()));
}
Also used : CohortEvaluation(com.ibm.cohort.engine.api.service.model.CohortEvaluation) ZipEntry(java.util.zip.ZipEntry) Patient(org.hl7.fhir.r4.model.Patient) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IAttachment(com.ibm.websphere.jaxrs20.multipart.IAttachment) FileInputStream(java.io.FileInputStream) Response(javax.ws.rs.core.Response) IMultipartBody(com.ibm.websphere.jaxrs20.multipart.IMultipartBody) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipOutputStream(java.util.zip.ZipOutputStream) FhirServerConfig(com.ibm.cohort.fhir.client.config.FhirServerConfig) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 25 with IAttachment

use of com.ibm.websphere.jaxrs20.multipart.IAttachment in project quality-measure-and-cohort-service by Alvearie.

the class CohortEngineRestHandlerTest method testEvaluateMeasureMoreThanOneFormOfId.

@PrepareForTest({ Response.class, TenantManager.class, ServiceBaseUtility.class })
@Test
public void testEvaluateMeasureMoreThanOneFormOfId() throws Exception {
    prepMocks();
    PowerMockito.mockStatic(ServiceBaseUtility.class);
    PowerMockito.when(ServiceBaseUtility.apiSetup(VERSION, logger, MethodNames.EVALUATE_MEASURE.getName())).thenReturn(null);
    mockResponseClasses();
    Library library = TestHelper.getTemplateLibrary();
    Measure measure = TestHelper.getTemplateMeasure(library);
    Patient patient = getPatient("patientId", AdministrativeGender.MALE, 40);
    mockFhirResourceRetrieval("/metadata", getCapabilityStatement());
    mockFhirResourceRetrieval(patient);
    FhirServerConfig clientConfig = getFhirServerConfig();
    Map<String, Parameter> parameterOverrides = new HashMap<>();
    parameterOverrides.put("Measurement Period", new IntervalParameter(new DateParameter("2019-07-04"), true, new DateParameter("2020-07-04"), true));
    MeasureContext measureContext = new MeasureContext(measure.getId(), parameterOverrides, new Identifier().setValue("identifier"));
    MeasureEvaluation evaluationRequest = new MeasureEvaluation();
    evaluationRequest.setDataServerConfig(clientConfig);
    evaluationRequest.setPatientId(patient.getId());
    evaluationRequest.setMeasureContext(measureContext);
    // evaluationRequest.setEvidenceOptions(evidenceOptions);
    FhirContext fhirContext = FhirContext.forR4();
    IParser parser = fhirContext.newJsonParser().setPrettyPrint(true);
    // Create the metadata part of the request
    ObjectMapper om = new ObjectMapper();
    String json = om.writeValueAsString(evaluationRequest);
    ByteArrayInputStream jsonIs = new ByteArrayInputStream(json.getBytes());
    IAttachment rootPart = mockAttachment(jsonIs);
    // Create the ZIP part of the request
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    TestHelper.createMeasureArtifact(baos, parser, measure, library);
    ByteArrayInputStream zipIs = new ByteArrayInputStream(baos.toByteArray());
    IAttachment measurePart = mockAttachment(zipIs);
    // Assemble them together into a reasonable facsimile of the real request
    IMultipartBody body = mock(IMultipartBody.class);
    when(body.getAttachment(CohortEngineRestHandler.REQUEST_DATA_PART)).thenReturn(rootPart);
    when(body.getAttachment(CohortEngineRestHandler.MEASURE_PART)).thenReturn(measurePart);
    Response loadResponse = restHandler.evaluateMeasure(mockRequestContext, VERSION, body);
    assertNotNull(loadResponse);
    PowerMockito.verifyStatic(Response.class);
    Response.status(400);
}
Also used : FhirContext(ca.uhn.fhir.context.FhirContext) HashMap(java.util.HashMap) MeasureEvaluation(com.ibm.cohort.engine.api.service.model.MeasureEvaluation) PatientListMeasureEvaluation(com.ibm.cohort.engine.api.service.model.PatientListMeasureEvaluation) Patient(org.hl7.fhir.r4.model.Patient) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IAttachment(com.ibm.websphere.jaxrs20.multipart.IAttachment) Response(javax.ws.rs.core.Response) MeasureContext(com.ibm.cohort.engine.measure.MeasureContext) IMultipartBody(com.ibm.websphere.jaxrs20.multipart.IMultipartBody) DateParameter(com.ibm.cohort.cql.evaluation.parameters.DateParameter) Identifier(com.ibm.cohort.engine.measure.Identifier) ByteArrayInputStream(java.io.ByteArrayInputStream) Measure(org.hl7.fhir.r4.model.Measure) DateParameter(com.ibm.cohort.cql.evaluation.parameters.DateParameter) Parameter(com.ibm.cohort.cql.evaluation.parameters.Parameter) IntervalParameter(com.ibm.cohort.cql.evaluation.parameters.IntervalParameter) FhirServerConfig(com.ibm.cohort.fhir.client.config.FhirServerConfig) Library(org.hl7.fhir.r4.model.Library) IntervalParameter(com.ibm.cohort.cql.evaluation.parameters.IntervalParameter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IParser(ca.uhn.fhir.parser.IParser) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

IAttachment (com.ibm.websphere.jaxrs20.multipart.IAttachment)31 Response (javax.ws.rs.core.Response)22 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)19 IMultipartBody (com.ibm.websphere.jaxrs20.multipart.IMultipartBody)16 ByteArrayInputStream (java.io.ByteArrayInputStream)16 FhirServerConfig (com.ibm.cohort.fhir.client.config.FhirServerConfig)15 Test (org.junit.Test)14 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 Patient (org.hl7.fhir.r4.model.Patient)9 PatientListMeasureEvaluation (com.ibm.cohort.engine.api.service.model.PatientListMeasureEvaluation)8 FhirContext (ca.uhn.fhir.context.FhirContext)7 IParser (ca.uhn.fhir.parser.IParser)7 ApiImplicitParams (io.swagger.annotations.ApiImplicitParams)7 ApiOperation (io.swagger.annotations.ApiOperation)7 ApiResponse (io.swagger.annotations.ApiResponse)7 ApiResponses (io.swagger.annotations.ApiResponses)7 File (java.io.File)7 Consumes (javax.ws.rs.Consumes)7 POST (javax.ws.rs.POST)7