Commit 54d3e7f2 authored by aninhacostaribeiro's avatar aninhacostaribeiro
Browse files

Added new tests

parent 4b5f4793
......@@ -58,6 +58,37 @@ public class AgentCommunicationComponent {
protected void setRegister(XEP0077 register) {
this.register = register;
}
protected static PacketFilter createPacketFilter(final String componentAddress) {
return new PacketFilter() {
@Override
public boolean accept(Packet packet) {
JID from = packet.getFrom();
if (from == null) {
return false;
}
if (!from.toString().equals(componentAddress)) {
return false;
}
if (packet.getError() != null) {
LOGGER.fatal("IAmAlive packet returned an error: " + packet.toXML());
return false;
}
if ((packet.getElement() == null) || (packet.getElement().element("query") == null)) {
LOGGER.fatal("There is no query element in the response packet");
return false;
}
Element queryEl = packet.getElement().element("query");
String ns = queryEl.getNamespaceURI();
if (!ns.equals("org.fogbowcloud.green.GoToBed")) {
LOGGER.fatal("Query element has a different namespace: " + ns);
return false;
}
return true;
}
};
}
public Boolean init() {
try {
......@@ -82,40 +113,19 @@ public class AgentCommunicationComponent {
}
this.client.process(false);
LOGGER.info("connected to the server");
client.getConnection().addPacketListener(new PacketListener() {
client.getConnection().addPacketListener(
createPacketListener(new TurnOff(prop)),
createPacketFilter(prop.getProperty("xmpp.component")));
return true;
}
protected static PacketListener createPacketListener(final TurnOff turnOff) {
return new PacketListener() {
@Override
public void processPacket(Packet packet) {
new TurnOff().suspend(prop.getProperty("green.TurnOffCommand"));
turnOff.suspend();
}
}, new PacketFilter() {
@Override
public boolean accept(Packet packet) {
JID from = packet.getFrom();
if (from == null) {
return false;
}
if (!from.toString().equals(prop.getProperty("xmpp.component"))) {
return false;
}
if (packet.getError() != null) {
LOGGER.fatal("IAmAlive packet returned an error: " + packet.toXML());
return false;
}
Element queryEl = packet.getElement().element("query");
if (queryEl == null) {
LOGGER.fatal("There is no query element in the response packet");
return false;
}
String ns = queryEl.getNamespaceURI();
if (!ns.equals("org.fogbowcloud.green.GoToBed")) {
LOGGER.fatal("Query element has a different namespace: " + ns);
return false;
}
return true;
}
});
return true;
};
}
public IQ sendIamAliveSignal() {
......
package org.fogbowcloud.green.agent;
import java.io.IOException;
import java.util.Properties;
import org.apache.log4j.Logger;
......@@ -8,16 +8,23 @@ public class TurnOff {
private static final Logger LOGGER = Logger.getLogger(TurnOff.class);
private static final String DEFAULT_SUSPEND_COMMAND = "pm-suspend";
public void suspend(String command) {
private Properties prop;
public TurnOff(Properties prop) {
this.prop = prop;
}
public void suspend() {
String command = prop.getProperty("green.TurnOffCommand");
if (command == null || command.isEmpty()){
command = DEFAULT_SUSPEND_COMMAND;
}
ProcessBuilder pb = new ProcessBuilder("sudo", "-S", command);
try {
pb.start();
} catch (IOException e) {
Process process = pb.start();
process.waitFor();
} catch (Exception e) {
LOGGER.warn("It was not possible to turn down this host", e);
}
}
......
......@@ -2,14 +2,17 @@ package org.fogbowcloud.green.agent;
import java.util.Properties;
import org.junit.Assert;
import org.jamppa.client.XMPPClient;
import org.jamppa.client.plugin.xep0077.XEP0077;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.PacketFilter;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
import org.xmpp.packet.IQ;
import org.xmpp.packet.JID;
import org.xmpp.packet.Packet;
public class TestAgentCommunicationComponent {
......@@ -63,6 +66,34 @@ public class TestAgentCommunicationComponent {
@Test
public void testPacketFromUnexpectedSource() {
Packet packet = Mockito.mock(Packet.class);
JID jid = Mockito.mock(JID.class);
Mockito.doReturn(jid).when(packet).getFrom();
Mockito.doReturn("otherComponent.com").when(jid).toString();
PacketFilter pf = AgentCommunicationComponent
.createPacketFilter("green.server.com");
Assert.assertEquals(false, pf.accept(packet));
}
@Test
public void testPacketWithoutFrom() {
Packet packet = Mockito.mock(Packet.class);
JID jid = null;
Mockito.doReturn(jid).when(packet).getFrom();
PacketFilter pf = AgentCommunicationComponent
.createPacketFilter("green.server.com");
Assert.assertEquals(false, pf.accept(packet));
}
@Test
public void testPacketWithNullQuerryElement() {
Packet packet = Mockito.mock(Packet.class);
JID jid = Mockito.mock(JID.class);
Mockito.doReturn(jid).when(packet).getFrom();
Mockito.doReturn("green.server.com").when(jid).toString();
PacketFilter pf = AgentCommunicationComponent
.createPacketFilter("green.server.com");
Assert.assertEquals(false, pf.accept(packet));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment