diff options
author | liamfallon <liam.fallon@ericsson.com> | 2018-09-28 12:11:59 +0100 |
---|---|---|
committer | liamfallon <liam.fallon@ericsson.com> | 2018-09-28 12:12:06 +0100 |
commit | 1899306c1087d55f2e923f32a7e9ea038aa5275a (patch) | |
tree | 8cc0270f6477b138cdc2b3a4eccf6dcafad30057 /core/core-deployment/src/test/java | |
parent | d67a4dbff408ebf73a25158c85169fb7165e357e (diff) |
Add unit test for core distribution
JUnit for the batch deployer and deploy facade.
Issue-ID: POLICY-1034
Change-Id: I3959578ba8666d64b48bbe4ee9aeab27b92d77ae
Signed-off-by: liamfallon <liam.fallon@ericsson.com>
Diffstat (limited to 'core/core-deployment/src/test/java')
4 files changed, 815 insertions, 35 deletions
diff --git a/core/core-deployment/src/test/java/org/onap/policy/apex/core/deployment/BatchDeployerTest.java b/core/core-deployment/src/test/java/org/onap/policy/apex/core/deployment/BatchDeployerTest.java new file mode 100644 index 000000000..fb2d2467b --- /dev/null +++ b/core/core-deployment/src/test/java/org/onap/policy/apex/core/deployment/BatchDeployerTest.java @@ -0,0 +1,202 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.core.deployment; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.PrintStream; + +import org.junit.Test; +import org.onap.policy.apex.model.basicmodel.concepts.ApexException; +import org.onap.policy.apex.model.basicmodel.handling.ApexModelException; +import org.onap.policy.apex.model.basicmodel.handling.ApexModelReader; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; + +/** + * Test the periodic event manager utility. + */ +public class BatchDeployerTest { + @Test + public void testBatchDeployerBad() { + try { + final String[] eventArgs = + { "-h" }; + + BatchDeployer.main(eventArgs); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("invalid arguments: [-h]", exc.getMessage().substring(0, 23)); + } + } + + @Test + public void testBatchDeployerBadPort() { + try { + final String[] eventArgs = + { "localhost", "aport", "afile" }; + + BatchDeployer.main(eventArgs); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("argument port is invalid", exc.getMessage().substring(0, 24)); + } + } + + @Test + public void testBatchDeployerOk() { + try { + final String[] eventArgs = + { "Host", "43443", "src/test/resources/models/SamplePolicyModelJAVASCRIPT.json" }; + + BatchDeployer.main(eventArgs); + } catch (Exception exc) { + assertEquals("model deployment failed on parameters Host 43443", exc.getMessage()); + } + } + + @Test + public void testBatchDeployerDeployString() { + final ByteArrayOutputStream baosOut = new ByteArrayOutputStream(); + + BatchDeployer deployer = new BatchDeployer("localhost", 12345, new PrintStream(baosOut, true)); + deployer.getEngineServiceFacade().setDeploymentClient(new DummyDeploymentClient("aHost", 54553)); + + try { + deployer.init(); + } catch (ApexDeploymentException ade) { + assertEquals("model deployment failed on parameters localhost 12345 true", ade.getMessage()); + } + + try { + deployer.init(); + } catch (ApexDeploymentException ade) { + ade.printStackTrace(); + fail("test should not throw an exception"); + } + + try { + deployer.deployModel("src/test/resources/models/SamplePolicyModelJAVASCRIPT.json", false, false); + } catch (ApexException ade) { + assertEquals("could not deploy apex model from src/test/resources/models/SamplePolicyModelJAVASCRIPT.json", + ade.getMessage()); + } + + try { + deployer.deployModel("src/test/resources/models/SamplePolicyModelJAVASCRIPT.json", false, false); + } catch (ApexException ade) { + fail("test should not throw an exception"); + } + + deployer.close(); + } + + @Test + public void testBatchDeployerStream() throws ApexModelException, FileNotFoundException { + + final ByteArrayOutputStream baosOut = new ByteArrayOutputStream(); + + BatchDeployer deployer = new BatchDeployer("localhost", 12345, new PrintStream(baosOut, true)); + deployer.getEngineServiceFacade().setDeploymentClient(new DummyDeploymentClient("aHost", 54553)); + + try { + deployer.init(); + } catch (ApexDeploymentException ade) { + assertEquals("model deployment failed on parameters localhost 12345 true", ade.getMessage()); + } + + try { + deployer.init(); + } catch (ApexDeploymentException ade) { + ade.printStackTrace(); + fail("test should not throw an exception"); + } + + final ApexModelReader<AxPolicyModel> modelReader = new ApexModelReader<>(AxPolicyModel.class); + modelReader.setValidateFlag(false); + final AxPolicyModel apexPolicyModel = modelReader.read( + new FileInputStream(new File("src/test/resources/models/SamplePolicyModelJAVASCRIPT.json"))); + + try { + deployer.deployModel(apexPolicyModel, false, false); + } catch (ApexException ade) { + assertEquals("failed response Operation failed received from serverlocalhost:12345", ade.getMessage()); + } + + try { + deployer.deployModel(apexPolicyModel, false, false); + } catch (ApexException ade) { + fail("test should not throw an exception"); + } + + deployer.close(); + } + + @Test + public void testBatchDeployerUninitialized() { + final ByteArrayOutputStream baosOut = new ByteArrayOutputStream(); + + BatchDeployer deployer = new BatchDeployer("localhost", 12345, new PrintStream(baosOut, true)); + deployer.getEngineServiceFacade().setDeploymentClient(new DummyDeploymentClient("aHost", 54553)); + + try { + deployer.deployModel("src/test/resources/models/SamplePolicyModelJAVASCRIPT.json", false, false); + fail("test should throw an exception"); + } catch (ApexException ade) { + assertEquals("cound not deploy apex model, deployer is not initialized", ade.getMessage()); + } + + try { + deployer.deployModel("src/test/resources/models/SamplePolicyModelJAVASCRIPT.json", false, false); + fail("test should throw an exception"); + } catch (ApexException ade) { + assertEquals("cound not deploy apex model, deployer is not initialized", ade.getMessage()); + } + + deployer.close(); + } + + @Test + public void testBatchDeployerStreamUninitialized() throws ApexModelException, FileNotFoundException { + final ByteArrayOutputStream baosOut = new ByteArrayOutputStream(); + + BatchDeployer deployer = new BatchDeployer("localhost", 12345, new PrintStream(baosOut, true)); + deployer.getEngineServiceFacade().setDeploymentClient(new DummyDeploymentClient("aHost", 54553)); + + final ApexModelReader<AxPolicyModel> modelReader = new ApexModelReader<>(AxPolicyModel.class); + modelReader.setValidateFlag(false); + final AxPolicyModel apexPolicyModel = modelReader.read( + new FileInputStream(new File("src/test/resources/models/SamplePolicyModelJAVASCRIPT.json"))); + + try { + deployer.deployModel(apexPolicyModel, false, false); + fail("test should throw an exception"); + } catch (ApexException ade) { + assertEquals("failed response Operation failed received from serverlocalhost:12345", ade.getMessage()); + } + + deployer.close(); + } +} diff --git a/core/core-deployment/src/test/java/org/onap/policy/apex/core/deployment/DummyDeploymentClient.java b/core/core-deployment/src/test/java/org/onap/policy/apex/core/deployment/DummyDeploymentClient.java new file mode 100644 index 000000000..d37bf3435 --- /dev/null +++ b/core/core-deployment/src/test/java/org/onap/policy/apex/core/deployment/DummyDeploymentClient.java @@ -0,0 +1,222 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.core.deployment; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; + +import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities; +import org.onap.policy.apex.core.protocols.Message; +import org.onap.policy.apex.core.protocols.engdep.messages.EngineServiceInfoResponse; +import org.onap.policy.apex.core.protocols.engdep.messages.GetEngineInfo; +import org.onap.policy.apex.core.protocols.engdep.messages.GetEngineServiceInfo; +import org.onap.policy.apex.core.protocols.engdep.messages.GetEngineStatus; +import org.onap.policy.apex.core.protocols.engdep.messages.Response; +import org.onap.policy.apex.core.protocols.engdep.messages.StartEngine; +import org.onap.policy.apex.core.protocols.engdep.messages.StartPeriodicEvents; +import org.onap.policy.apex.core.protocols.engdep.messages.StopEngine; +import org.onap.policy.apex.core.protocols.engdep.messages.StopPeriodicEvents; +import org.onap.policy.apex.core.protocols.engdep.messages.UpdateModel; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.utilities.TextFileUtils; + +/** + * Dummy deployment client. + */ +public class DummyDeploymentClient extends DeploymentClient implements Runnable { + private static final AxArtifactKey MODEL_KEY = new AxArtifactKey("Model", "0.0.1"); + private static final AxArtifactKey ENGINE_KEY = new AxArtifactKey("Engine", "0.0.1"); + private static final AxArtifactKey ENGINE_SERVICE_KEY = new AxArtifactKey("EngineService", "0.0.1"); + + private Thread thisThread; + + private final BlockingQueue<Message> receiveQueue = new LinkedBlockingQueue<>(); + + private boolean started = false; + + private boolean initSuccessful = false; + private boolean deployModelSuccessful = false; + private boolean startEngineSuccessful = false; + private boolean stopEngineSuccessful = false; + private boolean startPeriodicSuccessful = false; + private boolean stopPeriodicSuccessful = false; + private boolean statusSuccessful = false; + private boolean infoSuccessful = false; + + public DummyDeploymentClient(String host, int port) { + super(host, port); + } + + /** + * {@inheritDoc} + */ + @Override + public void run() { + // Set up the thread name + thisThread = Thread.currentThread(); + thisThread.setName(DeploymentClient.class.getName() + "-" + getHost() + ":" + getPort()); + + started = true; + + // Loop forever, sending messages as they appear on the queue + while (started && !thisThread.isInterrupted()) { + ThreadUtilities.sleep(50); + } + + // Thread has been interrupted + thisThread = null; + started = false; + } + + /** + * Send an EngDep message to the Apex server. + * + * @param message the message to send to the Apex server + */ + public void sendMessage(final Message message) { + if (message instanceof GetEngineServiceInfo) { + handleEngineServiceInfo(message); + } else if (message instanceof UpdateModel) { + deployModelSuccessful = handleAndReturnMessage(message, deployModelSuccessful); + } else if (message instanceof StartEngine) { + startEngineSuccessful = handleAndReturnMessage(message, startEngineSuccessful); + } else if (message instanceof StopEngine) { + stopEngineSuccessful = handleAndReturnMessage(message, stopEngineSuccessful); + } else if (message instanceof StartPeriodicEvents) { + startPeriodicSuccessful = handleAndReturnMessage(message, startPeriodicSuccessful); + } else if (message instanceof StopPeriodicEvents) { + stopPeriodicSuccessful = handleAndReturnMessage(message, stopPeriodicSuccessful); + } else if (message instanceof GetEngineStatus) { + statusSuccessful = handleAndReturnEngineStatus(message, statusSuccessful); + } else if (message instanceof GetEngineInfo) { + infoSuccessful = handleAndReturnMessage(message, infoSuccessful); + } + } + + /** + * Handle the EngineServiceInfo message. + * + * @param message the EngineServiceInfo message + */ + private void handleEngineServiceInfo(final Message message) { + EngineServiceInfoResponse infoResponse = new EngineServiceInfoResponse(ENGINE_KEY, initSuccessful, message); + infoResponse.setApexModelKey(MODEL_KEY); + + List<AxArtifactKey> engineKeyList = new ArrayList<>(); + engineKeyList.add(ENGINE_KEY); + infoResponse.setEngineKeyArray(engineKeyList); + + infoResponse.setEngineServiceKey(ENGINE_SERVICE_KEY); + + receiveQueue.add(infoResponse); + + initSuccessful = !initSuccessful; + } + + /** + * Handle and return the response to the engine status message. + * + * @param message the incoming status message + * @param successFlag true if the result should be successful + * @return + */ + private boolean handleAndReturnEngineStatus(Message message, boolean successFlag) { + if ("DoNotRespond".equals(message.getTarget().getName())) { + return !successFlag; + } + + if ("ReturnBadMessage".equals(message.getTarget().getName())) { + receiveQueue.add(message); + return !successFlag; + } + + if ("ReturnBadResponse".equals(message.getTarget().getName())) { + Response badResponse = new Response(ENGINE_KEY, successFlag,new StartEngine(message.getTarget())); + receiveQueue.add(badResponse); + return !successFlag; + } + + Response response = new Response(ENGINE_KEY, successFlag, message); + + if (successFlag) { + try { + response.setMessageData(TextFileUtils + .getTextFileAsString("src/test/resources/models/SamplePolicyModelJAVASCRIPT.json")); + } catch (IOException e) { + e.printStackTrace(); + } + } else { + response.setMessageData("Operation failed"); + } + + receiveQueue.add(response); + return !successFlag; + } + + /** + * Handle and return a message. + * + * @param message the message + */ + private boolean handleAndReturnMessage(final Message message, final boolean successFlag) { + Response response = new Response(ENGINE_KEY, successFlag, message); + + if (successFlag) { + response.setMessageData("Operation was successful"); + } else { + response.setMessageData("Operation failed"); + } + + receiveQueue.add(response); + return !successFlag; + } + + /** + * Stop the deployment client. + */ + public void stopClient() { + if (thisThread != null) { + thisThread.interrupt(); + } + started = false; + } + + /** + * Checks if the client thread is started. + * + * @return true, if the client thread is started + */ + public boolean isStarted() { + return started; + } + + /** + * Allows users of this class to get a reference to the receive queue to receove messages. + * + * @return the receive queue + */ + public BlockingQueue<Message> getReceiveQueue() { + return receiveQueue; + } +} diff --git a/core/core-deployment/src/test/java/org/onap/policy/apex/core/deployment/EngineServiceFacadeTest.java b/core/core-deployment/src/test/java/org/onap/policy/apex/core/deployment/EngineServiceFacadeTest.java new file mode 100644 index 000000000..414bd4144 --- /dev/null +++ b/core/core-deployment/src/test/java/org/onap/policy/apex/core/deployment/EngineServiceFacadeTest.java @@ -0,0 +1,214 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.core.deployment; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.fail; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; + +import org.junit.Test; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; + +/** + * Test the deployment web socket client. + */ +public class EngineServiceFacadeTest { + @Test + public void testEngineServiceFacade() throws Exception { + EngineServiceFacade facade = new EngineServiceFacade("localhost", 51273); + + facade.setDeploymentClient(new DummyDeploymentClient("localhost", 51273)); + + // First init should fail + facade.init(); + + assertNull(facade.getKey()); + assertNull(facade.getApexModelKey()); + assertNull(facade.getEngineKeyArray()); + + try { + facade.deployModel("src/test/resources/models/SamplePolicyModelJAVASCRIPT.json", false, false); + fail("test should throw an exception here"); + } catch (final Exception ade) { + assertEquals("cound not deploy apex model, deployer is not initialized", ade.getMessage()); + } + + // Second init should work + facade.init(); + + assertEquals("EngineService:0.0.1", facade.getKey().getId()); + assertEquals("Model:0.0.1", facade.getApexModelKey().getId()); + assertEquals("Engine:0.0.1", facade.getEngineKeyArray()[0].getId()); + + try { + facade.deployModel("src/test/resources/models/NonExistantModel.json", false, false); + fail("test should throw an exception here"); + } catch (final Exception ade) { + assertEquals("cound not create apex model, could not read from file " + + "src/test/resources/models/NonExistantModel.json", ade.getMessage()); + } + + try { + facade.deployModel("src/test/resources/models/JunkModel.json", false, false); + fail("test should throw an exception here"); + } catch (final Exception ade) { + assertEquals("could not deploy apex model from src/test/resources/models/JunkModel.json", ade.getMessage()); + } + + InputStream badStream = new ByteArrayInputStream("".getBytes()); + try { + facade.deployModel("MyModel", badStream, false, false); + fail("test should throw an exception here"); + } catch (final Exception ade) { + assertEquals("format of input for Apex concept is neither JSON nor XML", ade.getMessage()); + } + + InputStream closedStream = new ByteArrayInputStream("".getBytes()); + closedStream.close(); + try { + facade.deployModel("MyModel", closedStream, false, false); + fail("test should throw an exception here"); + } catch (final Exception ade) { + assertEquals("format of input for Apex concept is neither JSON nor XML", ade.getMessage()); + } + + try { + facade.deployModel("src/test/resources/models/SamplePolicyModelJAVASCRIPT.json", false, false); + fail("test should throw an exception here"); + } catch (final Exception ade) { + assertEquals("could not deploy apex model from src/test/resources/models/SamplePolicyModelJAVASCRIPT.json", + ade.getMessage()); + } + + try { + facade.deployModel("src/test/resources/models/SamplePolicyModelJAVASCRIPT.json", false, false); + } catch (final Exception ade) { + fail("test should not throw an exception here"); + } + + try { + facade.startEngine(facade.getEngineKeyArray()[0]); + fail("test should throw an exception here"); + } catch (final Exception ade) { + assertEquals("failed response Operation failed received from serverlocalhost:51273", ade.getMessage()); + } + + try { + facade.startEngine(facade.getEngineKeyArray()[0]); + } catch (final Exception ade) { + fail("test should not throw an exception here"); + } + + try { + facade.stopEngine(facade.getEngineKeyArray()[0]); + fail("test should throw an exception here"); + } catch (final Exception ade) { + assertEquals("failed response Operation failed received from serverlocalhost:51273", ade.getMessage()); + } + + try { + facade.stopEngine(facade.getEngineKeyArray()[0]); + } catch (final Exception ade) { + fail("test should not throw an exception here"); + } + + try { + facade.startPerioidicEvents(facade.getEngineKeyArray()[0], 1000); + fail("test should throw an exception here"); + } catch (final Exception ade) { + assertEquals("failed response Operation failed received from serverlocalhost:51273", ade.getMessage()); + } + + try { + facade.startPerioidicEvents(facade.getEngineKeyArray()[0], 1000); + } catch (final Exception ade) { + fail("test should not throw an exception here"); + } + + try { + facade.stopPerioidicEvents(facade.getEngineKeyArray()[0]); + fail("test should throw an exception here"); + } catch (final Exception ade) { + assertEquals("failed response Operation failed received from serverlocalhost:51273", ade.getMessage()); + } + + try { + facade.stopPerioidicEvents(facade.getEngineKeyArray()[0]); + } catch (final Exception ade) { + fail("test should not throw an exception here"); + } + + try { + facade.getEngineStatus(facade.getEngineKeyArray()[0]); + fail("test should throw an exception here"); + } catch (final Exception ade) { + assertEquals("failed response Operation failed received from serverlocalhost:51273", ade.getMessage()); + } + + try { + facade.getEngineStatus(facade.getEngineKeyArray()[0]); + } catch (final Exception ade) { + fail("test should not throw an exception here"); + } + + try { + facade.getEngineInfo(facade.getEngineKeyArray()[0]); + fail("test should throw an exception here"); + } catch (final Exception ade) { + assertEquals("failed response Operation failed received from serverlocalhost:51273", ade.getMessage()); + } + + try { + facade.getEngineInfo(facade.getEngineKeyArray()[0]); + } catch (final Exception ade) { + fail("test should not throw an exception here"); + } + + try { + facade.getEngineStatus(new AxArtifactKey("ReturnBadMessage", "0.0.1")); + fail("test should throw an exception here"); + } catch (final Exception ade) { + assertEquals("response received from server is of incorrect type " + + "org.onap.policy.apex.core.protocols.engdep.messages.GetEngineStatus, should be of type " + + "org.onap.policy.apex.core.protocols.engdep.messages.Response", ade.getMessage()); + } + + try { + facade.getEngineStatus(new AxArtifactKey("ReturnBadResponse", "0.0.1")); + fail("test should throw an exception here"); + } catch (final Exception ade) { + assertEquals("response received is not correct response to sent message GET_ENGINE_STATUS", + ade.getMessage()); + } + + try { + facade.getEngineStatus(new AxArtifactKey("DoNotRespond", "0.0.1")); + fail("test should throw an exception here"); + } catch (final Exception ade) { + assertEquals("no response received to sent message GET_ENGINE_STATUS", ade.getMessage()); + } + + facade.close(); + } +} diff --git a/core/core-deployment/src/test/java/org/onap/policy/apex/core/deployment/PeriodicEventManagerTest.java b/core/core-deployment/src/test/java/org/onap/policy/apex/core/deployment/PeriodicEventManagerTest.java index fb204c3a3..816c528bb 100644 --- a/core/core-deployment/src/test/java/org/onap/policy/apex/core/deployment/PeriodicEventManagerTest.java +++ b/core/core-deployment/src/test/java/org/onap/policy/apex/core/deployment/PeriodicEventManagerTest.java @@ -36,82 +36,225 @@ import org.junit.Test; */ public class PeriodicEventManagerTest { @Test - public void testPeroidicEventManager() { + public void testPeroidicEventManagerBad() { try { - final String[] EventArgs = + final String[] eventArgs = { "-h" }; - PeriodicEventManager.main(EventArgs); + PeriodicEventManager.main(eventArgs); + fail("test should throw an exception"); } catch (Exception exc) { - fail("test should not throw an exception"); + assertEquals("invalid arguments: [-h]", exc.getMessage().substring(0, 23)); + } + } + + @Test + public void testPeroidicEventManagerOk() { + try { + final String[] eventArgs = + { "Host", "43443", "start", "1000" }; + + PeriodicEventManager.main(eventArgs); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("periodic event setting failed on parameters Host 43443 true", exc.getMessage()); } } @Test public void testPeroidicEventManagerNoOptions() { - final String[] EventArgs = new String[] + final String[] eventArgs = new String[] {}; - final String outputString = runPeriodicEventManager(EventArgs); + final String outputString = testPeriodicEventManagerConstructor(eventArgs); - assertTrue(outputString - .contains("usage: Deployer <server address> <port address> <start/stop> <periods in ms>")); + assertTrue(outputString.contains( + "usage: PeriodicEventManager <server address> <port address> <start/stop> <periods in ms>")); } @Test public void testPeroidicEventManagerBadOptions() { - final String[] EventArgs = + final String[] eventArgs = { "-zabbu" }; - final String outputString = runPeriodicEventManager(EventArgs); + final String outputString = testPeriodicEventManagerConstructor(eventArgs); - assertTrue(outputString - .contains("usage: Deployer <server address> <port address> <start/stop> <periods in ms>")); + assertTrue(outputString.contains( + "usage: PeriodicEventManager <server address> <port address> <start/stop> <periods in ms>")); } @Test public void testPeroidicEventManagerNonNumeric3() { - final String[] EventArgs = + final String[] eventArgs = { "aaa", "bbb", "ccc", "ddd" }; + final String outputString = testPeriodicEventManagerConstructor(eventArgs); + + assertTrue(outputString.contains("argument port is invalid")); + } + + @Test + public void testPeroidicEventManagerNonNumeric2() { + final String[] eventArgs = + { "aaa", "12345", "start", "stop" }; + + final String outputString = testPeriodicEventManagerConstructor(eventArgs); + + assertTrue(outputString.contains("argument period is invalid")); + } + + @Test + public void testPeroidicEventManagerNotStartStop() { + final String[] eventArgs = + { "aaa", "12345", "1000", "1000" }; + + final String outputString = testPeriodicEventManagerConstructor(eventArgs); + + assertTrue(outputString.contains("argument 1000 must be \"start\" or \"stop\"")); + } + + @Test + public void testPeroidicEventManagerStart() { + final String[] eventArgs = + { "localhost", "12345", "start", "1000" }; + + final ByteArrayOutputStream baosOut = new ByteArrayOutputStream(); + + PeriodicEventManager peManager = null; + try { + peManager = new PeriodicEventManager(eventArgs, new PrintStream(baosOut, true)); + peManager.getEngineServiceFacade().setDeploymentClient(new DummyDeploymentClient("aHost", 54553)); + } catch (ApexDeploymentException ade) { + fail("test should not throw an exception"); + } + + try { + peManager.init(); + } catch (ApexDeploymentException ade) { + assertEquals("model deployment failed on parameters localhost 12345 true", ade.getMessage()); + } + try { - runPeriodicEventManager(EventArgs); - } catch (NumberFormatException nfe) { - assertEquals("For input string: \"bbb\"", nfe.getMessage()); + peManager.init(); + } catch (ApexDeploymentException ade) { + ade.printStackTrace(); + fail("test should not throw an exception"); + } + + try { + peManager.runCommand(); + } catch (ApexDeploymentException ade) { + assertEquals("failed response Operation failed received from serverlocalhost:12345", ade.getMessage()); } + + try { + peManager.runCommand(); + } catch (ApexDeploymentException ade) { + fail("test should not throw an exception"); + } + + peManager.close(); } @Test - public void testPeroidicEventManagerNonNumeric2() { - final String[] EventArgs = - { "aaa", "12345", "ccc", "1000" }; + public void testPeroidicEventManagerStop() { + final String[] eventArgs = + { "localhost", "12345", "stop", "1000" }; + + final ByteArrayOutputStream baosOut = new ByteArrayOutputStream(); + + PeriodicEventManager peManager = null; + try { + peManager = new PeriodicEventManager(eventArgs, new PrintStream(baosOut, true)); + peManager.getEngineServiceFacade().setDeploymentClient(new DummyDeploymentClient("aHost", 54553)); + } catch (ApexDeploymentException ade) { + fail("test should not throw an exception"); + } try { - runPeriodicEventManager(EventArgs); - } catch (NumberFormatException nfe) { - assertEquals("For input string: \"ddd\"", nfe.getMessage()); + peManager.init(); + } catch (ApexDeploymentException ade) { + assertEquals("model deployment failed on parameters localhost 12345 true", ade.getMessage()); + } + + try { + peManager.init(); + } catch (ApexDeploymentException ade) { + ade.printStackTrace(); + fail("test should not throw an exception"); + } + + try { + peManager.runCommand(); + } catch (ApexDeploymentException ade) { + assertEquals("failed response Operation failed received from serverlocalhost:12345", ade.getMessage()); } + + try { + peManager.runCommand(); + } catch (ApexDeploymentException ade) { + fail("test should not throw an exception"); + } + + peManager.close(); } @Test - public void testPeroidicEventManagerStart() { - final String[] EventArgs = + public void testPeroidicEventManagerStartUninitialized() { + final String[] eventArgs = { "localhost", "12345", "start", "1000" }; - final String outputString = runPeriodicEventManager(EventArgs); + final ByteArrayOutputStream baosOut = new ByteArrayOutputStream(); + + PeriodicEventManager peManager = null; + try { + peManager = new PeriodicEventManager(eventArgs, new PrintStream(baosOut, true)); + peManager.getEngineServiceFacade().setDeploymentClient(new DummyDeploymentClient("aHost", 54553)); + } catch (ApexDeploymentException ade) { + fail("test should not throw an exception"); + } - assertTrue(outputString.contains("\n*** StdErr ***\n\n*** exception ***")); + try { + peManager.runCommand(); + fail("test should throw an exception"); + } catch (ApexDeploymentException ade) { + assertEquals("connection to apex is not initialized", ade.getMessage()); + } + + try { + peManager.runCommand(); + fail("test should throw an exception"); + } catch (ApexDeploymentException ade) { + assertEquals("connection to apex is not initialized", ade.getMessage()); + ade.printStackTrace(); + } + + peManager.close(); } - @Test - public void testPeroidicEventManagerStop() { - final String[] EventArgs = + public void testPeroidicEventManagerStopUninitialized() { + final String[] eventArgs = { "localhost", "12345", "stop", "1000" }; - final String outputString = runPeriodicEventManager(EventArgs); + final ByteArrayOutputStream baosOut = new ByteArrayOutputStream(); + + PeriodicEventManager peManager = null; + try { + peManager = new PeriodicEventManager(eventArgs, new PrintStream(baosOut, true)); + peManager.getEngineServiceFacade().setDeploymentClient(new DummyDeploymentClient("aHost", 54553)); + } catch (ApexDeploymentException ade) { + fail("test should not throw an exception"); + } - assertTrue(outputString.contains("\n*** StdErr ***\n\n*** exception ***")); + try { + peManager.runCommand(); + fail("test should throw an exception"); + } catch (ApexDeploymentException ade) { + assertEquals("connection to apex is not initialized", ade.getMessage()); + } + + peManager.close(); } /** @@ -120,15 +263,14 @@ public class PeriodicEventManagerTest { * @param eventArgs the command arguments * @return a string containing the command output */ - private String runPeriodicEventManager(final String[] eventArgs) { + private String testPeriodicEventManagerConstructor(final String[] eventArgs) { final ByteArrayOutputStream baosOut = new ByteArrayOutputStream(); final ByteArrayOutputStream baosErr = new ByteArrayOutputStream(); - PeriodicEventManager peManager = new PeriodicEventManager(eventArgs, new PrintStream(baosOut, true)); - String exceptionString = ""; try { - peManager.init(); + PeriodicEventManager peManager = new PeriodicEventManager(eventArgs, new PrintStream(baosOut, true)); + peManager.getEngineServiceFacade().setDeploymentClient(new DummyDeploymentClient("aHost", 54553)); } catch (ApexDeploymentException ade) { exceptionString = ade.getCascadedMessage(); } |