Frank Kobzik has uploaded a new change for review.

Change subject: frontend: console protocol priority
......................................................................

frontend: console protocol priority

this patch adds priority to ConsoleProtocol enum. This priority
determines precedence of protocols when selecting default option in the
ui.

Change-Id: I9256ef44071f40552e47f03d829d95da810086ab
Bug-Url: https://bugzilla.redhat.com/1086244
Signed-off-by: Frantisek Kobzik <[email protected]>
---
M 
frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/ConsoleProtocol.java
M 
frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/ConsolesBase.java
A 
frontend/webadmin/modules/uicommonweb/src/test/java/org/ovirt/engine/ui/uicommonweb/models/ConsoleProtocolTest.java
3 files changed, 87 insertions(+), 8 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/62/27462/1

diff --git 
a/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/ConsoleProtocol.java
 
b/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/ConsoleProtocol.java
index 41ee971..eeae082 100644
--- 
a/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/ConsoleProtocol.java
+++ 
b/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/ConsoleProtocol.java
@@ -1,20 +1,32 @@
 package org.ovirt.engine.ui.uicommonweb.models;
 
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
 import org.ovirt.engine.ui.uicommonweb.models.vms.ConsoleModel;
 import org.ovirt.engine.ui.uicommonweb.models.vms.RdpConsoleModel;
 import org.ovirt.engine.ui.uicommonweb.models.vms.SpiceConsoleModel;
 import org.ovirt.engine.ui.uicommonweb.models.vms.VncConsoleModel;
 
-
+/**
+ * Enum representing console protocol.
+ * Console protocol is determined by it's backing class and priority (if a VM 
supports more than one protocol at the
+ * same time, priority determines precedence of protocols).
+ * Protocols with higher number have higher priority.
+ */
 public enum ConsoleProtocol {
-    SPICE(SpiceConsoleModel.class),
-    RDP(RdpConsoleModel.class),
-    VNC(VncConsoleModel.class);
+
+    SPICE(SpiceConsoleModel.class, 3),
+    VNC(VncConsoleModel.class, 2),
+    RDP(RdpConsoleModel.class, 1);
 
     private final Class<? extends ConsoleModel> model;
+    private final int priority;
 
-    private ConsoleProtocol(Class<? extends ConsoleModel> model) {
+    private ConsoleProtocol(Class<? extends ConsoleModel> model, int priority) 
{
         this.model = model;
+        this.priority = priority;
     }
 
     public boolean isBackedBy(Class<? extends ConsoleModel> model) {
@@ -31,6 +43,28 @@
         return null;
     }
 
+    static class PriorityComparator implements Comparator<ConsoleProtocol> {
+        @Override
+        public int compare(ConsoleProtocol fst, ConsoleProtocol snd) {
+            if (fst == null && snd == null) {
+                return 0;
+            }
+            if (fst == null) {
+                return -1;
+            }
+            if (snd == null) {
+                return 1;
+            }
+            return fst.priority - snd.priority;
+        }
+    }
+
+    public static List<ConsoleProtocol> getProtocolsByPriority() {
+        List<ConsoleProtocol> consoleProtocols = 
Arrays.asList(ConsoleProtocol.values());
+        Collections.sort(consoleProtocols, new PriorityComparator());
+        return consoleProtocols;
+    }
+
     public Class getBackingClass() {
         return model;
     }
diff --git 
a/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/ConsolesBase.java
 
b/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/ConsolesBase.java
index dd450b2..a002deb 100644
--- 
a/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/ConsolesBase.java
+++ 
b/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/ConsolesBase.java
@@ -1,7 +1,6 @@
 package org.ovirt.engine.ui.uicommonweb.models;
 
-import java.util.ArrayList;
-import java.util.Arrays;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -67,7 +66,8 @@
 
 
     protected void setDefaultSelectedProtocol() {
-        List<ConsoleProtocol> allProtocols = new 
ArrayList<ConsoleProtocol>(Arrays.asList(ConsoleProtocol.values()));
+        List<ConsoleProtocol> allProtocols = 
ConsoleProtocol.getProtocolsByPriority();
+        Collections.reverse(allProtocols);
 
         if (selectedProtocol != null) { // if it's selected, it's prefered -> 
set it to the 1st position
             allProtocols.remove(selectedProtocol);
diff --git 
a/frontend/webadmin/modules/uicommonweb/src/test/java/org/ovirt/engine/ui/uicommonweb/models/ConsoleProtocolTest.java
 
b/frontend/webadmin/modules/uicommonweb/src/test/java/org/ovirt/engine/ui/uicommonweb/models/ConsoleProtocolTest.java
new file mode 100644
index 0000000..37000f8
--- /dev/null
+++ 
b/frontend/webadmin/modules/uicommonweb/src/test/java/org/ovirt/engine/ui/uicommonweb/models/ConsoleProtocolTest.java
@@ -0,0 +1,45 @@
+package org.ovirt.engine.ui.uicommonweb.models;
+
+import java.util.Arrays;
+import org.junit.Test;
+
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+public class ConsoleProtocolTest {
+
+    private ConsoleProtocol.PriorityComparator cpComparator = new 
ConsoleProtocol.PriorityComparator();
+
+    @Test
+    public void testGetProtocolsByPriority() throws Exception {
+        assertThat(ConsoleProtocol.getProtocolsByPriority(),
+                is(Arrays.asList(new ConsoleProtocol[]{ConsoleProtocol.RDP, 
ConsoleProtocol.VNC, ConsoleProtocol.SPICE})));
+    }
+
+    @Test
+    public void testComparatorSameProtocols() throws Exception {
+        int compared = cpComparator.compare(ConsoleProtocol.SPICE, 
ConsoleProtocol.SPICE);
+        assertThat(compared, is(0));
+    }
+
+    @Test
+    public void testComparatorLowerPriorityFst() throws Exception {
+        int compared = cpComparator.compare(ConsoleProtocol.RDP, 
ConsoleProtocol.VNC);
+        assertTrue(compared < 0);
+    }
+
+    @Test
+    public void testComparatorHigherPriorityFst() throws Exception {
+        int compared = cpComparator.compare(ConsoleProtocol.SPICE, 
ConsoleProtocol.RDP);
+        assertTrue(compared > 0);
+    }
+
+    @Test
+    public void testComparatorNullFst() throws Exception {
+        int compared = cpComparator.compare(null, ConsoleProtocol.RDP);
+        assertTrue(compared < 0);
+    }
+
+}


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I9256ef44071f40552e47f03d829d95da810086ab
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: ovirt-engine-3.4
Gerrit-Owner: Frank Kobzik <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to