Commit b8f8b3d7 authored by aninhacostaribeiro's avatar aninhacostaribeiro
Browse files

Updated tests to ServerCommunicationComponent

parent 8a069158
......@@ -5,15 +5,18 @@ import java.util.Properties;
import org.apache.log4j.Logger;
import org.fogbowcloud.green.server.core.greenStrategy.GreenStrategy;
import org.jamppa.component.PacketSender;
import org.jamppa.component.XMPPComponent;
import org.xmpp.packet.IQ;
import org.xmpp.packet.IQ.Type;
public class ServerCommunicationComponent extends XMPPComponent {
private static final Logger LOGGER = Logger.getLogger(ServerCommunicationComponent.class);
private static final Logger LOGGER = Logger
.getLogger(ServerCommunicationComponent.class);
private GreenStrategy gs;
private Properties prop;
private PacketSender packetSender;
public ServerCommunicationComponent(Properties prop, GreenStrategy gs) {
super(prop.getProperty("xmpp.jid"), prop.getProperty("xmpp.password"),
......@@ -21,15 +24,25 @@ public class ServerCommunicationComponent extends XMPPComponent {
.getProperty("xmpp.port")));
this.prop = prop;
this.gs = gs;
this.packetSender = this;
addHandlers();
}
public void wakeUpHost(String macAddress) throws IOException, InterruptedException {
LOGGER.debug("Wake command: powerwake -b " +
prop.getProperty("wol.broadcast.address") + " " + macAddress);
void setPacketSender(PacketSender packetSender) {
this.packetSender = packetSender;
}
protected ProcessBuilder createProcessBuilder(String macAddress) {
LOGGER.debug("Wake command: powerwake -b "
+ prop.getProperty("wol.broadcast.address") + " " + macAddress);
ProcessBuilder pb = new ProcessBuilder("powerwake", "-b",
prop.getProperty("wol.broadcast.address"), macAddress);
return pb;
}
public void wakeUpHost(String macAddress) throws IOException,
InterruptedException {
ProcessBuilder pb = createProcessBuilder(macAddress);
Process process = pb.start();
process.waitFor();
}
......@@ -38,7 +51,8 @@ public class ServerCommunicationComponent extends XMPPComponent {
IQ iq = new IQ(Type.set);
iq.setTo(hostJID);
iq.getElement().addElement("query", "org.fogbowcloud.green.GoToBed");
send(iq);
System.out.println(iq);
packetSender.sendPacket(iq);
}
private void addHandlers() {
......
package org.fogbowcloud.green.server.communication;
import java.util.Properties;
import org.dom4j.Element;
import org.fogbowcloud.green.server.core.greenStrategy.DefaultGreenStrategy;
import org.jamppa.component.PacketSender;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.ArgumentMatcher;
import org.mockito.Matchers;
import org.mockito.Mockito;
import org.xmpp.packet.IQ;
public class TestServerCommunicationComponent {
private Properties createProperties() {
Properties prop = new Properties();
prop.put("xmpp.jid", "123.456.255.255");
prop.put("xmpp.password", "tellnoone");
prop.put("xmpp.host", "localhost");
prop.put("xmpp.port", "1");
return prop;
}
@Test
public void testCreationWakeUpCommand() {
Properties prop = createProperties();
prop.put("wol.broadcast.address", "123.456.255.255");
ServerCommunicationComponent scc = new ServerCommunicationComponent
(prop, Mockito.mock(DefaultGreenStrategy.class));
ProcessBuilder pb = scc.createProcessBuilder("A1:B2:C3:D4:E5:67");
Assert.assertEquals("powerwake", pb.command().get(0));
Assert.assertEquals("-b", pb.command().get(1));
Assert.assertEquals("123.456.255.255", pb.command().get(2));
Assert.assertEquals("A1:B2:C3:D4:E5:67", pb.command().get(3));
}
private IQ matchIQ() {
return Matchers.argThat(new ArgumentMatcher<IQ>() {
@Override
public boolean matches(Object argument) {
if (!(argument instanceof IQ)) {
return false;
}
IQ argIQ = (IQ) argument;
if ((argIQ.getTo() == null) || (!argIQ.getTo().toBareJID().equals("host@jid.com"))) {
return false;
}
if(argIQ.getElement().element("query") == null) {
return false;
}
Element query = argIQ.getElement().element("query");
if(!query.getNamespaceURI().equals("org.fogbowcloud.green.GoToBed")) {
return false;
}
return true;
}
}
);
}
@Test
public void testSendIdleHostsToBed() {
Properties prop = createProperties();
ServerCommunicationComponent scc = new ServerCommunicationComponent
(prop, Mockito.mock(DefaultGreenStrategy.class));
PacketSender packetSender = Mockito.mock(PacketSender.class);
scc.setPacketSender(packetSender);
scc.sendIdleHostToBed("host@jid.com");
Mockito.verify(packetSender).sendPacket(matchIQ());
}
}
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