Commit 1e87be58 authored by aninhacostaribeiro's avatar aninhacostaribeiro
Browse files

Added new tests

parent 7cd9e96c
......@@ -27,7 +27,7 @@ public class AgentCommunicationComponent {
private ScheduledExecutorService executor = Executors
.newScheduledThreadPool(1);
private Properties prop;
private long sleepingTime;
private int threadTime;
private XMPPClient client;
public AgentCommunicationComponent(Properties prop) {
......@@ -37,8 +37,14 @@ public class AgentCommunicationComponent {
this.prop.getProperty("xmpp.password"),
this.prop.getProperty("xmpp.host"),
Integer.parseInt(this.prop.getProperty("xmpp.port")));
this.sleepingTime = Long.parseLong(this.prop
.getProperty("green.sleepingTime"));
if (this.prop.getProperty("green.threadTime") != null) {
this.threadTime = Integer.parseInt(this.prop
.getProperty("green.threadTime"));
}
else{
this.threadTime = 60;
}
} catch (Exception e) {
LOGGER.fatal("The configuration file is not correct", e);
}
......@@ -130,7 +136,7 @@ public class AgentCommunicationComponent {
public void run() {
sendIamAliveSignal();
}
}, 0, sleepingTime, TimeUnit.MILLISECONDS);
}, 0, threadTime, TimeUnit.SECONDS);
}
}
......@@ -33,21 +33,29 @@ public class DefaultGreenStrategy implements GreenStrategy {
private long graceTime;
private long expirationTime;
private int threadTime;
private ScheduledExecutorService executorService = Executors
.newScheduledThreadPool(1);
public DefaultGreenStrategy(Properties greenProperties) {
this.openStackPlugin = new OpenStackInfoPlugin(greenProperties
.getProperty("openstack.endpoint").toString(), greenProperties
.getProperty("openstack.username").toString(), greenProperties
.get("openstack.password").toString(), greenProperties
public DefaultGreenStrategy(Properties prop) {
this.openStackPlugin = new OpenStackInfoPlugin(prop
.getProperty("openstack.endpoint").toString(), prop
.getProperty("openstack.username").toString(), prop
.get("openstack.password").toString(), prop
.getProperty("openstack.tenant").toString());
this.dateWrapper = new DateWrapper();
this.graceTime = Long.parseLong(greenProperties
this.graceTime = Long.parseLong(prop
.getProperty("greenstrategy.gracetime")) * 1000;
this.expirationTime = Long.parseLong(greenProperties
this.expirationTime = Long.parseLong(prop
.getProperty("greenstrategy.expirationtime")) * 1000;
if (prop.getProperty("green.threadTime") != null) {
this.threadTime = Integer.parseInt(prop
.getProperty("green.threadTime"));
}
else{
this.threadTime = 60;
}
}
/*
......@@ -217,6 +225,6 @@ public class DefaultGreenStrategy implements GreenStrategy {
LOGGER.warn("Exception thrown at the main thread", e);
}
}
}, 0, 1, TimeUnit.MINUTES);
}, 0, threadTime, TimeUnit.SECONDS);
}
}
\ No newline at end of file
......@@ -44,7 +44,6 @@ public class OpenStackInfoPlugin implements CloudInfoPlugin {
for (Hypervisor hypervisor : hypervisors) {
hostsName.add(hypervisor.getHypervisorHostname());
}
System.out.println(hostsName.toString());
return hostsName;
}
......
......@@ -4,40 +4,46 @@ 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.junit.Test;
import org.mockito.Mockito;
import org.xmpp.packet.IQ;
public class TestAgentCommunicationComponent {
private XMPPClient createXMPPClientMock() {
@Test
public void testSendIamAliveSignal() {
XMPPClient client = Mockito.mock(XMPPClient.class);
Mockito.doReturn(Mockito.mock(XMPPConnection.class)).when(client)
.getConnection();
return client;
}
private Properties createPropMock(String hostName, String ip,
String macAddress) {
Properties prop = Mockito.mock(Properties.class);
Mockito.doReturn(ip).when(prop).getProperty("host.ip");
Mockito.doReturn(hostName).when(prop).getProperty("host.name");
Mockito.doReturn(macAddress).when(prop).getProperty("host.macAddress");
return prop;
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);
acc.setClient(client);
IQ expectedIQ = acc.sendIamAliveSignal();
Assert.assertEquals(expectedIQ.getElement().element("query")
.elementText("ip"), "123.456.78.9");
Assert.assertEquals(expectedIQ.getElement().element("query")
.elementText("hostName"), "host");
Assert.assertEquals(expectedIQ.getElement().element("query")
.elementText("macAddress"), "A1:B2:C3:D4:E5:67");
}
@Test
public void testSendIamAliveSignal() {
XMPPClient client = createXMPPClientMock();
Properties prop = createPropMock("host", "123.456.78.9", "A1:B2:C3:D4:E5:67");
public void testInitConnectException() throws XMPPException {
XMPPClient client = Mockito.mock(XMPPClient.class);
XMPPException e = new XMPPException();
Mockito.doThrow(e).when(client).connect();
Mockito.doThrow(e).when(client).registerPlugin(new XEP0077());
Properties prop = Mockito.mock(Properties.class);
AgentCommunicationComponent acc = new AgentCommunicationComponent(prop);
acc.setClient(client);
IQ expectedIQ = acc.sendIamAliveSignal();
Assert.assertEquals(expectedIQ.getElement().element("query").elementText("ip"),"123.456.78.9");
Assert.assertEquals(expectedIQ.getElement().element("query").elementText("hostName"),"host");
Assert.assertEquals(expectedIQ.getElement().element("query").elementText("macAddress"),"A1:B2:C3:D4:E5:67");
Assert.assertEquals(false, acc.init());
}
}
......@@ -26,7 +26,6 @@ public class TestWakeUpRequestHandler {
query.addElement("minRAM").setText(Integer.toString(1024));
WakeUpRequestHandler wurh = new WakeUpRequestHandler(this.createGreenStrategyMock(1, 1024));
IQ result = wurh.handle(iq);
System.out.println(result);
Assert.assertEquals(result.getFrom().toString(), "green.test.com");
}
......
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