Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RSDEV-326 Update existing tests and add new tests covering time validation for sample fields #88

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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\" }";

Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -448,14 +449,11 @@ public void createEditSampleWithFields() throws Exception {
}

private Optional<Sample> getComplexSampleTemplate(User user) {
Optional<Sample> 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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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\" }";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void exportDefaultUserSamplesToCsv() throws IOException {
List<String> 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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> nameDescriptionMapping = new HashMap<>();
nameDescriptionMapping.put("Name", "name");
nameDescriptionMapping.put("Expiry Date", "expiry date");
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
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
Loading