Search in sources :

Example 51 with CrnkBoot

use of io.crnk.core.boot.CrnkBoot in project crnk-framework by crnk-project.

the class BraveResponseFilterTest method setup.

@Before
public void setup() {
    boot = new CrnkBoot();
    boot.setServiceDiscovery(new ReflectionsServiceDiscovery("io.crnk.test.mock.repository", new SampleJsonServiceLocator()));
    boot.boot();
    span = Mockito.mock(Span.class);
    tracing = Mockito.mock(Tracing.class);
    tracer = Mockito.mock(Tracer.class);
    Mockito.when(tracing.tracer()).thenReturn(tracer);
    Mockito.when(tracer.nextSpan()).thenReturn(span);
    moduleContext = Mockito.mock(Module.ModuleContext.class);
    Mockito.when(moduleContext.getResourceRegistry()).thenReturn(boot.getResourceRegistry());
    QuerySpec querySpec = new QuerySpec(Task.class);
    queryAdapter = new QuerySpecAdapter(querySpec, boot.getResourceRegistry());
    ResourceInformation taskResourceInformation = boot.getResourceRegistry().getEntry(Task.class).getResourceInformation();
    requestSpec = Mockito.mock(RepositoryRequestSpec.class);
    Mockito.when(requestSpec.getMethod()).thenReturn(HttpMethod.GET);
    Mockito.when(requestSpec.getQueryAdapter()).thenReturn(queryAdapter);
    Mockito.when(requestSpec.getQuerySpec(taskResourceInformation)).thenReturn(querySpec);
    filter = new BraveRepositoryFilter(tracing, moduleContext);
    filterContext = Mockito.mock(RepositoryFilterContext.class);
    Mockito.when(filterContext.getRequest()).thenReturn(requestSpec);
    filterChain = Mockito.mock(RepositoryRequestFilterChain.class);
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) Task(io.crnk.test.mock.models.Task) RepositoryRequestSpec(io.crnk.core.engine.dispatcher.RepositoryRequestSpec) Tracer(brave.Tracer) SampleJsonServiceLocator(io.crnk.legacy.locator.SampleJsonServiceLocator) BraveRepositoryFilter(io.crnk.monitor.brave.internal.BraveRepositoryFilter) RepositoryFilterContext(io.crnk.core.engine.filter.RepositoryFilterContext) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) Span(brave.Span) CrnkBoot(io.crnk.core.boot.CrnkBoot) ReflectionsServiceDiscovery(io.crnk.core.module.discovery.ReflectionsServiceDiscovery) Tracing(brave.Tracing) QuerySpec(io.crnk.core.queryspec.QuerySpec) RepositoryRequestFilterChain(io.crnk.core.engine.filter.RepositoryRequestFilterChain) Before(org.junit.Before)

Example 52 with CrnkBoot

use of io.crnk.core.boot.CrnkBoot in project crnk-framework by crnk-project.

the class AbstractMetaTest method setup.

@Before
public void setup() {
    boot = new CrnkBoot();
    boot.addModule(new JaxrsModule(null));
    boot.setServiceUrlProvider(new ConstantServiceUrlProvider("http://localhost"));
    boot.addModule(new TestModule());
    boot.addModule(new io.crnk.test.mock.dynamic.DynamicModule());
    configure();
    boot.boot();
    resourceProvider = new ResourceMetaProvider();
    lookup = new MetaLookup();
    lookup.addProvider(resourceProvider);
    lookup.setModuleContext(boot.getModuleRegistry().getContext());
    lookup.initialize();
}
Also used : CrnkBoot(io.crnk.core.boot.CrnkBoot) ResourceMetaProvider(io.crnk.meta.provider.resource.ResourceMetaProvider) JaxrsModule(io.crnk.rs.internal.JaxrsModule) ConstantServiceUrlProvider(io.crnk.core.engine.url.ConstantServiceUrlProvider) TestModule(io.crnk.test.mock.TestModule) Before(org.junit.Before)

Example 53 with CrnkBoot

use of io.crnk.core.boot.CrnkBoot in project crnk-framework by crnk-project.

the class MetaMetaTest method setup.

@Before
public void setup() {
    CrnkBoot boot = new CrnkBoot();
    boot.setServiceUrlProvider(new ConstantServiceUrlProvider("http://localhost"));
    boot.addModule(new TestModule());
    resourceProvider = new ResourceMetaProvider();
    MetaModuleConfig moduleConfig = new MetaModuleConfig();
    moduleConfig.addMetaProvider(resourceProvider);
    MetaModule module = MetaModule.createServerModule(moduleConfig);
    boot.addModule(module);
    boot.boot();
    lookup = module.getLookup();
}
Also used : CrnkBoot(io.crnk.core.boot.CrnkBoot) ResourceMetaProvider(io.crnk.meta.provider.resource.ResourceMetaProvider) ConstantServiceUrlProvider(io.crnk.core.engine.url.ConstantServiceUrlProvider) TestModule(io.crnk.test.mock.TestModule) Before(org.junit.Before)

Example 54 with CrnkBoot

use of io.crnk.core.boot.CrnkBoot in project crnk-framework by crnk-project.

the class JsonApiResponseFilter method filter.

/**
 * Creates JSON API responses for custom JAX-RS actions returning Crnk resources.
 */
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
    Object response = responseContext.getEntity();
    if (response == null) {
        if (feature.getBoot().isNullDataResponseEnabled()) {
            Document document = new Document();
            document.setData(Nullable.nullValue());
            responseContext.setEntity(document);
            responseContext.setStatus(Response.Status.OK.getStatusCode());
            responseContext.getHeaders().put("Content-Type", Collections.singletonList((Object) JsonApiMediaType.APPLICATION_JSON_API));
        }
        return;
    }
    // only modify responses which contain a single or a list of Crnk resources
    if (isResourceResponse(response)) {
        CrnkBoot boot = feature.getBoot();
        DocumentMapper documentMapper = boot.getDocumentMapper();
        HttpRequestContextProvider httpRequestContextProvider = boot.getModuleRegistry().getHttpRequestContextProvider();
        try {
            HttpRequestContext context = new HttpRequestContextBaseAdapter(new JaxrsRequestContext(requestContext, feature));
            httpRequestContextProvider.onRequestStarted(context);
            JsonApiResponse jsonApiResponse = new JsonApiResponse();
            jsonApiResponse.setEntity(response);
            // use the Crnk document mapper to create a JSON API response
            responseContext.setEntity(documentMapper.toDocument(jsonApiResponse, null));
            responseContext.getHeaders().put("Content-Type", Collections.singletonList((Object) JsonApiMediaType.APPLICATION_JSON_API));
        } finally {
            httpRequestContextProvider.onRequestFinished();
        }
    } else if (isJsonApiResponse(responseContext) && !doNotWrap(response)) {
        Document document = new Document();
        document.setData(Nullable.of(response));
        responseContext.setEntity(document);
    }
}
Also used : HttpRequestContextBaseAdapter(io.crnk.core.engine.internal.http.HttpRequestContextBaseAdapter) CrnkBoot(io.crnk.core.boot.CrnkBoot) DocumentMapper(io.crnk.core.engine.internal.document.mapper.DocumentMapper) HttpRequestContext(io.crnk.core.engine.http.HttpRequestContext) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Document(io.crnk.core.engine.document.Document) HttpRequestContextProvider(io.crnk.core.engine.http.HttpRequestContextProvider)

Aggregations

CrnkBoot (io.crnk.core.boot.CrnkBoot)54 ConstantServiceUrlProvider (io.crnk.core.engine.url.ConstantServiceUrlProvider)34 Before (org.junit.Before)34 ReflectionsServiceDiscovery (io.crnk.core.module.discovery.ReflectionsServiceDiscovery)28 Test (org.junit.Test)11 TestModule (io.crnk.test.mock.TestModule)9 SimpleModule (io.crnk.core.module.SimpleModule)7 JsonApiUrlBuilder (io.crnk.core.engine.internal.utils.JsonApiUrlBuilder)6 ResourceMetaProvider (io.crnk.meta.provider.resource.ResourceMetaProvider)6 MetaModule (io.crnk.meta.MetaModule)5 NullPropertiesProvider (io.crnk.core.engine.properties.NullPropertiesProvider)4 PropertiesProvider (io.crnk.core.engine.properties.PropertiesProvider)4 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)4 Task (io.crnk.core.mock.models.Task)4 TaskRepository (io.crnk.core.mock.repository.TaskRepository)4 QuerySpec (io.crnk.core.queryspec.QuerySpec)4 QuerySpecAdapter (io.crnk.core.queryspec.internal.QuerySpecAdapter)4 JaxrsModule (io.crnk.rs.internal.JaxrsModule)4 JsonApiExceptionMapper (io.crnk.core.engine.error.JsonApiExceptionMapper)3 HttpRequestContextProvider (io.crnk.core.engine.http.HttpRequestContextProvider)3