Commit 92fa2db7 authored by aninhacostaribeiro's avatar aninhacostaribeiro
Browse files

Changed Properties Mock for the real method

parent b8f8b3d7
......@@ -140,7 +140,7 @@ public class AgentCommunicationComponent {
.elements("query").get(0);
query.addElement("ip").setText(this.prop.getProperty("host.ip"));
query.addElement("macAddress").setText(
this.prop.getProperty("host.macAddress"));
this.prop.getProperty("host.macAddress"));
query.addElement("hostName")
.setText(this.prop.getProperty("host.name"));
client.getConnection().sendPacket(iq);
......
......@@ -38,7 +38,6 @@ public class TurnOff {
try {
Process process = pb.start();
process.waitFor();
System.out.println(process.getInputStream().read());
} catch (Exception e) {
LOGGER.warn("It was not possible to turn down this host", e);
}
......
......@@ -51,7 +51,6 @@ public class ServerCommunicationComponent extends XMPPComponent {
IQ iq = new IQ(Type.set);
iq.setTo(hostJID);
iq.getElement().addElement("query", "org.fogbowcloud.green.GoToBed");
System.out.println(iq);
packetSender.sendPacket(iq);
}
......
......@@ -23,12 +23,12 @@ public class TestAgentCommunicationComponent {
XMPPClient client = Mockito.mock(XMPPClient.class);
Mockito.doReturn(Mockito.mock(XMPPConnection.class)).when(client)
.getConnection();
Properties prop = Mockito.mock(Properties.class);
Mockito.doReturn("123.456.78.9").when(prop).getProperty("host.ip");
Mockito.doReturn("host").when(prop).getProperty("host.name");
Mockito.doReturn("A1:B2:C3:D4:E5:67").when(prop)
.getProperty("host.macAddress");
AgentCommunicationComponent acc = new AgentCommunicationComponent(prop);
Properties prop = new Properties();
prop.put("host.ip", "123.456.78.9");
prop.put("host.name", "host");
prop.put("host.macAddress", "A1:B2:C3:D4:E5:67");
AgentCommunicationComponent acc =
new AgentCommunicationComponent(prop);
acc.setClient(client);
IQ expectedIQ = acc.sendIamAliveSignal();
Assert.assertEquals(expectedIQ.getElement().element("query")
......@@ -44,7 +44,7 @@ public class TestAgentCommunicationComponent {
XMPPClient client = Mockito.mock(XMPPClient.class);
XMPPException e = new XMPPException();
Mockito.doThrow(e).when(client).connect();
Properties prop = Mockito.mock(Properties.class);
Properties prop = new Properties();
AgentCommunicationComponent acc = new AgentCommunicationComponent(prop);
acc.setClient(client);
Assert.assertEquals(false, acc.init());
......@@ -54,11 +54,11 @@ public class TestAgentCommunicationComponent {
public void testAccountAlreadyCreated () throws XMPPException {
XMPPException e = new XMPPException();
XEP0077 register = Mockito.mock(XEP0077.class);
Properties prop = Mockito.mock(Properties.class);
Properties prop = new Properties();
prop.put("xmpp.jid", "jid@testagent.com");
prop.put("xmpp.password", "tellnoone");
XMPPClient client = Mockito.mock(XMPPClient.class);
Mockito.doReturn(Mockito.mock(XMPPConnection.class)).when(client).getConnection();
Mockito.doReturn("jid@testagent.com").when(prop).getProperty("xmpp.jid");
Mockito.doReturn("tellnoone").when(prop).getProperty("xmpp.password");
Mockito.doThrow(e).when(register).createAccount("jid@testagent.com", "tellnoone");
AgentCommunicationComponent acc = new AgentCommunicationComponent(prop);
acc.setRegister(register);
......
......@@ -4,13 +4,12 @@ import java.util.Properties;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
public class TestTurnOff {
@Test
public void testTurnDownCommandIsNull() {
Properties prop = Mockito.mock(Properties.class);
Properties prop = new Properties();
TurnOff turnOff = new TurnOff(prop);
turnOff.startTurnOff();
Assert.assertEquals("sudo", turnOff.getPb().command().get(0));
......@@ -20,9 +19,8 @@ public class TestTurnOff {
@Test
public void testTurnDownCommandIsSettedandASudoOne() {
Properties prop = Mockito.mock(Properties.class);
Mockito.doReturn("pm-hibernate").when(prop)
.getProperty("green.TurnOffCommand");
Properties prop = new Properties();
prop.put("green.TurnOffCommand", "pm-hibernate");
TurnOff turnOff = new TurnOff(prop);
turnOff.startTurnOff();
Assert.assertEquals("sudo", turnOff.getPb().command().get(0));
......@@ -32,9 +30,8 @@ public class TestTurnOff {
@Test
public void testTurnDownCommandisNotInSudoers() {
Properties prop = Mockito.mock(Properties.class);
Mockito.doReturn("other-suspend-command").when(prop)
.getProperty("green.TurnOffCommand");
Properties prop = new Properties();
prop.put("green.TurnOffCommand", "other-suspend-command");
TurnOff turnOff = new TurnOff(prop);
turnOff.startTurnOff();
Assert.assertEquals("other-suspend-command",
......
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;
......@@ -13,7 +12,6 @@ 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");
......@@ -22,53 +20,53 @@ public class TestServerCommunicationComponent {
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));
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;
}
}
);
@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));
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());
}
}
}
\ No newline at end of file
......@@ -25,7 +25,7 @@ public class TestDefaultGreenStrategy {
}
@Test
public void testCheckHostLastSeen() {
public void testCheckExpiredHots() {
Host lost = new Host("lost", 0, true, true, 0, 0, 0);
lost.setLastSeen(0);
Host stilll = new Host("stilll", 0, true, true, 0, 0, 0);
......@@ -48,7 +48,7 @@ public class TestDefaultGreenStrategy {
}
@Test
public void testRetrievingHosts() {
public void testRetrievingHostsFromTheCloud() {
Host toBeFound = new Host("found", 0, true, true, 0, 0, 0);
List<Host> hosts = new LinkedList<Host>();
hosts.add(toBeFound);
......
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