Skip to content

Commit

Permalink
Program ab unit test (#1381)
Browse files Browse the repository at this point in the history
* pesky open file handle when loading bot info...

* removing jabba

* updates to programab test to use a temp folder for it's work.
  • Loading branch information
kwatters authored Dec 11, 2023
1 parent 50eef50 commit a5b7b19
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 40 deletions.
1 change: 1 addition & 0 deletions src/main/java/org/myrobotlab/programab/BotInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public BotInfo(ProgramAB programab, File path) {
try {
FileInputStream fis = new FileInputStream(FileIO.gluePaths(path.getAbsolutePath(), "manifest.txt"));
properties.load(new InputStreamReader(fis, Charset.forName("UTF-8")));
fis.close();
log.info("loaded properties");
} catch (FileNotFoundException e) {
programab.warn("bot %s does not have a manifest.txt", name);
Expand Down
100 changes: 62 additions & 38 deletions src/test/java/org/myrobotlab/service/ProgramABTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,64 @@
import java.util.ArrayList;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.myrobotlab.framework.Service;
import org.myrobotlab.io.FileIO;
import org.myrobotlab.logging.LoggerFactory;
import org.myrobotlab.programab.Response;
import org.myrobotlab.service.data.Locale;
import org.slf4j.Logger;

/**
* ProgramABTest - This is the unit test to validate that the ProgramAB service continues
* to behave as expected.
*
* It tests the behavior of some reference sets of aiml.
*
* There are 2 AIML sets used in this unit test
* lloyd and pikachu.
*
* The lloyd aiml set is used to test most functionality,
* the pikachu aiml set is used to validate that japanese tokenization is working
*
*/
public class ProgramABTest extends AbstractServiceTest {

public final static Logger log = LoggerFactory.getLogger(ProgramABTest.class);
private String botname = "lloyd";
// TODO: move this to test resources
private String testResources = "src/test/resources/ProgramAB";

private String path = null;
private ProgramAB testService;


// the botname and test username to use (for most of the test suite)
private String botname = "lloyd";
private String username = "testUser";

public void addCategoryTest() throws IOException {
testService.addCategory("BOOG", "HOWDY");
Response resp = testService.getResponse(username, "BOOG");
assertTrue(resp.msg.equals("HOWDY"));
}

// The aiml sets will be modified in this test, to avoid the junit annotation makes sure this will
// be cleaned up after test execution.
// watch for open file handles which will prevent these directories from being deleted.
@Rule
public TemporaryFolder testFolder = new TemporaryFolder();
// the location of the test bot folders containing the aiml an dother configs.
private String testResources = "src/test/resources/ProgramAB";

public Service createService() {

// let's do a file copy to the temp folder.
try {
FileUtils.copyDirectory(new File(testResources), testFolder.getRoot());
log.info("Using JUnit temp folder: {}", testFolder);
} catch (IOException e) {
log.error("create temp folder threw", e);
return null;
}

try {
// LoggingFactory.init("INFO");
log.info("Setting up the Program AB Service ########################################");
Expand All @@ -44,33 +74,23 @@ public Service createService() {
// TODO: this should probably be created by Runtime,
// OOB tags might not know what the service name is ?!
testService = (ProgramAB) Runtime.start(botname, "ProgramAB");
testService.setPath(path);
// testService = new ProgramAB("simple");
// testService.setPath("c:/mrl/develop/ProgramAB");

// Important, tell this service to use the test folder as it's path for loading bots.
testService.setPath(testFolder.getRoot().getAbsolutePath());
// start the service.
testService.startService();
// load the bot brain for the chat with the user
testService.startSession(username, botname);
// clean out any aimlif the bot that might
// have been saved in a previous test run!
String aimlIFPath = path + "/bots/" + botname + "/aimlif";
File aimlIFPathF = new File(aimlIFPath);
if (aimlIFPathF.isDirectory()) {
for (File f : aimlIFPathF.listFiles()) {
// if there's a file here.
log.info("Deleting pre-existing AIMLIF files : {}", f.getAbsolutePath());
f.delete();
}
}
// TODO: same thing for predicates! (or other artifacts from a previous
// aiml
// test run)
testService.startSession(username, botname);
} catch (Exception e) {
log.error("createService threw", e);
}
return testService;
}

public void addCategoryTest() throws IOException {
testService.addCategory("BOOG", "HOWDY");
Response resp = testService.getResponse(username, "BOOG");
assertTrue(resp.msg.equals("HOWDY"));
}

public void listPatternsTest() {
ArrayList<String> res = testService.listPatterns(botname);
Expand Down Expand Up @@ -142,8 +162,7 @@ public void testAddEntryToSetAndMaps() throws IOException {
}

@Test
public void testJapanese() throws IOException {

public void testJapanese() throws IOException, InterruptedException {
ProgramAB pikachu = (ProgramAB) Runtime.start("pikachu", "ProgramAB");
pikachu.setPath(path);
// pikachu the service.
Expand All @@ -152,6 +171,7 @@ public void testJapanese() throws IOException {
pikachu.startSession(path, username, "pikachu", new java.util.Locale("ja"));
Response resp = pikachu.getResponse("私はケビンです");
assertEquals("あなたに会えてよかったケビン", resp.msg);
// Release the service we just created
pikachu.releaseService();
}

Expand Down Expand Up @@ -201,7 +221,7 @@ public void testOOBTags() throws Exception {
// wait up to 5 seconds for python service to start
long maxWait = 6000;
int i = 0;
Runtime.start("python", "Python");
Python python = (Python)Runtime.start("python", "Python");
while (Runtime.getService("python") == null) {
Thread.sleep(100);
log.info("Waiting for python to start...");
Expand All @@ -211,6 +231,8 @@ public void testOOBTags() throws Exception {
}
}
Assert.assertNotNull(Runtime.getService("python"));

python.releaseService();

}

Expand Down Expand Up @@ -314,20 +336,21 @@ public void testTopicCategories() throws IOException {

public void umlautTest() throws IOException {
Response resp = testService.getResponse(username, "Lars Ümlaüt");
// @GroG says - "this is not working"
assertEquals("He's a character from Guitar Hero!", resp.msg);
}

@Test
public void testLocales() {
// have locales
ProgramAB lloyd = (ProgramAB)Runtime.start("pikachu", "ProgramAB");
ProgramAB pikachu = (ProgramAB)Runtime.start("pikachu", "ProgramAB");
// lloyd.setPath(path);
lloyd.addBotsDir(path + File.separator + "bots");
lloyd.setCurrentBotName("pikachu");
Map<String, Locale> locales = lloyd.getLocales();
pikachu.addBotsDir(path + File.separator + "bots");
pikachu.setCurrentBotName("pikachu");
Map<String, Locale> locales = pikachu.getLocales();
assertTrue(locales.size() > 0);
assertTrue(locales.containsKey("ja"));
// release the service we created in this method.
pikachu.releaseService();
}

@Test
Expand Down Expand Up @@ -357,16 +380,17 @@ public void testDefaultSession() throws IOException {
lloyd.setPath(path);
lloyd.setCurrentBotName("lloyd");
assertTrue(lloyd.getBots().size() > 0);

// test for a response
Response response = lloyd.getResponse("Hello");
assertTrue(!response.msg.startsWith("I have no"));

// not sure if this is worth testing - there might be more
// assertEquals("Alice", alice.getCurrentBotName());
// assertEquals("default", alice.getCurrentUserName());
lloyd.releaseService();

}

// TODO - tests
// ProgramAB starts - it should find its own bot info's
// set username = default
Expand Down
1 change: 0 additions & 1 deletion src/test/resources/ProgramAB/bots/lloyd/maps/starwars.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ LUKE:Luke Skywalker
3PO:C-3PO
LEAH:Princess Leia Organa
HAN:Han Solo
JABBA:Jabba the Hut
PRINCESS LEAH:Princess Leia Organa
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ LUKE
3PO
LEAH
HAN
JABBA
PRINCESS LEAH

0 comments on commit a5b7b19

Please sign in to comment.