diff options
Diffstat (limited to 'src/test/java/pl')
8 files changed, 391 insertions, 6 deletions
diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/Container.java b/src/test/java/pl/edu/mimuw/cloudatlas/Container.java new file mode 100644 index 0000000..db5a156 --- /dev/null +++ b/src/test/java/pl/edu/mimuw/cloudatlas/Container.java @@ -0,0 +1,5 @@ +package pl.edu.mimuw.cloudatlas; + +public class Container<T> { + public T thing; +} diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/agent/AgentIntegrationTest.java b/src/test/java/pl/edu/mimuw/cloudatlas/agent/AgentIntegrationTest.java index 33fb56f..fcdfb47 100644 --- a/src/test/java/pl/edu/mimuw/cloudatlas/agent/AgentIntegrationTest.java +++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/AgentIntegrationTest.java @@ -110,8 +110,7 @@ public class AgentIntegrationTest { String name = "&query"; String queryCode = "SELECT 1 AS one"; api.installQuery(name, queryCode); - AttributesMap attributes = api.getZoneAttributeValues("/pjwstk"); - assertEquals(new ValueQuery(queryCode), attributes.get(name)); + // TODO: test something here } @Test @@ -119,8 +118,9 @@ public class AgentIntegrationTest { String name = "&query"; String queryCode = "SELECT 1 AS one"; api.installQuery(name, queryCode); - AttributesMap attributes = api.getZoneAttributeValues("/pjwstk"); - assertEquals(new ValueInt(1l), attributes.get("one")); + // TODO: test this eventually runs + // AttributesMap attributes = api.getZoneAttributeValues("/pjwstk"); + // assertEquals(new ValueInt(1l), attributes.get("one")); } @Test @@ -131,6 +131,7 @@ public class AgentIntegrationTest { api.uninstallQuery(name); AttributesMap attributes = api.getZoneAttributeValues("/pjwstk"); assertNull(attributes.getOrNull(name)); + // TODO: test this correctly } @Test diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementationTests.java b/src/test/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementationTests.java index c964ed9..0a7a8df 100644 --- a/src/test/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementationTests.java +++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementationTests.java @@ -3,7 +3,6 @@ package pl.edu.mimuw.cloudatlas.agent; import org.junit.Before; import org.junit.Test; import static org.junit.Assert.*; -import static org.junit.Assert.assertThat; import static org.hamcrest.CoreMatchers.hasItems; import java.util.ArrayList; diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/agent/MockEventBus.java b/src/test/java/pl/edu/mimuw/cloudatlas/agent/MockEventBus.java new file mode 100644 index 0000000..e8f8bfa --- /dev/null +++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/MockEventBus.java @@ -0,0 +1,17 @@ +package pl.edu.mimuw.cloudatlas.agent; + +import java.util.concurrent.LinkedBlockingQueue; + +import pl.edu.mimuw.cloudatlas.agent.messages.AgentMessage; + +public class MockEventBus extends EventBus { + public LinkedBlockingQueue<AgentMessage> events; + + public MockEventBus() { + events = new LinkedBlockingQueue<AgentMessage>(); + } + + public void addMessage(AgentMessage msg) throws InterruptedException { + events.put(msg); + } +} diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementationTests.java b/src/test/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementationTests.java new file mode 100644 index 0000000..6330648 --- /dev/null +++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementationTests.java @@ -0,0 +1,198 @@ +package pl.edu.mimuw.cloudatlas.agent; + +import org.junit.Before; +import org.junit.Test; +import static org.junit.Assert.*; +import static org.hamcrest.CoreMatchers.hasItems; + +import java.util.concurrent.TimeUnit; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +import pl.edu.mimuw.cloudatlas.Container; +import pl.edu.mimuw.cloudatlas.agent.modules.ModuleType; +import pl.edu.mimuw.cloudatlas.agent.messages.AgentMessage; +import pl.edu.mimuw.cloudatlas.agent.messages.RequestStateMessage; +import pl.edu.mimuw.cloudatlas.agent.messages.SetAttributeMessage; +import pl.edu.mimuw.cloudatlas.agent.messages.StanikMessage; +import pl.edu.mimuw.cloudatlas.agent.messages.StateMessage; +import pl.edu.mimuw.cloudatlas.agent.messages.UpdateQueriesMessage; +import pl.edu.mimuw.cloudatlas.model.Attribute; +import pl.edu.mimuw.cloudatlas.model.AttributesMap; +import pl.edu.mimuw.cloudatlas.model.TestUtil; +import pl.edu.mimuw.cloudatlas.model.ValueInt; +import pl.edu.mimuw.cloudatlas.model.ValueString; +import pl.edu.mimuw.cloudatlas.model.ValueQuery; +import pl.edu.mimuw.cloudatlas.model.ValueTime; +import pl.edu.mimuw.cloudatlas.model.ZMI; + +public class NewApiImplementationTests { + private NewApiImplementation api; + private MockEventBus eventBus; + + @Before + public void initializeApi() throws Exception { + eventBus = new MockEventBus(); + api = new NewApiImplementation(eventBus); + } + + @Test + public void testGetZoneSet() throws Exception { + final Set<String> zoneSet = new HashSet(); + final Container<Exception> exceptionContainer = new Container(); + Thread apiThread = new Thread(() -> { + try { + zoneSet.addAll(api.getZoneSet()); + } catch (Exception e) { + exceptionContainer.thing = e; + } + }); + apiThread.start(); + + AgentMessage message = eventBus.events.poll(100, TimeUnit.MILLISECONDS); + assertNotNull(message); + assertEquals(ModuleType.RMI, message.getDestinationModule()); + RequestStateMessage requestMessage = (RequestStateMessage) message; + + ZMI root = new ZMI(); + StateMessage responseMessage = new StateMessage("", ModuleType.RMI, 0, 0, root, null); + requestMessage.getFuture().complete(responseMessage); + + apiThread.join(100); + assertFalse(apiThread.isAlive()); + assertNull(exceptionContainer.thing); + + assertThat(zoneSet, hasItems("/")); + } + + @Test + public void testRootGetZoneAttributeValues() throws Exception { + final Container<AttributesMap> attributes = new Container(); + final Container<Exception> exceptionContainer = new Container(); + Thread apiThread = new Thread(() -> { + try { + attributes.thing = api.getZoneAttributeValues("/"); + } catch (Exception e) { + exceptionContainer.thing = e; + } + }); + apiThread.start(); + + AgentMessage message = eventBus.events.poll(100, TimeUnit.MILLISECONDS); + assertNotNull(message); + assertEquals(ModuleType.RMI, message.getDestinationModule()); + RequestStateMessage requestMessage = (RequestStateMessage) message; + + ZMI zmi = new ZMI(); + zmi.getAttributes().add("timestamp", new ValueTime(42l)); + StateMessage response = new StateMessage("", ModuleType.RMI, 0, 0, zmi, new HashMap()); + requestMessage.getFuture().complete(response); + + apiThread.join(100); + assertFalse(apiThread.isAlive()); + assertNull(exceptionContainer.thing); + + assertEquals(new ValueTime(42l), attributes.thing.getOrNull("timestamp")); + } + + @Test + public void testGetZoneAttributeValues() throws Exception { + final Container<AttributesMap> attributes = new Container(); + final Container<Exception> exceptionContainer = new Container(); + Thread apiThread = new Thread(() -> { + try { + attributes.thing = api.getZoneAttributeValues("/son"); + } catch (Exception e) { + exceptionContainer.thing = e; + } + }); + apiThread.start(); + + AgentMessage message = eventBus.events.poll(100, TimeUnit.MILLISECONDS); + assertNotNull(message); + assertEquals(ModuleType.RMI, message.getDestinationModule()); + RequestStateMessage requestMessage = (RequestStateMessage) message; + + ZMI zmi = new ZMI(); + zmi.getAttributes().add("timestamp", new ValueTime(42l)); + ZMI son = new ZMI(zmi); + zmi.addSon(son); + son.getAttributes().add("name", new ValueString("son")); + son.getAttributes().add("timestamp", new ValueTime(43l)); + StateMessage response = new StateMessage("", ModuleType.RMI, 0, 0, zmi, new HashMap()); + requestMessage.getFuture().complete(response); + + apiThread.join(100); + assertFalse(apiThread.isAlive()); + assertNull(exceptionContainer.thing); + + assertEquals(new ValueTime(43l), attributes.thing.getOrNull("timestamp")); + assertEquals(new ValueString("son"), attributes.thing.getOrNull("name")); + } + + @Test + public void testInstallQuery() throws Exception { + String name = "&query"; + String queryCode = "SELECT 1 AS one"; + long timeBefore = System.currentTimeMillis(); + api.installQuery(name, queryCode); + long timeAfter = System.currentTimeMillis(); + + assertEquals(1, eventBus.events.size()); + AgentMessage message = eventBus.events.take(); + assertEquals(ModuleType.STATE, message.getDestinationModule()); + StanikMessage stanikMessage = (StanikMessage) message; + assertEquals(StanikMessage.Type.UPDATE_QUERIES, stanikMessage.getType()); + UpdateQueriesMessage updateMessage = (UpdateQueriesMessage) stanikMessage; + Map<Attribute, Entry<ValueQuery, ValueTime>> queries = updateMessage.getQueries(); + assertEquals(1, TestUtil.iterableSize(queries.keySet())); + assertEquals(new ValueQuery("SELECT 1 AS one"), queries.get(new Attribute("&query")).getKey()); + long timestamp = queries.get(new Attribute("&query")).getValue().getValue(); + assertTrue(timeBefore <= timestamp); + assertTrue(timestamp <= timeAfter); + } + + @Test + public void testUninstallQuery() throws Exception { + String name = "&query"; + long timeBefore = System.currentTimeMillis(); + api.uninstallQuery(name); + long timeAfter = System.currentTimeMillis(); + + assertEquals(1, eventBus.events.size()); + AgentMessage message = eventBus.events.take(); + assertEquals(ModuleType.STATE, message.getDestinationModule()); + StanikMessage stanikMessage = (StanikMessage) message; + assertEquals(StanikMessage.Type.UPDATE_QUERIES, stanikMessage.getType()); + UpdateQueriesMessage updateMessage = (UpdateQueriesMessage) stanikMessage; + Map<Attribute, Entry<ValueQuery, ValueTime>> queries = updateMessage.getQueries(); + assertEquals(1, TestUtil.iterableSize(queries.keySet())); + assertNull(queries.get(new Attribute("&query")).getKey()); + long timestamp = queries.get(new Attribute("&query")).getValue().getValue(); + assertTrue(timeBefore <= timestamp); + assertTrue(timestamp <= timeAfter); + } + + @Test + public void testSetAttributeValueChange() throws Exception { + ValueInt numProcesses = new ValueInt(42l); + long timeBefore = System.currentTimeMillis(); + api.setAttributeValue("/uw/khaki13", "num_processes", numProcesses); + long timeAfter = System.currentTimeMillis(); + + assertEquals(1, eventBus.events.size()); + AgentMessage message = eventBus.events.take(); + assertEquals(ModuleType.STATE, message.getDestinationModule()); + StanikMessage stanikMessage = (StanikMessage) message; + assertEquals(StanikMessage.Type.SET_ATTRIBUTE, stanikMessage.getType()); + SetAttributeMessage setMessage = (SetAttributeMessage) stanikMessage; + assertEquals(new Attribute("num_processes"), setMessage.getAttribute()); + assertEquals(new ValueInt(42l), setMessage.getValue()); + long timestamp = setMessage.getUpdateTimestamp().getValue(); + assertTrue(timeBefore <= timestamp); + assertTrue(timestamp <= timeAfter); + } +} diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/QurnikTest.java b/src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/QurnikTest.java index 0c10092..b6e4ea8 100644 --- a/src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/QurnikTest.java +++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/QurnikTest.java @@ -16,6 +16,7 @@ import pl.edu.mimuw.cloudatlas.model.Attribute; import pl.edu.mimuw.cloudatlas.model.AttributesMap; import pl.edu.mimuw.cloudatlas.model.TestUtil; import pl.edu.mimuw.cloudatlas.model.ValueInt; +import pl.edu.mimuw.cloudatlas.model.ValueNull; import pl.edu.mimuw.cloudatlas.model.ValueQuery; import pl.edu.mimuw.cloudatlas.model.ValueString; import pl.edu.mimuw.cloudatlas.model.ValueTime; @@ -200,4 +201,51 @@ public class QurnikTest { assertTrue(timeBefore <= timestamp2); assertTrue(timestamp2 <= timeAfter); } + + @Test + public void ignoresNullQueries() throws Exception { + ZMI root = setupSampleHierarchy(); + + Map<Attribute, Entry<ValueQuery, ValueTime>> queries = new HashMap(); + queries.put(new Attribute("&query1"), new SimpleImmutableEntry( + new ValueQuery("SELECT 1 AS one"), + new ValueTime(42l) + ) + ); + queries.put(new Attribute("&query2"), new SimpleImmutableEntry( + null, + new ValueTime(43l) + ) + ); + queries.put(new Attribute("&query3"), new SimpleImmutableEntry( + new ValueQuery("SELECT 2 AS two"), + new ValueTime(44l) + ) + ); + StateMessage message = new StateMessage("", ModuleType.QUERY, 0, 0, root, queries); + long timeBefore = System.currentTimeMillis(); + qurnik.handleTyped(message); + long timeAfter = System.currentTimeMillis(); + + UpdateAttributesMessage message1 = (UpdateAttributesMessage) executor.messagesToPass.take(); + assertEquals("/uw", message1.getPathName()); + AttributesMap attributes1 = message1.getAttributes(); + assertEquals(4, TestUtil.iterableSize(attributes1)); + assertEquals(new ValueInt(1l), attributes1.getOrNull("one")); + assertEquals(new ValueInt(2l), attributes1.getOrNull("two")); + long timestamp1 = ((ValueTime) attributes1.getOrNull("timestamp")).getValue(); + assertTrue(timeBefore <= timestamp1); + assertTrue(timestamp1 <= timeAfter); + + UpdateAttributesMessage message2 = (UpdateAttributesMessage) executor.messagesToPass.take(); + assertEquals("/", message2.getPathName()); + AttributesMap attributes2 = message2.getAttributes(); + System.out.println("got attributes " + attributes2.toString()); + assertEquals(3, TestUtil.iterableSize(attributes2)); + assertEquals(new ValueInt(1l), attributes2.getOrNull("one")); + assertEquals(new ValueInt(2l), attributes2.getOrNull("two")); + long timestamp2 = ((ValueTime) attributes2.getOrNull("timestamp")).getValue(); + assertTrue(timeBefore <= timestamp2); + assertTrue(timestamp2 <= timeAfter); + } } diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/RemikTest.java b/src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/RemikTest.java new file mode 100644 index 0000000..8ac8bed --- /dev/null +++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/RemikTest.java @@ -0,0 +1,61 @@ +package pl.edu.mimuw.cloudatlas.agent.modules; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; +import java.util.HashMap; + +import pl.edu.mimuw.cloudatlas.agent.messages.AgentMessage; +import pl.edu.mimuw.cloudatlas.agent.messages.GetStateMessage; +import pl.edu.mimuw.cloudatlas.agent.messages.RequestStateMessage; +import pl.edu.mimuw.cloudatlas.agent.messages.ResponseMessage; +import pl.edu.mimuw.cloudatlas.agent.messages.StanikMessage; +import pl.edu.mimuw.cloudatlas.agent.messages.StateMessage; +import pl.edu.mimuw.cloudatlas.agent.MockExecutor; +import pl.edu.mimuw.cloudatlas.model.ValueTime; +import pl.edu.mimuw.cloudatlas.model.ZMI; + +import org.junit.Before; +import org.junit.Test; +import static org.junit.Assert.*; + +public class RemikTest { + private Remik remik; + private MockExecutor executor; + + @Before + public void setupLocals() { + remik = new Remik(); + executor = new MockExecutor(remik); + } + + @Test + public void asksForStateOnStateRequest() throws Exception { + CompletableFuture<ResponseMessage> future = new CompletableFuture(); + RequestStateMessage message = new RequestStateMessage("", 0, future); + remik.handleTyped(message); + AgentMessage receivedMessage = (AgentMessage) executor.messagesToPass.take(); + assertEquals(ModuleType.STATE, receivedMessage.getDestinationModule()); + StanikMessage stanikMessage = (StanikMessage) receivedMessage; + assertEquals(StanikMessage.Type.GET_STATE, stanikMessage.getType()); + GetStateMessage getStateMessage = (GetStateMessage) stanikMessage; + assertEquals(ModuleType.RMI, getStateMessage.getRequestingModule()); + } + + @Test + public void completesFutureOnReceivedState() throws Exception { + CompletableFuture<ResponseMessage> future = new CompletableFuture(); + RequestStateMessage message = new RequestStateMessage("", 0, future); + remik.handleTyped(message); + + ZMI zmi = new ZMI(); + zmi.getAttributes().add("timestamp", new ValueTime(42l)); + StateMessage response = new StateMessage("", ModuleType.RMI, 0, 0, zmi, new HashMap()); + remik.handleTyped(response); + + ResponseMessage passedResponse = future.get(100, TimeUnit.MILLISECONDS); + assertNotNull(passedResponse); + assertEquals(ResponseMessage.Type.STATE, passedResponse.getType()); + StateMessage stateMessage = (StateMessage) passedResponse; + assertEquals(new ValueTime(42l), stateMessage.getZMI().getAttributes().get("timestamp")); + } +} 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 index 495a012..2e1ccea 100644 --- a/src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/StanikTest.java +++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/StanikTest.java @@ -7,9 +7,10 @@ import java.util.Map.Entry; import pl.edu.mimuw.cloudatlas.agent.messages.AgentMessage; import pl.edu.mimuw.cloudatlas.agent.messages.GetStateMessage; -import pl.edu.mimuw.cloudatlas.agent.messages.StateMessage; import pl.edu.mimuw.cloudatlas.agent.messages.RemoveZMIMessage; import pl.edu.mimuw.cloudatlas.agent.messages.ResponseMessage; +import pl.edu.mimuw.cloudatlas.agent.messages.SetAttributeMessage; +import pl.edu.mimuw.cloudatlas.agent.messages.StateMessage; import pl.edu.mimuw.cloudatlas.agent.messages.UpdateAttributesMessage; import pl.edu.mimuw.cloudatlas.agent.messages.UpdateQueriesMessage; import pl.edu.mimuw.cloudatlas.agent.MockExecutor; @@ -236,4 +237,59 @@ public class StanikTest { stanik.getHierarchy().findDescendant("/new"); } + + @Test + public void setOldAttribute() throws Exception { + AttributesMap attributes = new AttributesMap(); + attributes.add("foo", new ValueInt(1337l)); + attributes.add("name", new ValueString("new")); + attributes.add("timestamp", new ValueTime(42l)); + UpdateAttributesMessage message = new UpdateAttributesMessage("test_msg", 0, "/new", attributes); + stanik.handleTyped(message); + + SetAttributeMessage setMessage = new SetAttributeMessage("test_msg2", 0, "/new", new Attribute("foo"), new ValueInt(43l), new ValueTime(40l)); + stanik.handleTyped(setMessage); + + AttributesMap actualAttributes = stanik.getHierarchy().findDescendant("/new").getAttributes(); + assertEquals(3, TestUtil.iterableSize(actualAttributes)); + assertEquals(new ValueInt(43l), actualAttributes.getOrNull("foo")); + assertEquals(new ValueTime(42l), actualAttributes.getOrNull("timestamp")); + } + + @Test + public void setOldAttribute2() throws Exception { + AttributesMap attributes = new AttributesMap(); + attributes.add("foo", new ValueInt(1337l)); + attributes.add("name", new ValueString("new")); + attributes.add("timestamp", new ValueTime(42l)); + UpdateAttributesMessage message = new UpdateAttributesMessage("test_msg", 0, "/new", attributes); + stanik.handleTyped(message); + + SetAttributeMessage setMessage = new SetAttributeMessage("test_msg2", 0, "/new", new Attribute("foo"), new ValueInt(43l), new ValueTime(43l)); + stanik.handleTyped(setMessage); + + AttributesMap actualAttributes = stanik.getHierarchy().findDescendant("/new").getAttributes(); + assertEquals(3, TestUtil.iterableSize(actualAttributes)); + assertEquals(new ValueInt(43l), actualAttributes.getOrNull("foo")); + assertEquals(new ValueTime(43l), actualAttributes.getOrNull("timestamp")); + } + + @Test + public void setNewAttribute() throws Exception { + AttributesMap attributes = new AttributesMap(); + attributes.add("foo", new ValueInt(1337l)); + attributes.add("name", new ValueString("new")); + attributes.add("timestamp", new ValueTime(42l)); + UpdateAttributesMessage message = new UpdateAttributesMessage("test_msg", 0, "/new", attributes); + stanik.handleTyped(message); + + SetAttributeMessage setMessage = new SetAttributeMessage("test_msg2", 0, "/new", new Attribute("bar"), new ValueInt(43l), new ValueTime(43l)); + stanik.handleTyped(setMessage); + + AttributesMap actualAttributes = stanik.getHierarchy().findDescendant("/new").getAttributes(); + assertEquals(4, TestUtil.iterableSize(actualAttributes)); + assertEquals(new ValueInt(1337l), actualAttributes.getOrNull("foo")); + assertEquals(new ValueInt(43l), actualAttributes.getOrNull("bar")); + assertEquals(new ValueTime(43l), actualAttributes.getOrNull("timestamp")); + } } |