diff --git a/src/test/java/com/researchspace/api/v1/controller/InventoryImportApiControllerMVCIT.java b/src/test/java/com/researchspace/api/v1/controller/InventoryImportApiControllerMVCIT.java index 09b08eb6c..d1399b42f 100644 --- a/src/test/java/com/researchspace/api/v1/controller/InventoryImportApiControllerMVCIT.java +++ b/src/test/java/com/researchspace/api/v1/controller/InventoryImportApiControllerMVCIT.java @@ -346,8 +346,8 @@ public void importComplexTemplateCsvWithoutTemplateCreation() throws Exception { ApiSampleWithFullSubSamples ninthSample = (ApiSampleWithFullSubSamples) sampleResults.getResults().get(8).getRecord(); assertEquals("TestSample9", ninthSample.getName()); - assertEquals(1, ninthSample.getFields().get(8).getSelectedOptions().size()); - assertEquals("optionB", ninthSample.getFields().get(8).getSelectedOptions().get(0)); + assertEquals(1, ninthSample.getFields().get(9).getSelectedOptions().size()); + assertEquals("optionB", ninthSample.getFields().get(9).getSelectedOptions().get(0)); } @Test diff --git a/src/test/java/com/researchspace/api/v1/controller/InventoryPublicApiControllerMVCIT.java b/src/test/java/com/researchspace/api/v1/controller/InventoryPublicApiControllerMVCIT.java index 15b35459c..a8cbcde07 100644 --- a/src/test/java/com/researchspace/api/v1/controller/InventoryPublicApiControllerMVCIT.java +++ b/src/test/java/com/researchspace/api/v1/controller/InventoryPublicApiControllerMVCIT.java @@ -220,7 +220,7 @@ public void checkOnlyPublicInformationPresentInApiResponse() throws Exception { assertNull(foundRecord.getName()); // name always null assertEquals(1, foundRecord.getTags().size()); assertEquals("complexSampleDescription", foundRecord.getDescription()); - assertEquals(9, ((ApiSampleWithoutSubSamples) foundRecord).getFields().size()); + assertEquals(10, ((ApiSampleWithoutSubSamples) foundRecord).getFields().size()); assertEquals(1, foundRecord.getExtraFields().size()); } } diff --git a/src/test/java/com/researchspace/api/v1/controller/SampleTemplatesApiControllerMVCIT.java b/src/test/java/com/researchspace/api/v1/controller/SampleTemplatesApiControllerMVCIT.java index a462992ca..44eeb24f3 100644 --- a/src/test/java/com/researchspace/api/v1/controller/SampleTemplatesApiControllerMVCIT.java +++ b/src/test/java/com/researchspace/api/v1/controller/SampleTemplatesApiControllerMVCIT.java @@ -274,7 +274,7 @@ public void createUpdateSampleTemplateWithFields() throws Exception { ApiSampleField string = createBasicApiSampleField("string", ApiFieldType.STRING, "string value"); ApiSampleField date = createBasicApiSampleField("date", ApiFieldType.DATE, "2020-10-31"); - ApiSampleField time = createBasicApiSampleField("time", ApiFieldType.TIME, "23:45:12"); + ApiSampleField time = createBasicApiSampleField("time", ApiFieldType.TIME, "23:45"); ApiSampleField number = createBasicApiSampleField("number", ApiFieldType.NUMBER, "112.34"); ApiSampleField choice = diff --git a/src/test/java/com/researchspace/api/v1/controller/SamplesApiControllerMVCIT.java b/src/test/java/com/researchspace/api/v1/controller/SamplesApiControllerMVCIT.java index e90327719..8bba5689b 100644 --- a/src/test/java/com/researchspace/api/v1/controller/SamplesApiControllerMVCIT.java +++ b/src/test/java/com/researchspace/api/v1/controller/SamplesApiControllerMVCIT.java @@ -16,6 +16,7 @@ import com.researchspace.api.v1.model.ApiInventoryRecordRevisionList; import com.researchspace.api.v1.model.ApiLinkItem; import com.researchspace.api.v1.model.ApiSample; +import com.researchspace.api.v1.model.ApiSampleField; import com.researchspace.api.v1.model.ApiSampleInfo; import com.researchspace.api.v1.model.ApiSampleSearchResult; import com.researchspace.api.v1.model.ApiSampleWithFullSubSamples; @@ -44,7 +45,7 @@ public class SamplesApiControllerMVCIT extends API_MVC_InventoryTestBase { - static final int NUM_FIELDS_IN_COMPLEX_SAMPLE = 9; + static final int NUM_FIELDS_IN_COMPLEX_SAMPLE = 10; @Before public void setup() throws Exception { @@ -310,7 +311,7 @@ public void createEditSampleWithFields() throws Exception { "{ \"templateId\": \"" + complexSampleTemplateId + "\",\"fields\": [ { \"content\": \"3.14\" }, {}, {}, { \"type\": \"Text\"," - + " \"content\": \"text content\" }, {}, {}, {}, {}, {}], \"extraFields\" : [ {" + + " \"content\": \"text content\" }, {}, {}, {}, {}, {}, {}], \"extraFields\" : [ {" + " \"name\": \"extraFieldName\", \"type\" : \"number\", \"content\": \"3.15\" } ]," + " \"expiryDate\":null, \"name\": \"sample1\" }"; @@ -406,7 +407,7 @@ public void createEditSampleWithFields() throws Exception { ApiSample sampleRev1Full = getFromJsonResponseBody(result, ApiSample.class); assertEquals("sample1", sampleRev1Full.getName()); assertTrue(sampleRev1Full.getTags().isEmpty()); - assertEquals(9, sampleRev1Full.getFields().size()); + assertEquals(10, sampleRev1Full.getFields().size()); assertEquals("text content", sampleRev1Full.getFields().get(3).getContent()); assertEquals(1, sampleRev1Full.getExtraFields().size()); @@ -448,14 +449,11 @@ public void createEditSampleWithFields() throws Exception { } private Optional getComplexSampleTemplate(User user) { - Optional complexTemplate = - sampleApiMgr.getAllTemplates(user).stream() - .filter( - t -> - t.getName() - .equals(ContentInitializerForDevRunManager.COMPLEX_SAMPLE_TEMPLATE_NAME)) - .findAny(); - return complexTemplate; + return sampleApiMgr.getAllTemplates(user).stream() + .filter( + t -> + ContentInitializerForDevRunManager.COMPLEX_SAMPLE_TEMPLATE_NAME.equals(t.getName())) + .findAny(); } @Test @@ -545,7 +543,7 @@ public void createSampleWithFieldsErrors() throws Exception { String incorrectFieldContentJson = String.format( "{\"name\": \"sample1\", \"templateId\" : \"%s\", " - + "\"fields\" : [ { \"content\": \"test\" }, {}, {}, {}, {}, {}, {}, {},{}] }", + + "\"fields\" : [ { \"content\": \"test\" }, {}, {}, {}, {}, {}, {}, {},{},{}] }", complexTemplateInfo.getId()); result = this.mockMvc @@ -588,7 +586,7 @@ public void createSampleWithFieldsErrors() throws Exception { json = String.format( "{\"name\": \"sample1\", \"templateId\" : \"%s\", \"fields\" : [ { \"content\": \"23\"" - + " }, {}, {}, { \"content\": \"final call\" }, {}, {}, {}, {},{} ] }", + + " }, {}, {}, { \"content\": \"final call\" }, {}, {}, {}, {}, {}, {}] }", complexTemplateInfo.getId()); result = this.mockMvc @@ -1029,7 +1027,7 @@ public void sampleRevisionHistory() throws Exception { .andReturn(); ApiSample sampleRev1Full = getFromJsonResponseBody(result, ApiSample.class); assertEquals("myComplexSample", sampleRev1Full.getName()); - assertEquals(9, sampleRev1Full.getFields().size()); + assertEquals(10, sampleRev1Full.getFields().size()); assertEquals(1, sampleRev1Full.getExtraFields().size()); assertEquals(1, sampleRev1Full.getSubSamples().size()); assertEquals(1, sampleRev1Full.getSubSamplesCount()); @@ -1083,4 +1081,66 @@ public void changeSampleOwner() throws Exception { assertNotNull(editedSample); assertEquals(anotherUser.getUsername(), editedSample.getOwner().getUsername()); } + + @Test + public void createSampleFromTemplateWithValidTimeIsAccepted() throws Exception { + User anyUser = createInitAndLoginAnyUser(); + String apiKey = createApiKeyForuser(anyUser); + + Long sampleTemplateId = getComplexSampleTemplate(anyUser).get().getId(); + String validTime = "10:15"; + String sampleWithExtraFieldsJSON = + makeJsonSampleWithIdAndTime(sampleTemplateId.toString(), validTime); + + MvcResult result = + this.mockMvc + .perform( + createBuilderForPostWithJSONBody( + apiKey, "/samples", anyUser, sampleWithExtraFieldsJSON)) + .andExpect(status().isCreated()) + .andReturn(); + + ApiSampleWithFullSubSamples createdSample = + mvcUtils.getFromJsonResponseBody(result, ApiSampleWithFullSubSamples.class); + + ApiSampleField timeField = + createdSample.getFields().stream() + .filter(f -> f.getType().equals(ApiFieldType.TIME)) + .findFirst() + .get(); + assertEquals(validTime, timeField.getContent()); + } + + @Test + public void createSampleFromTemplateWithInvalidTimeIsRejected() throws Exception { + User anyUser = createInitAndLoginAnyUser(); + String apiKey = createApiKeyForuser(anyUser); + + Long sampleTemplateId = getComplexSampleTemplate(anyUser).get().getId(); + String invalidTime = "9:15"; // time format should be 24 hours with 4 digits e.g. 09:15 + String sampleWithExtraFieldsJSON = + makeJsonSampleWithIdAndTime(sampleTemplateId.toString(), invalidTime); + + MvcResult result = + this.mockMvc + .perform( + createBuilderForPostWithJSONBody( + apiKey, "/samples", anyUser, sampleWithExtraFieldsJSON)) + .andExpect(status().isBadRequest()) + .andReturn(); + + String responseText = result.getResponse().getContentAsString(); + String expectedErrorMessage = "9:15 is an invalid 24hour time format. Valid format is 00:00."; + assertTrue(responseText.contains(expectedErrorMessage)); + } + + private String makeJsonSampleWithIdAndTime(String id, String time) { + return "{ \"templateId\": \"" + + id + + "\",\"fields\": [ { \"content\": \"3.14\" }, {}, {}, { \"type\": \"Text\"," + + " \"content\": \"text content\" }, {}, {}, {}, {\"type\": \"time\", \"content\": \"" + + time + + "\"}, {}, {}]," + + " \"expiryDate\":null, \"name\": \"sample1\" }"; + } } diff --git a/src/test/java/com/researchspace/api/v1/controller/SamplesApiControllerTest.java b/src/test/java/com/researchspace/api/v1/controller/SamplesApiControllerTest.java index 9e7ccd034..ce3266d34 100644 --- a/src/test/java/com/researchspace/api/v1/controller/SamplesApiControllerTest.java +++ b/src/test/java/com/researchspace/api/v1/controller/SamplesApiControllerTest.java @@ -274,8 +274,8 @@ public void createRetrieveComplexSample() throws Exception { recordFactory.createComplexSampleTemplate("API sample template", "API test", testUser); // add default value to various fields sampleTemplate.getActiveFields().get(4).setData("text"); // text - sampleTemplate.getActiveFields().get(7).setData("option1"); // radio - sampleTemplate.getActiveFields().get(8).setSelectedOptions(List.of("optionA")); // choice + sampleTemplate.getActiveFields().get(8).setData("option1"); // radio + sampleTemplate.getActiveFields().get(9).setSelectedOptions(List.of("optionA")); // choice Sample savedTemplate = sampleDao.persistSampleTemplate(sampleTemplate); newSample.setTemplateId(savedTemplate.getId()); @@ -313,8 +313,8 @@ public void createRetrieveComplexSample() throws Exception { // check fields and default values assignment assertEquals(NUM_FIELDS_IN_COMPLEX_SAMPLE, createdSample.getFields().size()); assertEquals("text", createdSample.getFields().get(4).getContent()); - assertEquals(List.of("option1"), createdSample.getFields().get(7).getSelectedOptions()); - assertEquals(List.of("optionA"), createdSample.getFields().get(8).getSelectedOptions()); + assertEquals(List.of("option1"), createdSample.getFields().get(8).getSelectedOptions()); + assertEquals(List.of("optionA"), createdSample.getFields().get(9).getSelectedOptions()); assertEquals(1, createdSample.getExtraFields().size()); // retrieve @@ -355,6 +355,8 @@ public void createComplexSampleWithProvidedFieldContent() throws Exception { fields.add(refrenceField); ApiSampleField attachmentField = new ApiSampleField(); fields.add(attachmentField); + ApiSampleField timeField = new ApiSampleField(); + fields.add(timeField); ApiSampleField radioField = new ApiSampleField(); radioField.setSelectedOptions(List.of("option1")); fields.add(radioField); @@ -382,9 +384,9 @@ public void createComplexSampleWithProvidedFieldContent() throws Exception { .getFields() .get(1) .getContent()); // defaults get overridden by content in request - assertEquals(List.of("option1"), createdSample.getFields().get(7).getSelectedOptions()); + assertEquals(List.of("option1"), createdSample.getFields().get(8).getSelectedOptions()); assertEquals( - List.of("optionA", "optionB"), createdSample.getFields().get(8).getSelectedOptions()); + List.of("optionA", "optionB"), createdSample.getFields().get(9).getSelectedOptions()); } @Test diff --git a/src/test/java/com/researchspace/api/v1/model/ApiFieldToModelFieldFactoryTest.java b/src/test/java/com/researchspace/api/v1/model/ApiFieldToModelFieldFactoryTest.java index cd423b2fb..574c524a0 100644 --- a/src/test/java/com/researchspace/api/v1/model/ApiFieldToModelFieldFactoryTest.java +++ b/src/test/java/com/researchspace/api/v1/model/ApiFieldToModelFieldFactoryTest.java @@ -19,7 +19,12 @@ public void allEnumsCovered() { for (ApiFieldType type : EnumSet.complementOf( EnumSet.of( - ApiFieldType.CHOICE, ApiFieldType.URI, ApiFieldType.RADIO, ApiFieldType.NUMBER))) { + ApiFieldType.CHOICE, + ApiFieldType.URI, + ApiFieldType.RADIO, + ApiFieldType.NUMBER, + ApiField.ApiFieldType.TIME, + ApiField.ApiFieldType.DATE))) { ApiSampleField any = new ApiSampleField(); any.setContent("some content"); any.setType(type); @@ -74,6 +79,18 @@ public void radioField() { assertNotNull(factory.apiSampleFieldToModelField(anyApiField)); } + @Test + public void timeField() { + // time field should be 24hour in pattern HH:mm + assertValues("10:24", "9:24", ApiFieldType.TIME); + } + + @Test + public void dateField() { + // date field should be in pattern yyyy-MM-dd + assertValues("2024-08-19", "24/08/19", ApiFieldType.DATE); + } + private void assertValues(String valid, String invalid, ApiFieldType type) { ApiSampleField any = new ApiSampleField(); any.setContent(invalid); diff --git a/src/test/java/com/researchspace/service/inventory/InventoryAuditApiManagerIT.java b/src/test/java/com/researchspace/service/inventory/InventoryAuditApiManagerIT.java index a362c30cd..a9e79e1d4 100644 --- a/src/test/java/com/researchspace/service/inventory/InventoryAuditApiManagerIT.java +++ b/src/test/java/com/researchspace/service/inventory/InventoryAuditApiManagerIT.java @@ -95,6 +95,6 @@ public void getSampleSubSampleRevisions() { assertEquals("myComplexSample", apiSampleRev1.getName()); assertEquals(sampleFirstRevisionId, apiSampleRev1.getRevisionId()); assertEquals(1, apiSampleRev1.getExtraFields().size()); - assertEquals(9, apiSampleRev1.getFields().size()); + assertEquals(10, apiSampleRev1.getFields().size()); } } diff --git a/src/test/java/com/researchspace/service/inventory/InventoryIdentifierApiManagerIT.java b/src/test/java/com/researchspace/service/inventory/InventoryIdentifierApiManagerIT.java index 5fdbe023a..2f520f8d6 100644 --- a/src/test/java/com/researchspace/service/inventory/InventoryIdentifierApiManagerIT.java +++ b/src/test/java/com/researchspace/service/inventory/InventoryIdentifierApiManagerIT.java @@ -29,13 +29,13 @@ public void retrieveLastPublishedVersionOfItemWithPublicLink() { User user = createInitAndLoginAnyUser(); ApiSampleWithFullSubSamples createdSample = createComplexSampleForUser(user); - assertEquals(9, createdSample.getFields().size()); + assertEquals(10, createdSample.getFields().size()); assertEquals(1, createdSample.getTags().size()); assertEquals(0, createdSample.getIdentifiers().size()); ApiInventoryRecordInfo updatedSample = inventoryIdentifierApiMgr.registerNewIdentifier(createdSample.getOid(), user); - assertEquals(9, ((ApiSample) updatedSample).getFields().size()); + assertEquals(10, ((ApiSample) updatedSample).getFields().size()); assertEquals(1, updatedSample.getTags().size()); assertEquals(1, updatedSample.getIdentifiers().size()); ApiInventoryDOI sampleDoi = updatedSample.getIdentifiers().get(0); @@ -63,7 +63,7 @@ public void retrieveLastPublishedVersionOfItemWithPublicLink() { inventoryIdentifierApiMgr.findPublishedItemVersionByPublicLink(publicLink); assertNotNull(publishedRecord); assertEquals("myComplexSample", publishedRecord.getName()); - assertEquals(9, ((ApiSample) publishedRecord).getFields().size()); + assertEquals(10, ((ApiSample) publishedRecord).getFields().size()); assertEquals(1, publishedRecord.getTags().size()); assertEquals(1, publishedRecord.getIdentifiers().size()); diff --git a/src/test/java/com/researchspace/service/inventory/SampleApiManagerTest.java b/src/test/java/com/researchspace/service/inventory/SampleApiManagerTest.java index c6d68774e..bed03f88b 100644 --- a/src/test/java/com/researchspace/service/inventory/SampleApiManagerTest.java +++ b/src/test/java/com/researchspace/service/inventory/SampleApiManagerTest.java @@ -205,7 +205,7 @@ public void saveUpdateComplexSample() { ApiSample retrievedSample = sampleApiMgr.getApiSampleById(newSample.getId(), testUser); assertEquals("myComplexSample", retrievedSample.getName()); assertNotNull(retrievedSample.getSubSamples()); - assertEquals(9, retrievedSample.getFields().size()); + assertEquals(10, retrievedSample.getFields().size()); assertEquals("23", retrievedSample.getFields().get(0).getContent()); assertEquals(1, retrievedSample.getExtraFields().size()); assertEquals("3.14", retrievedSample.getExtraFields().get(0).getContent()); @@ -265,7 +265,7 @@ public void saveUpdateComplexSample() { sampleApiMgr.updateApiSample(sampleUpdates, testUser); retrievedSample = sampleApiMgr.getApiSampleById(newSample.getId(), testUser); assertEquals("updated myComplexSample", retrievedSample.getName()); - assertEquals(9, retrievedSample.getFields().size()); // same number of sample fields + assertEquals(10, retrievedSample.getFields().size()); // same number of sample fields assertEquals("24", retrievedSample.getFields().get(0).getContent()); assertEquals(2, retrievedSample.getExtraFields().size()); // increased number of extra fields assertEquals("3.15", retrievedSample.getExtraFields().get(0).getContent()); diff --git a/src/test/java/com/researchspace/service/inventory/csvexport/CsvSampleExporterTest.java b/src/test/java/com/researchspace/service/inventory/csvexport/CsvSampleExporterTest.java index 58e245b69..1411269db 100644 --- a/src/test/java/com/researchspace/service/inventory/csvexport/CsvSampleExporterTest.java +++ b/src/test/java/com/researchspace/service/inventory/csvexport/CsvSampleExporterTest.java @@ -86,7 +86,7 @@ public void exportDefaultUserSamplesToCsv() throws IOException { List csvColumnNames = sampleExporter.writeSampleCsvHeaderIntoOutput( exampleSamples.getResults(), exportMode, null, outputStream); - assertEquals(22, csvColumnNames.size()); + assertEquals(23, csvColumnNames.size()); String csvHeaderLineForSamples = outputStream.toString(); String expectedHeaderLineStart = "Global ID,Name,Tags,Owner,Description,Parent Template (Global ID),Parent Template" diff --git a/src/test/java/com/researchspace/service/inventory/impl/InventoryImportManagerTest.java b/src/test/java/com/researchspace/service/inventory/impl/InventoryImportManagerTest.java index e740f1e00..2ccba7756 100644 --- a/src/test/java/com/researchspace/service/inventory/impl/InventoryImportManagerTest.java +++ b/src/test/java/com/researchspace/service/inventory/impl/InventoryImportManagerTest.java @@ -578,11 +578,11 @@ public void testSampleCsvImportIntoPreexistingComplexTemplate() // csv file with 3 samples: columns mapped to name/expiry date/template fields/quantity InputStream samplesCsvIS = IOUtils.toInputStream( - "Expiry Date, Name, MyNumber, MyDate, MyString, MyText, MyURL, MyRef, MyAtt, MyRadio," - + " MyChoice, MyQuantity\n" - + "2030-01-01, TestSample1,3.14,,,,,,,,,\n" - + "2030-01-01, TestSample2,,2021-11-23,,,,,,,,5.25ml\n" - + "2030-01-01, TestSample3,,, dummyText,,,,,,,200 ml\n"); + "Expiry Date, Name, MyNumber, MyDate, MyString, MyText, MyURL, MyRef, MyAtt, MyTime," + + " MyRadio, MyChoice, MyQuantity\n" + + "2030-01-01, TestSample1,3.14,,,,,,,,,,\n" + + "2030-01-01, TestSample2,,2021-11-23,,,,,,,,,5.25ml\n" + + "2030-01-01, TestSample3,,, dummyText,,,,,,,,200 ml\n"); HashMap nameDescriptionMapping = new HashMap<>(); nameDescriptionMapping.put("Name", "name"); nameDescriptionMapping.put("Expiry Date", "expiry date"); @@ -673,7 +673,7 @@ public void testSampleCsvImportIntoPreexistingTemplateErrors() throws IOExceptio // check error String sampleErrorMsg = sampleResult.getTemplate().getError().getErrors().get(0); assertEquals( - "Number of unmapped CSV columns is 8, but number of fields in sample template is 9. " + "Number of unmapped CSV columns is 8, but number of fields in sample template is 10. " + "The CSV file must exactly map all the template fields.", sampleErrorMsg); } diff --git a/src/test/resources/TestResources/inventory/antibody_import_complex_template.csv b/src/test/resources/TestResources/inventory/antibody_import_complex_template.csv index c72719bfa..8fd27d463 100644 --- a/src/test/resources/TestResources/inventory/antibody_import_complex_template.csv +++ b/src/test/resources/TestResources/inventory/antibody_import_complex_template.csv @@ -1,10 +1,10 @@ -Name, MyNumber, MyDate, MyString, MyText, MyURL, MyRef, MyAtt, MyRadio, MyChoice -TestSample1,3.14,,,,,,,, -TestSample2,2,2021-11-23,,,,,,, -TestSample3,3,,dummyString,,,,,, -TestSample4,4,,,dummyText,,,,, -TestSample5,5,,,,http://dummy.url,,,, -TestSample6,6,,,,,,,, -TestSample7,7,,,,,,,, -TestSample8,8,,,,,,,option1, -TestSample9,9,,,,,,,,optionB \ No newline at end of file +Name, MyNumber, MyDate, MyString, MyText, MyURL, MyRef, MyAtt, MyTime, MyRadio, MyChoice +TestSample1,3.14,,,,,,,,, +TestSample2,2,2021-11-23,,,,,,,, +TestSample3,3,,dummyString,,,,,,, +TestSample4,4,,,dummyText,,,,,, +TestSample5,5,,,,http://dummy.url,,,,, +TestSample6,6,,,,,,,,, +TestSample7,7,,,,,,,,, +TestSample8,8,,,,,,,,option1, +TestSample9,9,,,,,,,,,optionB \ No newline at end of file