Skip to content
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
88 changes: 82 additions & 6 deletions modules/utils/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@
<packaging>bundle</packaging>
<name>CARDS - Utilities</name>

<properties>
<coverage.instructionRatio>0.04</coverage.instructionRatio>
</properties>

<build>
<resources>
<resource>
Expand All @@ -56,6 +52,15 @@
</instructions>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<!-- The default 500ms is not always enough when several Oak-backed test contexts start in parallel -->
<sling.mock.resourceresolverfactoryactivator.timeout.ms>10000</sling.mock.resourceresolverfactoryactivator.timeout.ms>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>

Expand Down Expand Up @@ -105,14 +110,85 @@
<artifactId>jakarta.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>io.uhndata.cards</groupId>
<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>org.apache.jackrabbit</groupId>
<artifactId>oak-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>cards-data-model-items-api</artifactId>
<version>${project.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>cards-data-model-links-api</artifactId>
<version>${project.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>cards-data-model-resources-api</artifactId>
<version>${project.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</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>runtime</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>4.0.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.testing.sling-mock.junit4</artifactId>
<version>4.0.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.testing.sling-mock-oak</artifactId>
<version>4.1.0-1.86.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.testing.jcr-mock</artifactId>
<version>1.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,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 @@ -89,7 +94,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
Expand Up @@ -80,12 +80,10 @@ public SelectorDetails(String name, String description, Boolean enabledByDefault
this.name = name;
this.description = description;
this.enabledByDefault = enabledByDefault;
if (options.length >= 2) {
int numOptions = Math.floorDiv(options.length, 2);
this.options = new SelectorOption[numOptions];
for (int i = 0; i < numOptions; i++) {
this.options[i] = new SelectorOption(options[2 * i], options[2 * i + 1]);
}
int numOptions = Math.floorDiv(options.length, 2);
this.options = new SelectorOption[numOptions];
for (int i = 0; i < numOptions; i++) {
this.options[i] = new SelectorOption(options[2 * i], options[2 * i + 1]);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,13 @@ public static ZonedDateTime parseDateTime(final String str)
if (result instanceof ZonedDateTime) {
// Good, we managed to parse the timezone from the string, just return the result
return (ZonedDateTime) result;
} else if (result instanceof LocalDateTime) {
// No timezone in the string, use the system default
return ((LocalDateTime) result).atZone(ZoneId.systemDefault());
}
// No timezone in the string, use the system default
return ((LocalDateTime) result).atZone(ZoneId.systemDefault());
} catch (Exception ex) {
// Not important, the date string doesn't match the expected format, just try the next format
return null;
}
return null;
}).filter(Objects::nonNull).findFirst().orElse(null);
return date;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* 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.SlingJakartaHttpServletResponse;
import org.junit.Before;
import org.junit.Test;

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

/**
* Unit tests for {@link ContentTypeSetter}.
*
* @version $Id$
*/
public class ContentTypeSetterTest
{
private final ContentTypeSetter contentTypeSetter = new ContentTypeSetter();

private final Bindings bindings = mock(Bindings.class);

private final SlingJakartaHttpServletResponse response = mock(SlingJakartaHttpServletResponse.class);

@Before
public void setUp()
{
when(this.bindings.get("jakartaResponse")).thenReturn(this.response);
this.contentTypeSetter.init(this.bindings);
}

@Test
public void initGetsJakartaResponseFromBindings()
{
verify(this.bindings).get("jakartaResponse");
}

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

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

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

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

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

@Test
public void markdownSetsMarkdownContentType()
{
this.contentTypeSetter.markdown();
verify(this.response).setContentType("text/markdown;charset=UTF-8");
}
}
Loading
Loading