From 5b8049d96567b9f0fa74b129947d042ef85c95a5 Mon Sep 17 00:00:00 2001 From: sofi2002sofi Date: Wed, 5 Apr 2023 19:58:43 -0300 Subject: [PATCH] CARDS-2175: Increase test coverage cards-form-completion-status tests for DateRangeMinMaxAnswersValidator --- modules/form-completion-status/pom.xml | 2 +- .../DateRangeMinMaxAnswersValidatorTest.java | 162 ++++++++++++++++++ .../internal/MinMaxAnswersValidatorTest.java | 18 +- .../src/test/resources/Questionnaires.json | 9 + 4 files changed, 184 insertions(+), 7 deletions(-) create mode 100644 modules/form-completion-status/src/test/java/io/uhndata/cards/formcompletionstatus/internal/DateRangeMinMaxAnswersValidatorTest.java diff --git a/modules/form-completion-status/pom.xml b/modules/form-completion-status/pom.xml index 4009925c17..b0c85ad46e 100644 --- a/modules/form-completion-status/pom.xml +++ b/modules/form-completion-status/pom.xml @@ -30,7 +30,7 @@ Flags form answers as incomplete or invalid - 0.30 + 0.32 diff --git a/modules/form-completion-status/src/test/java/io/uhndata/cards/formcompletionstatus/internal/DateRangeMinMaxAnswersValidatorTest.java b/modules/form-completion-status/src/test/java/io/uhndata/cards/formcompletionstatus/internal/DateRangeMinMaxAnswersValidatorTest.java new file mode 100644 index 0000000000..5a1e027547 --- /dev/null +++ b/modules/form-completion-status/src/test/java/io/uhndata/cards/formcompletionstatus/internal/DateRangeMinMaxAnswersValidatorTest.java @@ -0,0 +1,162 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.uhndata.cards.formcompletionstatus.internal; + +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import java.util.UUID; + +import javax.jcr.Node; +import javax.jcr.RepositoryException; +import javax.jcr.Session; + +import org.apache.jackrabbit.oak.api.Type; +import org.apache.jackrabbit.oak.plugins.memory.EmptyNodeState; +import org.apache.jackrabbit.oak.spi.state.NodeBuilder; +import org.apache.sling.testing.mock.sling.ResourceResolverType; +import org.apache.sling.testing.mock.sling.junit.SlingContext; +import org.assertj.core.api.Assertions; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.runners.MockitoJUnitRunner; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Unit tests for {@link DateRangeMinMaxAnswersValidator}. + * + * @version $Id$ + */ +@RunWith(MockitoJUnitRunner.class) +public class DateRangeMinMaxAnswersValidatorTest +{ + private static final String NODE_TYPE = "jcr:primaryType"; + private static final String NODE_IDENTIFIER = "jcr:uuid"; + private static final String ANSWER_TYPE = "cards:Answer"; + private static final String QUESTION_PROPERTY = "question"; + private static final String VALUE_PROPERTY = "value"; + private static final String TEST_QUESTIONNAIRE_PATH = "/Questionnaires/TestQuestionnaire"; + private static final String FLAG_INVALID = "INVALID"; + private static final String FLAG_INCOMPLETE = "INCOMPLETE"; + private static final int PRIORITY = 15; + + @Rule + public SlingContext context = new SlingContext(ResourceResolverType.JCR_OAK); + + @InjectMocks + private DateRangeMinMaxAnswersValidator dateRangeMinMaxAnswersValidator; + + @Test + public void getPriorityTest() + { + assertEquals(PRIORITY, this.dateRangeMinMaxAnswersValidator.getPriority()); + } + + @Test + public void validateForAnswerWithDatesNotCreateIntervalValueProperty() throws RepositoryException + { + Node question = this.context.resourceResolver().adaptTo(Session.class) + .getNode(TEST_QUESTIONNAIRE_PATH + "/question_3"); + String answerInSectionUuid = UUID.randomUUID().toString(); + NodeBuilder answerInSectionNodeBuilder = createTestAnswer(answerInSectionUuid, question.getIdentifier()); + answerInSectionNodeBuilder.setProperty(VALUE_PROPERTY, Set.of("2023-01-01"), Type.STRINGS); + + Map flags = createStatusFlagsMap(); + this.dateRangeMinMaxAnswersValidator.validate(answerInSectionNodeBuilder, question, true, flags); + assertTrue(flags.containsKey(FLAG_INCOMPLETE)); + assertTrue(flags.get(FLAG_INCOMPLETE)); + assertFalse(flags.containsKey(FLAG_INVALID)); + } + + @Test + public void validateForAnswerWithMoreThanAllowedValueProperty() throws RepositoryException + { + Node question = this.context.resourceResolver().adaptTo(Session.class) + .getNode(TEST_QUESTIONNAIRE_PATH + "/question_3"); + String answerInSectionUuid = UUID.randomUUID().toString(); + NodeBuilder answerInSectionNodeBuilder = createTestAnswer(answerInSectionUuid, question.getIdentifier()); + answerInSectionNodeBuilder.setProperty(VALUE_PROPERTY, Set.of("2023-01-01", "2023-02-01"), Type.STRINGS); + + Map flags = createStatusFlagsMap(); + this.dateRangeMinMaxAnswersValidator.validate(answerInSectionNodeBuilder, question, true, flags); + assertFalse(flags.containsKey(FLAG_INCOMPLETE)); + assertFalse(flags.containsKey(FLAG_INVALID)); + } + + @Test + public void validateCatchesRepositoryException() throws RepositoryException + { + Node question = mock(Node.class); + when(question.getProperty("dataType")).thenThrow(new RepositoryException()); + String answerInSectionUuid = UUID.randomUUID().toString(); + NodeBuilder answerInSectionNodeBuilder = createTestAnswer(answerInSectionUuid, UUID.randomUUID().toString()); + + Assertions.assertThatCode( + () -> this.dateRangeMinMaxAnswersValidator.validate(answerInSectionNodeBuilder, question, true, + new HashMap<>())).doesNotThrowAnyException(); + } + + @Test + public void validateForNotDateTypeQuestionNotChangesFlagsMap() throws RepositoryException + { + Node question = this.context.resourceResolver().adaptTo(Session.class) + .getNode(TEST_QUESTIONNAIRE_PATH + "/question_1"); + String answerInSectionUuid = UUID.randomUUID().toString(); + NodeBuilder answerInSectionNodeBuilder = createTestAnswer(answerInSectionUuid, question.getIdentifier()); + + Map flags = createStatusFlagsMap(); + this.dateRangeMinMaxAnswersValidator.validate(answerInSectionNodeBuilder, question, true, flags); + assertTrue(flags.containsKey(FLAG_INCOMPLETE)); + assertFalse(flags.get(FLAG_INCOMPLETE)); + assertTrue(flags.containsKey(FLAG_INVALID)); + assertFalse(flags.get(FLAG_INVALID)); + } + + @Before + public void setupRepo() + { + this.context.build().resource("/Questionnaires", NODE_TYPE, "cards:QuestionnairesHomepage").commit(); + this.context.load().json("/Questionnaires.json", TEST_QUESTIONNAIRE_PATH); + } + + private Map createStatusFlagsMap() + { + Map flags = new HashMap<>(); + flags.put(FLAG_INCOMPLETE, false); + flags.put(FLAG_INVALID, false); + return flags; + } + + private NodeBuilder createTestAnswer(String uuid, String questionUuid) + { + NodeBuilder answerBuilder = EmptyNodeState.EMPTY_NODE.builder(); + answerBuilder.setProperty(NODE_TYPE, ANSWER_TYPE); + answerBuilder.setProperty(QUESTION_PROPERTY, questionUuid); + answerBuilder.setProperty(NODE_IDENTIFIER, uuid); + + return answerBuilder; + } + +} diff --git a/modules/form-completion-status/src/test/java/io/uhndata/cards/formcompletionstatus/internal/MinMaxAnswersValidatorTest.java b/modules/form-completion-status/src/test/java/io/uhndata/cards/formcompletionstatus/internal/MinMaxAnswersValidatorTest.java index 8cbed635b0..1a6e921de3 100644 --- a/modules/form-completion-status/src/test/java/io/uhndata/cards/formcompletionstatus/internal/MinMaxAnswersValidatorTest.java +++ b/modules/form-completion-status/src/test/java/io/uhndata/cards/formcompletionstatus/internal/MinMaxAnswersValidatorTest.java @@ -82,11 +82,10 @@ public void validateForAnswerWithoutValueProperty() throws RepositoryException String answerInSectionUuid = UUID.randomUUID().toString(); NodeBuilder answerInSectionNodeBuilder = createTestAnswer(answerInSectionUuid, question.getIdentifier()); - Map flags = new HashMap<>(); - flags.put(FLAG_INCOMPLETE, false); - flags.put(FLAG_INVALID, false); + Map flags = createStatusFlagsMap(); this.minMaxAnswersValidator.validate(answerInSectionNodeBuilder, question, true, flags); assertTrue(flags.containsKey(FLAG_INCOMPLETE)); + assertTrue(flags.get(FLAG_INCOMPLETE)); assertFalse(flags.containsKey(FLAG_INVALID)); } @@ -99,12 +98,11 @@ public void validateForAnswerWithMoreThanAllowedValueProperty() throws Repositor NodeBuilder answerInSectionNodeBuilder = createTestAnswer(answerInSectionUuid, question.getIdentifier()); answerInSectionNodeBuilder.setProperty(VALUE_PROPERTY, Set.of("100", "200", "300"), Type.STRINGS); - Map flags = new HashMap<>(); - flags.put(FLAG_INCOMPLETE, false); - flags.put(FLAG_INVALID, false); + Map flags = createStatusFlagsMap(); this.minMaxAnswersValidator.validate(answerInSectionNodeBuilder, question, true, flags); assertFalse(flags.containsKey(FLAG_INCOMPLETE)); assertTrue(flags.containsKey(FLAG_INVALID)); + assertTrue(flags.get(FLAG_INVALID)); } @Test @@ -127,6 +125,14 @@ public void setupRepo() this.context.load().json("/Questionnaires.json", TEST_QUESTIONNAIRE_PATH); } + private Map createStatusFlagsMap() + { + Map flags = new HashMap<>(); + flags.put(FLAG_INCOMPLETE, false); + flags.put(FLAG_INVALID, false); + return flags; + } + private NodeBuilder createTestAnswer(String uuid, String questionUuid) { NodeBuilder answerBuilder = EmptyNodeState.EMPTY_NODE.builder(); diff --git a/modules/form-completion-status/src/test/resources/Questionnaires.json b/modules/form-completion-status/src/test/resources/Questionnaires.json index 0a40f9fa07..8f62dfc29b 100644 --- a/modules/form-completion-status/src/test/resources/Questionnaires.json +++ b/modules/form-completion-status/src/test/resources/Questionnaires.json @@ -18,5 +18,14 @@ "minAnswers": 1, "maxAnswers": 2 } + }, + "question_3": { + "jcr:primaryType": "cards:Question", + "text": "Date Question", + "description": "A date question", + "dataType": "date", + "dateFormat": "yyyy-MM-dd", + "type": "interval", + "minAnswers": 1 } }