Skip to content

Commit

Permalink
NATT Core update
Browse files Browse the repository at this point in the history
  • Loading branch information
0xMartin committed Aug 11, 2024
1 parent 0fd1ea3 commit ded1d49
Show file tree
Hide file tree
Showing 17 changed files with 90 additions and 75 deletions.
Binary file modified examples/plugin-example/plugin/libs/natt-spi-1.0.0.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,58 @@

import utb.fai.natt.spi.INATTContext;
import utb.fai.natt.spi.NATTKeyword;
import utb.fai.natt.spi.NATTAnnotation;
import utb.fai.natt.spi.exception.InternalErrorException;
import utb.fai.natt.spi.exception.InvalidSyntaxInConfigurationException;
import utb.fai.natt.spi.exception.NonUniqueModuleNamesException;

/**
* This is your keyword. You must register it in your plugin's main class that implements INATTPlugin.
* This is your keyword. You must register it in your plugin's main class that
* implements INATTPlugin.
*
* Format of this example keyword in yaml test configuration:
*
* my_keyword_1: "module-1"
*
* or
*
* my_keyword_1:
* name: "module-1"
*
* This keyword create custom module only.
*/
@NATTAnnotation.Keyword(name = "my_keyword_1")
public class MyKeyword1 extends NATTKeyword {

protected String moduleName;

private MyModule1 module;

@Override
public void deleteAction(INATTContext ctx) throws InternalErrorException {
throw new UnsupportedOperationException("Unimplemented method 'deleteAction'");
public void keywordInit(INATTContext ctx) throws InvalidSyntaxInConfigurationException {
// load keyword parameter value. name of parameter is "name" or
// DEFAULT_PARAMETER_NAME (DEFAULT_PARAMETER_NAME = in yaml is no need to
// specify parameter name, like this my_keyword_1: "module-1")
ParameterValue val = this.getParameterValue(new String[] { NATTKeyword.DEFAULT_PARAMETER_NAME, "name" },
ParameterValueType.STRING, true);
if (val != null) {
moduleName = val.getValue().toString();
}
}

@Override
public boolean execute(INATTContext ctx) throws InternalErrorException, NonUniqueModuleNamesException {
throw new UnsupportedOperationException("Unimplemented method 'execute'");
// create module
this.module = new MyModule1(moduleName, ctx);
return true;
}

@Override
public void keywordInit(INATTContext ctx) throws InvalidSyntaxInConfigurationException {
throw new UnsupportedOperationException("Unimplemented method 'keywordInit'");
public void deleteAction(INATTContext ctx) throws InternalErrorException {
// terminate module
if (this.module != null) {
this.module.terminateModule();
}
}

}
Original file line number Diff line number Diff line change
@@ -1,32 +1,41 @@
package natt.plugin;

import utb.fai.natt.spi.INATTContext;
import utb.fai.natt.spi.NATTLogger;
import utb.fai.natt.spi.NATTModule;
import utb.fai.natt.spi.exception.InternalErrorException;
import utb.fai.natt.spi.exception.NonUniqueModuleNamesException;

/**
* This is your module. Modules are used to send and receive messages. Is not needed to register it in your plugin's main class.
* Plugins are used by keywords. After creating an instance of a module, its reference is automatically inserted into the NATTContext
* and you can access it from several different keywords using this method "ctx.getModule(name)".
* This is your module. Modules are used to send and receive messages. Is not
* needed to register it in your plugin's main class. Plugins are used by
* keywords. After creating an instance of a module, its reference is
* automatically inserted into the NATTContext and you can access it from
* several different keywords using this method "ctx.getModule(name)".
*/
public class MyModule1 extends NATTModule {

private NATTLogger logger = new NATTLogger(MyModule1.class);

public MyModule1(String name, INATTContext ctx) throws NonUniqueModuleNamesException, InternalErrorException {
super(name, ctx);
}

@Override
public void runModule() throws InternalErrorException {

logger.info(super.getNameForLogger() + " Is running now!");
}

@Override
public boolean sendMessage(String arg0) throws InternalErrorException {
public boolean sendMessage(String message) throws InternalErrorException {
logger.info(super.getNameForLogger() + " sending message: " + message);
return true;
}

@Override
public boolean terminateModule() {
logger.info(super.getNameForLogger() + " is terminating...");
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ public class PluginMain implements INATTPlugin {

@Override
public String getName() {
// name of your plugin
return "My Plugin";
}

@Override
public void initialize(INATTContext ctx) {
// Register your keywords here
// Register all your keywords here
ctx.registerKeyword(new MyKeyword1());
}

Expand Down
9 changes: 0 additions & 9 deletions natt-spi/src/main/java/utb/fai/natt/spi/NATTAnnotation.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,4 @@ public class NATTAnnotation {
public String name();
}

/**
* Annotation for simple and clear definition of module parameters.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public static @interface Module {
public String value();
}

}
24 changes: 4 additions & 20 deletions natt-spi/src/main/java/utb/fai/natt/spi/NATTModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public MessageFilter(String text, String tag, String mode, Boolean caseSensitive
}
}

// Contains the module type name
private String moduleTypeName;
// Reference to the context
private INATTContext nattCtx;

// Contains the name of the object. Used to distinguish multiple identical modules.
private String name;
Expand All @@ -58,18 +58,11 @@ public NATTModule(String name, INATTContext nattCtx) throws NonUniqueModuleNames
throw new InternalErrorException("Name of module is empty");
}

// Module name
// initialize attributes
this.nattCtx = nattCtx;
this.name = name;

this.isRunning = false;

// Module type name
Class<?> clazz = this.getClass();
if (clazz.isAnnotationPresent(NATTAnnotation.Module.class)) {
NATTAnnotation.Module annotation = clazz.getAnnotation(NATTAnnotation.Module.class);
this.moduleTypeName = annotation.value();
}

// Create message buffer for this module
nattCtx.getMessageBuffer().createMessageBufferForModule(this.name);

Expand Down Expand Up @@ -105,15 +98,6 @@ protected void setRunning(boolean isRunning) {
this.isRunning = isRunning;
}

/**
* Gets the module type name. This is the name of its type
*
* @return Name of the module type
*/
public String getModuleTypeName() {
return moduleTypeName;
}

/**
* Returns the list of action filters
*
Expand Down
Binary file modified natt/libs/natt-spi-1.0.0.jar
Binary file not shown.
46 changes: 32 additions & 14 deletions natt/src/main/java/utb/fai/natt/core/NATTCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ public NATTCore(String[] args) throws InternalErrorException {
options.addOption("h", "help", false, "Help on how to use");
options.addOption("v", "validate", false, "Validates the test suite configuration");
options.addOption("p", "plugins", false, "List of all loaded plugins");
options.addOption("k", "keywords", false, "List of all registered keywords and their documentation");
options.addOption("kd", "keywordDoc", false,
"List of all registered keywords and their documentation in json format");
options.addOption("k", "keywords", false, "List of all registered keywords");

CommandLine cmd;
try {
Expand All @@ -105,15 +107,7 @@ public NATTCore(String[] args) throws InternalErrorException {

/************************************************************************************************************************* */

// help
boolean helpRequested = cmd.hasOption("h");
if (helpRequested) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("java -jar NATT.jar", options);
System.exit(0);
}

// title
// title vystupniho reportu
this.testReportName = cmd.getOptionValue("t");

// c parametr
Expand All @@ -130,26 +124,50 @@ public NATTCore(String[] args) throws InternalErrorException {
this.loadConfigFromLocalHost = false;
}

// validate only parameter
// validace konfiguracniho souboru
if (cmd.hasOption("v")) {
this.validateOnly = true;
} else {
this.validateOnly = false;
}

// show plugins
// napoveda k pouziti
if (cmd.hasOption("h")) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("java -jar NATT.jar", options);
System.exit(0);
}

// zobrazi vsechny nactene pluginy
if (cmd.hasOption("p")) {
this.pluginLoader.loadPlugins();
int i = 1;
logger.info("\nLoaded plugins:");
logger.info("Loaded plugins:");
for (INATTPlugin plugin : this.pluginLoader.getPlugins()) {
logger.info(String.format("%d: %s", i++, plugin.getName()));
}
System.exit(0);
}

// show keywords documentation
// zobrazi seznam vsech registrovanych keywordu (jejich nazvu)
if (cmd.hasOption("k")) {
logger.info("Registered keywords:");
for (Map.Entry<String, java.lang.Class<?>> entry : NATTContext.instance().getKeywordSet().entrySet()) {
try {
java.lang.Class<?> keywordClass = entry.getValue();
java.lang.reflect.Constructor<?> constructor = keywordClass.getDeclaredConstructor();
NATTKeyword keywordInstance = (NATTKeyword) constructor.newInstance();
if (keywordInstance != null) {
logger.info(keywordInstance.getKeywordName());
}
} catch (Exception e) {
logger.warning("Failed to create keyword instance for keyword " + entry.getKey() + ".");
}
}
}

// ve json formatu vypisi dokumentaci vsech registrovanych keyword
if (cmd.hasOption("kd")) {
LinkedList<KeywordDocumentation> documentationList = new LinkedList<KeywordDocumentation>();

for (Map.Entry<String, java.lang.Class<?>> entry : NATTContext.instance().getKeywordSet().entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.util.List;

import utb.fai.natt.spi.NATTModule;
import utb.fai.natt.spi.NATTAnnotation;
import utb.fai.natt.spi.exception.InternalErrorException;
import utb.fai.natt.spi.exception.NonUniqueModuleNamesException;

Expand All @@ -21,7 +20,6 @@
* Trida urcena pro spousteni externich aplikaci. Umoznuje spusteni, ukonceni,
* odeslani a prijimani zprav na standartni stream teto aplikace.
*/
@NATTAnnotation.Module("app.std.out")
public class ExternalProgramRunner extends NATTModule {

private NATTLogger logger = new NATTLogger(ExternalProgramRunner.class);
Expand Down
2 changes: 0 additions & 2 deletions natt/src/main/java/utb/fai/natt/module/MQTTBroker.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package utb.fai.natt.module;

import utb.fai.natt.spi.NATTModule;
import utb.fai.natt.spi.NATTAnnotation;
import utb.fai.natt.spi.exception.InternalErrorException;
import utb.fai.natt.spi.exception.NonUniqueModuleNamesException;

Expand All @@ -17,7 +16,6 @@
/**
* Modul umoznuje spustit MQTT broker
*/
@NATTAnnotation.Module("mqtt-broker")
public class MQTTBroker extends NATTModule {

protected NATTLogger logger = new NATTLogger(MQTTBroker.class);
Expand Down
2 changes: 0 additions & 2 deletions natt/src/main/java/utb/fai/natt/module/MQTTClientTester.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.eclipse.paho.client.mqttv3.MqttException;

import utb.fai.natt.spi.NATTModule;
import utb.fai.natt.spi.NATTAnnotation;
import utb.fai.natt.spi.exception.InternalErrorException;
import utb.fai.natt.spi.exception.NonUniqueModuleNamesException;

Expand All @@ -22,7 +21,6 @@
* tak jak prichazeji od komunikujici protistrany. Tag je vzdy nastaven na
* hodnotu topicu, ze ktereho zprava prisla
*/
@NATTAnnotation.Module("mqtt-client")
public class MQTTClientTester extends NATTModule {

protected NATTLogger logger = new NATTLogger(MQTTClientTester.class);
Expand Down
2 changes: 0 additions & 2 deletions natt/src/main/java/utb/fai/natt/module/RESTTester.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package utb.fai.natt.module;

import utb.fai.natt.spi.NATTModule;
import utb.fai.natt.spi.NATTAnnotation;
import utb.fai.natt.spi.exception.InternalErrorException;
import utb.fai.natt.spi.exception.NonUniqueModuleNamesException;

Expand Down Expand Up @@ -33,7 +32,6 @@
* je do message buferu vlozen chybovy status kod. Tag je nastaven ne adresu
* endpointu, ze ktereho zprava prisla jako odpoved na pozadavek.
*/
@NATTAnnotation.Module("rest-tester")
public class RESTTester extends NATTModule {

/**
Expand Down
2 changes: 0 additions & 2 deletions natt/src/main/java/utb/fai/natt/module/SMTPEmailServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.io.IOException;

import utb.fai.natt.spi.NATTModule;
import utb.fai.natt.spi.NATTAnnotation;
import utb.fai.natt.spi.exception.InternalErrorException;
import utb.fai.natt.spi.exception.NonUniqueModuleNamesException;

Expand All @@ -25,7 +24,6 @@
* tak jak prichazeji od odeslitele. Tag je vzdy nastaven na hodnotu "subject"
* emailu a obsah zpravy odpovida obsahu samotneho emailu.
*/
@NATTAnnotation.Module("email-server")
public class SMTPEmailServer extends NATTModule {

protected NATTLogger logger = new NATTLogger(SMTPEmailServer.class);
Expand Down
2 changes: 0 additions & 2 deletions natt/src/main/java/utb/fai/natt/module/SOAPTester.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package utb.fai.natt.module;

import utb.fai.natt.spi.NATTModule;
import utb.fai.natt.spi.NATTAnnotation;
import utb.fai.natt.spi.exception.InternalErrorException;
import utb.fai.natt.spi.exception.NonUniqueModuleNamesException;

Expand Down Expand Up @@ -32,7 +31,6 @@
* Prijate zpravy jsou do message bufferu ukladany v textove podobne prevedene
* do json formatu. Tag je vzdy prazdny ""
*/
@NATTAnnotation.Module("soap-tester")
public class SOAPTester extends NATTModule {

/**
Expand Down
2 changes: 0 additions & 2 deletions natt/src/main/java/utb/fai/natt/module/TelnetClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.net.Socket;

import utb.fai.natt.spi.NATTModule;
import utb.fai.natt.spi.NATTAnnotation;
import utb.fai.natt.spi.exception.InternalErrorException;
import utb.fai.natt.spi.exception.NonUniqueModuleNamesException;

Expand All @@ -21,7 +20,6 @@
* Prijate zpravy jsou do message bufferu ukladany v textove podobne neupravene
* tak jak prichazeji od komunikujici protistrany. Tag je vzdy prazdny ""
*/
@NATTAnnotation.Module("telnet-client")
public class TelnetClient extends NATTModule {

protected NATTLogger logger = new NATTLogger(TelnetClient.class);
Expand Down
Loading

0 comments on commit ded1d49

Please sign in to comment.