Merge pull request #3 from myrle-krantz/develop
added helpful examples to template.
diff --git a/api/src/main/java/io/mifos/template/api/v1/EventConstants.java b/api/src/main/java/io/mifos/template/api/v1/events/EventConstants.java
similarity index 95%
rename from api/src/main/java/io/mifos/template/api/v1/EventConstants.java
rename to api/src/main/java/io/mifos/template/api/v1/events/EventConstants.java
index 69b1343..6207c03 100644
--- a/api/src/main/java/io/mifos/template/api/v1/EventConstants.java
+++ b/api/src/main/java/io/mifos/template/api/v1/events/EventConstants.java
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package io.mifos.template.api.v1;
+package io.mifos.template.api.v1.events;
@SuppressWarnings("unused")
public interface EventConstants {
diff --git a/component-test/src/main/java/io/mifos/template/SuiteTestEnvironment.java b/component-test/src/main/java/io/mifos/template/SuiteTestEnvironment.java
new file mode 100644
index 0000000..dd5bfd0
--- /dev/null
+++ b/component-test/src/main/java/io/mifos/template/SuiteTestEnvironment.java
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2017 The Mifos Initiative.
+ *
+ * Licensed 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.
+ */
+package io.mifos.template;
+
+import io.mifos.core.test.env.TestEnvironment;
+import io.mifos.core.test.fixture.cassandra.CassandraInitializer;
+import io.mifos.core.test.fixture.mariadb.MariaDBInitializer;
+import org.junit.ClassRule;
+import org.junit.rules.RuleChain;
+import org.junit.rules.RunExternalResourceOnce;
+import org.junit.rules.TestRule;
+
+/**
+ * This contains the database resources required by the test. They are in a separate
+ * class so that the test suite can initialize them before the classes it calls. This
+ * makes test runs faster and prevents tests from "stepping on each other's toes" when
+ * initializing and de-initializing external resources.
+ */
+public class SuiteTestEnvironment {
+ static final String APP_NAME = "template-v1";
+ static final TestEnvironment testEnvironment = new TestEnvironment(APP_NAME);
+ static final CassandraInitializer cassandraInitializer = new CassandraInitializer();
+ static final MariaDBInitializer mariaDBInitializer = new MariaDBInitializer();
+
+ @ClassRule
+ public static TestRule orderClassRules = RuleChain
+ .outerRule(new RunExternalResourceOnce(testEnvironment))
+ .around(new RunExternalResourceOnce(cassandraInitializer))
+ .around(new RunExternalResourceOnce(mariaDBInitializer));
+}
diff --git a/component-test/src/main/java/io/mifos/template/TestSample.java b/component-test/src/main/java/io/mifos/template/TestSample.java
index b829355..924512b 100644
--- a/component-test/src/main/java/io/mifos/template/TestSample.java
+++ b/component-test/src/main/java/io/mifos/template/TestSample.java
@@ -17,24 +17,20 @@
import io.mifos.anubis.test.v1.TenantApplicationSecurityEnvironmentTestRule;
import io.mifos.core.api.context.AutoUserContext;
-import io.mifos.core.test.env.TestEnvironment;
import io.mifos.core.test.fixture.TenantDataStoreContextTestRule;
-import io.mifos.core.test.fixture.cassandra.CassandraInitializer;
-import io.mifos.core.test.fixture.mariadb.MariaDBInitializer;
import io.mifos.core.test.listener.EnableEventRecording;
import io.mifos.core.test.listener.EventRecorder;
-import io.mifos.template.api.v1.EventConstants;
+import io.mifos.template.api.v1.events.EventConstants;
import io.mifos.template.api.v1.client.TemplateManager;
import io.mifos.template.api.v1.domain.Sample;
import io.mifos.template.service.TemplateConfiguration;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.*;
-import org.junit.rules.RuleChain;
-import org.junit.rules.TestRule;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
@@ -48,9 +44,10 @@
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
-public class TestSample {
+public class TestSample extends SuiteTestEnvironment {
+ private static final String LOGGER_NAME = "test-logger";
+ private static final String TEST_USER = "homer";
- private static final String APP_NAME = "template-v1";
@Configuration
@EnableEventRecording
@@ -63,24 +60,14 @@
super();
}
- @Bean()
+ @Bean(name = LOGGER_NAME)
public Logger logger() {
- return LoggerFactory.getLogger("test-logger");
+ return LoggerFactory.getLogger(LOGGER_NAME);
}
}
- private static final String TEST_USER = "homer";
-
- private final static TestEnvironment testEnvironment = new TestEnvironment(APP_NAME);
- private final static CassandraInitializer cassandraInitializer = new CassandraInitializer();
- private final static MariaDBInitializer mariaDBInitializer = new MariaDBInitializer();
- private final static TenantDataStoreContextTestRule tenantDataStoreContext = TenantDataStoreContextTestRule.forRandomTenantName(cassandraInitializer, mariaDBInitializer);
@ClassRule
- public static TestRule orderClassRules = RuleChain
- .outerRule(testEnvironment)
- .around(cassandraInitializer)
- .around(mariaDBInitializer)
- .around(tenantDataStoreContext);
+ public final static TenantDataStoreContextTestRule tenantDataStoreContext = TenantDataStoreContextTestRule.forRandomTenantName(cassandraInitializer, mariaDBInitializer);
@Rule
public final TenantApplicationSecurityEnvironmentTestRule tenantApplicationSecurityEnvironment
@@ -94,6 +81,11 @@
@Autowired
private EventRecorder eventRecorder;
+ @SuppressWarnings("WeakerAccess")
+ @Autowired
+ @Qualifier(LOGGER_NAME)
+ Logger logger;
+
public TestSample() {
super();
}
@@ -119,6 +111,7 @@
@Test
public void shouldCreateSample() throws InterruptedException {
+ logger.info("Running test shouldCreateSample.");
final Sample sample = Sample.create(RandomStringUtils.randomAlphanumeric(8), RandomStringUtils.randomAlphanumeric(512));
this.testSubject.createEntity(sample);
@@ -130,6 +123,7 @@
@Test
public void shouldListSamples() {
+ logger.info("Running test shouldListSamples.");
final List<Sample> allEntities = this.testSubject.findAllEntities();
Assert.assertNotNull(allEntities);
}
diff --git a/api/src/main/java/io/mifos/template/api/v1/EventConstants.java b/component-test/src/main/java/io/mifos/template/TestSuite.java
similarity index 60%
copy from api/src/main/java/io/mifos/template/api/v1/EventConstants.java
copy to component-test/src/main/java/io/mifos/template/TestSuite.java
index 69b1343..44ed340 100644
--- a/api/src/main/java/io/mifos/template/api/v1/EventConstants.java
+++ b/component-test/src/main/java/io/mifos/template/TestSuite.java
@@ -13,15 +13,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package io.mifos.template.api.v1;
+package io.mifos.template;
-@SuppressWarnings("unused")
-public interface EventConstants {
+import org.junit.runner.RunWith;
+import org.junit.runners.Suite;
- String DESTINATION = "template-v1";
- String SELECTOR_NAME = "action";
- String INITIALIZE = "initialize";
- String POST_SAMPLE = "post-sample";
- String SELECTOR_INITIALIZE = SELECTOR_NAME + " = '" + INITIALIZE + "'";
- String SELECTOR_POST_SAMPLE = SELECTOR_NAME + " = '" + POST_SAMPLE + "'";
+@RunWith(Suite.class)
+@Suite.SuiteClasses({
+ TestSample.class,
+ //TODO: when you create a new component test, add it here so you can run it with the suite.
+})
+public class TestSuite extends SuiteTestEnvironment {
}
diff --git a/component-test/src/main/java/io/mifos/template/listener/MigrationEventListener.java b/component-test/src/main/java/io/mifos/template/listener/MigrationEventListener.java
index 82408bb..52be53b 100644
--- a/component-test/src/main/java/io/mifos/template/listener/MigrationEventListener.java
+++ b/component-test/src/main/java/io/mifos/template/listener/MigrationEventListener.java
@@ -17,7 +17,7 @@
import io.mifos.core.lang.config.TenantHeaderFilter;
import io.mifos.core.test.listener.EventRecorder;
-import io.mifos.template.api.v1.EventConstants;
+import io.mifos.template.api.v1.events.EventConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.messaging.handler.annotation.Header;
diff --git a/component-test/src/main/java/io/mifos/template/listener/SampleEventListener.java b/component-test/src/main/java/io/mifos/template/listener/SampleEventListener.java
index 2e3c2cb..b54a819 100644
--- a/component-test/src/main/java/io/mifos/template/listener/SampleEventListener.java
+++ b/component-test/src/main/java/io/mifos/template/listener/SampleEventListener.java
@@ -17,7 +17,7 @@
import io.mifos.core.lang.config.TenantHeaderFilter;
import io.mifos.core.test.listener.EventRecorder;
-import io.mifos.template.api.v1.EventConstants;
+import io.mifos.template.api.v1.events.EventConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.messaging.handler.annotation.Header;
diff --git a/service/src/main/java/io/mifos/template/service/internal/command/InitializeServiceCommand.java b/service/src/main/java/io/mifos/template/service/internal/command/InitializeServiceCommand.java
index db85b9d..e02d2d0 100644
--- a/service/src/main/java/io/mifos/template/service/internal/command/InitializeServiceCommand.java
+++ b/service/src/main/java/io/mifos/template/service/internal/command/InitializeServiceCommand.java
@@ -20,4 +20,9 @@
public InitializeServiceCommand() {
super();
}
+
+ @Override
+ public String toString() {
+ return "InitializeServiceCommand{}";
+ }
}
diff --git a/service/src/main/java/io/mifos/template/service/internal/command/SampleCommand.java b/service/src/main/java/io/mifos/template/service/internal/command/SampleCommand.java
index 8ee6d92..5957f1b 100644
--- a/service/src/main/java/io/mifos/template/service/internal/command/SampleCommand.java
+++ b/service/src/main/java/io/mifos/template/service/internal/command/SampleCommand.java
@@ -29,4 +29,11 @@
public Sample sample() {
return this.sample;
}
+
+ @Override
+ public String toString() {
+ return "SampleCommand{" +
+ "sample=" + sample.getIdentifier() +
+ '}';
+ }
}
diff --git a/service/src/main/java/io/mifos/template/service/internal/command/handler/MigrationAggregate.java b/service/src/main/java/io/mifos/template/service/internal/command/handler/MigrationAggregate.java
index e4e3de1..920465a 100644
--- a/service/src/main/java/io/mifos/template/service/internal/command/handler/MigrationAggregate.java
+++ b/service/src/main/java/io/mifos/template/service/internal/command/handler/MigrationAggregate.java
@@ -17,9 +17,10 @@
import io.mifos.core.command.annotation.Aggregate;
import io.mifos.core.command.annotation.CommandHandler;
+import io.mifos.core.command.annotation.CommandLogLevel;
import io.mifos.core.command.annotation.EventEmitter;
import io.mifos.core.mariadb.domain.FlywayFactoryBean;
-import io.mifos.template.api.v1.EventConstants;
+import io.mifos.template.api.v1.events.EventConstants;
import io.mifos.template.service.ServiceConstants;
import io.mifos.template.service.internal.command.InitializeServiceCommand;
import org.slf4j.Logger;
@@ -49,7 +50,7 @@
this.flywayFactoryBean = flywayFactoryBean;
}
- @CommandHandler
+ @CommandHandler(logStart = CommandLogLevel.INFO, logFinish = CommandLogLevel.INFO)
@Transactional
@EventEmitter(selectorName = EventConstants.SELECTOR_NAME, selectorValue = EventConstants.INITIALIZE)
public String initialize(final InitializeServiceCommand initializeServiceCommand) {
diff --git a/service/src/main/java/io/mifos/template/service/internal/command/handler/SampleAggregate.java b/service/src/main/java/io/mifos/template/service/internal/command/handler/SampleAggregate.java
index 4de453a..9a7c572 100644
--- a/service/src/main/java/io/mifos/template/service/internal/command/handler/SampleAggregate.java
+++ b/service/src/main/java/io/mifos/template/service/internal/command/handler/SampleAggregate.java
@@ -17,8 +17,9 @@
import io.mifos.core.command.annotation.Aggregate;
import io.mifos.core.command.annotation.CommandHandler;
+import io.mifos.core.command.annotation.CommandLogLevel;
import io.mifos.core.command.annotation.EventEmitter;
-import io.mifos.template.api.v1.EventConstants;
+import io.mifos.template.api.v1.events.EventConstants;
import io.mifos.template.service.internal.command.SampleCommand;
import io.mifos.template.service.internal.repository.SampleJpaEntity;
import io.mifos.template.service.internal.repository.SampleJpaEntityRepository;
@@ -37,7 +38,19 @@
this.sampleJpaEntityRepository = sampleJpaEntityRepository;
}
- @CommandHandler
+ //TODO: Think about your command handler logging, then delete this comment.
+ // The log levels provided in the command handler cause log messages to be emitted each time this
+ // command handler is called before and after the call. Before the call, the command is logged
+ // using its toString() method, and after the call, the emitted event is logged via its toString()
+ // method.
+ //
+ // If you wish to adjust the information in the log messages, do so via the toString() methods.
+ // Financial transactions, passwords, and customer address data are examples of information which
+ // should not be placed in the logs.
+ //
+ // If a command handler should not emit a log message, change logStart and logFinish to:
+ // CommandLogLevel.NONE.
+ @CommandHandler(logStart = CommandLogLevel.INFO, logFinish = CommandLogLevel.INFO)
@Transactional
@EventEmitter(selectorName = EventConstants.SELECTOR_NAME, selectorValue = EventConstants.POST_SAMPLE)
public String sample(final SampleCommand sampleCommand) {