Skip to content

Commit

Permalink
More testing and code refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
codegeek committed Apr 8, 2024
1 parent da36435 commit 65ee3ac
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 103 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/codacy-coverage-reporter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Codacy Coverage Reporter

on:
push:
branches:
- main

jobs:
codacy-coverage-reporter:
runs-on: ubuntu-latest
name: codacy-coverage-reporter
steps:
- uses: actions/checkout@v4
- name: Run codacy-coverage-reporter
uses: codacy/codacy-coverage-reporter-action@v1
with:
project-token: ${{ secrets.CODACY_PROJECT_TOKEN }}
# or
# api-token: ${{ secrets.CODACY_API_TOKEN }}
coverage-reports: target/site/jacoco/jacoco.xml
# or a comma-separated list for multiple reports
# coverage-reports: <PATH_TO_REPORT>, <PATH_TO_REPORT>
10 changes: 5 additions & 5 deletions .github/workflows/java-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ jobs:
name: Build and analyze
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: Set up JDK 17
uses: actions/setup-java@v3
uses: actions/setup-java@v4
with:
java-version: 17
distribution: 'zulu' # Alternative distribution options are available.
- name: Cache SonarCloud packages
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ~/.sonar/cache
key: ${{ runner.os }}-sonar
restore-keys: ${{ runner.os }}-sonar
- name: Cache Maven packages
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
Expand All @@ -34,4 +34,4 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
run: mvn -B -Pcoverage verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=jfed-dev_jfed-activitystreams
run: mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=jfed-dev_jfed-activitystreams
65 changes: 30 additions & 35 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
<dependency>
<groupId>com.apicatalog</groupId>
<artifactId>titanium-json-ld</artifactId>
<version>1.3.1</version>
<version>1.4.0</version>
</dependency>

<!-- Test dependencies -->
Expand Down Expand Up @@ -114,37 +114,32 @@
</dependency>
</dependencies>

<profiles>
<profile>
<id>coverage</id>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.12</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<goals>
<goal>report</goal>
</goals>
<configuration>
<formats>
<format>XML</format>
</formats>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.12</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<goals>
<goal>report</goal>
</goals>
<configuration>
<formats>
<format>XML</format>
</formats>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
11 changes: 8 additions & 3 deletions src/main/java/dev/jfed/activitystreams/NaturalValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@
* @see <a href="https://www.w3.org/TR/activitystreams-core/#naturalLanguageValues">AS2#naturalLanguageValues</a>
*/
public class NaturalValue {
public static final String UNDEFINED = "und";
private final Locale locale;
private final Map<Locale, String> valueMap;

public static NaturalValueBuilder builder() {
return new NaturalValueBuilder(Locale.getDefault());
return new NaturalValueBuilder(Locale.ROOT);
}

public static NaturalValueBuilder builder(Locale locale) {
Expand Down Expand Up @@ -72,7 +73,7 @@ public String getValue() {
}

public String getValue(String language) {
return getValue(Locale.forLanguageTag(language));
return getValue(UNDEFINED.equalsIgnoreCase(language) ? Locale.ROOT : Locale.forLanguageTag(language));
}

public String getValue(Locale locale) {
Expand All @@ -84,13 +85,17 @@ public void setValue(String value) {
}

public void setValue(String language, String text) {
setValue(Locale.forLanguageTag(language), text);
setValue(UNDEFINED.equalsIgnoreCase(language) ? Locale.ROOT : Locale.forLanguageTag(language), text);
}

public void setValue(Locale locale, String text) {
valueMap.put(locale, text);
}

public Locale getLocale() {
return locale;
}

public boolean hasValueForLanguage(Locale locale) {
return valueMap.containsKey(locale);
}
Expand Down
20 changes: 0 additions & 20 deletions src/main/java/dev/jfed/activitystreams/ObjectParser.java

This file was deleted.

21 changes: 14 additions & 7 deletions src/main/java/dev/jfed/activitystreams/core/Link.java
Original file line number Diff line number Diff line change
Expand Up @@ -189,28 +189,35 @@ public static Optional<Link> fromJsonObject(final JsonObject jsonObject) {
}

private static void processEntry(final LinkBuilder builder, final Map.Entry<String, JsonValue> property) {
String value = ((JsonString)property.getValue()).getString();
var value = property.getValue();
switch(property.getKey()) {
case Keywords.CONTEXT, Keywords.TYPE, ASProperties.TYPE, ASProperties.HREF:
// ignore
break;
case ASProperties.NAME:
builder.name(NaturalValue.builder().withValue(value).build());
final var name = ((JsonString)value).getString();
builder.name(NaturalValue.builder().withValue(name).build());
break;
case ASProperties.NAME_MAP:
final var valueBuilder = NaturalValue.builder();
property.getValue().asJsonObject()
.forEach((k, v) -> valueBuilder.withValue(k, ((JsonString)v).getString()));
builder.name(valueBuilder.build());
break;
case ASProperties.REL:
builder.rel(value);
builder.rel(((JsonString)value).getString());
break;
case ASProperties.MEDIA_TYPE:
builder.mediaType(value);
builder.mediaType(((JsonString)value).getString());
break;
case ASProperties.HREFLANG:
builder.hreflang(value);
builder.hreflang(((JsonString)value).getString());
break;
case ASProperties.HEIGHT:
builder.height(Integer.parseInt(value));
builder.height(Integer.parseInt(((JsonString)value).getString()));
break;
case ASProperties.WIDTH:
builder.width(Integer.parseInt(value));
builder.width(Integer.parseInt(((JsonString)value).getString()));
break;
default:
log.atWarn().setMessage("Property not found: key={}, value={}").addArgument(property.getKey()).addArgument(property.getValue()).log();
Expand Down
46 changes: 46 additions & 0 deletions src/test/java/dev/jfed/activitystreams/JsonTestUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2022-2024 Guillermo Castro
*
* Licensed 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 dev.jfed.activitystreams;

import com.apicatalog.jsonld.JsonLd;
import com.apicatalog.jsonld.JsonLdError;
import com.apicatalog.jsonld.document.JsonDocument;
import com.apicatalog.jsonld.http.media.MediaType;
import jakarta.json.JsonObject;

import java.net.URI;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
* @author Guillermo Castro
* @since 0.0.1
*/
public class JsonTestUtil {
private JsonTestUtil() {}

public static JsonObject getJsonObject(String name) throws JsonLdError {
var document = JsonDocument.of(MediaType.of("application", "activity+json"), JsonTestUtil.class.getClassLoader().getResourceAsStream(name));
assertNotNull(document);
assertFalse(document.getJsonContent().isEmpty());
var object = JsonLd.compact(document, URI.create(ASType.CONTEXT_VALUE)).get();
assertNotNull(object);
return object;
}

}
59 changes: 48 additions & 11 deletions src/test/java/dev/jfed/activitystreams/core/ASObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

package dev.jfed.activitystreams.core;

import com.apicatalog.jsonld.document.JsonDocument;
import com.apicatalog.jsonld.JsonLdError;
import dev.jfed.activitystreams.JsonTestUtil;
import dev.jfed.activitystreams.NaturalValue;
import jakarta.json.JsonObject;
import org.json.JSONException;
import org.junit.jupiter.api.Test;
import org.skyscreamer.jsonassert.JSONAssert;
Expand All @@ -39,6 +39,7 @@ class ASObjectTest {

private static final String TEST_ID = "https://test.example.com/object/1";
private static final String TEST_NAME = "Test Object";
private static final String TEST_NAME_ES = "Objeto de Prueba";

@Test
void testMinimal() throws JSONException {
Expand All @@ -59,24 +60,34 @@ void testAllProperties() throws Exception {
var result = obj.toJson();

assertNotNull(result);
log.atInfo().setMessage("test json: {}").addArgument(result).log();

JSONAssert.assertEquals("{'@context': 'https://www.w3.org/ns/activitystreams'}", result, false);
JSONAssert.assertEquals("{'type': 'Object'}", result, false);
JSONAssert.assertEquals("{'id': '" + TEST_ID + "'}", result, false);
JSONAssert.assertEquals("{'name': '" + TEST_NAME + "'}", result, false);
}

@Test
void testWithNameMap() throws Exception {
var result = ASObject.builder()
.withId(URI.create(TEST_ID))
.withName(NaturalValue.builder()
.withValue("en", TEST_NAME)
.withValue("es", TEST_NAME_ES)
.build())
.build().toJson();

assertNotNull(result);
log.atInfo().setMessage("test json: {}").addArgument(result).log();

JSONAssert.assertEquals("{'id': '" + TEST_ID + "'}", result, false);
JSONAssert.assertEquals("{'nameMap': {'en': '" + TEST_NAME + "', 'es': '" + TEST_NAME_ES + "'}}", result, false);
}

@Test
void testFromJson() throws Exception {
JsonDocument document = JsonDocument.of(getClass().getClassLoader().getResourceAsStream("test/core-ex8-jsonld.json"));
assertNotNull(document);
assertFalse(document.getJsonContent().isEmpty());
JsonObject object = document.getJsonContent().get().asJsonObject();
assertNotNull(object);
final var result = ASObject.fromJsonObject(object);
assertFalse(result.isEmpty());
var testObject = result.get();
void testNameMap() throws Exception {
var testObject = getAsObject("test/core-ex8-jsonld.json");

assertTrue(testObject.getName().hasMultipleLanguages());
assertEquals("This is the title", testObject.getName().getValue("en"));
Expand All @@ -86,4 +97,30 @@ void testFromJson() throws Exception {
log.atInfo().setMessage("test Object: {}").addArgument(testObject).log();
}

@Test
void testUndefinedNameMap() throws Exception {
var testObject = getAsObject("test/core-ex11b-jsonld.json");

assertFalse(testObject.getName().hasMultipleLanguages());
assertEquals("This is the title", testObject.getName().getValue(NaturalValue.UNDEFINED));
assertEquals("This is the title", testObject.getName().getValue());
}

@Test
void testLanguageInContext() throws Exception {
var testObject = getAsObject("test/core-ex11c-jsonld.json");

assertFalse(testObject.getName().hasMultipleLanguages());
assertEquals("This is the title", testObject.getName().getValue("en"));
}

private ASObject getAsObject(String name) throws JsonLdError {
var jsonObject = JsonTestUtil.getJsonObject(name);
assertNotNull(jsonObject);
assertFalse(jsonObject.isEmpty());
var result = ASObject.fromJsonObject(jsonObject);
assertNotNull(result);
assertFalse(result.isEmpty());
return result.get();
}
}
Loading

0 comments on commit 65ee3ac

Please sign in to comment.