Java tutorial
/******************************************************************************* * Educational Online Test Delivery System * Copyright (c) 2013 American Institutes for Research * * Distributed under the AIR Open Source License, Version 1.0 * See accompanying file AIR-License-1_0.txt or at * http://www.smarterapp.org/documents/American_Institutes_for_Research_Open_Source_Software_License.pdf ******************************************************************************/ package org.opentestsystem.delivery.testreg.integration; import static org.hamcrest.core.Is.is; import static org.hamcrest.core.IsNull.notNullValue; import static org.junit.Assert.assertThat; import static org.opentestsystem.delivery.testreg.domain.TestRegPermission.ACCOMMODATIONS_UPLOAD; import static org.opentestsystem.delivery.testreg.domain.TestRegPermission.ENTITY_UPLOAD; import static org.opentestsystem.delivery.testreg.domain.TestRegPermission.STUDENTGROUP_UPLOAD; import static org.opentestsystem.delivery.testreg.domain.TestRegPermission.STUDENT_UPLOAD; import static org.opentestsystem.delivery.testreg.domain.TestRegPermission.USER_UPLOAD; import java.io.ByteArrayOutputStream; import java.io.IOException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.junit.Before; import org.junit.Test; import org.opentestsystem.delivery.testreg.domain.ClientEntity; import org.opentestsystem.delivery.testreg.domain.FileUploadResponse; import org.opentestsystem.delivery.testreg.domain.FileUploadSummary; import org.opentestsystem.delivery.testreg.domain.GroupOfStatesEntity; import org.opentestsystem.delivery.testreg.domain.StateEntity; import org.springframework.http.HttpStatus; import org.springframework.mock.web.MockMultipartFile; public class FileUploadDataControllerTest extends AbstractSecuredRestEmbeddedMongoTest { @Before public void init() throws Exception { if (this.mongoTemplate.findAll(ClientEntity.class).isEmpty()) { this.mongoTemplate.insert( new ClientEntity.Builder("1000", "Test Client", "asfas-asdf-asdf-aasdf", "true").build()); } } @Test public void testuploadFile() throws Exception { final FileUploadResponse response = uploadStateFile(); assertThat(response, notNullValue()); assertThat(response.getFileGridFsId(), notNullValue()); assertThat(response.getFileName(), is("State_Upload.xlsx")); } @Test public void testSaveEntity() throws Exception { // Insert parent entity this.mongoTemplate.insert( new GroupOfStatesEntity.Builder("12345", "MIDWEST STATES", "CLIENT", "1000", "ADD").build()); // Insert an entity this.mongoTemplate .insert(new StateEntity.Builder("WI", "Wisconsin", "GROUPOFSTATES", "12345", "ADD").build()); final FileUploadResponse response = uploadStateFile(); assertThat(response, notNullValue()); final FileUploadSummary[] fus = callGETRestService("/saveEntity/" + response.getFileGridFsId(), FileUploadSummary[].class, ACCOMMODATIONS_UPLOAD, STUDENT_UPLOAD, ENTITY_UPLOAD, STUDENTGROUP_UPLOAD, USER_UPLOAD); assertThat(fus, notNullValue()); assertThat(fus.length, is(1)); assertThat(fus[0].getAddedRecords(), is(0)); assertThat(fus[0].getUpdatedRecords(), is(3)); // All Added records turns out as Update (UPSERT) assertThat(fus[0].getDeletedRecords(), is(1)); } private FileUploadResponse uploadStateFile() throws Exception { final MockMultipartFile mockMultipartFile = new MockMultipartFile("uploadFile", "State_Upload.xlsx", null, createStateWorkbook()); return callUploadFileRestService("/uploadFile?formatType=STATE", mockMultipartFile, FileUploadResponse.class, HttpStatus.CREATED, ACCOMMODATIONS_UPLOAD, STUDENT_UPLOAD, ENTITY_UPLOAD, STUDENTGROUP_UPLOAD, USER_UPLOAD); } private byte[] createStateWorkbook() throws IOException { final Workbook wb = new XSSFWorkbook(); addRows(wb.createSheet("test"), getStateData()); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); wb.write(baos); return baos.toByteArray(); } private String[][] getStateData() { return new String[][] { { "STATEABBREVIATION", "STATENAME", "PARENTENTITYTYPE", "PARENTEXTERNALID", "DELETE" }, { "MN", "Minnesota", "GROUPOFSTATES", "12345", "" }, { "MT", "Montana", "GROUPOFSTATES", "12345", "" }, // N/A in DB { "WI", "Wisconsin", "GROUPOFSTATES", "12345", "DELETE" }, { "CA", "California", "GROUPOFSTATES", "12345", null } }; } private void addRows(final Sheet sheet, final String[][] data) { final int totalRows = data.length; final int totalCols = data[0].length; for (int i = 0; i < totalRows; i++) { final Row row = sheet.createRow(i); for (int j = 0; j < totalCols; j++) { final Cell cell = row.createCell(j); cell.setAsActiveCell(); cell.setCellValue(data[i][j]); } } } }