Skip to content

Commit

Permalink
Merge pull request #14 from fogbow/develop
Browse files Browse the repository at this point in the history
Refactoring code and increasing Test coverage.
  • Loading branch information
abmargb committed Apr 8, 2014
2 parents 13cb4a2 + 89de5bf commit e4bf04a
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 18 deletions.
11 changes: 3 additions & 8 deletions src/main/java/org/fogbowcloud/manager/xmpp/core/ManagerItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,15 @@ public String getFormattedTime() {
SimpleDateFormat dateFormatISO8601 = new SimpleDateFormat(
ISO_8601_DATE_FORMAT, Locale.ROOT);
dateFormatISO8601.setTimeZone(TimeZone.getTimeZone("GMT"));

Date date = new Date(lastTime);

return dateFormatISO8601.format(date);
return dateFormatISO8601.format(new Date(lastTime));
}

/**
* This method was implemented just for unit test.
*
* @param lastTime
*/
protected void setLastTime(long lastTime){
public void setLastTime(long lastTime){
this.lastTime = lastTime;
}


}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.fogbowcloud.manager.xmpp.core;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class ManagerModel {

private List<ManagerItem> managers = new ArrayList<ManagerItem>();
private List<ManagerItem> managers = new LinkedList<ManagerItem>();

public void update(List<ManagerItem> managers) {
if (managers == null) throw new IllegalArgumentException();
Expand All @@ -15,5 +15,4 @@ public void update(List<ManagerItem> managers) {
public List<ManagerItem> getMembers() {
return managers;
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.fogbowcloud.manager.xmpp.core;

public class ResourcesInfo {

private String id;
private String cpuIdle;
private String cpuInUse;
Expand All @@ -9,7 +10,6 @@ public class ResourcesInfo {

public ResourcesInfo(String id, String cpuIdle, String cpuInUse,
String memIdle, String memInUse) {

setId(id);
setCpuIdle(cpuIdle);
setCpuInUse(cpuInUse);
Expand All @@ -22,7 +22,7 @@ public String getId() {
}

public void setId(String id) {
if (id == null || "".equals(id)) {
if (id == null || id.isEmpty()) {
throw new IllegalArgumentException("ResourceInfo id is invalid.");
}
this.id = id;
Expand Down
75 changes: 75 additions & 0 deletions src/test/java/org/fogbowcloud/manager/xmpp/TestManagerItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package org.fogbowcloud.manager.xmpp;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.fogbowcloud.manager.xmpp.core.ManagerItem;
import org.fogbowcloud.manager.xmpp.core.ResourcesInfo;
import org.fogbowcloud.manager.xmpp.model.ManagerTestHelper;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class TestManagerItem {

private ResourcesInfo resources;
private String DATE = "2000-10-31T01:30:00.000+0000";

@Before
public void setUp() {
resources = ManagerTestHelper.getResources();
}

@Test(expected = IllegalArgumentException.class)
public void testInvalidResources() {
new ManagerItem(null);
}

@Test(expected = IllegalArgumentException.class)
public void testNullResourceId() {
new ResourcesInfo(null, "cpuIdle",
"cpuInUse", "memIdle", "memInUse");
}

@Test(expected = IllegalArgumentException.class)
public void testNullResourcesCpuIdle() {
new ResourcesInfo("id", null,
"cpuInUse", "memIdle", "memInUse");
}

@Test(expected = IllegalArgumentException.class)
public void testNullResourcesCpuInUse() {
new ResourcesInfo("id", "CpuIdle",
null, "memIdle", "memInUse");
}

@Test(expected = IllegalArgumentException.class)
public void testNullResourcesMemIdle() {
new ResourcesInfo("id", "CpuIdle",
"cpuInUse", null, "memInUse");
}

@Test(expected = IllegalArgumentException.class)
public void testNullResourcesMemInUse() {
new ResourcesInfo("id", "CpuIdle",
"cpuInUse", "memIdle", null);
}

@Test
public void testConstructor() {
ManagerItem managerItem = new ManagerItem(resources);
Assert.assertEquals(resources, managerItem.getResourcesInfo());
}

@Test
public void getFormattedTime() throws ParseException {
ManagerItem managerItem = new ManagerItem(resources);
SimpleDateFormat dateFormat = new SimpleDateFormat(
ManagerItem.ISO_8601_DATE_FORMAT);
Date date = dateFormat.parse(DATE);
Long lastTime = date.getTime();
managerItem.setLastTime(lastTime);
Assert.assertEquals(DATE, managerItem.getFormattedTime());
}
}
18 changes: 13 additions & 5 deletions src/test/java/org/fogbowcloud/manager/xmpp/TestManagerModel.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.fogbowcloud.manager.xmpp;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.fogbowcloud.manager.xmpp.core.ManagerItem;
Expand Down Expand Up @@ -29,23 +29,31 @@ public void testUpdateWithException() {

@Test
public void testUpdateEmptyList() {
List<ManagerItem> resources = new ArrayList<ManagerItem>();
List<ManagerItem> resources = new LinkedList<ManagerItem>();
managerModel.update(resources);
Assert.assertEquals(resources, managerModel.getMembers());
}

@Test
public void testUpdate1Member() {
List<ManagerItem> members = new ArrayList<ManagerItem>();
List<ManagerItem> members = new LinkedList<ManagerItem>();
members.add(new ManagerItem(resources));
managerModel.update(members);
Assert.assertEquals(1, members.size());
Assert.assertEquals(members, managerModel.getMembers());
}



@Test(expected = IllegalArgumentException.class)
public void testUpdateWithIvalidMember() {
List<ManagerItem> members = new LinkedList<ManagerItem>();
members.add(new ManagerItem(null));
managerModel.update(members);
}

@Test
public void testUpdateManyMembers() {
List<ManagerItem> members = new ArrayList<ManagerItem>();
List<ManagerItem> members = new LinkedList<ManagerItem>();
for (int i = 0; i < 10; i++) {
resources.setId("" + i);
members.add(new ManagerItem(resources));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.fogbowcloud.manager.xmpp.core.model.DateUtils;

public class RendezvousItemCopy {

public static final String ISO_8601_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
private long lastTime;
private ResourcesInfo resourcesInfo;
Expand Down

0 comments on commit e4bf04a

Please sign in to comment.