Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage with ResourceTestRule #30

Closed
jplock opened this issue Apr 19, 2017 · 10 comments
Closed

Usage with ResourceTestRule #30

jplock opened this issue Apr 19, 2017 · 10 comments

Comments

@jplock
Copy link

jplock commented Apr 19, 2017

Are there any examples of using Dropwizard's ResourceTestRule with this module?

I'm getting a:

ERROR [2017-04-18 22:00:18,608] org.glassfish.jersey.internal.Errors: Following issues have been detected: 
WARNING: No injection source found for a parameter of type public ... (org.pac4j.core.profile.CommonProfile) at index 0.

error when trying to test my resource.

@jplock
Copy link
Author

jplock commented Apr 19, 2017

Looks like doing:

@ClassRule
public static final ResourceTestRule resources = ResourceTestRule.builder()
    .addResource(new UserResource(store))
    .addProvider(new Pac4JValueFactoryProvider.Binder()).build();

fixes the above error, but now outputs:

ERROR [2017-04-18 22:21:15,674] io.dropwizard.jersey.errors.LoggingExceptionMapper: Error handling a request: f70b46c2d79620dd
! org.pac4j.core.exception.TechnicalException: ContextResolver<JaxRsContextFactory> cannot be null
! at org.pac4j.core.util.CommonHelper.assertTrue(CommonHelper.java:109)
! at org.pac4j.core.util.CommonHelper.assertNotNull(CommonHelper.java:130)
! at org.pac4j.jax.rs.helpers.ProvidersHelper.getContext(ProvidersHelper.java:20)
! at org.pac4j.jax.rs.jersey.features.Pac4JValueFactoryProvider$AbstractJaxRsContextValueFactory.getContext(Pac4JValueFactoryProvider.java:201)
! at org.pac4j.jax.rs.jersey.features.Pac4JValueFactoryProvider$ProfileValueFactory.provide(Pac4JValueFactoryProvider.java:153)
! at org.pac4j.jax.rs.jersey.features.Pac4JValueFactoryProvider$ProfileValueFactory.provide(Pac4JValueFactoryProvider.java:137)
! at org.glassfish.jersey.server.spi.internal.ParamValueFactoryWithSource.provide(ParamValueFactoryWithSource.java:71)
! at org.glassfish.jersey.server.spi.internal.ParameterValueHelper.getParameterValues(ParameterValueHelper.java:90)
! at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$AbstractMethodParamInvoker.getParamValues(JavaResourceMethodDispatcherProvider.java:127)
! at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:205)
! at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99)
! at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
! at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
! at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
! at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:326)
! at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
! at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
! at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
! at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
! at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
! at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
! at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)
! at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)
! at org.glassfish.jersey.test.inmemory.InMemoryConnector.apply(InMemoryConnector.java:275)
! at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:252)
! at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:701)
! at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
! at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
! at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
! at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444)
! at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:697)
! at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:420)
! at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:316)
! at com.myapp.application.resources.UserResourceTest.testGetUser(UserResourceTest.java:50)
! at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
! at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
! at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
! at java.lang.reflect.Method.invoke(Method.java:498)
! at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
! at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
! at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
! at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
! at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
! at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
! at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
! at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
! at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
! at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
! at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
! at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
! at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
! at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
! at io.dropwizard.testing.junit.ResourceTestRule$1.evaluate(ResourceTestRule.java:198)
! at org.junit.rules.RunRules.evaluate(RunRules.java:20)
! at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
! at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
! at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
! at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
! at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
! at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
! at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

I have @Pac4JProfile CommonProfile profile on all of my resource methods which I basically want to inject in a dummy value in my tests.

@jplock
Copy link
Author

jplock commented Apr 19, 2017

@ClassRule
public static final ResourceTestRule resources = ResourceTestRule.builder()
        .addResource(new UserResource(store))
        .addProvider(new JaxRsConfigProvider(new Config()))
        .addProvider(new JaxRsContextFactoryProvider())
        .addProvider(new Pac4JValueFactoryProvider.Binder()).build();

is looking more promising:

ERROR [2017-04-18 22:34:50,388] io.dropwizard.jersey.errors.LoggingExceptionMapper: Error handling a request: 744d2aa873df933e
! java.lang.NullPointerException: null
! at org.pac4j.core.context.WebContext.getSessionAttribute(WebContext.java:94)
! at org.pac4j.core.profile.ProfileManager.retrieveAll(ProfileManager.java:68)
! at org.pac4j.core.profile.ProfileManager.get(ProfileManager.java:35)
! at org.pac4j.jax.rs.jersey.features.Pac4JValueFactoryProvider$ProfileValueFactory.provide(Pac4JValueFactoryProvider.java:153)
! at org.pac4j.jax.rs.jersey.features.Pac4JValueFactoryProvider$ProfileValueFactory.provide(Pac4JValueFactoryProvider.java:137)
! at org.glassfish.jersey.server.spi.internal.ParamValueFactoryWithSource.provide(ParamValueFactoryWithSource.java:71)
! at org.glassfish.jersey.server.spi.internal.ParameterValueHelper.getParameterValues(ParameterValueHelper.java:90)
! at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$AbstractMethodParamInvoker.getParamValues(JavaResourceMethodDispatcherProvider.java:127)
! at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:205)
! at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99)
! at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
! at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
! at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
! at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:326)
! at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
! at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
! at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
! at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
! at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
! at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
! at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)
! at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)
! at org.glassfish.jersey.test.inmemory.InMemoryConnector.apply(InMemoryConnector.java:275)
! at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:252)
! at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:701)
! at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
! at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
! at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
! at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444)
! at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:697)
! at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:420)
! at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:316)
! at com.myapp.application.resources.UserResourceTest.testGetUser(UserResourceTest.java:54)
! at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
! at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
! at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
! at java.lang.reflect.Method.invoke(Method.java:498)
! at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
! at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
! at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
! at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
! at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
! at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
! at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
! at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
! at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
! at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
! at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
! at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
! at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
! at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
! at io.dropwizard.testing.junit.ResourceTestRule$1.evaluate(ResourceTestRule.java:198)
! at org.junit.rules.RunRules.evaluate(RunRules.java:20)
! at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
! at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
! at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
! at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
! at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
! at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
! at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

@victornoel
Copy link
Member

There is no easy solution to this question, because you could want to mock the CommonProfile for your test…

This is what I do for example: https://gitlab.com/linagora/petals-cockpit/blob/master/backend/src/test/java/org/ow2/petals/cockpit/server/resources/AbstractCockpitResourceTest.java#L136 with https://gitlab.com/linagora/petals-cockpit/blob/master/backend/src/test/java/org/ow2/petals/cockpit/server/mocks/MockProfileParamValueFactoryProvider.java

That's not great at all ^^ I will try to make something easier to use at least :)

@jplock
Copy link
Author

jplock commented Apr 19, 2017

That sounds great @victornoel. I was also fumbling around with Grizzle test as well but wasn't having much luck. Thanks for the help.

@victornoel
Copy link
Member

np, let's keep this open until we have a cleaner solution to easily use ResourceTestRule :)

@victornoel
Copy link
Member

@jplock I've committed some changes in jax-rs-pac4j to make this easier to use.

Basically you can now pass some mock profile and profile manager to the ParamValueFactoryProvider.Binder() constructor.

The rest of the providers should be the same as those used in the dropwizard bundle.

Don't hesitate to tell me if you think we need more than that :)

@victornoel victornoel added this to the 2.0.0-RC3 milestone Apr 23, 2017
@jplock
Copy link
Author

jplock commented Apr 23, 2017

This seemed to be the minimum amount of code for my test to pass. Grizzly definitely seems to be required. Thanks for the help!

private static Config getConfig() {
    final Authenticator<UsernamePasswordCredentials> auth = new SimpleTestUsernamePasswordAuthenticator();
    final DirectBasicAuthClient basicAuth = new DirectBasicAuthClient(auth);
    final Clients clients = new Clients(basicAuth);

    final JaxRsConfig config = new JaxRsConfig();
    config.setClients(clients);
    return config;
}

@ClassRule
public static final ResourceTestRule resources = ResourceTestRule.builder()
            .setTestContainerFactory(new GrizzlyWebTestContainerFactory())
            .addResource(new UserResource(store))
            .addProvider(new JaxRsConfigProvider(getConfig()))
            .addProvider(new Pac4JValueFactoryProvider.Binder(new CommonProfile()))
            .build();

@victornoel
Copy link
Member

Yes, that's seem similar to what I have !

And yes, you are right, it won't work without grizzly I think! I don't remember what was the explanation, but I think it is acceptable to use grizzly :)
Maybe it was related to no session support with the in-memory container... not sure!

@lucianonet
Copy link

Hi Victor.
I have a question related to testing with @rule ResourceTestRule.
@ClassRule
public static final ResourceTestRule resources = ResourceTestRule.builder()
.setTestContainerFactory(new GrizzlyWebTestContainerFactory())
.addResource(new UserResource(store))
.addProvider(new JaxRsConfigProvider(getConfig()))
.addProvider(new Pac4JValueFactoryProvider.Binder(new CommonProfile()))
.build();

In the above example, I saw that you injected CommonProfile to your resource by addProvider.
Is there any posibility to modify the resource test rule and injecting other CommonProfile object in a @test method?

@victornoel
Copy link
Member

@lucianonet that's how I do it: https://gitlab.com/linagora/petals-cockpit/blob/f9f7f44235a9c84f4dc0c45ac9e2be6d2619bba8/backend/src/test/java/org/ow2/petals/cockpit/server/rules/CockpitResourceRule.java

Basically I have a class member of my test class that contain the profile I want, and then the Pac4JValueFactoryProvider is used passed a factory of factory instead of the normal object and it works nicely :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants