Laszlo Hornyak has uploaded a new change for review. Change subject: space war ......................................................................
space war replaced all tabs to spaces Change-Id: I705030ceb98c887f5636476476c30cdd1823a968 Signed-off-by: Laszlo Hornyak <[email protected]> --- M tests/java/src/main/java/org/ovirt/schedulerproxy/SchedulerProxy.java M tests/java/src/test/java/org/ovirt/schedulerproxy/SchedulerProxyTest.java 2 files changed, 190 insertions(+), 184 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-scheduler-proxy refs/changes/69/18169/1 diff --git a/tests/java/src/main/java/org/ovirt/schedulerproxy/SchedulerProxy.java b/tests/java/src/main/java/org/ovirt/schedulerproxy/SchedulerProxy.java index 1ac9aa3..debc2ef 100644 --- a/tests/java/src/main/java/org/ovirt/schedulerproxy/SchedulerProxy.java +++ b/tests/java/src/main/java/org/ovirt/schedulerproxy/SchedulerProxy.java @@ -11,138 +11,143 @@ import org.apache.xmlrpc.client.XmlRpcClientConfigImpl; public class SchedulerProxy { - final XmlRpcClient client; + final XmlRpcClient client; - public SchedulerProxy(String url) throws MalformedURLException { - XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl(); - config.setServerURL(new URL(url)); - client = new XmlRpcClient(); - client.setConfig(config); - } + public SchedulerProxy(String url) throws MalformedURLException { + XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl(); + config.setServerURL(new URL(url)); + client = new XmlRpcClient(); + client.setConfig(config); + } - public HashMap<String, HashMap<String, String[]>> discover() throws XmlRpcException { - Object execute = client.execute("discover", new Object[] {}); - return parseDiscover(execute); - } - - private HashMap<String, HashMap<String, String[]>> parseDiscover(Object result){ - if (result == null || ! (result instanceof HashMap)){ - System.out.println("discover error"); - return null; - } - HashMap<String, HashMap<String, Object[]>> castedResult = (HashMap<String, HashMap<String, Object[]>>)result; - //Its a list of host IDs - HashMap<String, HashMap<String, String[]>> retValue = new HashMap<String, HashMap<String, String[]>>(); - for (String keyType : castedResult.keySet()) { - HashMap<String, Object[]> typeMap = castedResult.get(keyType); - HashMap<String, String[]> newTypeMap = new HashMap<String, String[]>(); - for (String keyModuleName : typeMap.keySet()) { - String[] keys = new String[2]; - for (int i = 0; i < 2; i++) { - keys[i] = (String)typeMap.get(keyModuleName)[i]; - } - newTypeMap.put(keyModuleName, keys); - } - retValue.put(keyType, newTypeMap); - } - return retValue; - } - - public List<String> filter(String[] filterNames, String[] HostID, String vmID, String args) throws XmlRpcException { - Object[] sentObject = new Object[4]; - //filters name - sentObject[0] = filterNames; - //hosts xml - sentObject[1] = HostID; - //vm xml - sentObject[2] = vmID; - //additional args - sentObject[3] = args; - - Object execute = client.execute("runFilters", sentObject); - return parseFilterResult(execute); - } - - private List<String> parseFilterResult(Object result){ - if (result == null || ! (result instanceof Object[])){ - System.out.println("Filter error"); - return null; - } - //Its a list of host IDs - List<String> retValue = new LinkedList<String>(); - for (Object hostID : (Object[])result) { - retValue.add(hostID.toString()); - } - return retValue; - } - - public HashMap<String, Integer> score(String[] scoreNames, Integer[] weights, String[] HostID, String vmID, String args) throws XmlRpcException { - Object[] sentObject = new Object[4]; - - if(scoreNames == null || weights == null || scoreNames.length != weights.length){ - return null; - } - - Object[] pairs = new Object[scoreNames.length]; - - for (int i = 0; i < pairs.length; i++) { - pairs[i] = new Object[] { scoreNames[i], weights[i] }; - } - //score name + weight pairs - sentObject[0] = pairs; - //hosts xml - sentObject[1] = HostID; - //vm xml - sentObject[2] = vmID; - //additional args - sentObject[3] = args; - - Object execute = client.execute("runCostFunctions", sentObject); - return parseScoreResults(execute); - } - - private HashMap<String, Integer> parseScoreResults(Object result){ - if (result == null || ! (result instanceof Object[])){ - System.out.println("Score error"); - } - HashMap<String, Integer> retValue = new HashMap<String, Integer>(); - //Its a list of (hostID,score) pairs - for (Object hostsIDAndScore : (Object[])result) { - if( ! (hostsIDAndScore instanceof Object[]) || ((Object[])hostsIDAndScore).length != 2 ){ - //some kind of error - System.out.println("Got bad score"); - return null; - } - Object[] castedHostsIDAndScore = (Object[])hostsIDAndScore; - retValue.put(castedHostsIDAndScore[0].toString(), (Integer)castedHostsIDAndScore[1]); - } - return retValue; - } - - public HashMap<String, List<String>> balance(String balanceName, String[] HostID, String args) throws XmlRpcException { - Object[] sentObject = new Object[3]; - //balance name - sentObject[0] = balanceName; - //hosts xml - sentObject[1] = HostID; - //additional args - sentObject[2] = args; - - Object execute = client.execute("runLoadBalancing", sentObject); - return parseBalanceResults(execute); - } - - private HashMap<String, List<String>> parseBalanceResults(Object result){ - if (result == null || ! (result instanceof Object[])){ - System.out.println("balance error"); - } - Object[] castedResult = (Object[]) result; - HashMap<String, List<String>> retValue = new HashMap<String, List<String>>(); - List<String> hostIDs = new LinkedList<String>(); - for (Object hostID : (Object[])castedResult[1]) { - hostIDs.add(hostID.toString()); - } - retValue.put(castedResult[0].toString(), hostIDs); - return retValue; - } + public HashMap<String, HashMap<String, String[]>> discover() throws XmlRpcException { + Object execute = client.execute("discover", new Object[] {}); + return parseDiscover(execute); + } + + private HashMap<String, HashMap<String, String[]>> parseDiscover(Object result) { + if (result == null || !(result instanceof HashMap)) { + System.out.println("discover error"); + return null; + } + HashMap<String, HashMap<String, Object[]>> castedResult = (HashMap<String, HashMap<String, Object[]>>) result; + // Its a list of host IDs + HashMap<String, HashMap<String, String[]>> retValue = new HashMap<String, HashMap<String, String[]>>(); + for (String keyType : castedResult.keySet()) { + HashMap<String, Object[]> typeMap = castedResult.get(keyType); + HashMap<String, String[]> newTypeMap = new HashMap<String, String[]>(); + for (String keyModuleName : typeMap.keySet()) { + String[] keys = new String[2]; + for (int i = 0; i < 2; i++) { + keys[i] = (String) typeMap.get(keyModuleName)[i]; + } + newTypeMap.put(keyModuleName, keys); + } + retValue.put(keyType, newTypeMap); + } + return retValue; + } + + public List<String> filter(String[] filterNames, String[] HostID, String vmID, String args) throws XmlRpcException { + Object[] sentObject = new Object[4]; + // filters name + sentObject[0] = filterNames; + // hosts xml + sentObject[1] = HostID; + // vm xml + sentObject[2] = vmID; + // additional args + sentObject[3] = args; + + Object execute = client.execute("runFilters", sentObject); + return parseFilterResult(execute); + } + + private List<String> parseFilterResult(Object result) { + if (result == null || !(result instanceof Object[])) { + System.out.println("Filter error"); + return null; + } + // Its a list of host IDs + List<String> retValue = new LinkedList<String>(); + for (Object hostID : (Object[]) result) { + retValue.add(hostID.toString()); + } + return retValue; + } + + public HashMap<String, Integer> score(String[] scoreNames, + Integer[] weights, + String[] HostID, + String vmID, + String args) throws XmlRpcException { + Object[] sentObject = new Object[4]; + + if (scoreNames == null || weights == null || scoreNames.length != weights.length) { + return null; + } + + Object[] pairs = new Object[scoreNames.length]; + + for (int i = 0; i < pairs.length; i++) { + pairs[i] = new Object[] { scoreNames[i], weights[i] }; + } + // score name + weight pairs + sentObject[0] = pairs; + // hosts xml + sentObject[1] = HostID; + // vm xml + sentObject[2] = vmID; + // additional args + sentObject[3] = args; + + Object execute = client.execute("runCostFunctions", sentObject); + return parseScoreResults(execute); + } + + private HashMap<String, Integer> parseScoreResults(Object result) { + if (result == null || !(result instanceof Object[])) { + System.out.println("Score error"); + } + HashMap<String, Integer> retValue = new HashMap<String, Integer>(); + // Its a list of (hostID,score) pairs + for (Object hostsIDAndScore : (Object[]) result) { + if (!(hostsIDAndScore instanceof Object[]) || ((Object[]) hostsIDAndScore).length != 2) { + // some kind of error + System.out.println("Got bad score"); + return null; + } + Object[] castedHostsIDAndScore = (Object[]) hostsIDAndScore; + retValue.put(castedHostsIDAndScore[0].toString(), (Integer) castedHostsIDAndScore[1]); + } + return retValue; + } + + public HashMap<String, List<String>> balance(String balanceName, String[] HostID, String args) + throws XmlRpcException { + Object[] sentObject = new Object[3]; + // balance name + sentObject[0] = balanceName; + // hosts xml + sentObject[1] = HostID; + // additional args + sentObject[2] = args; + + Object execute = client.execute("runLoadBalancing", sentObject); + return parseBalanceResults(execute); + } + + private HashMap<String, List<String>> parseBalanceResults(Object result) { + if (result == null || !(result instanceof Object[])) { + System.out.println("balance error"); + } + Object[] castedResult = (Object[]) result; + HashMap<String, List<String>> retValue = new HashMap<String, List<String>>(); + List<String> hostIDs = new LinkedList<String>(); + for (Object hostID : (Object[]) castedResult[1]) { + hostIDs.add(hostID.toString()); + } + retValue.put(castedResult[0].toString(), hostIDs); + return retValue; + } } \ No newline at end of file diff --git a/tests/java/src/test/java/org/ovirt/schedulerproxy/SchedulerProxyTest.java b/tests/java/src/test/java/org/ovirt/schedulerproxy/SchedulerProxyTest.java index 3c24abc..0a0c5b4 100644 --- a/tests/java/src/test/java/org/ovirt/schedulerproxy/SchedulerProxyTest.java +++ b/tests/java/src/test/java/org/ovirt/schedulerproxy/SchedulerProxyTest.java @@ -1,6 +1,5 @@ package org.ovirt.schedulerproxy; - import static org.junit.Assert.assertTrue; import java.net.MalformedURLException; import java.util.HashMap; @@ -11,58 +10,60 @@ import org.junit.Test; public class SchedulerProxyTest { - - static String FILE_NAME = "dummy"; - static String HOST_ID1 = "11111111-1111-1111-1111-111111111111"; - static String HOST_ID2 = "22222222-2222-2222-2222-222222222222"; - static String[] HOST_ARRAY = new String[] { HOST_ID1, HOST_ID2 }; - static String VM_ID = "33333333-3333-3333-3333-333333333333"; - static String FILTER_DESCRIPTION = "This is a simple filter that returns all given host ID"; - static String SCORE_DESCRIPTION = "This is a simple score function that returns all given host ID with score 50"; - static String BALANCE_DESCRIPTION = "This is a fake balance function that returns always return the guid 33333333-3333-3333-3333-333333333333"; - SchedulerProxy proxy; + static String FILE_NAME = "dummy"; + static String HOST_ID1 = "11111111-1111-1111-1111-111111111111"; + static String HOST_ID2 = "22222222-2222-2222-2222-222222222222"; + static String[] HOST_ARRAY = new String[] { HOST_ID1, HOST_ID2 }; + static String VM_ID = "33333333-3333-3333-3333-333333333333"; + static String FILTER_DESCRIPTION = "This is a simple filter that returns all given host ID"; + static String SCORE_DESCRIPTION = "This is a simple score function that returns all given host ID with score 50"; + static String BALANCE_DESCRIPTION = + "This is a fake balance function that returns always return the guid 33333333-3333-3333-3333-333333333333"; - @Before - public void setUp() throws MalformedURLException { - proxy = new SchedulerProxy("http://localhost:18781/"); - } + SchedulerProxy proxy; - @Test - public void testDiscover() throws XmlRpcException { - HashMap<String, HashMap<String, String[]>> result = proxy.discover(); - assertTrue(result.containsKey("filters")); - assertTrue(result.get("filters").containsKey((FILE_NAME))); - assertTrue(result.get("filters").get(FILE_NAME)[0].equals(FILTER_DESCRIPTION)); - assertTrue(result.containsKey("scores")); - assertTrue(result.get("scores").containsKey((FILE_NAME))); - assertTrue(result.get("scores").get(FILE_NAME)[0].equals(SCORE_DESCRIPTION)); - assertTrue(result.containsKey("filters")); - assertTrue(result.get("balance").containsKey((FILE_NAME))); - assertTrue(result.get("balance").get(FILE_NAME)[0].equals(BALANCE_DESCRIPTION)); - - } - - @Test - public void testFilter() throws XmlRpcException { - List<String> result = proxy.filter(new String[] { FILE_NAME }, HOST_ARRAY, VM_ID, ""); - assertTrue(result.size() == HOST_ARRAY.length); - assertTrue(result.contains(HOST_ID1)); - assertTrue(result.contains(HOST_ID2)); - } - - @Test - public void testScore() throws XmlRpcException { - HashMap<String,Integer> result = proxy.score(new String[] { FILE_NAME }, new Integer[] { 2 }, HOST_ARRAY, VM_ID, ""); - assertTrue(result.size() == 2); - assertTrue(result.get(HOST_ID1) == 100); - assertTrue(result.get(HOST_ID2) == 100); - } - - @Test - public void testBalance() throws XmlRpcException { - HashMap<String, List<String>> result = proxy.balance(FILE_NAME, HOST_ARRAY, ""); - assertTrue(result.containsKey(VM_ID)); - assertTrue(result.get(VM_ID).contains(HOST_ID1)); - } + @Before + public void setUp() throws MalformedURLException { + proxy = new SchedulerProxy("http://localhost:18781/"); + } + + @Test + public void testDiscover() throws XmlRpcException { + HashMap<String, HashMap<String, String[]>> result = proxy.discover(); + assertTrue(result.containsKey("filters")); + assertTrue(result.get("filters").containsKey((FILE_NAME))); + assertTrue(result.get("filters").get(FILE_NAME)[0].equals(FILTER_DESCRIPTION)); + assertTrue(result.containsKey("scores")); + assertTrue(result.get("scores").containsKey((FILE_NAME))); + assertTrue(result.get("scores").get(FILE_NAME)[0].equals(SCORE_DESCRIPTION)); + assertTrue(result.containsKey("filters")); + assertTrue(result.get("balance").containsKey((FILE_NAME))); + assertTrue(result.get("balance").get(FILE_NAME)[0].equals(BALANCE_DESCRIPTION)); + + } + + @Test + public void testFilter() throws XmlRpcException { + List<String> result = proxy.filter(new String[] { FILE_NAME }, HOST_ARRAY, VM_ID, ""); + assertTrue(result.size() == HOST_ARRAY.length); + assertTrue(result.contains(HOST_ID1)); + assertTrue(result.contains(HOST_ID2)); + } + + @Test + public void testScore() throws XmlRpcException { + HashMap<String, Integer> result = + proxy.score(new String[] { FILE_NAME }, new Integer[] { 2 }, HOST_ARRAY, VM_ID, ""); + assertTrue(result.size() == 2); + assertTrue(result.get(HOST_ID1) == 100); + assertTrue(result.get(HOST_ID2) == 100); + } + + @Test + public void testBalance() throws XmlRpcException { + HashMap<String, List<String>> result = proxy.balance(FILE_NAME, HOST_ARRAY, ""); + assertTrue(result.containsKey(VM_ID)); + assertTrue(result.get(VM_ID).contains(HOST_ID1)); + } } \ No newline at end of file -- To view, visit http://gerrit.ovirt.org/18169 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I705030ceb98c887f5636476476c30cdd1823a968 Gerrit-PatchSet: 1 Gerrit-Project: ovirt-scheduler-proxy Gerrit-Branch: master Gerrit-Owner: Laszlo Hornyak <[email protected]> _______________________________________________ Engine-patches mailing list [email protected] http://lists.ovirt.org/mailman/listinfo/engine-patches
