Skip to content

Commit

Permalink
#212 Update examples (#213)
Browse files Browse the repository at this point in the history
first iteration of example updates

- switched from QueryParams to QuerySpec
- made use of QuerySpecResourceRepositoryBase for interface-based repos
- Spring now has both interface and annotation-based repos
- wildfly example switched to CDI-based service discover with katharsis-cdi
- new Spring Config KatharsisConfigV3 with QuerySpec setup and ApplicationContext-based lookup of repositories and modules. 

More advanced use cases have to be added in a next iteration
(katharsis-client/jpa/validation/...).
  • Loading branch information
Remo authored Nov 15, 2016
1 parent 348949e commit e5bf6f8
Show file tree
Hide file tree
Showing 37 changed files with 568 additions and 562 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ public class KatharsisBoot {

private ServiceDiscoveryFactory serviceDiscoveryFactory = new DefaultServiceDiscoveryFactory();

private ServiceDiscovery serviceDiscovery;

private ExceptionMapperRegistry exceptionMapperRegistry;

public void setObjectMapper(ObjectMapper objectMapper) {
PreconditionUtil.assertNull("ObjectMapper already set", this.objectMapper);
this.objectMapper = objectMapper;
Expand All @@ -86,6 +90,11 @@ public void setServiceDiscoveryFactory(ServiceDiscoveryFactory factory) {
this.serviceDiscoveryFactory = factory;
}

public void setServiceDiscovery(ServiceDiscovery serviceDiscovery) {
this.serviceDiscovery = serviceDiscovery;
moduleRegistry.setServiceDiscovery(serviceDiscovery);
}

public void setQueryParamsBuilds(QueryParamsBuilder queryParamsBuilder) {
this.queryParamsBuilder = queryParamsBuilder;
this.querySpecDeserializer = null;
Expand Down Expand Up @@ -142,10 +151,12 @@ public void boot() {
}

private void setupServiceDiscovery() {
// revert to reflection-based approach if no ServiceDiscovery is found
FallbackServiceDiscoveryFactory fallback = new FallbackServiceDiscoveryFactory(serviceDiscoveryFactory, serviceLocator,
propertiesProvider);
moduleRegistry.setServiceDiscovery(fallback.getInstance());
if (serviceDiscovery == null) {
// revert to reflection-based approach if no ServiceDiscovery is found
FallbackServiceDiscoveryFactory fallback = new FallbackServiceDiscoveryFactory(serviceDiscoveryFactory,
serviceLocator, propertiesProvider);
setServiceDiscovery(fallback.getInstance());
}
}

private void bootDiscovery() {
Expand All @@ -158,12 +169,16 @@ private void bootDiscovery() {
JsonApiModuleBuilder jsonApiModuleBuilder = new JsonApiModuleBuilder();
objectMapper.registerModule(jsonApiModuleBuilder.build(resourceRegistry, false));

ExceptionMapperRegistry exceptionMapperRegistry = buildExceptionMapperRegistry();
exceptionMapperRegistry = buildExceptionMapperRegistry();

requestDispatcher = createRequestDispatcher(exceptionMapperRegistry);

}

public ExceptionMapperRegistry getExceptionMapperRegistry() {
return exceptionMapperRegistry;
}

private RequestDispatcher createRequestDispatcher(ExceptionMapperRegistry exceptionMapperRegistry) {
TypeParser typeParser = new TypeParser();
ControllerRegistryBuilder controllerRegistryBuilder = new ControllerRegistryBuilder(resourceRegistry, typeParser,
Expand All @@ -188,7 +203,9 @@ private ExceptionMapperRegistry buildExceptionMapperRegistry() {
}

private void setupComponents() {
ServiceDiscovery serviceDiscovery = moduleRegistry.getServiceDiscovery();
if (resourceFieldNameTransformer == null) {
resourceFieldNameTransformer = new ResourceFieldNameTransformer(objectMapper.getSerializationConfig());
}

// not that the provided default implementation here are added last and as a consequence,
// can be overriden by other modules, like the JaxrsResourceRepositoryInformationBuilder.
Expand Down Expand Up @@ -319,4 +336,8 @@ public void setDefaultPageLimit(Long defaultPageLimit) {
public ModuleRegistry getModuleRegistry() {
return moduleRegistry;
}

public QuerySpecDeserializer getQuerySpecDeserializer() {
return querySpecDeserializer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import io.katharsis.resource.registry.RegistryEntry;
import io.katharsis.resource.registry.ResourceLookup;
import io.katharsis.resource.registry.ResourceRegistry;
import io.katharsis.resource.registry.ResourceRegistryAware;
import io.katharsis.resource.registry.repository.AnnotatedRelationshipEntryBuilder;
import io.katharsis.resource.registry.repository.AnnotatedResourceEntry;
import io.katharsis.resource.registry.repository.DirectResponseRelationshipEntry;
Expand Down Expand Up @@ -401,6 +402,9 @@ public ResourceInformationBuilder getResourceInformationBuilder() {
else {
relationshipRepositories.put((RelationshipRepositoryInformation) repositoryInformation, repository);
}
if(repository instanceof ResourceRegistryAware){
((ResourceRegistryAware)repository).setResourceRegistry(resourceRegistry);
}
}

for (Map.Entry<ResourceRepositoryInformation, Object> entry: resourceRepositories.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Arrays;
import java.util.Iterator;

import io.katharsis.resource.exception.ResourceNotFoundException;
import io.katharsis.resource.registry.RegistryEntry;
import io.katharsis.resource.registry.ResourceRegistry;
import io.katharsis.resource.registry.ResourceRegistryAware;
Expand Down Expand Up @@ -55,7 +56,7 @@ public T findOne(I id, QuerySpec querySpec) {
return resource;
}
else {
return null;
throw new ResourceNotFoundException("resource not found");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;

import com.fasterxml.jackson.databind.ObjectMapper;

Expand All @@ -14,6 +15,7 @@
import io.katharsis.module.ServiceDiscovery;
import io.katharsis.module.SimpleModule;
import io.katharsis.queryParams.QueryParams;
import io.katharsis.queryspec.QuerySpecDeserializer;
import io.katharsis.queryspec.internal.QueryParamsAdapter;
import io.katharsis.resource.field.ResourceFieldNameTransformer;
import io.katharsis.resource.mock.models.Task;
Expand All @@ -26,7 +28,31 @@
public class KatharsisBootTest {

@Test
public void test() {
public void setObjectMapper() {
KatharsisBoot boot = new KatharsisBoot();
ObjectMapper mapper = new ObjectMapper();
boot.setObjectMapper(mapper);
Assert.assertSame(mapper, boot.getObjectMapper());
}

@Test
public void setServiceDiscovery() {
KatharsisBoot boot = new KatharsisBoot();
ServiceDiscovery serviceDiscovery = Mockito.mock(ServiceDiscovery.class);
boot.setServiceDiscovery(serviceDiscovery);
Assert.assertSame(serviceDiscovery, boot.getServiceDiscovery());
}

@Test
public void setQuerySpecDeserializer() {
KatharsisBoot boot = new KatharsisBoot();
QuerySpecDeserializer deserializer = Mockito.mock(QuerySpecDeserializer.class);
boot.setQuerySpecDeserializer(deserializer);
Assert.assertSame(deserializer, boot.getQuerySpecDeserializer());
}

@Test
public void boot() {
KatharsisBoot boot = new KatharsisBoot();
ObjectMapper objectMapper = boot.getObjectMapper();
ResourceFieldNameTransformer resourceFieldNameTransformer = new ResourceFieldNameTransformer(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ private void checkCrud(QueryAdapter queryAdapter) {
taskAdapter.delete(task.getId(), queryAdapter);
tasks = (List<Task>) taskAdapter.findAll(queryAdapter).getEntity();
Assert.assertEquals(0, tasks.size());
Assert.assertNull(taskAdapter.findOne(2L, queryAdapter).getEntity());
tasks = (List<Task>) taskAdapter.findAll(Arrays.asList(2L), queryAdapter).getEntity();
Assert.assertEquals(0, tasks.size());

Expand Down
4 changes: 4 additions & 0 deletions katharsis-examples/dropwizard-simple-example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

In order to run this example do:
* run the dropwizard application with the ```server``` parameter
* The service will be available at: http://localhost:8080/projects
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
katharsis:
searchPackage: io.katharsis.example.dropwizardSimple.domain
searchPackage: io.katharsis.example.dropwizard.simple.domain

server:
type: simple
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package io.katharsis.example.dropwizardSimple;

import io.dropwizard.Configuration;
import io.katharsis.example.dropwizardSimple.domain.model.Project;
package io.katharsis.example.dropwizard.simple;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import io.dropwizard.Configuration;

public class DropwizardConfiguration extends Configuration {

@Valid
Expand All @@ -16,6 +15,6 @@ public class KatharsisConfiguration {

@Valid
@NotNull
public String searchPackage = Project.class.getPackage().getName();
public String searchPackage = "io.katharsis.example.dropwizard.simple.domain";
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package io.katharsis.example.dropwizardSimple;
package io.katharsis.example.dropwizard.simple;

import static io.katharsis.rs.KatharsisProperties.RESOURCE_SEARCH_PACKAGE;

import io.dropwizard.Application;
import io.dropwizard.setup.Environment;
import io.katharsis.locator.SampleJsonServiceLocator;
import io.katharsis.queryParams.DefaultQueryParamsParser;
import io.katharsis.queryParams.QueryParamsBuilder;
import io.katharsis.queryspec.DefaultQuerySpecDeserializer;
import io.katharsis.rs.KatharsisFeature;

import static io.katharsis.rs.KatharsisProperties.RESOURCE_SEARCH_PACKAGE;

public class DropwizardService extends Application<DropwizardConfiguration> {

@Override
Expand All @@ -18,7 +17,7 @@ public void run(DropwizardConfiguration dropwizardConfiguration, Environment env
environment.jersey().property(RESOURCE_SEARCH_PACKAGE, dropwizardConfiguration.katharsis.searchPackage);

KatharsisFeature katharsisFeature = new KatharsisFeature(environment.getObjectMapper(),
new QueryParamsBuilder(new DefaultQueryParamsParser()),
new DefaultQuerySpecDeserializer(),
new SampleJsonServiceLocator());
environment.jersey().register(katharsisFeature);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.katharsis.example.dropwizard.simple.domain.model;

import io.katharsis.resource.annotations.JsonApiId;
import io.katharsis.resource.annotations.JsonApiResource;

@JsonApiResource(type = "projects")
public class Project {

@JsonApiId
private Long id;

private String name;

public Project() {
}

public Project(long id, String name) {
this.id = id;
this.name = name;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.katharsis.example.dropwizard.simple.domain.repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

import io.katharsis.example.dropwizard.simple.domain.model.Project;
import io.katharsis.queryspec.QuerySpec;
import io.katharsis.queryspec.QuerySpecResourceRepositoryBase;

public class ProjectRepository extends QuerySpecResourceRepositoryBase<Project, Long> {

private static final AtomicLong ID_GENERATOR = new AtomicLong(124);

private Map<Long, Project> projects = new HashMap<>();

public ProjectRepository() {
super(Project.class);
List<String> interests = new ArrayList<>();
interests.add("coding");
interests.add("art");
save(new Project(123L, "Great Project"));
}

@Override
public synchronized void delete(Long id) {
projects.remove(id);
}

@Override
public synchronized <S extends Project> S save(S project) {
if (project.getId() == null) {
project.setId(ID_GENERATOR.getAndIncrement());
}
projects.put(project.getId(), project);
return project;
}

@Override
public synchronized List<Project> findAll(QuerySpec querySpec) {
return querySpec.apply(projects.values());
}

}

This file was deleted.

Loading

0 comments on commit e5bf6f8

Please sign in to comment.