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-2154: cards-utils tests #1418

Draft
wants to merge 17 commits into
base: dev
Choose a base branch
from
Draft
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
93 changes: 93 additions & 0 deletions modules/utils/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
<packaging>bundle</packaging>
<name>CARDS - Utilities</name>

<properties>
<coverage.instructionRatio>0.97</coverage.instructionRatio>
</properties>

<build>
<resources>
<resource>
Expand All @@ -49,6 +53,17 @@
</instructions>
</configuration>
</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 @@ -89,10 +104,88 @@
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.jackrabbit</groupId>
<artifactId>oak-jackrabbit-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.jackrabbit</groupId>
<artifactId>oak-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.uhndata.cards</groupId>
<artifactId>cards-data-model-forms-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>cards-data-model-subjects-api</artifactId>
<version>${project.version}</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>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</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 @@ -80,6 +80,11 @@ public <A> A getAdapter(final Object adaptable, final Class<A> type)
return null;
}
final Resource resource = (Resource) adaptable;
final Node node = resource.adaptTo(Node.class);
if (node == null) {
return null;
}

// The list of processors that are enabled for the current resource serialization.
List<ResourceJsonProcessor> enabledProcessors = setupProcessors(resource);

Expand All @@ -88,7 +93,6 @@ public <A> A getAdapter(final Object adaptable, final Class<A> type)
Stack<String> processedNodes = new Stack<>();

start(resource, enabledProcessors);
final Node node = resource.adaptTo(Node.class);
JsonValue result = serializeNode(node, enabledProcessors, processedNodes);
end(resource, enabledProcessors);
if (result != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* 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.scripting;

import javax.script.Bindings;

import org.apache.sling.api.SlingHttpServletResponse;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

/**
* Unit tests for {@link ContentTypeSetter}.
*
* @version $Id$
*/
@RunWith(MockitoJUnitRunner.class)
public class ContentTypeSetterTest
{
@InjectMocks
private ContentTypeSetter contentTypeSetter;

@Mock
private SlingHttpServletResponse response;

@Test
public void initGetsResponseKeyFromBindings()
{
Bindings bindings = mock(Bindings.class);
this.contentTypeSetter.init(bindings);
verify(bindings, times(1)).get("response");
}

@Test
public void htmlSetsHtmlContentType()
{
this.contentTypeSetter.html();
verify(this.response, times(1)).setContentType("text/html;charset=UTF-8");
}

@Test
public void javascriptSetsJavascriptContentType()
{
this.contentTypeSetter.javascript();
verify(this.response, times(1)).setContentType("application/javascript;charset=UTF-8");
}

@Test
public void jsonSetsJsonContentType()
{
this.contentTypeSetter.json();
verify(this.response, times(1)).setContentType("application/json;charset=UTF-8");
}

@Test
public void csvSetsCsvContentType()
{
this.contentTypeSetter.csv();
verify(this.response, times(1)).setContentType("text/csv;charset=UTF-8");
}

@Test
public void textSetsPlainContentType()
{
this.contentTypeSetter.text();
verify(this.response, times(1)).setContentType("text/plain;charset=UTF-8");
}

@Test
public void markdownSetsMarkdownContentType()
{
this.contentTypeSetter.markdown();
verify(this.response, times(1)).setContentType("text/markdown;charset=UTF-8");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
* 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.scripting;

import javax.script.Bindings;
import javax.servlet.http.HttpServletResponse;

import org.apache.sling.api.SlingHttpServletResponse;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

/**
* Unit tests for {@link StatusCodeSetter}.
*
* @version $Id$
*/
@RunWith(MockitoJUnitRunner.class)
public class StatusCodeSetterTest
{
private static final int LOCKED = 423;
@InjectMocks
private StatusCodeSetter statusCodeSetter;

@Mock
private SlingHttpServletResponse response;

@Test
public void initGetsResponseKeyFromBindings()
{
Bindings bindings = mock(Bindings.class);
this.statusCodeSetter.init(bindings);
verify(bindings, times(1)).get("response");
}

@Test
public void okSetsOkStatus()
{
this.statusCodeSetter.ok();
verify(this.response, times(1)).setStatus(HttpServletResponse.SC_OK);
}

@Test
public void createdSetsCreatedStatus()
{
this.statusCodeSetter.created();
verify(this.response, times(1)).setStatus(HttpServletResponse.SC_CREATED);
}

@Test
public void acceptedSetsAcceptedStatus()
{
this.statusCodeSetter.accepted();
verify(this.response, times(1)).setStatus(HttpServletResponse.SC_ACCEPTED);
}

@Test
public void noContentNoContentSetsStatus()
{
this.statusCodeSetter.noContent();
verify(this.response, times(1)).setStatus(HttpServletResponse.SC_NO_CONTENT);
}

@Test
public void badRequestSetsBadRequestStatus()
{
this.statusCodeSetter.badRequest();
verify(this.response, times(1)).setStatus(HttpServletResponse.SC_BAD_REQUEST);
}

@Test
public void unauthorizedSetsUnauthorizedStatus()
{
this.statusCodeSetter.unauthorized();
verify(this.response, times(1)).setStatus(HttpServletResponse.SC_UNAUTHORIZED);
}

@Test
public void forbiddenSetsForbiddenStatus()
{
this.statusCodeSetter.forbidden();
verify(this.response, times(1)).setStatus(HttpServletResponse.SC_FORBIDDEN);
}

@Test
public void notFoundSetsNotFoundStatus()
{
this.statusCodeSetter.notFound();
verify(this.response, times(1)).setStatus(HttpServletResponse.SC_NOT_FOUND);
}

@Test
public void methodNotAllowedSetsMethodNotAllowedStatus()
{
this.statusCodeSetter.methodNotAllowed();
verify(this.response, times(1)).setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
}

@Test
public void notAcceptableSetsNotAcceptableStatus()
{
this.statusCodeSetter.notAcceptable();
verify(this.response, times(1)).setStatus(HttpServletResponse.SC_NOT_ACCEPTABLE);
}

@Test
public void conflictSetsConflictStatus()
{
this.statusCodeSetter.conflict();
verify(this.response, times(1)).setStatus(HttpServletResponse.SC_CONFLICT);
}

@Test
public void lockedSetsLockedStatus()
{
this.statusCodeSetter.locked();
verify(this.response, times(1)).setStatus(LOCKED);
}

@Test
public void internalServerErrorSetsInternalServerErrorStatus()
{
this.statusCodeSetter.internalServerError();
verify(this.response, times(1)).setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}

@Test
public void notImplementedSetsNotImplementedStatus()
{
this.statusCodeSetter.notImplemented();
verify(this.response, times(1)).setStatus(HttpServletResponse.SC_NOT_IMPLEMENTED);
}

}
Loading