diff options
Diffstat (limited to 'src/test/java')
-rw-r--r-- | src/test/java/pl/edu/mimuw/cloudatlas/agent/MockExecutor.java | 24 | ||||
-rw-r--r-- | src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/StanikTest.java | 49 |
2 files changed, 73 insertions, 0 deletions
diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/agent/MockExecutor.java b/src/test/java/pl/edu/mimuw/cloudatlas/agent/MockExecutor.java new file mode 100644 index 0000000..1da50ad --- /dev/null +++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/MockExecutor.java @@ -0,0 +1,24 @@ +package pl.edu.mimuw.cloudatlas.agent; + +import java.util.concurrent.LinkedBlockingQueue; + +import pl.edu.mimuw.cloudatlas.agent.modules.Module; +import pl.edu.mimuw.cloudatlas.agent.messages.AgentMessage; + +/* + * Instead of passing messages to an EventBus, this one just collects them + * locally for inspection in unit tests. + */ +public class MockExecutor extends Executor { + public LinkedBlockingQueue<AgentMessage> messagesToPass; + + public MockExecutor(Module module) { + super(module); + messagesToPass = new LinkedBlockingQueue<AgentMessage>(); + } + + @Override + public void passMessage(AgentMessage message) throws InterruptedException { + messagesToPass.put(message); + } +} diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/StanikTest.java b/src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/StanikTest.java new file mode 100644 index 0000000..7c869e7 --- /dev/null +++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/StanikTest.java @@ -0,0 +1,49 @@ +package pl.edu.mimuw.cloudatlas.agent.modules; + +import java.util.List; +import java.util.Map.Entry; + +import pl.edu.mimuw.cloudatlas.agent.messages.AgentMessage; +import pl.edu.mimuw.cloudatlas.agent.messages.GetHierarchyMessage; +import pl.edu.mimuw.cloudatlas.agent.messages.HierarchyMessage; +import pl.edu.mimuw.cloudatlas.agent.messages.ResponseMessage; +import pl.edu.mimuw.cloudatlas.agent.MockExecutor; +import pl.edu.mimuw.cloudatlas.model.Attribute; +import pl.edu.mimuw.cloudatlas.model.Value; +import pl.edu.mimuw.cloudatlas.model.ZMI; + +import org.junit.Before; +import org.junit.Test; +import static org.junit.Assert.*; + +public class StanikTest { + private Stanik stanik; + private MockExecutor executor; + + @Before + public void setupEventBus() { + stanik = new Stanik(); + executor = new MockExecutor(stanik); + } + + @Test + public void getEmptyHierarchy() throws Exception { + GetHierarchyMessage message = new GetHierarchyMessage("test_msg", 0, ModuleType.TEST, 42); + stanik.handleTyped(message); + assertEquals(1, executor.messagesToPass.size()); + ResponseMessage receivedMessage = (ResponseMessage) executor.messagesToPass.take(); + assertEquals(ModuleType.TEST, receivedMessage.getDestinationModule()); + assertEquals(ResponseMessage.Type.HIERARCHY, receivedMessage.getType()); + assertEquals(42, receivedMessage.getRequestId()); + HierarchyMessage hierarchyMessage = (HierarchyMessage) receivedMessage; + ZMI zmi = hierarchyMessage.getZMI(); + assertNull(zmi.getFather()); + assertTrue(zmi.getSons().isEmpty()); + boolean empty = true; + for (Entry<Attribute, Value> entry : zmi.getAttributes()) { + empty = false; + break; + } + assertTrue(empty); + } +} |