Ravi Nori has uploaded a new change for review.

Change subject: restapi : Add /applications sub-collection under vm(#926928)
......................................................................

restapi : Add /applications sub-collection under vm(#926928)

Add /applications sub-collection under vm

Change-Id: I8b7db49d2cc7b47c39ef3d4fff261a99c24dae42
Bug-Url: https://bugzilla.redhat.com/926928
Signed-off-by: Ravi Nori <rn...@redhat.com>
---
A 
backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/Application.java
M 
backend/manager/modules/restapi/interface/common/jaxrs/src/main/java/org/ovirt/engine/api/common/util/LinkHelper.java
M 
backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/resource/VmResource.java
M 
backend/manager/modules/restapi/interface/definition/src/main/resources/api.xsd
A 
backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendVmApplicationResource.java
A 
backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendVmApplicationsResource.java
M 
backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendVmResource.java
M 
backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendVmsResource.java
A 
backend/manager/modules/restapi/jaxrs/src/test/java/org/ovirt/engine/api/restapi/resource/AbstractBackendVmApplicationsResourceTest.java
A 
backend/manager/modules/restapi/jaxrs/src/test/java/org/ovirt/engine/api/restapi/resource/BackendVmApplicationResourceTest.java
A 
backend/manager/modules/restapi/jaxrs/src/test/java/org/ovirt/engine/api/restapi/resource/BackendVmApplicationsResourceTest.java
A 
backend/manager/modules/restapi/types/src/main/java/org/ovirt/engine/api/restapi/types/ApplicationMapper.java
A 
backend/manager/modules/restapi/types/src/test/java/org/ovirt/engine/api/restapi/types/ApplicationMapperTest.java
13 files changed, 719 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/93/13493/1

diff --git 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/Application.java
 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/Application.java
new file mode 100644
index 0000000..b4864fa
--- /dev/null
+++ 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/Application.java
@@ -0,0 +1,51 @@
+package org.ovirt.engine.core.common.businessentities;
+
+import java.io.Serializable;
+
+
+import org.ovirt.engine.core.common.utils.ObjectUtils;
+
+public class Application extends IVdcQueryable implements Serializable {
+    private static final long serialVersionUID = 8177640907822845847L;
+
+    private String name;
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+
+    public Application() {
+    }
+
+    public Application(String name) {
+        this.name = name;
+    }
+
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ((name == null) ? 0 : name.hashCode());
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj == null) {
+            return false;
+        }
+        if (obj.getClass() != this.getClass()) {
+            return false;
+        }
+        Application other = (Application) obj;
+        return ObjectUtils.objectsEqual(name, other.name);
+    }
+}
diff --git 
a/backend/manager/modules/restapi/interface/common/jaxrs/src/main/java/org/ovirt/engine/api/common/util/LinkHelper.java
 
b/backend/manager/modules/restapi/interface/common/jaxrs/src/main/java/org/ovirt/engine/api/common/util/LinkHelper.java
index 7046da4..5b6e615 100644
--- 
a/backend/manager/modules/restapi/interface/common/jaxrs/src/main/java/org/ovirt/engine/api/common/util/LinkHelper.java
+++ 
b/backend/manager/modules/restapi/interface/common/jaxrs/src/main/java/org/ovirt/engine/api/common/util/LinkHelper.java
@@ -28,6 +28,7 @@
 
 import org.ovirt.engine.api.model.ActionableResource;
 import org.ovirt.engine.api.model.ActionsBuilder;
+import org.ovirt.engine.api.model.Application;
 import org.ovirt.engine.api.model.BaseResource;
 import org.ovirt.engine.api.model.CdRom;
 import org.ovirt.engine.api.model.Cluster;
@@ -194,6 +195,9 @@
         map = new ParentToCollectionMap(DeviceResource.class, 
DevicesResource.class, VM.class);
         TYPES.put(CdRom.class, map);
 
+        map = new ParentToCollectionMap(DeviceResource.class, 
DevicesResource.class, VM.class);
+        TYPES.put(Application.class, map);
+
         map = new ParentToCollectionMap(VmReportedDeviceResource.class, 
VmReportedDevicesResource.class, VM.class);
         TYPES.put(ReportedDevice.class, map);
 
diff --git 
a/backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/resource/VmResource.java
 
b/backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/resource/VmResource.java
index 905b888..05c09ba 100644
--- 
a/backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/resource/VmResource.java
+++ 
b/backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/resource/VmResource.java
@@ -26,6 +26,8 @@
 import org.jboss.resteasy.annotations.providers.jaxb.Formatted;
 import org.ovirt.engine.api.model.Action;
 import org.ovirt.engine.api.model.Actionable;
+import org.ovirt.engine.api.model.Application;
+import org.ovirt.engine.api.model.Applications;
 import org.ovirt.engine.api.model.CdRom;
 import org.ovirt.engine.api.model.CdRoms;
 import org.ovirt.engine.api.model.VM;
@@ -108,6 +110,9 @@
     @Path("cancelmigration")
     public Response cancelMigration(Action action);
 
+    @Path("applications")
+    public DevicesResource<Application, Applications> 
getApplicationsResource();
+
     @Path("cdroms")
     public DevicesResource<CdRom, CdRoms> getCdRomsResource();
 
diff --git 
a/backend/manager/modules/restapi/interface/definition/src/main/resources/api.xsd
 
b/backend/manager/modules/restapi/interface/definition/src/main/resources/api.xsd
index b0dc308..ef666c5 100644
--- 
a/backend/manager/modules/restapi/interface/definition/src/main/resources/api.xsd
+++ 
b/backend/manager/modules/restapi/interface/definition/src/main/resources/api.xsd
@@ -2311,6 +2311,10 @@
     </xs:complexContent>
   </xs:complexType>
 
+  <xs:element name="application" type="Application"/>
+
+  <xs:element name="applications" type="Applications"/>
+
   <xs:element name="cdrom" type="CdRom"/>
 
   <xs:element name="cdroms" type="CdRoms"/>
@@ -2327,6 +2331,32 @@
 
   <xs:element name="nics" type="Nics"/>
 
+  <xs:complexType name="Application">
+    <xs:complexContent>
+      <xs:extension base="BaseDevice">
+        <xs:sequence>
+          <xs:element name="appName" type="xs:string" minOccurs="0"/>
+        </xs:sequence>
+      </xs:extension>
+    </xs:complexContent>
+  </xs:complexType>
+
+  <xs:complexType name="Applications">
+    <xs:complexContent>
+      <xs:extension base="BaseDevices">
+        <xs:sequence>
+          <xs:element ref="application" minOccurs="0" maxOccurs="unbounded">
+            <xs:annotation>
+              <xs:appinfo>
+                <jaxb:property name="Applications"/>
+              </xs:appinfo>
+            </xs:annotation>
+          </xs:element>
+        </xs:sequence>
+      </xs:extension>
+    </xs:complexContent>
+  </xs:complexType>
+
   <xs:complexType name="CdRom">
     <xs:complexContent>
       <xs:extension base="BaseDevice">
diff --git 
a/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendVmApplicationResource.java
 
b/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendVmApplicationResource.java
new file mode 100644
index 0000000..58b9e08
--- /dev/null
+++ 
b/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendVmApplicationResource.java
@@ -0,0 +1,52 @@
+package org.ovirt.engine.api.restapi.resource;
+
+import org.ovirt.engine.core.common.action.VdcActionType;
+import org.ovirt.engine.core.common.businessentities.VM;
+import org.ovirt.engine.core.compat.Guid;
+
+import org.ovirt.engine.api.model.Application;
+import org.ovirt.engine.api.model.Applications;
+import org.ovirt.engine.api.resource.DeviceResource;
+
+public class BackendVmApplicationResource extends 
BackendDeviceResource<Application, Applications, VM> implements 
DeviceResource<Application>{
+
+    public static final String CURRENT_CONSTRAINT_PARAMETER = "current";
+
+    public BackendVmApplicationResource(Class<Application> modelType,
+                                 Class<VM> entityType,
+                                 final Guid guid,
+                                 final 
AbstractBackendReadOnlyDevicesResource<Application, Applications, VM> 
collection,
+                                 VdcActionType updateType,
+                                 ParametersProvider<Application, VM> 
updateParametersProvider,
+                                 String[] requiredUpdateFields,
+                                 String... subCollections) {
+        super(modelType, entityType, guid, collection, updateType, 
updateParametersProvider, requiredUpdateFields, subCollections);
+    }
+
+    @Override
+    protected Application map(VM entity) {
+        try {
+            int index = getIndex(id);
+            String[] apps = entity.getAppList() == null ? new String[0] : 
entity.getAppList().split(",");
+            if (index > apps.length) {
+                return notFound();
+            }
+            VM entityVM = new VM();
+            entityVM.setAppList(apps[index]);
+            return map(entityVM, null);
+        } catch(Exception e) {
+            return notFound();
+        }
+    }
+
+    private int getIndex(String id) throws Exception {
+        int index = id.lastIndexOf("-");
+        String indexStr = id.substring(index+1);
+        return Integer.parseInt(indexStr);
+    }
+
+    @Override
+    public Application update(Application resource) {
+        return resource;
+    }
+}
diff --git 
a/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendVmApplicationsResource.java
 
b/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendVmApplicationsResource.java
new file mode 100644
index 0000000..16d419a
--- /dev/null
+++ 
b/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendVmApplicationsResource.java
@@ -0,0 +1,168 @@
+package org.ovirt.engine.api.restapi.resource;
+
+//import java.util.List;
+import java.util.ArrayList;
+import java.util.List;
+import org.ovirt.engine.core.common.businessentities.VM;
+import org.ovirt.engine.api.model.Application;
+import org.ovirt.engine.api.model.Applications;
+import org.ovirt.engine.api.resource.DeviceResource;
+import org.ovirt.engine.api.resource.DevicesResource;
+import org.ovirt.engine.core.common.action.VdcActionParametersBase;
+import org.ovirt.engine.core.common.action.VmManagementParametersBase;
+import org.ovirt.engine.core.common.businessentities.VmStatic;
+import org.ovirt.engine.core.common.queries.GetVmByVmIdParameters;
+import org.ovirt.engine.core.common.queries.VdcQueryParametersBase;
+import org.ovirt.engine.core.common.queries.VdcQueryType;
+import org.ovirt.engine.core.compat.Guid;
+import org.ovirt.engine.core.common.action.VdcActionType;
+
+public class BackendVmApplicationsResource
+        extends AbstractBackendDevicesResource<Application, Applications, VM>
+        implements DevicesResource<Application, Applications> {
+
+    public BackendVmApplicationsResource(Guid parentId,
+                                 VdcQueryType queryType,
+                                 VdcQueryParametersBase queryParams) {
+        super(Application.class,
+              Applications.class,
+              VM.class,
+              parentId,
+              queryType,
+              queryParams,
+              VdcActionType.UpdateVm,
+              VdcActionType.UpdateVm,
+              VdcActionType.UpdateVm);
+    }
+
+    @Override
+    protected Applications mapCollection(List<VM> entities, boolean addLinks) {
+        Applications collection = instantiate(collectionType);
+        List<Application> list = getList(collection);
+        int id = 0;
+        for (VM entity : entities) {
+            List<VM> vmEntities = getVmEntities(entity);
+            for (VM vmEntity : vmEntities) {
+                Application candidate = populate(map(vmEntity), vmEntity);
+                if (validate(candidate)) {
+                    candidate.setId(getNextId(id++));
+                    if (addLinks) {
+                        candidate = addLinks(candidate);
+                    }
+                    list.add(candidate);
+                }
+            }
+        }
+        return collection;
+    }
+
+    private String getNextId(int id) {
+        StringBuilder buf = new StringBuilder("00000000-0000-0000-0000-");
+        String idStr = ""+id;
+        while (idStr.length() < 12) {
+            idStr = "0" + idStr;
+        }
+        buf.append(idStr);
+        return buf.toString();
+    }
+
+    private List<VM> getVmEntities(VM vm) {
+        List<VM> vmEntities = new ArrayList<VM>();
+        String[] apps = vm.getAppList() == null ? new String[0] : 
vm.getAppList().split(",");
+        for (String app : apps) {
+            VM vmEntity = new VM();
+            vmEntity.setAppList(app.trim());
+            vmEntities.add(vmEntity);
+        }
+        return vmEntities;
+    }
+
+//    @Override
+//    public Applications list() {
+//        return mapCollection(getBackendCollection(queryType, queryParams));
+//    }
+//
+//    @Override
+//    protected Applications mapCollection(List<VM> entities) {
+//        Applications applications = new Applications();
+//        if (!entities.isEmpty()) {
+//            VM vm = entities.get(0);
+//            String[] apps = vm.getAppList() == null ? new String[0] : 
vm.getAppList().split(",");
+//            for (String app : apps) {
+//                Application application = new Application();
+//                application.setAppName(app);
+//                applications.getApplications().add(application);
+//            }
+//        }
+//        return applications;
+//    }
+
+    @Override
+    protected <T> boolean matchEntity(VM entity, T id) {
+        System.out.println("BackendVmApplicationsResource matchEntity id = 
"+id);
+        return parentId.equals(entity.getQueryableId());
+    }
+
+    @Override
+    protected boolean matchEntity(VM entity, String name) {
+        return false;
+    }
+
+    @Override
+    protected String[] getRequiredAddFields() {
+        return new String[] { "appName" };
+    }
+
+    @Override
+    protected String[] getRequiredUpdateFields() {
+        return new String[] { "appName" };
+    }
+
+    @Override
+    protected VdcActionParametersBase getAddParameters(VM mapped, Application 
cdrom) {
+        return new 
VmManagementParametersBase(getUpdatable(mapped.getStaticData().getIsoPath()));
+    }
+
+    @Override
+    protected VdcActionParametersBase getRemoveParameters(String id) {
+        return new VmManagementParametersBase(getUpdatable(null));
+    }
+
+    protected VmStatic getUpdatable(String isoPath) {
+        VmStatic updatable = getEntity(VM.class,
+                                       VdcQueryType.GetVmByVmId,
+                                       new GetVmByVmIdParameters(parentId),
+                                       parentId.toString()).getStaticData();
+        updatable.setIsoPath(isoPath);
+        return updatable;
+    }
+
+    @Override
+    protected AbstractBackendSubResource.ParametersProvider<Application, VM> 
getUpdateParametersProvider() {
+        return new UpdateParametersProvider();
+    }
+
+    protected class UpdateParametersProvider implements 
AbstractBackendSubResource.ParametersProvider<Application, VM> {
+        @Override
+        public VdcActionParametersBase getParameters(Application incoming, VM 
entity) {
+            return new 
VmManagementParametersBase(getUpdatable(incoming.getId()));
+        }
+    }
+
+    @Override
+    @SingleEntityResource
+    public DeviceResource<Application> getDeviceSubResource(String id) {
+        return inject(new BackendVmApplicationResource(modelType,
+                                               entityType,
+                                               asGuidOr404(id),
+                                               this,
+                                               updateType,
+                                               getUpdateParametersProvider(),
+                                               getRequiredUpdateFields()));
+    }
+
+    @Override
+    protected Application doPopulate(Application model, VM entity) {
+        return model;
+    }
+}
diff --git 
a/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendVmResource.java
 
b/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendVmResource.java
index 056acca..7a9a457 100644
--- 
a/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendVmResource.java
+++ 
b/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendVmResource.java
@@ -14,6 +14,8 @@
 import org.ovirt.engine.api.common.util.DetailHelper.Detail;
 import org.ovirt.engine.api.common.util.LinkHelper;
 import org.ovirt.engine.api.model.Action;
+import org.ovirt.engine.api.model.Application;
+import org.ovirt.engine.api.model.Applications;
 import org.ovirt.engine.api.model.CdRom;
 import org.ovirt.engine.api.model.CdRoms;
 import org.ovirt.engine.api.model.Certificate;
@@ -160,6 +162,14 @@
     }
 
     @Override
+    public DevicesResource<Application, Applications> 
getApplicationsResource() {
+        return inject(new BackendVmApplicationsResource
+                                        (guid,
+                                         VdcQueryType.GetVmByVmId,
+                                         new GetVmByVmIdParameters(guid)));
+    }
+
+    @Override
     public AssignedPermissionsResource getPermissionsResource() {
         return inject(new BackendAssignedPermissionsResource(guid,
                                                              
VdcQueryType.GetPermissionsForObject,
diff --git 
a/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendVmsResource.java
 
b/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendVmsResource.java
index ebef9cb..39111a2 100644
--- 
a/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendVmsResource.java
+++ 
b/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendVmsResource.java
@@ -59,7 +59,7 @@
         AbstractBackendCollectionResource<VM, 
org.ovirt.engine.core.common.businessentities.VM>
         implements VmsResource {
 
-    static final String[] SUB_COLLECTIONS = { "disks", "nics", "cdroms", 
"snapshots", "tags", "permissions",
+    static final String[] SUB_COLLECTIONS = { "applications", "disks", "nics", 
"cdroms", "snapshots", "tags", "permissions",
             "statistics", "reporteddevices" };
 
     public BackendVmsResource() {
diff --git 
a/backend/manager/modules/restapi/jaxrs/src/test/java/org/ovirt/engine/api/restapi/resource/AbstractBackendVmApplicationsResourceTest.java
 
b/backend/manager/modules/restapi/jaxrs/src/test/java/org/ovirt/engine/api/restapi/resource/AbstractBackendVmApplicationsResourceTest.java
new file mode 100644
index 0000000..0448ea2
--- /dev/null
+++ 
b/backend/manager/modules/restapi/jaxrs/src/test/java/org/ovirt/engine/api/restapi/resource/AbstractBackendVmApplicationsResourceTest.java
@@ -0,0 +1,142 @@
+package org.ovirt.engine.api.restapi.resource;
+
+import java.util.List;
+import java.util.ArrayList;
+
+
+import org.junit.Ignore;
+import org.junit.Test;
+
+import org.ovirt.engine.api.model.Application;
+import org.ovirt.engine.api.model.Applications;
+import org.ovirt.engine.core.common.businessentities.VM;
+import org.ovirt.engine.core.common.businessentities.VMStatus;
+import org.ovirt.engine.core.common.queries.VdcQueryParametersBase;
+import org.ovirt.engine.core.common.queries.VdcQueryType;
+import org.ovirt.engine.core.compat.Guid;
+
+import static org.easymock.classextension.EasyMock.expect;
+import org.ovirt.engine.core.common.businessentities.VmDynamic;
+
+@Ignore
+public class AbstractBackendVmApplicationsResourceTest<T extends 
AbstractBackendReadOnlyDevicesResource<Application, Applications, VM>>
+        extends AbstractBackendCollectionResourceTest<Application, VM, T> {
+
+    protected final static Guid PARENT_ID = GUIDS[1];
+    protected final static String ISO_PATH = "Fedora-13-x86_64-Live.iso";
+
+    protected VdcQueryType queryType;
+    protected VdcQueryParametersBase queryParams;
+    protected String queryIdName;
+
+    public AbstractBackendVmApplicationsResourceTest(T collection,
+                                             VdcQueryType queryType,
+                                             VdcQueryParametersBase 
queryParams,
+                                             String queryIdName) {
+        super(collection, null, "");
+        this.queryType = queryType;
+        this.queryParams = queryParams;
+        this.queryIdName = queryIdName;
+    }
+
+    @Test
+    @Ignore
+    public void testQuery() throws Exception {
+        // skip test inherited from base class as searching
+        // over Applications is unsupported by the backend
+    }
+
+    protected void setUpQueryExpectations(String query) throws Exception {
+        setUpEntityQueryExpectations(1);
+        control.replay();
+    }
+
+    protected void setUpQueryExpectations(String query, Object failure) throws 
Exception {
+        setUpEntityQueryExpectations(1, failure);
+        control.replay();
+    }
+
+    protected void setUpEntityQueryExpectations(int times) throws Exception {
+        setUpEntityQueryExpectations(times, null);
+    }
+
+    protected void setUpEntityQueryExpectations(int times, Object failure) 
throws Exception {
+        while (times-- > 0) {
+            setUpEntityQueryExpectations(queryType,
+                                         queryParams.getClass(),
+                                         new String[] { queryIdName },
+                                         new Object[] { PARENT_ID },
+                                         getEntityList(),
+                                         failure);
+        }
+    }
+
+    protected List<VM> getEntityList() {
+        List<VM> entities = new ArrayList<VM>();
+        entities.add(getEntity(0));
+        return entities;
+    }
+
+    protected VM getEntity(int index) {
+        return setUpEntityExpectations(control.createMock(VM.class),
+                                       control.createMock(VmDynamic.class),
+                                       index);
+    }
+
+    static VM setUpEntityExpectations(VM entity, VmDynamic dynamicVm, int 
index) {
+        return setUpEntityExpectations(entity, dynamicVm, null, index);
+    }
+
+    static VM setUpEntityExpectations(VM entity, VmDynamic dynamicVm, VMStatus 
status, int index) {
+        expect(entity.getQueryableId()).andReturn(PARENT_ID).anyTimes();
+        expect(entity.getDynamicData()).andReturn(dynamicVm).anyTimes();
+        expect(entity.getAppList()).andReturn(getAppList()).anyTimes();
+        if (status != null) {
+            expect(entity.getStatus()).andReturn(status).anyTimes();
+        }
+        return entity;
+    }
+
+    static String getAppList() {
+        StringBuilder buf = new StringBuilder();
+        for (String name : NAMES) {
+            if (buf.length() > 0) {
+                buf.append(",");
+            }
+            buf.append(name);
+        }
+        return buf.toString();
+    }
+
+    protected List<Application> getCollection() {
+        return collection.list().getApplications();
+    }
+
+    static Application getModel(int index) {
+        Application model = new Application();
+        model.setId(getNextId(index));
+        return model;
+    }
+
+    static String getNextId(int id) {
+        StringBuilder buf = new StringBuilder("00000000-0000-0000-0000-");
+        String idStr = ""+id;
+        while (idStr.length() < 12) {
+            idStr = "0" + idStr;
+        }
+        buf.append(idStr);
+        return buf.toString();
+    }
+
+    protected void verifyModel(Application model, int index) {
+        verifyModelSpecific(model, index);
+        verifyLinks(model);
+    }
+
+    static void verifyModelSpecific(Application model, int index) {
+        assertNotNull(model.getId());
+        assertTrue(model.isSetVm());
+        assertEquals(PARENT_ID.toString(), model.getVm().getId());
+        assertEquals(getNextId(index), model.getId());
+    }
+}
diff --git 
a/backend/manager/modules/restapi/jaxrs/src/test/java/org/ovirt/engine/api/restapi/resource/BackendVmApplicationResourceTest.java
 
b/backend/manager/modules/restapi/jaxrs/src/test/java/org/ovirt/engine/api/restapi/resource/BackendVmApplicationResourceTest.java
new file mode 100644
index 0000000..57a63d0
--- /dev/null
+++ 
b/backend/manager/modules/restapi/jaxrs/src/test/java/org/ovirt/engine/api/restapi/resource/BackendVmApplicationResourceTest.java
@@ -0,0 +1,145 @@
+package org.ovirt.engine.api.restapi.resource;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.ws.rs.WebApplicationException;
+
+import org.junit.Test;
+
+
+import org.ovirt.engine.core.common.action.VdcActionType;
+import org.ovirt.engine.core.common.businessentities.VM;
+import org.ovirt.engine.core.common.businessentities.VMStatus;
+import org.ovirt.engine.core.common.queries.GetVmByVmIdParameters;
+import org.ovirt.engine.core.common.queries.VdcQueryType;
+import org.ovirt.engine.core.compat.Guid;
+
+import org.ovirt.engine.api.model.Application;
+import org.ovirt.engine.api.model.Applications;
+import static 
org.ovirt.engine.api.restapi.resource.AbstractBackendVmApplicationsResourceTest.setUpEntityExpectations;
+import static 
org.ovirt.engine.api.restapi.resource.AbstractBackendVmApplicationsResourceTest.verifyModelSpecific;
+import static 
org.ovirt.engine.api.restapi.resource.AbstractBackendVmApplicationsResourceTest.PARENT_ID;
+import org.ovirt.engine.core.common.businessentities.VmDynamic;
+
+public class BackendVmApplicationResourceTest
+        extends AbstractBackendSubResourceTest<Application, VM, 
BackendDeviceResource<Application, Applications, VM>> {
+
+    protected final static String ISO_PATH = "Fedora-13-x86_64-Live.iso";
+    protected static BackendVmApplicationsResource collection = 
getCollection();
+
+    public BackendVmApplicationResourceTest() {
+        super(getResource(GUIDS[0]));
+    }
+
+    protected static BackendDeviceResource<Application, Applications, VM> 
getResource(Guid id) {
+        return new BackendVmApplicationResource(Application.class,
+                VM.class,
+                id,
+                collection,
+                VdcActionType.UpdateVm,
+                collection.getUpdateParametersProvider(),
+                collection.getRequiredUpdateFields());
+    }
+
+    protected BackendDeviceResource<Application, Applications, VM> 
getNotFoundResource() {
+        BackendDeviceResource<Application, Applications, VM> ret = 
getResource(new Guid("0d0264ef-40de-45a1-b746-83a0088b47a7"));
+        ret.setUriInfo(setUpBasicUriExpectations());
+        initResource(ret);
+        initResource(ret.getCollection());
+        return ret;
+    }
+
+    protected static BackendVmApplicationsResource getCollection() {
+        return new BackendVmApplicationsResource(PARENT_ID,
+                VdcQueryType.GetVmByVmId,
+                new GetVmByVmIdParameters(PARENT_ID));
+    }
+
+    protected void init() {
+        super.init();
+        initResource(resource.getCollection());
+    }
+
+    @Test
+    public void testGetNotFound() throws Exception {
+        BackendDeviceResource<Application, Applications, VM> resource = 
getNotFoundResource();
+        setUriInfo(setUpBasicUriExpectations());
+        setUpEntityQueryExpectations(1);
+        control.replay();
+        try {
+            resource.get();
+            fail("expected WebApplicationException");
+        } catch (WebApplicationException wae) {
+            verifyNotFoundException(wae);
+        }
+    }
+
+    @Test
+    public void testGet() throws Exception {
+        setUriInfo(setUpBasicUriExpectations());
+        setUpEntityQueryExpectations(1);
+        control.replay();
+
+        Application application = resource.get();
+        verifyModelSpecific(application, 0);
+        verifyLinks(application);
+    }
+
+    @Override
+    protected VM getEntity(int index) {
+        return setUpEntityExpectations(control.createMock(VM.class),
+                control.createMock(VmDynamic.class),
+                index);
+    }
+
+    protected VM getEntity(int index, VMStatus status) {
+        return setUpEntityExpectations(control.createMock(VM.class),
+                control.createMock(VmDynamic.class),
+                status,
+                index);
+    }
+
+    protected List<VM> getEntityList(VMStatus status) {
+        List<VM> entities = new ArrayList<VM>();
+        for (int i = 0; i < NAMES.length; i++) {
+            if (status != null) {
+                entities.add(getEntity(i, status));
+            } else {
+                entities.add(getEntity(i));
+            }
+        }
+        return entities;
+
+    }
+
+    protected List<VM> getEntityList() {
+        return getEntityList(null);
+
+    }
+
+    protected void setUpEntityQueryExpectations(int times, VMStatus status) 
throws Exception {
+        while (times-- > 0) {
+            setUpEntityQueryExpectations(VdcQueryType.GetVmByVmId,
+                    GetVmByVmIdParameters.class,
+                    new String[]{"Id"},
+                    new Object[]{PARENT_ID},
+                    getEntityList(status));
+        }
+    }
+
+    protected void setUpEntityQueryExpectations(int times) throws Exception {
+        setUpGetEntityExpectations(times, null);
+    }
+
+    protected void setUpGetEntityExpectations(int times, VMStatus status) 
throws Exception {
+        while (times-- > 0) {
+            setUpGetEntityExpectations(VdcQueryType.GetVmByVmId,
+                    GetVmByVmIdParameters.class,
+                    new String[]{"Id"},
+                    new Object[]{PARENT_ID},
+                    status != null ? getEntity(0, status)
+                    : getEntity(0));
+        }
+    }
+}
diff --git 
a/backend/manager/modules/restapi/jaxrs/src/test/java/org/ovirt/engine/api/restapi/resource/BackendVmApplicationsResourceTest.java
 
b/backend/manager/modules/restapi/jaxrs/src/test/java/org/ovirt/engine/api/restapi/resource/BackendVmApplicationsResourceTest.java
new file mode 100644
index 0000000..9024102
--- /dev/null
+++ 
b/backend/manager/modules/restapi/jaxrs/src/test/java/org/ovirt/engine/api/restapi/resource/BackendVmApplicationsResourceTest.java
@@ -0,0 +1,61 @@
+package org.ovirt.engine.api.restapi.resource;
+
+import java.util.List;
+import org.junit.Test;
+
+import javax.ws.rs.WebApplicationException;
+import org.ovirt.engine.api.model.Application;
+import org.ovirt.engine.api.resource.DeviceResource;
+import org.ovirt.engine.core.common.queries.GetVmByVmIdParameters;
+import org.ovirt.engine.core.common.queries.VdcQueryParametersBase;
+import org.ovirt.engine.core.common.queries.VdcQueryType;
+
+public class BackendVmApplicationsResourceTest
+        extends 
AbstractBackendVmApplicationsResourceTest<BackendVmApplicationsResource> {
+
+    public BackendVmApplicationsResourceTest() {
+        super(new BackendVmApplicationsResource(PARENT_ID,
+                                       VdcQueryType.GetVmByVmId,
+                                       new GetVmByVmIdParameters(PARENT_ID)),
+              VdcQueryType.GetVmByVmId,
+              new GetVmByVmIdParameters(PARENT_ID),
+              "Id");
+    }
+
+    @Test
+    public void testSubResourceLocator() throws Exception {
+        control.replay();
+        assertTrue(collection.getDeviceSubResource(GUIDS[0].toString()) 
instanceof DeviceResource);
+    }
+
+    @Test
+    public void testSubResourceLocatorBadGuid() throws Exception {
+        control.replay();
+        try {
+            collection.getDeviceSubResource("foo");
+            fail("expected WebApplicationException");
+        } catch (WebApplicationException wae) {
+            verifyNotFoundException(wae);
+        }
+    }
+
+    protected void setUpEntityQueryExpectations(VdcQueryType query,
+            Class<? extends VdcQueryParametersBase> queryClass,
+            String[] queryNames,
+            Object[] queryValues,
+            Object queryReturn,
+            int times) {
+        while (times-->0) {
+            setUpEntityQueryExpectations(query, queryClass, queryNames, 
queryValues, queryReturn);
+        }
+    }
+
+    @Override
+    protected void verifyCollection(List<Application> collection) throws 
Exception {
+        assertNotNull(collection);
+        assertEquals(NAMES.length, collection.size());
+        for (int i = 0; i < NAMES.length; i++) {
+            verifyModel(collection.get(i), i);
+        }
+    }
+}
diff --git 
a/backend/manager/modules/restapi/types/src/main/java/org/ovirt/engine/api/restapi/types/ApplicationMapper.java
 
b/backend/manager/modules/restapi/types/src/main/java/org/ovirt/engine/api/restapi/types/ApplicationMapper.java
new file mode 100644
index 0000000..4a03074
--- /dev/null
+++ 
b/backend/manager/modules/restapi/types/src/main/java/org/ovirt/engine/api/restapi/types/ApplicationMapper.java
@@ -0,0 +1,31 @@
+package org.ovirt.engine.api.restapi.types;
+
+import org.ovirt.engine.api.model.Application;
+import org.ovirt.engine.core.common.businessentities.VM;
+import org.ovirt.engine.core.compat.Guid;
+import org.ovirt.engine.core.compat.StringHelper;
+
+public class ApplicationMapper {
+
+    public static final Guid APP_ID = Guid.Empty;
+
+    @Mapping(from = Application.class, to = VM.class)
+    public static VM map(Application model, VM template) {
+        VM entity = template != null ? template : new VM();
+        if (!StringHelper.isNullOrEmpty(model.getAppName())) {
+            entity.getDynamicData().setAppList(model.getAppName());
+        }
+        return entity;
+    }
+
+    @Mapping(from = VM.class, to = Application.class)
+    public static Application map(VM entity, Application template) {
+        Application model = template != null ? template : new Application();
+        model.setId(APP_ID.toString());
+        if (!StringHelper.isNullOrEmpty(entity.getDynamicData().getAppList())) 
{
+            model.setAppName(entity.getDynamicData().getAppList());
+        }
+        return model;
+    }
+
+}
diff --git 
a/backend/manager/modules/restapi/types/src/test/java/org/ovirt/engine/api/restapi/types/ApplicationMapperTest.java
 
b/backend/manager/modules/restapi/types/src/test/java/org/ovirt/engine/api/restapi/types/ApplicationMapperTest.java
new file mode 100644
index 0000000..0133ed2
--- /dev/null
+++ 
b/backend/manager/modules/restapi/types/src/test/java/org/ovirt/engine/api/restapi/types/ApplicationMapperTest.java
@@ -0,0 +1,19 @@
+package org.ovirt.engine.api.restapi.types;
+
+import org.ovirt.engine.api.model.Application;
+import org.ovirt.engine.core.common.businessentities.VM;
+
+public class ApplicationMapperTest extends 
AbstractInvertibleMappingTest<Application, VM, VM> {
+
+    public ApplicationMapperTest() {
+        super(Application.class, VM.class, VM.class);
+    }
+
+    @Override
+    protected void verify(Application model, Application transform) {
+        assertNotNull(transform);
+        assertNotNull(transform.getId());
+        assertNotNull(transform.getAppName());
+        assertEquals(model.getAppName(), transform.getAppName());
+    }
+}


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

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

Reply via email to