All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.jexxa.jexxatest.JexxaTest Maven / Gradle / Ivy

package io.jexxa.jexxatest;

import io.jexxa.common.drivenadapter.persistence.objectstore.imdb.IMDBObjectStore;
import io.jexxa.common.drivenadapter.persistence.repository.imdb.IMDBRepository;
import io.jexxa.common.facade.utils.annotation.CheckReturnValue;
import io.jexxa.common.facade.utils.function.ThrowingConsumer;
import io.jexxa.core.JexxaMain;
import io.jexxa.jexxatest.infrastructure.messaging.recording.MessageRecorder;
import io.jexxa.jexxatest.infrastructure.messaging.recording.MessageRecorderManager;
import io.jexxa.jexxatest.infrastructure.messaging.recording.MessageRecordingStrategy;

import java.util.Optional;
import java.util.Properties;

import static io.jexxa.common.drivenadapter.messaging.MessageSenderFactory.setDefaultMessageSender;
import static io.jexxa.common.drivenadapter.outbox.TransactionalOutboxProperties.outboxTable;
import static io.jexxa.common.drivenadapter.persistence.ObjectStoreFactory.setDefaultObjectStore;
import static io.jexxa.common.drivenadapter.persistence.RepositoryFactory.setDefaultRepository;
import static io.jexxa.common.facade.logger.SLF4jLogger.getLogger;


/**
 * This class supports unit testing of your application core, at least if you use infrastructure strategies
 * provided by Jexxa. To do so, this class performs the following steps:
 * 
    *
  • Configuring an IMDB database for repositories
  • *
  • Configuring a message recorder for sending messages
  • *
* An example of how to use this class can be found in tutorial Bookstore * */ public class JexxaTest { public static final String JEXXA_TEST_PROPERTIES = "/jexxa-test.properties"; private static JexxaMain jexxaMain; private JexxaTest() { jexxaMain.addProperties( loadJexxaTestProperties() ); initForUnitTests(); } public static synchronized JexxaTest getJexxaTest(Class jexxaApplication) { if (jexxaMain == null) { jexxaMain = new JexxaMain(jexxaApplication); } return new JexxaTest(); } @CheckReturnValue public T getRepository(Class repository) { if (!repository.isInterface()) { throw new IllegalArgumentException("Given attribute of getRepository must be an interface"); } return jexxaMain.getInstanceOfPort(repository); } @CheckReturnValue public T getInstanceOfPort(Class inboundPort) { return jexxaMain.getInstanceOfPort(inboundPort); } @CheckReturnValue public MessageRecorder getMessageRecorder(Class outboundPort) { var realImplementation = jexxaMain.getInstanceOfPort(outboundPort); return MessageRecorderManager.getMessageRecorder(realImplementation.getClass()); } public JexxaMain getJexxaMain() { return jexxaMain; } @SuppressWarnings("unused") public void registerStub(Class outboundPort) { jexxaMain.registerDrivenAdapter(outboundPort); } @CheckReturnValue public Properties getProperties() { return getJexxaMain().getProperties(); } private void initForUnitTests( ) { setDefaultRepository(IMDBRepository.class); setDefaultObjectStore(IMDBObjectStore.class); setDefaultMessageSender(MessageRecordingStrategy.class); IMDBRepository.clear(); //Note: This clears IMDBRepository and IMDBObjectStore because IMDBObjectStore extends IMDBRepository and uses its internal data structure MessageRecorderManager.clear(); } static Properties loadJexxaTestProperties() { var properties = new Properties(); Optional.ofNullable(JexxaMain.class.getResourceAsStream(JEXXA_TEST_PROPERTIES)) .ifPresentOrElse( ThrowingConsumer.exceptionLogger(properties::load, getLogger(JexxaTest.class)), () -> getLogger(JexxaTest.class).warn("Properties file '{}' not found", JEXXA_TEST_PROPERTIES) ); if (!properties.containsKey(outboxTable())) { properties.put(outboxTable(),"jexxaoutboxmessage_test"); } return properties; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy