Skip to content

Commit

Permalink
Testing improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
remmeier committed Jul 31, 2017
1 parent 9cdc7b0 commit 704f60e
Show file tree
Hide file tree
Showing 126 changed files with 2,336 additions and 2,846 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -17,4 +17,5 @@ env:
- secure: HDAaT4hx/r01MoihfDKUDbEQG1dfoGdhjxO5RBjE7871WRV4cv8rk7u22Le/H/PhCMHKDFjELnpz9kZKGwqjtHonVLC8smJNNkKUg6q9Uit6mwsqS6S+dPhB6V5hjqHskROHReHyfMM9if25L/p2mKWWhTQIdsdzDxJSHcDEKlIWWPlCBRWYimu3i4cn/EvwUAwSe2Zw5OleBzCbGtRAlAuXpHO+7yHJuCtNw1mClSgCTvSptef4FYVVXgbcPu4gvvX94lOgvHuZ0rgOlBCX+s8dBWVTD3x+J1VzCqoWjWsl21B5ADQHXRiQNkE4tnSKPhjwRo8rZyobFOMCxMz8Esvi3mXMVw+OAzIj2LnwIWNTd33mS2OF4sc4VFYGYpz1VdHnx7gPfUOnBhz8tCMThwQYYHpx0bTE8x0LdtRLV5zGKLs2XzR/CSqZuiI7PoOVXUvUjvYMG86OlYDxF3gtmOJybSI9qynU88kJAFpDFj/ldecAiLVYZqLd3qL1L0wa8oVHgogsLHUTGmud+P8URg6GYTIwbvomp8drpVzYyn5FNTSugBvZQxPzkCyCzXgFdQvrS9EyrbZibu44S8TjOjg507Zuo9+tjM6ouYOZsbkffvYle3WS/RjtDS3ROi0MTWVN4/7IZCG9I6R9UuKukEsPmJ/+YZcgS8HFa7YeCSI=

after_success:
- ./gradlew coveralls
- ./deploy.sh
88 changes: 83 additions & 5 deletions build.gradle
Expand Up @@ -13,6 +13,7 @@ buildscript {
classpath 'com.moowork.gradle:gradle-node-plugin:1.1.1'
classpath 'org.ajoberstar:gradle-git-publish:0.2.1'
classpath 'org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.5-rc1'
classpath "org.kt3k.gradle.plugin:coveralls-gradle-plugin:2.8.1"
}
}

Expand All @@ -30,8 +31,7 @@ ext {
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss")
if (ext.timestampedVersion) {
version = BUILD_VERSION_PREFIX + "." + format.format(new Date())
}
else {
} else {
version = '0.0.0-SNAPSHOT'
}
group = GROUP_ID
Expand All @@ -50,7 +50,7 @@ gradle.beforeProject { Project project ->

def docs = project.name == 'crnk-documentation'
def ui = project.name == 'crnk-ui'
def test = project.name == 'crnk-test'
def testProject = project.name == 'crnk-test'
def examples = project.name.contains('example')

if (!docs) {
Expand All @@ -63,9 +63,15 @@ gradle.beforeProject { Project project ->
testCompile group: 'org.mockito', name: 'mockito-core', version: '1.10.19'
testCompile group: 'org.assertj', name: 'assertj-core', version: '2.2.0'
}

test {
testLogging {
exceptionFormat = 'full'
}
}
}

if (!docs && !examples && !test) {
if (!docs && !examples && !testProject) {
// https://about.sonarqube.com/get-started/
apply plugin: "org.sonarqube"
apply plugin: "jacoco"
Expand All @@ -74,13 +80,39 @@ gradle.beforeProject { Project project ->
toolVersion = "0.7.6.201602180812"
}

rootProject.tasks.jacocoMerge.executionData tasks.withType(Test)
rootProject.tasks.jacocoRootReport.additionalSourceDirs files(sourceSets.main.allSource.srcDirs)

def sourceDirs = rootProject.tasks.jacocoRootReport.sourceDirectories
def classDirs = rootProject.tasks.jacocoRootReport.classDirectories

def mainOutput = files(files(sourceSets.main.output).collect {
fileTree(dir: it, exclude: '**/legacy/**')
})

if (sourceDirs == null) {
rootProject.tasks.jacocoRootReport.sourceDirectories = files(sourceSets.main.allSource.srcDirs)
rootProject.tasks.jacocoRootReport.classDirectories = mainOutput

} else {
rootProject.tasks.jacocoRootReport.sourceDirectories = sourceDirs.plus(files(sourceSets.main.allSource.srcDirs))
rootProject.tasks.jacocoRootReport.classDirectories = classDirs.plus(mainOutput)
}
rootProject.coveralls.sourceDirs.addAll(sourceSets.main.allSource.srcDirs.flatten())


sonarqube {
properties {
property 'sonar.coverage.exclusions', "**/legacy/**"
}
}

jacocoTestReport {
reports {
xml.enabled = true // coveralls plugin depends on xml format report
html.enabled = true
}
}
}

apply plugin: 'maven-publish'
Expand Down Expand Up @@ -171,7 +203,7 @@ gradle.beforeProject { Project project ->
}

tasks.publish.dependsOn(tasks.uploadArchives)
}else{
} else {
apply plugin: 'maven-publish'

publishing {
Expand All @@ -188,3 +220,49 @@ gradle.beforeProject { Project project ->
}
}

// coveralls setup
apply plugin: "jacoco"
apply plugin: "com.github.kt3k.coveralls"

def publishedProjects = subprojects.findAll {
it.name != 'crnk-documentation' && it.name != 'crnk-ui' && it.name != 'crnk-test' && !it.name.contains('example')
}

task jacocoMerge(type: JacocoMerge) {
destinationFile = new File(project.buildDir, 'jacoco/test.exec')
doFirst {
executionData = files(executionData.findAll { it.exists() })
}
for (publishedProject in publishedProjects) {
dependsOn publishedProject.path + ":check"
}
}


task jacocoRootReport(type: JacocoReport, group: 'Coverage reports') {
description = 'Generates an aggregate report from all subprojects'

dependsOn tasks.jacocoMerge

executionData tasks.jacocoMerge.destinationFile

reports {
html.enabled = true // human readable
xml.enabled = true // required by coveralls
}

}


coveralls {
jacocoReportPath = "${buildDir}/reports/jacoco/jacocoRootReport/jacocoRootReport.xml"
}

tasks.coveralls {
group = 'Coverage reports'
description = 'Uploads the aggregated coverage report to Coveralls'

dependsOn jacocoRootReport
}


Expand Up @@ -2,7 +2,9 @@

import io.crnk.cdi.internal.CdiServiceDiscovery;
import io.crnk.core.boot.CrnkBoot;
import io.crnk.core.engine.error.ExceptionMapper;
import io.crnk.core.engine.error.JsonApiExceptionMapper;
import io.crnk.core.module.Module;
import io.crnk.core.module.discovery.DefaultServiceDiscoveryFactory;
import io.crnk.core.module.discovery.ServiceDiscovery;
import io.crnk.core.repository.Repository;
Expand Down Expand Up @@ -44,6 +46,10 @@ public void testExceptionMapper() {
CrnkBoot boot = new CrnkBoot();
boot.boot();

CdiServiceDiscovery d = new CdiServiceDiscovery();
System.out.println(d.getInstancesByType(ExceptionMapper.class));
System.out.println(d.getInstancesByType(Module.class));

Optional<JsonApiExceptionMapper> mapper = boot.getExceptionMapperRegistry().findMapperFor(IllegalStateException.class);
Assert.assertTrue(mapper.isPresent());
Assert.assertTrue(mapper.get() instanceof CdiTestExceptionMapper);
Expand Down
4 changes: 2 additions & 2 deletions crnk-core/src/main/java/io/crnk/core/boot/CrnkBoot.java
Expand Up @@ -196,12 +196,12 @@ private void setupResourceRegistry() {
hierarchialPart.putPart(entry.getKey(), entry.getValue());
}
if (!registryParts.containsKey("")) {
moduleRegistry.getContext().addRegistryPart("", new DefaultResourceRegistryPart() );
moduleRegistry.getContext().addRegistryPart("", new DefaultResourceRegistryPart());
}
rootPart = hierarchialPart;
}

for(RegistryEntry entry : moduleRegistry.getRegistryEntries()){
for (RegistryEntry entry : moduleRegistry.getRegistryEntries()) {
rootPart.addEntry(entry);
}

Expand Down
@@ -1,18 +1,12 @@
package io.crnk.core.engine.error;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import io.crnk.core.engine.dispatcher.Response;
import io.crnk.core.engine.document.Document;
import io.crnk.core.engine.document.ErrorData;
import io.crnk.core.engine.internal.dispatcher.path.JsonPath;
import io.crnk.core.engine.query.QueryAdapter;
import io.crnk.core.repository.response.JsonApiResponse;

import java.util.*;

public final class ErrorResponse {

public static final String ERRORS = "errors";
Expand Down Expand Up @@ -46,14 +40,6 @@ public JsonApiResponse getResponse() {
.setEntity(data);
}

public JsonPath getJsonPath() {
return null;
}

public QueryAdapter getQueryAdapter() {
return null;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
Expand Up @@ -40,23 +40,15 @@ public abstract Response handle(JsonPath jsonPath, QueryAdapter queryAdapter, Re
parameterProvider, Document document);


protected void verifyTypes(HttpMethod methodType, String resourceEndpointName, RegistryEntry endpointRegistryEntry,
protected void verifyTypes(HttpMethod methodType, RegistryEntry endpointRegistryEntry,
RegistryEntry bodyRegistryEntry) {
if (endpointRegistryEntry.equals(bodyRegistryEntry)) {
return;
}
if (bodyRegistryEntry == null || !bodyRegistryEntry.isParent(endpointRegistryEntry)) {
String message = String.format("Inconsistent type definition between path and body: body type: " +
"%s, request type: %s", methodType, resourceEndpointName);
throw new RequestBodyException(methodType, resourceEndpointName, message);
}
}

protected Object extractResource(Object responseOrResource) {
if (responseOrResource instanceof JsonApiResponse) {
return ((JsonApiResponse) responseOrResource).getEntity();
} else {
return responseOrResource;
"%s, request type: %s", methodType, endpointRegistryEntry.getResourceInformation().getResourceType());
throw new RequestBodyException(methodType, endpointRegistryEntry.getResourceInformation().getResourceType(), message);
}
}
}
Expand Up @@ -43,9 +43,7 @@ public Response handle(JsonPath jsonPath, QueryAdapter queryAdapter, RepositoryM
RegistryEntry registryEntry = resourceRegistry.getEntry(resourceName);
Serializable castedResourceId = getResourceId(resourceIds, registryEntry);
ResourceField relationshipField = registryEntry.getResourceInformation().findRelationshipFieldByName(elementName);
if (relationshipField == null) {
throw new ResourceFieldNotFoundException(elementName);
}
verifyFieldNotNull(relationshipField, elementName);

// TODO remove Class usage and replace by resourceId
Class<?> baseRelationshipFieldClass = relationshipField.getType();
Expand Down
@@ -1,8 +1,5 @@
package io.crnk.core.engine.internal.dispatcher.controller;

import java.io.Serializable;
import java.util.Collections;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.crnk.core.engine.dispatcher.Response;
import io.crnk.core.engine.document.Document;
Expand All @@ -17,16 +14,18 @@
import io.crnk.core.engine.internal.document.mapper.DocumentMapper;
import io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter;
import io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter;
import io.crnk.core.engine.internal.utils.ClassUtils;
import io.crnk.core.engine.internal.utils.PreconditionUtil;
import io.crnk.core.engine.parser.TypeParser;
import io.crnk.core.engine.properties.PropertiesProvider;
import io.crnk.core.engine.query.QueryAdapter;
import io.crnk.core.engine.registry.RegistryEntry;
import io.crnk.core.engine.registry.ResourceRegistry;
import io.crnk.core.exception.ResourceFieldNotFoundException;
import io.crnk.core.repository.response.JsonApiResponse;
import io.crnk.legacy.internal.RepositoryMethodParameterProvider;

import java.io.Serializable;
import java.util.Collections;

/**
* Creates a new post in a similar manner as in {@link ResourcePost}, but additionally adds a relation to a field.
*/
Expand All @@ -39,9 +38,7 @@ public FieldResourcePost(ResourceRegistry resourceRegistry, PropertiesProvider p

@Override
public boolean isAcceptable(JsonPath jsonPath, String requestType) {
if (jsonPath == null) {
throw new IllegalArgumentException();
}
PreconditionUtil.assertNotNull("path cannot be null", jsonPath);
return !jsonPath.isCollection()
&& FieldPath.class.equals(jsonPath.getClass())
&& HttpMethod.POST.name()
Expand All @@ -61,15 +58,15 @@ public Response handle(JsonPath jsonPath, QueryAdapter queryAdapter,
Serializable castedResourceId = getResourceId(resourceIds, endpointRegistryEntry);
ResourceField relationshipField = endpointRegistryEntry.getResourceInformation()
.findRelationshipFieldByName(jsonPath.getElementName());
if (relationshipField == null) {
throw new ResourceFieldNotFoundException(jsonPath.getElementName());
}
verifyFieldNotNull(relationshipField, jsonPath.getElementName());

Class<?> baseRelationshipFieldClass = relationshipField.getType();

RegistryEntry relationshipRegistryEntry = resourceRegistry.getEntry(relationshipField.getOppositeResourceType());
String relationshipResourceType = relationshipField.getOppositeResourceType();

verifyTypes(HttpMethod.POST, relationshipRegistryEntry, bodyRegistryEntry);

Object newResource = buildNewResource(relationshipRegistryEntry, resourceBody, relationshipResourceType);
setAttributes(resourceBody, newResource, relationshipRegistryEntry.getResourceInformation());
ResourceRepositoryAdapter resourceRepository = relationshipRegistryEntry.getResourceRepository(parameterProvider);
Expand Down
Expand Up @@ -40,9 +40,7 @@ public Response handle(JsonPath jsonPath, QueryAdapter queryAdapter, RepositoryM
Serializable castedResourceId = getResourceId(resourceIds, registryEntry);
String elementName = jsonPath.getElementName();
ResourceField relationshipField = registryEntry.getResourceInformation().findRelationshipFieldByName(elementName);
if (relationshipField == null) {
throw new ResourceFieldNotFoundException(elementName);
}
verifyFieldNotNull(relationshipField, elementName);

boolean isCollection = Iterable.class.isAssignableFrom(relationshipField.getType());

Expand Down
@@ -1,8 +1,5 @@
package io.crnk.core.engine.internal.dispatcher.controller;

import java.io.Serializable;
import java.util.Collections;

import io.crnk.core.engine.dispatcher.Response;
import io.crnk.core.engine.document.Document;
import io.crnk.core.engine.document.ResourceIdentifier;
Expand All @@ -16,7 +13,6 @@
import io.crnk.core.engine.internal.document.mapper.DocumentMapper;
import io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter;
import io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter;
import io.crnk.core.engine.internal.utils.ClassUtils;
import io.crnk.core.engine.internal.utils.PreconditionUtil;
import io.crnk.core.engine.parser.TypeParser;
import io.crnk.core.engine.query.QueryAdapter;
Expand All @@ -26,6 +22,9 @@
import io.crnk.core.exception.ResourceFieldNotFoundException;
import io.crnk.legacy.internal.RepositoryMethodParameterProvider;

import java.io.Serializable;
import java.util.Collections;

public abstract class RelationshipsResourceUpsert extends ResourceIncludeField {

RelationshipsResourceUpsert(ResourceRegistry resourceRegistry, TypeParser typeParser, DocumentMapper documentMapper) {
Expand Down Expand Up @@ -87,12 +86,10 @@ public final Response handle(JsonPath jsonPath, QueryAdapter queryAdapter,
Serializable castedResourceId = getResourceId(resourceIds, registryEntry);
ResourceField relationshipField = registryEntry.getResourceInformation().findRelationshipFieldByName(jsonPath
.getElementName());
if (relationshipField == null) {
throw new ResourceFieldNotFoundException(jsonPath.getElementName());
}
verifyFieldNotNull(relationshipField, jsonPath.getElementName());
ResourceRepositoryAdapter resourceRepository = registryEntry.getResourceRepository(parameterProvider);
@SuppressWarnings("unchecked")
Object resource = extractResource(resourceRepository.findOne(castedResourceId, queryAdapter));
Object resource = resourceRepository.findOne(castedResourceId, queryAdapter).getEntity();

ResourceInformation targetInformation = getRegistryEntry(relationshipField.getOppositeResourceType()).getResourceInformation();

Expand All @@ -116,6 +113,8 @@ public final Response handle(JsonPath jsonPath, QueryAdapter queryAdapter,
return new Response(new Document(), HttpStatus.NO_CONTENT_204);
}



private Serializable getResourceId(PathIds resourceIds, RegistryEntry registryEntry) {
String resourceId = resourceIds.getIds().get(0);
@SuppressWarnings("unchecked") Class<? extends Serializable> idClass = (Class<? extends Serializable>) registryEntry
Expand Down

0 comments on commit 704f60e

Please sign in to comment.