You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@sling.apache.org by sam ” <sk...@gmail.com> on 2012/01/27 19:33:59 UTC

integration tests as .jsp or inside osgi bundle?

Hey,

I have a test script that will make a series of HTTP requests to Sling.
I need to write servlets that will handle the requests.
I can either use jsp (/apps/tests/test1/json.jsp, for example).
Or, I can write a @Service that implements Servlet interface.

Good thing about writing .jsp to the repository is that I can configure ACL
to block access to /apps/tests.
If I write a @Service that implements Servlet interface (that handles
requests to /bin/tests, for example), anonymous users can make requests.
Maybe there is a way to configure ACL for resources that do not exist in
the repository (such as /bin/tests) ??

How do you write integration tests?

Thanks.
Sam.

Re: integration tests as .jsp or inside osgi bundle?

Posted by Bertrand Delacretaz <bd...@apache.org>.
On Fri, Jan 27, 2012 at 9:24 PM, sam ” <sk...@gmail.com> wrote:
> http://sling.apache.org/site/sling-testing-tools.html
> This is what I needed.
>
> One thing is that  I want to put my Test classes under src/test/,  not
> src/main/....

For test classes that are embedded in the bundle and run server-side
with those Sling test tools, src/main is the right place IMO. src/test
is for test classes that run during the Maven build.

There's a complete example (which you've probably seen) at [1].

-Bertrand

[1] http://svn.apache.org/repos/asf/sling/trunk/testing/samples/integration-tests/

Re: integration tests as .jsp or inside osgi bundle?

Posted by sam ” <sk...@gmail.com>.
http://sling.apache.org/site/sling-testing-tools.html
This is what I needed.


One thing is that  I want to put my Test classes under src/test/,  not
src/main/.
But, I don't know how to configure maven-bundle-plugin so that some of
classes under src/test/ to be copied to the bundle .jar.

Is there a way?  Or, should I put Test classes under src/main/,  too?



On Fri, Jan 27, 2012 at 1:33 PM, sam ” <sk...@gmail.com> wrote:

> Hey,
>
> I have a test script that will make a series of HTTP requests to Sling.
> I need to write servlets that will handle the requests.
> I can either use jsp (/apps/tests/test1/json.jsp, for example).
> Or, I can write a @Service that implements Servlet interface.
>
> Good thing about writing .jsp to the repository is that I can configure
> ACL to block access to /apps/tests.
> If I write a @Service that implements Servlet interface (that handles
> requests to /bin/tests, for example), anonymous users can make requests.
> Maybe there is a way to configure ACL for resources that do not exist in
> the repository (such as /bin/tests) ??
>
> How do you write integration tests?
>
> Thanks.
> Sam.
>
>
>

Re: integration tests as .jsp or inside osgi bundle?

Posted by Justin Edelson <ju...@justinedelson.com>.
On Mon, Jan 30, 2012 at 1:35 PM, sam ” <sk...@gmail.com> wrote:
> Hey Justin,
>
> What do you mean by the following?:
>
> most of the integration
>> tests I write run in Maven and interact with servlets/scripts running
>> inside Sling via HTTP.

Hi sam,
I meant just that - I have regular JUnit tests which interact with
Sling via HTTP and make assertions against the results. Just like the
Sling ITs.

I'll give a recent example. For a project, I had to write a
NodeNameGenerator service. There were ultimately two test classes for
this:

1) Unit tests using mocks. These were in the src/test/java structure
of my bundle.
2) Integration tests. These were in a separate Maven project, also in
the src/test/java structure.

The ITs made HTTP requests (POSTs in this case) to different paths and
with different payloads and then asserted that nodes did (or didn't)
get created with the appropriate names.

This could certainly be done with the in-container test framework. It
just seems more natural to me personally to test components in the
HTTP pipeline via HTTP. I probably should figure out a way to write
tests in curl and grep :)

Justin


>
>
>
> Do you have a separate test OSGi bundle  where you invoke `mvn test` at and
> it'll deploy the test bundle and run tests?
>
>
> Are there actual integration tests written with
> http://sling.apache.org/site/sling-testing-tools.html   (not just simple
> samples) ?
>
>
> I am not sure how to manage ResourceResolver for my Test class.
> class Test {
>    @TestReference private ResourceResolverFactory resolverFactory;
>    ResourceResolver resourceResolver;
> }
>
> Given above, do I call
> resolverFactory.getAdministrativeResourceResolver(null);  @Before  each
> @Test method?
>
> And, call resourceResolver.logout();  @After each @Test?
>
> Ideally, I would login/logout to repository once before and after running a
> bunch of @Test methods.
>
> I tried with @BeforeClass and @AfterClass. But, I don't know how to make
> @TestReference inject to static fields.
>
> Why is junit chosen as integration testing? At least testNG supports test
> dependencies so that I can login to/logout from repository once..
>
> Do you know which projects actually use sling integration tests?
>
> http://svn.apache.org/repos/asf/sling/trunk/launchpad/integration-tests
> This looks like the only integration test for sling. But, these don't use
> ResourceResolverFactory  (@TestReference).
>
>
>
>
>
>
>
> On Fri, Jan 27, 2012 at 9:54 PM, Justin Edelson <ju...@justinedelson.com>wrote:
>
>> Sam-
>> Regardless of whether it is a JSP or Servlet, the correct way to
>> handle this is to create a node with a sling:resourceType property.
>> Your JSP or Servlet should be associated with that resource type. If
>> you need to secure the content, secure the content, but that's an
>> orthogonal concern to the scripts used to render the content. Mounting
>> a servlet at a particular path is sometimes necessary and appropriate,
>> but more often than not it is something you can and should avoid. And,
>> as you note, this puts the onus of security on your servlet, not the
>> repository.
>>
>> To your question about integration tests, most of the integration
>> tests I write run in Maven and interact with servlets/scripts running
>> inside Sling via HTTP.  I try not to write test-specific servlets to
>> the greatest extent possible. We obviously need to create test
>> services for testing the framework itself, but not for applications
>> running inside of the framework. I also try to focus on writing unit
>> tests with good mocks as those tend to be significantly faster than
>> integration tests. YMMV, of course.
>>
>> Justin
>>
>> On Fri, Jan 27, 2012 at 1:33 PM, sam ” <sk...@gmail.com> wrote:
>> > Hey,
>> >
>> > I have a test script that will make a series of HTTP requests to Sling.
>> > I need to write servlets that will handle the requests.
>> > I can either use jsp (/apps/tests/test1/json.jsp, for example).
>> > Or, I can write a @Service that implements Servlet interface.
>> >
>> > Good thing about writing .jsp to the repository is that I can configure
>> ACL
>> > to block access to /apps/tests.
>> > If I write a @Service that implements Servlet interface (that handles
>> > requests to /bin/tests, for example), anonymous users can make requests.
>> > Maybe there is a way to configure ACL for resources that do not exist in
>> > the repository (such as /bin/tests) ??
>> >
>> > How do you write integration tests?
>> >
>> > Thanks.
>> > Sam.
>>

Re: integration tests as .jsp or inside osgi bundle?

Posted by Bertrand Delacretaz <bd...@apache.org>.
On Mon, Jan 30, 2012 at 7:35 PM, sam ” <sk...@gmail.com> wrote:
> ...Are there actual integration tests written with
> http://sling.apache.org/site/sling-testing-tools.html   (not just simple
> samples) ?...

The Sling integration tests are found here:
http://svn.apache.org/repos/asf/sling/trunk/launchpad/integration-tests
and run from here:
http://svn.apache.org/repos/asf/sling/trunk/launchpad/testing

They use similar techniques than the "new" sling testing tools that
you mention. The sling testing tools are a cleaner and more modular
reimplementation of that, that I and some of my colleagues are using
in several projects.

If you're starting a new project I would recommend using the sling
testing tools, and to study how the actual Sling testing tools work
you can have a look at the above two launchpad modules. Those don't
use server-side JUnit tests, but a number of them inject scripts in
the instance under test to reach similar goals, in a slightly less
integrated and modular way.

-Bertrand

Re: integration tests as .jsp or inside osgi bundle?

Posted by sam ” <sk...@gmail.com>.
Hey Justin,

What do you mean by the following?:

most of the integration
> tests I write run in Maven and interact with servlets/scripts running
> inside Sling via HTTP.



Do you have a separate test OSGi bundle  where you invoke `mvn test` at and
it'll deploy the test bundle and run tests?


Are there actual integration tests written with
http://sling.apache.org/site/sling-testing-tools.html   (not just simple
samples) ?


I am not sure how to manage ResourceResolver for my Test class.
class Test {
    @TestReference private ResourceResolverFactory resolverFactory;
    ResourceResolver resourceResolver;
}

Given above, do I call
resolverFactory.getAdministrativeResourceResolver(null);  @Before  each
@Test method?

And, call resourceResolver.logout();  @After each @Test?

Ideally, I would login/logout to repository once before and after running a
bunch of @Test methods.

I tried with @BeforeClass and @AfterClass. But, I don't know how to make
@TestReference inject to static fields.

Why is junit chosen as integration testing? At least testNG supports test
dependencies so that I can login to/logout from repository once..

Do you know which projects actually use sling integration tests?

http://svn.apache.org/repos/asf/sling/trunk/launchpad/integration-tests
This looks like the only integration test for sling. But, these don't use
ResourceResolverFactory  (@TestReference).







On Fri, Jan 27, 2012 at 9:54 PM, Justin Edelson <ju...@justinedelson.com>wrote:

> Sam-
> Regardless of whether it is a JSP or Servlet, the correct way to
> handle this is to create a node with a sling:resourceType property.
> Your JSP or Servlet should be associated with that resource type. If
> you need to secure the content, secure the content, but that's an
> orthogonal concern to the scripts used to render the content. Mounting
> a servlet at a particular path is sometimes necessary and appropriate,
> but more often than not it is something you can and should avoid. And,
> as you note, this puts the onus of security on your servlet, not the
> repository.
>
> To your question about integration tests, most of the integration
> tests I write run in Maven and interact with servlets/scripts running
> inside Sling via HTTP.  I try not to write test-specific servlets to
> the greatest extent possible. We obviously need to create test
> services for testing the framework itself, but not for applications
> running inside of the framework. I also try to focus on writing unit
> tests with good mocks as those tend to be significantly faster than
> integration tests. YMMV, of course.
>
> Justin
>
> On Fri, Jan 27, 2012 at 1:33 PM, sam ” <sk...@gmail.com> wrote:
> > Hey,
> >
> > I have a test script that will make a series of HTTP requests to Sling.
> > I need to write servlets that will handle the requests.
> > I can either use jsp (/apps/tests/test1/json.jsp, for example).
> > Or, I can write a @Service that implements Servlet interface.
> >
> > Good thing about writing .jsp to the repository is that I can configure
> ACL
> > to block access to /apps/tests.
> > If I write a @Service that implements Servlet interface (that handles
> > requests to /bin/tests, for example), anonymous users can make requests.
> > Maybe there is a way to configure ACL for resources that do not exist in
> > the repository (such as /bin/tests) ??
> >
> > How do you write integration tests?
> >
> > Thanks.
> > Sam.
>

Re: integration tests as .jsp or inside osgi bundle?

Posted by Justin Edelson <ju...@justinedelson.com>.
Sam-
Regardless of whether it is a JSP or Servlet, the correct way to
handle this is to create a node with a sling:resourceType property.
Your JSP or Servlet should be associated with that resource type. If
you need to secure the content, secure the content, but that's an
orthogonal concern to the scripts used to render the content. Mounting
a servlet at a particular path is sometimes necessary and appropriate,
but more often than not it is something you can and should avoid. And,
as you note, this puts the onus of security on your servlet, not the
repository.

To your question about integration tests, most of the integration
tests I write run in Maven and interact with servlets/scripts running
inside Sling via HTTP.  I try not to write test-specific servlets to
the greatest extent possible. We obviously need to create test
services for testing the framework itself, but not for applications
running inside of the framework. I also try to focus on writing unit
tests with good mocks as those tend to be significantly faster than
integration tests. YMMV, of course.

Justin

On Fri, Jan 27, 2012 at 1:33 PM, sam ” <sk...@gmail.com> wrote:
> Hey,
>
> I have a test script that will make a series of HTTP requests to Sling.
> I need to write servlets that will handle the requests.
> I can either use jsp (/apps/tests/test1/json.jsp, for example).
> Or, I can write a @Service that implements Servlet interface.
>
> Good thing about writing .jsp to the repository is that I can configure ACL
> to block access to /apps/tests.
> If I write a @Service that implements Servlet interface (that handles
> requests to /bin/tests, for example), anonymous users can make requests.
> Maybe there is a way to configure ACL for resources that do not exist in
> the repository (such as /bin/tests) ??
>
> How do you write integration tests?
>
> Thanks.
> Sam.