Laszlo Hornyak has uploaded a new change for review.

Change subject: engine: test for ExternalSchedulerDiscoveryThread
......................................................................

engine: test for ExternalSchedulerDiscoveryThread

new test for ExternalSchedulerDiscoveryThread

Change-Id: I616f4063847686c6bab2366b57030f68ac80b73c
Signed-off-by: Laszlo Hornyak <lhorn...@redhat.com>
---
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/scheduling/external/ExternalSchedulerDiscoveryThread.java
A 
backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/scheduling/external/ExternalSchedulerDiscoveryThreadTest.java
2 files changed, 141 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/33/18333/1

diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/scheduling/external/ExternalSchedulerDiscoveryThread.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/scheduling/external/ExternalSchedulerDiscoveryThread.java
index a85630f..3df5d1e 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/scheduling/external/ExternalSchedulerDiscoveryThread.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/scheduling/external/ExternalSchedulerDiscoveryThread.java
@@ -57,6 +57,10 @@
         // found in the db but not found in discovery, mark as such
         markExternalPoliciesAsDisabled(allPolicyUnits);
 
+        reloadPolicyUnits();
+    }
+
+    void reloadPolicyUnits() {
         SchedulingManager.getInstance().reloadPolicyUnits();
     }
 
diff --git 
a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/scheduling/external/ExternalSchedulerDiscoveryThreadTest.java
 
b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/scheduling/external/ExternalSchedulerDiscoveryThreadTest.java
new file mode 100644
index 0000000..8be43b3
--- /dev/null
+++ 
b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/scheduling/external/ExternalSchedulerDiscoveryThreadTest.java
@@ -0,0 +1,137 @@
+package org.ovirt.engine.core.bll.scheduling.external;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.xmlrpc.XmlRpcException;
+import org.apache.xmlrpc.XmlRpcHandler;
+import org.apache.xmlrpc.XmlRpcRequest;
+import org.apache.xmlrpc.server.XmlRpcHandlerMapping;
+import org.apache.xmlrpc.server.XmlRpcNoSuchHandlerException;
+import org.apache.xmlrpc.webserver.WebServer;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Rule;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.ovirt.engine.core.common.config.ConfigValues;
+import org.ovirt.engine.core.common.scheduling.PolicyUnit;
+import org.ovirt.engine.core.common.scheduling.PolicyUnitType;
+import org.ovirt.engine.core.compat.Guid;
+import org.ovirt.engine.core.dao.scheduling.PolicyUnitDao;
+import org.ovirt.engine.core.utils.MockConfigRule;
+
+public class ExternalSchedulerDiscoveryThreadTest {
+    final static private int testPort = 12345;
+    private static WebServer server;
+
+    @Rule
+    public MockConfigRule mcr = new 
MockConfigRule(MockConfigRule.mockConfig(ConfigValues.ExternalSchedulerServiceURL,
+            "http://localhost:"; + testPort + "/"),
+            
MockConfigRule.mockConfig(ConfigValues.ExternalSchedulerConnectionTimeout, 100),
+            
MockConfigRule.mockConfig(ConfigValues.ExternalSchedulerResponseTimeout, 1000),
+            MockConfigRule.mockConfig(ConfigValues.EnableVdsLoadBalancing, 
true));
+
+    @BeforeClass
+    public static void setupServer() throws IOException, XmlRpcException {
+        final XmlRpcHandler discovery = new XmlRpcHandler() {
+
+            @Override
+            public Object execute(XmlRpcRequest pRequest) throws 
XmlRpcException {
+                HashMap<String, Map<String, Object[]>> map = new 
HashMap<String, Map<String, Object[]>>();
+                HashMap<String, Object[]> filters = new HashMap<String, 
Object[]>();
+                filters.put("foo", new Object[] { "", "success=true;yes=no" });
+                HashMap<String, Object[]> scores = new HashMap<String, 
Object[]>();
+                scores.put("bar", new Object[] { "", "success=true;yes=no" });
+                map.put("filters", filters);
+                map.put("scores", scores);
+                return map;
+            }
+        };
+        server = new WebServer(testPort);
+        server.getXmlRpcServer().setHandlerMapping(new XmlRpcHandlerMapping() {
+
+            @Override
+            public XmlRpcHandler getHandler(String handlerName) throws 
XmlRpcNoSuchHandlerException, XmlRpcException {
+                switch (handlerName) {
+                case "discover":
+                    return discovery;
+                default:
+                    return null;
+                }
+            }
+        });
+        server.start();
+
+    }
+
+    @AfterClass
+    public static void shutdownServer() throws IOException {
+        server.shutdown();
+    }
+
+    @Test
+    public void testRun() throws InterruptedException {
+        final PolicyUnitDao policyUnitDao = Mockito.mock(PolicyUnitDao.class);
+        ExternalSchedulerDiscoveryThread thread = new 
ExternalSchedulerDiscoveryThread() {
+
+            @Override
+            void reloadPolicyUnits() {
+                // do nothing
+            }
+
+            @Override
+            public PolicyUnitDao getPolicyUnitDao() {
+                return policyUnitDao;
+            }
+
+        };
+        thread.start();
+        thread.join();
+        Mockito.verify(policyUnitDao).getAll();
+        // saved the 'foo' and the 'bar' balance to db (since they are not in 
db)
+        Mockito.verify(policyUnitDao, 
Mockito.times(2)).save(Mockito.any(PolicyUnit.class));
+    }
+
+    @Test
+    public void testRunAlreadyInDb() throws InterruptedException {
+        final PolicyUnitDao policyUnitDao = Mockito.mock(PolicyUnitDao.class);
+        ArrayList<PolicyUnit> allPolicyUnits = new ArrayList<PolicyUnit>();
+        PolicyUnit fooPolicyUnit = new PolicyUnit();
+        fooPolicyUnit.setId(new Guid("48b257f1-c900-4914-aeaf-e50f37f98dda"));
+        fooPolicyUnit.setName("foo");
+        fooPolicyUnit.setInternal(false);
+        fooPolicyUnit.setPolicyUnitType(PolicyUnitType.Filter);
+        allPolicyUnits.add(fooPolicyUnit);
+
+        PolicyUnit barPolicyUnit = new PolicyUnit();
+        barPolicyUnit.setId(new Guid("48b257f1-c900-4914-aeaf-e50f37f98ddb"));
+        barPolicyUnit.setName("bar");
+        barPolicyUnit.setInternal(false);
+        barPolicyUnit.setPolicyUnitType(PolicyUnitType.Weight);
+        allPolicyUnits.add(barPolicyUnit);
+
+        Mockito.when(policyUnitDao.getAll()).thenReturn(allPolicyUnits);
+        ExternalSchedulerDiscoveryThread thread = new 
ExternalSchedulerDiscoveryThread() {
+
+            @Override
+            void reloadPolicyUnits() {
+                // do nothing
+            }
+
+            @Override
+            public PolicyUnitDao getPolicyUnitDao() {
+                return policyUnitDao;
+            }
+
+        };
+        thread.start();
+        thread.join();
+        Mockito.verify(policyUnitDao).getAll();
+        // verifies that it was not called, since 'foo' and 'bar' are already 
in the DB
+        Mockito.verify(policyUnitDao, 
Mockito.times(0)).save(Mockito.any(PolicyUnit.class));
+    }
+
+}


-- 
To view, visit http://gerrit.ovirt.org/18333
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I616f4063847686c6bab2366b57030f68ac80b73c
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Laszlo Hornyak <lhorn...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to