From a5b7b19f50443c30d6b9be5a6d0316a699643e0c Mon Sep 17 00:00:00 2001 From: Kevin Watters Date: Mon, 11 Dec 2023 13:43:00 -0500 Subject: [PATCH] Program ab unit test (#1381) * pesky open file handle when loading bot info... * removing jabba * updates to programab test to use a temp folder for it's work. --- .../org/myrobotlab/programab/BotInfo.java | 1 + .../org/myrobotlab/service/ProgramABTest.java | 100 +++++++++++------- .../ProgramAB/bots/lloyd/maps/starwars.txt | 1 - .../bots/lloyd/sets/starwarsnames.txt | 1 - 4 files changed, 63 insertions(+), 40 deletions(-) diff --git a/src/main/java/org/myrobotlab/programab/BotInfo.java b/src/main/java/org/myrobotlab/programab/BotInfo.java index 8fb3546513..7e8e416cbf 100644 --- a/src/main/java/org/myrobotlab/programab/BotInfo.java +++ b/src/main/java/org/myrobotlab/programab/BotInfo.java @@ -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); diff --git a/src/test/java/org/myrobotlab/service/ProgramABTest.java b/src/test/java/org/myrobotlab/service/ProgramABTest.java index a72f9e7b00..b6b7aa494c 100644 --- a/src/test/java/org/myrobotlab/service/ProgramABTest.java +++ b/src/test/java/org/myrobotlab/service/ProgramABTest.java @@ -8,9 +8,14 @@ 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; @@ -18,24 +23,49 @@ 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 ########################################"); @@ -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 res = testService.listPatterns(botname); @@ -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. @@ -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(); } @@ -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..."); @@ -211,6 +231,8 @@ public void testOOBTags() throws Exception { } } Assert.assertNotNull(Runtime.getService("python")); + + python.releaseService(); } @@ -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 locales = lloyd.getLocales(); + pikachu.addBotsDir(path + File.separator + "bots"); + pikachu.setCurrentBotName("pikachu"); + Map locales = pikachu.getLocales(); assertTrue(locales.size() > 0); assertTrue(locales.containsKey("ja")); + // release the service we created in this method. + pikachu.releaseService(); } @Test @@ -357,7 +380,6 @@ 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")); @@ -365,8 +387,10 @@ public void testDefaultSession() throws IOException { // 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 diff --git a/src/test/resources/ProgramAB/bots/lloyd/maps/starwars.txt b/src/test/resources/ProgramAB/bots/lloyd/maps/starwars.txt index 18f192609c..b2313eb6de 100644 --- a/src/test/resources/ProgramAB/bots/lloyd/maps/starwars.txt +++ b/src/test/resources/ProgramAB/bots/lloyd/maps/starwars.txt @@ -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 diff --git a/src/test/resources/ProgramAB/bots/lloyd/sets/starwarsnames.txt b/src/test/resources/ProgramAB/bots/lloyd/sets/starwarsnames.txt index 5766b16924..9991645d8c 100755 --- a/src/test/resources/ProgramAB/bots/lloyd/sets/starwarsnames.txt +++ b/src/test/resources/ProgramAB/bots/lloyd/sets/starwarsnames.txt @@ -2,5 +2,4 @@ LUKE 3PO LEAH HAN -JABBA PRINCESS LEAH