Skip to content

Commit

Permalink
CARDS-2175: Increase test coverage cards-form-completion-status
Browse files Browse the repository at this point in the history
tests for DateRangeMinMaxAnswersValidator
  • Loading branch information
sofi2002sofi committed Apr 5, 2023
1 parent fb24238 commit 5b8049d
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 7 deletions.
2 changes: 1 addition & 1 deletion modules/form-completion-status/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<name>Flags form answers as incomplete or invalid</name>

<properties>
<coverage.instructionRatio>0.30</coverage.instructionRatio>
<coverage.instructionRatio>0.32</coverage.instructionRatio>
</properties>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Boolean> 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<String, Boolean> 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<String, Boolean> 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<String, Boolean> createStatusFlagsMap()
{
Map<String, Boolean> 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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,10 @@ public void validateForAnswerWithoutValueProperty() throws RepositoryException
String answerInSectionUuid = UUID.randomUUID().toString();
NodeBuilder answerInSectionNodeBuilder = createTestAnswer(answerInSectionUuid, question.getIdentifier());

Map<String, Boolean> flags = new HashMap<>();
flags.put(FLAG_INCOMPLETE, false);
flags.put(FLAG_INVALID, false);
Map<String, Boolean> flags = createStatusFlagsMap();
this.minMaxAnswersValidator.validate(answerInSectionNodeBuilder, question, true, flags);
assertTrue(flags.containsKey(FLAG_INCOMPLETE));
assertTrue(flags.get(FLAG_INCOMPLETE));
assertFalse(flags.containsKey(FLAG_INVALID));
}

Expand All @@ -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<String, Boolean> flags = new HashMap<>();
flags.put(FLAG_INCOMPLETE, false);
flags.put(FLAG_INVALID, false);
Map<String, Boolean> 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
Expand All @@ -127,6 +125,14 @@ public void setupRepo()
this.context.load().json("/Questionnaires.json", TEST_QUESTIONNAIRE_PATH);
}

private Map<String, Boolean> createStatusFlagsMap()
{
Map<String, Boolean> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit 5b8049d

Please sign in to comment.