Skip to content

DaemonIntegration

Kamil Baczkowicz edited this page Oct 9, 2016 · 1 revision

Introduction

Apart from running mqtt-spy-daemon from the command line, you can also embed it in your project or unit/integration tests. This approach allows you then to trigger a set of predefined actions.

Usage

  • Download the jar
  • Add it to the classpath of your project (either directly or e.g. via Maven)
  • Import the MqttSpyDaemon class from pl.baczkowicz.mqttspy.daemon package
  • Create a new instance of the daemon and you are ready to start!

Actions

  • Start (takes the location of the XML configuration file)
  • Can publish? (i.e. am I connected?)
  • Run script
  • Run script's function
  • Stop script
  • Run test case
  • Stop

API

	 /**
	 * Start the daemon with the provided configuration file location.
	 * 
	 * @param configurationFile The daemon's configuration file location
	 * 
	 * @return True if started correctly
	 */
	boolean start(final String configurationFile);
	
	/**
	 * Checks if we can publish/send any messages (connection is established).
	 * 
	 * @return True if OK
	 */
	boolean canPublish();

	/**
	 * Runs a script from a given location.
	 * 
	 * @param scriptLocation Script location
	 * 
	 * @return The created script object, for reference
	 */
	Script runScript(final String scriptLocation);
	
	/**
	 * Runs a script from a given location.
	 * 
	 * @param scriptLocation Script location
	 * @param async Whether the execution should be asynchronous
	 * @param args A map of key/value pairs with arguments for the script; available as "args" in the script
	 * 
	 * @return The created script object, for reference
	 */
	Script runScript(final String scriptLocation, final boolean async, final Map<String, Object> args);
	
	/**
	 * Runs a particular script's function.
	 * 
	 * @param scriptLocation The script location
	 * @param functionName Name of the function
	 * @param args A map of key/value pairs with arguments for the script; available as "args" in the script
	 * 
	 * @return Function's result
	 */
	Object runScriptFunction(final String scriptLocation, final String functionName, final Map<String, Object> args);
	
	/**
	 * Stops the given script.
	 * 
	 * @param script The script object to stop
	 */
	void stopScript(final Script script);
	
	/**
	 * Stops the given script.
	 * 
	 * @param scriptName The script name to stop
	 */
	void stopScript(final String scriptName);

	/**
	 * Runs a test case from the given location.
	 * 
	 * @param testCaseLocation Test case location
	 * 
	 * @return Test result
	 */
	TestCaseResult runTestCase(final String testCaseLocation);

	/**
	 * Runs a test case from the given location.
	 * 
	 * @param testCaseLocation Test case location
	 * @param args A map of key/value pairs with arguments for the script; available as "args" in the script
	 * 
	 * @return Test result
	 */
	TestCaseResult runTestCase(final String testCaseLocation, final Map<String, Object> args);	
	
	/**
	 * Runs a test case from the given location.
	 * 
	 * @param testCaseLocation Test case location
	 * @param args A map of key/value pairs with arguments for the script; available as "args" in the script 
	 * @param options Additional test case options (autoExport, stepInterval, recordRepeatedSteps)
	 * 
	 * @return Test result
	 */
	TestCaseResult runTestCase(final String testCaseLocation, final Map<String, Object> args, final TestCaseOptions options);	
	
	/**
	 * Stops the daemon.
	 */
	void stop();	

Sample

This sample shows how mqtt-spy-daemon tests itself (using moquitto.org).

@Test
public void testSslWithMosquittoConfiguration()
{
	final MqttSpyDaemon daemon = new MqttSpyDaemon();
	
	assertTrue(daemon.start("src/test/resources/test_configurations/mosquitto-org.xml"));
	
	while (!daemon.canPublish())
	{
		logger.debug("Client not connected yet - can't start test cases... [waiting another 1000ms]");
		ThreadingUtils.sleep(1000);
	}
	
	final TestCaseResult result = daemon.runTestCase("src/test/resources/test_cases/test3/tc3.js");
	assertTrue(result.getResult().equals(TestCaseStatus.PASSED));
	assertTrue(result.getStepResults().size() >= 3);
	
	logger.info("Steps = {}", 
		result.getStepResults().stream().map(Object::toString).collect(Collectors.joining(", ")));
	
	daemon.stop();
}