= Integration Testing

:Notice: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at. http://www.apache.org/licenses/LICENSE-2.0 . Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR  CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

In an earlier section of this tutorial we looked at unit testing, but integration tests are at least as important, probably more so, as they exercise the entire application from an end-users perspective, rather than an individual part.

We don't write integration tests using Selenium or similar, and so avoid the fragility and maintenance effort that such tests often entail.
Instead, the framework provides the xref:refguide:applib:index/services/wrapper/WrapperFactory.adoc[WrapperFactory] domain service which simulates the user interface in a type-safe way.
Another term sometimes used is _subcutaneous_ testing; we execute the test "under the skin".


[#exercise-9-1-testing-bookvisit-using-an-integtest]
== Ex 9.1: Testing bookVisit using an integtest

In this exercise we'll test the `bookVisit` mixin action (on `Pet` entity).


=== Solution

[source,bash,subs="attributes+"]
----
git checkout tags/{tag-version}/09-01-bookVisit-integ-test
mvn clean install
----

=== Tasks

* in the `pom.xml` of the visits module, add the following dependencies:
+
[source,xml]
.module-visits/pom.xml
----
<dependency>
    <groupId>org.apache.causeway.testing</groupId>
    <artifactId>causeway-testing-integtestsupport-applib</artifactId>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>org.apache.causeway.persistence</groupId>
    <artifactId>causeway-persistence-jpa-eclipselink</artifactId>
    <scope>test</scope>
</dependency>
----


* add an abstract class `VisitModuleIntegTestAbstract` for the `visits` module, for other integ tests to subclass:
+
[source,java]
.VisitModuleIntegTestAbstract.java
----
@SpringBootTest(
        classes = VisitModuleIntegTestAbstract.TestApp.class
)
@ActiveProfiles("test")
public abstract class VisitModuleIntegTestAbstract
        extends CausewayIntegrationTestAbstractWithFixtures {

    @SpringBootConfiguration
    @EnableAutoConfiguration
    @Import({

            CausewayModuleCoreRuntimeServices.class,
            CausewayModuleSecurityBypass.class,
            CausewayModulePersistenceJpaEclipselink.class,
            CausewayModuleTestingFixturesApplib.class,

            VisitModule.class                           // <.>
    })
    @PropertySources({
            @PropertySource(CausewayPresets.H2InMemory_withUniqueSchema),
            @PropertySource(CausewayPresets.UseLog4j2Test),
    })
    public static class TestApp {
    }
}
----
<.> Most of this class is boilerplate, but it does reference the module under test.

* also update the `application-test.yml` file for the `visit` module, to ensure that the database schemas for both modules are created:
+
[source,yaml]
.module-visit/src/test/resources/application-test.yml
----
causeway:
  persistence:
    schema:
      auto-create-schemas: petowner,visit
----

* add a class `Bootstrap_IntegTest` integration test, inheriting from the `VisitsModuleIntegTestAbstract:
+
[source,java]
.Bootstrap_IntegTest.java
----
public class Bootstrap_IntegTest extends VisitModuleIntegTestAbstract {

    @Test
    public void checks_can_bootstrap() {}
}
----
+
Make sure this test runs and passes in both the IDE and using "mvn clean install".


Now we can write our actual test:

* Now add a class `PetOwner_bookVisit_IntegTest` integration test, also inheriting from the `VisitModuleIntegTestAbstract:
+
[source,java]
.PetOwner_bookVisit_IntegTest.java
----
public class PetOwner_bookVisit_IntegTest extends VisitModuleIntegTestAbstract {

    @BeforeEach
    void setup() {
        fixtureScripts.run(new PetOwner_persona.PersistAll());                       // <.>
    }

    @Test
    public void happy_case() {

        // given
        PetOwner somePetOwner = fakeDataService.enums()                             // <.>
                .anyOf(PetOwner_persona.class)
                .findUsing(serviceRegistry);
        Pet somePet = fakeDataService.collections()
                .anyOf(somePetOwner.getPets());

        List<Visit> before = visitRepository.findByPetOwner(somePetOwner);
        assertThat(before).isEmpty();

        // when
        LocalDateTime visitAt = clockService.getClock().nowAsLocalDateTime()        // <.>
                .plusDays(fakeDataService.ints().between(1, 3));

        wrapMixin(PetOwner_bookVisit.class, somePetOwner).act(somePet, visitAt);    // <.>

        // then
        List<Visit> after = visitRepository.findByPetOwner(somePetOwner);
        assertThat(after).hasSize(1);

        Visit visit = after.get(0);

        assertThat(visit.getPet()).isSameAs(somePet);                               // <.>
        assertThat(visit.getPet().getPetOwner()).isSameAs(somePetOwner);            // <.>
        assertThat(visit.getVisitAt()).isEqualTo(visitAt);                          // <6>
    }

    @Inject FakeDataService fakeDataService;
    @Inject VisitRepository visitRepository;
    @Inject ClockService clockService;

}
----
<.> uses same fixture script used for prototyping to set up ``Pet``s and their ``PetOwner``s.
<.> uses the xref:refguide:testing:index/fakedata/applib/services/FakeDataService.adoc[FakeDataService] to select a random `PetOwner` and corresponding `Pet`
<.> sets up some randomised but valid argument values
<.> invokes the action, using the xref:refguide:applib:index/services/wrapper/WrapperFactory.adoc[WrapperFactory] to simulate the UI
<.> asserts that one new `Visit` has been created for the `Pet`.
<.> asserts that the state of this new `Visit` is correct
+
Run the test and check that it passes.


* write an error scenario which checks that the `visitAt` date cannot be in the past:
+
[source,java]
.PetOwner_bookVisit_IntegTest.java
----
@Test
public void cannot_book_in_the_past() {

    // given
    PetOwner somePetOwner = fakeDataService.enums()
            .anyOf(PetOwner_persona.class)
            .findUsing(serviceRegistry);
    Pet somePet = fakeDataService.collections()
            .anyOf(somePetOwner.getPets());

    // when, then
    LocalDateTime visitAt = clockService.getClock().nowAsLocalDateTime();

    assertThatThrownBy(() ->
            wrapMixin(PetOwner_bookVisit.class, somePetOwner).act(somePet, visitAt)
    )
            .isInstanceOf(InvalidException.class)
            .hasMessage("Must book in the future");
}
----

[#exercise-9-2-adds-visit-fixture]
== Ex 9.2: Adds fixture for ``Visit``s

Currently we have a fixture for ``PetOwner``s and their ``Pet``s, but none for ``Visit``s.
If we want to write additional integration tests for ``Visit``s also, then it's a good idea to have some fixtures.
They can also be used when prototyping.

In this exercise we'll therefore add a new fixture for the `visit` module.


=== Solution

[source,bash,subs="attributes+"]
----
git checkout tags/{tag-version}/09-02-adds-visit-fixture
mvn clean install
----

=== Tasks

* copy in the following persona enum (we'll add the `Builder` next):
+
[source,java]
.Visit_persona.java
----
/**
 * Returns the most recent Visit, or the one scheduled.
 */
@RequiredArgsConstructor
public enum Visit_persona
implements Persona<Visit, Visit_persona.Builder> {

    JAMAL_VISITS(PetOwner_persona.JAMAL),
    CAMILA_VISITS(PetOwner_persona.CAMILA),
    ARJUN_VISITS(PetOwner_persona.ARJUN),
    NIA_VISITS(PetOwner_persona.NIA),
    OLIVIA_VISITS(PetOwner_persona.OLIVIA),
    LEILA_VISITS(PetOwner_persona.LEILA),
    MATT_VISITS(PetOwner_persona.MATT),
    BENJAMIN_VISITS(PetOwner_persona.BENJAMIN),
    JESSICA_VISITS(PetOwner_persona.JESSICA),
    DANIEL_VISITS(PetOwner_persona.DANIEL);

    private final PetOwner_persona petOwner_p;

    @Override
    public Builder builder() {
        return new Builder().setPersona(this);
    }

    @Override
    public Visit findUsing(final ServiceRegistry serviceRegistry) {
        final var owner = petOwner_p.findUsing(serviceRegistry);
        final var visits = serviceRegistry.lookupService(VisitRepository.class)
                .map(x -> x.findByPetOwner(owner))
                .orElseThrow();
        return lastOf(visits);
    }

    private static Visit lastOf(List<Visit> visits) {
        return visits.get(visits.size()-1);
    }

    // ...
}
----

* now add in the `Builder`:
+
[source,java]
.Visit_persona.java
----
@RequiredArgsConstructor
public enum Visit_persona
implements Persona<Visit, Visit_persona.Builder> {
    // ...

    @Accessors(chain = true)
    public static class Builder extends BuilderScriptWithResult<Visit> {

        @Getter @Setter private Visit_persona persona;

        @Override
        protected Visit buildResult(final ExecutionContext ec) {

            final var petOwner = ec.executeChildT(this, persona.petOwner_p);    // <.>

            petOwner.getPets().forEach(pet -> {

                // in the past
                final var numVisits = fakeDataService.ints().between(2, 4);     // <.>
                for (var i = 0; i < numVisits; i++) {
                    final var daysAgo = fakeDataService.ints().between(5, 500);
                    final var minsInTheDay = randomAppointmentTime();
                    final var appointmentTime = randomAppointmentTimeFromToday(-daysAgo, minsInTheDay);
                    wrapperFactory.wrapMixin(PetOwner_bookVisit.class, petOwner, SyncControl.control().withSkipRules()).act(pet, appointmentTime);
                }

                // in the future
                if (fakeDataService.booleans().coinFlip()) {                    // <.>
                    final var daysAhead = fakeDataService.ints().between(1, 10);
                    final var minsInTheDay = randomAppointmentTime();
                    final var appointmentTime = randomAppointmentTimeFromToday(daysAhead, minsInTheDay);
                    wrapperFactory.wrapMixin(PetOwner_bookVisit.class, petOwner, SyncControl.control().withSkipRules()).act(pet, appointmentTime);
                }
            });

            final var numDaysAgo = fakeDataService.ints().between(2, 100);
            final var lastVisit = clockService.getClock().nowAsLocalDate().minusDays(numDaysAgo);
            petOwner.setLastVisit(lastVisit);

            final var visits = wrapperFactory.wrapMixin(PetOwner_visits.class, petOwner, SyncControl.control().withSkipRules()).coll();
            return lastOf(visits);
        }

        private LocalDateTime randomAppointmentTimeFromToday(int days, int appointmentTime) {
            return clockService.getClock().nowAsLocalDate().atStartOfDay().plusDays(days).plusMinutes(appointmentTime);
        }

        private int randomAppointmentTime() {
            return (9 * 60) + (fakeDataService.ints().between(0, 32) * 15);
        }

        // -- DEPENDENCIES

        @Inject ClockService clockService;
        @Inject FakeDataService fakeDataService;
    }
}
----
<.> Using the supplied `ExecutionContext`, we can execute any prerequisites fixtures (in this case to obtain the corresponding `PetOwner` and ``Pet``s)
<.> we create between 2 and 4 ``Visit``s in the past for each ``Pet``.
<.> we create a ``Visit`` in the future for approximately every other ``Pet``.

* add in the `PersistAll`:
+
[source,java]
.Visit_persona.java
----
@RequiredArgsConstructor
public enum Visit_persona
implements Persona<Visit, Visit_persona.Builder> {
    // ...

    public static class PersistAll
            extends PersonaEnumPersistAll<Visit, Visit_persona, Builder> {
        public PersistAll() {
            super(Visit_persona.class);
        }
    }
}
----

* update the top-level `DomainAppDemo` fixture to create visits rather than petowners/pets:
+
[source,java]
.DomainAppDemo.java
----
public class DomainAppDemo extends FixtureScript {

    @Override
    protected void execute(final ExecutionContext ec) {
        ec.executeChildren(this, moduleWithFixturesService.getTeardownFixture());
        ec.executeChild(this, new Visit_persona.PersistAll());      // <.>
    }

    @Inject ModuleWithFixturesService moduleWithFixturesService;
}
----
<.> Because `Visit_persona` automatically creates its prereqs, there's no need to run the `PetOwner_persona` fixture.
+
[NOTE]
====
Currently the `PetOwner_persona` isn't rerunnable, so we have to take some care.
However, it's easy to refactor if we wanted to:

[source,java]
.PetOwner_persona.java
----
val petOwner = serviceRegistry.lookupService(PetOwners.class)
                    .map(x -> x.findByNameExact(persona.name))
                    .orElseGet(
                        () -> petOwners.create(persona.name, null, null, null)
                    );
----

Note that the above change _isn't_ one of the exercises.
====
