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

CARDS-2175: form-completion-status tests #1453

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions modules/form-completion-status/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
<packaging>bundle</packaging>
<name>Flags form answers as incomplete or invalid</name>

<properties>
<coverage.instructionRatio>0.88</coverage.instructionRatio>
</properties>

<build>
<plugins>
<!-- This is an OSGi bundle -->
Expand All @@ -37,6 +41,17 @@
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgs>
<arg>-Werror</arg>
</compilerArgs>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
<failOnWarning>true</failOnWarning>
</configuration>
</plugin>
</plugins>
</build>

Expand Down Expand Up @@ -79,5 +94,67 @@
<artifactId>cards-data-model-forms-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.uhndata.cards</groupId>
<artifactId>cards-data-model-subjects-api</artifactId>
<version>0.9-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.resourcebuilder</artifactId>
<version>1.0.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.testing.sling-mock.core</artifactId>
<version>3.4.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.jcr.resource</artifactId>
<version>3.0.18</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.testing.sling-mock.junit4</artifactId>
<version>3.4.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.testing.sling-mock-oak</artifactId>
<version>3.1.4-1.40.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.testing.jcr-mock</artifactId>
<version>1.5.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.24.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void validate(final NodeBuilder answer, final Node question, final boolea

// Ranges are stored as pairs of values, so the true number of values is half of the number of actual values
final long valuesCount = getNumberOfValues(answer) / 2;
checkNumberOfValues(valuesCount, question, initialAnswer, flags);
checkNumberOfValues(valuesCount, question, flags);
} catch (final RepositoryException ex) {
// If something goes wrong then we cannot verify the answer, leave it as it was before
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ public void validate(final NodeBuilder answer, final Node question, final boolea
{
try {
final long valuesCount = getNumberOfValues(answer);
checkNumberOfValues(valuesCount, question, initialAnswer, flags);
checkNumberOfValues(valuesCount, question, flags);
} catch (final RepositoryException ex) {
// If something goes wrong then we cannot verify the answer, leave it as it was before
}
}

protected void checkNumberOfValues(final long valuesCount, final Node question, final boolean initialAnswer,
final Map<String, Boolean> flags) throws RepositoryException
protected void checkNumberOfValues(final long valuesCount, final Node question, final Map<String, Boolean> flags)
throws RepositoryException
{
final long minAnswers =
question.hasProperty("minAnswers") ? question.getProperty("minAnswers").getLong() : 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* 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;

import java.util.List;

import javax.jcr.Session;

import org.apache.jackrabbit.oak.api.CommitFailedException;
import org.apache.jackrabbit.oak.spi.commit.CommitInfo;
import org.apache.jackrabbit.oak.spi.commit.Editor;
import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
import org.apache.jackrabbit.oak.spi.state.NodeState;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.testing.mock.sling.ResourceResolverType;
import org.apache.sling.testing.mock.sling.junit.SlingContext;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.internal.util.reflection.Whitebox;
import org.mockito.runners.MockitoJUnitRunner;

import io.uhndata.cards.forms.api.FormUtils;
import io.uhndata.cards.resolverProvider.ThreadResourceResolverProvider;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
* Unit tests for {@link AnswerCompletionStatusEditorProvider}.
*
* @version $Id$
*/
@RunWith(MockitoJUnitRunner.class)
public class AnswerCompletionStatusEditorProviderTest
{
@Rule
public SlingContext context = new SlingContext(ResourceResolverType.JCR_OAK);

@InjectMocks
private AnswerCompletionStatusEditorProvider answerCompletionStatusEditorProvider;

@Mock
private ThreadResourceResolverProvider rrp;

@Mock
private FormUtils formUtils;

@Test
public void getRootEditorReturnsAnswerCompletionStatusEditor() throws CommitFailedException
{
final ResourceResolver resourceResolver = this.context.resourceResolver();
final Session session = resourceResolver.adaptTo(Session.class);

NodeBuilder currentNodeBuilder = mock(NodeBuilder.class);
when(this.formUtils.isForm(currentNodeBuilder)).thenReturn(true);
when(this.rrp.getThreadResourceResolver()).thenReturn(resourceResolver);
Whitebox.setInternalState(this.answerCompletionStatusEditorProvider, "allValidators", List.of());

Editor editor = this.answerCompletionStatusEditorProvider.getRootEditor(mock(NodeState.class),
mock(NodeState.class), currentNodeBuilder, new CommitInfo(session.toString(), session.getUserID()));
assertNotNull(editor);
assertTrue(editor instanceof AnswerCompletionStatusEditor);
}

@Test
public void getRootEditorForNullResourceResolverReturnsNull() throws CommitFailedException
{
final ResourceResolver resourceResolver = this.context.resourceResolver();
final Session session = resourceResolver.adaptTo(Session.class);
when(this.rrp.getThreadResourceResolver()).thenReturn(null);

Editor editor = this.answerCompletionStatusEditorProvider.getRootEditor(mock(NodeState.class),
mock(NodeState.class), mock(NodeBuilder.class), new CommitInfo(session.toString(),
session.getUserID()));
assertNull(editor);
}

}
Loading