/*
 * Copyright 2015 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.cloud.hypervisor.xenserver.resource;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.apache.xmlrpc.XmlRpcException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.util.HashSet;
import java.util.Set;

import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.utils.script.Script;
import com.xensource.xenapi.Connection;
import com.xensource.xenapi.Types;
import com.xensource.xenapi.VBD;
import com.xensource.xenapi.VM;

@RunWith(PowerMockRunner.class)
public class XenServer625ResourceTest extends CitrixResourceBaseTest{

  private final Xenserver625Resource xenServer625Resource = new Xenserver625Resource();

  @Test
  public void testPatchFilePath() {
    final String patchFilePath = xenServer625Resource.getPatchFilePath();
    final String patch = "scripts/vm/hypervisor/xenserver/xenserver62/patch";

    assertEquals(patch, patchFilePath);
  }
  @Test(expected = CloudRuntimeException.class)
  @PrepareForTest(Script.class )
  public void testGetFiles(){
    testGetPathFilesExeption(xenServer625Resource);
  }
  @Test
  @PrepareForTest(Script.class )
  public void testGetFilesListReturned(){
    testGetPathFilesListReturned(xenServer625Resource);
  }

  @Test
  public void testisDeviceUsedTrue() throws Types.XenAPIException, XmlRpcException {
    final Connection conn = mock(Connection.class);
    final VM vm = mock(VM.class);
    final VBD vbd = mock(VBD.class);

    final Set<VBD> vbds = new HashSet< >();
    vbds.add(vbd);

    final String xen_userDevice = "3";
    final Long deviceId = 3L;

    when(vm.getVBDs(conn)).thenReturn(vbds);

    when(vbd.getUserdevice(conn)).thenReturn(xen_userDevice);

    assertTrue(xenServer625Resource.isDeviceUsed(conn, vm, deviceId));
  }

  @Test
  public void testisDeviceUsedFalse() throws Types.XenAPIException, XmlRpcException {
    final Connection conn = mock(Connection.class);
    final VM vm = mock(VM.class);
    final VBD vbd = mock(VBD.class);

    final Set<VBD> vbds = new HashSet< >();
    vbds.add(vbd);

    final String xen_userDevice = "3";
    final Long deviceId = 4L;

    when(vm.getVBDs(conn)).thenReturn(vbds);

    when(vbd.getUserdevice(conn)).thenReturn(xen_userDevice);

    assertFalse(xenServer625Resource.isDeviceUsed(conn, vm, deviceId));
  }
}
@@ -831,16 +831,99 @@
 o.mock;%0A
+import static org.mockito.Mockito.times;%0Aimport static org.mockito.Mockito.verify;%0A
 import s
@@ -2410,32 +2410,98 @@
 ck(VBD.class);%0A%0A
+    final Set%3CString%3E allowedVBDDevices = new HashSet%3CString%3E();%0A%0A
     final Set%3CVB
@@ -2552,47 +2552,8 @@
 );%0A%0A
-    final String xen_userDevice = %223%22;%0A
     
@@ -2574,17 +2574,17 @@
 iceId = 
-3
+4
 L;%0A%0A    
@@ -2598,65 +2598,25 @@
 .get
-VBDs(conn)).thenReturn(vbds);%0A%0A    when(vbd.getUserd
+AllowedVBDD
 evice
+s
 (con
@@ -2626,38 +2626,41 @@
 .thenReturn(
-xen_user
+allowedVBD
 Device
+s
 );%0A%0A    asse
@@ -2717,24 +2717,78 @@
  deviceId));
+%0A%0A    verify(vm, times(1)).getAllowedVBDDevices(conn);
 %0A  %7D%0A%0A  @Tes
@@ -3009,32 +3009,48 @@
   final Set%3C
-VBD%3E vbd
+String%3E allowedVBDDevice
 s = new Hash
@@ -3045,33 +3045,38 @@
 s = new HashSet%3C
- 
+String
 %3E();%0A    vbds.ad
@@ -3064,36 +3064,49 @@
 ing%3E();%0A    
-vbd
+allowedVBDDevice
 s.add(
-vbd
+%224%22
 );%0A%0A    fina
@@ -3112,139 +3112,124 @@
 al S
-tring xen_userDevice = %223%22;%0A    final Long deviceId = 4L;%0A%0A    when(vm.getVBDs(conn)).thenReturn(vbds);%0A%0A    when(vbd.getUserd
+et%3CVBD%3E vbds = new HashSet%3C %3E();%0A    vbds.add(vbd);%0A%0A    final Long deviceId = 4L;%0A%0A    when(vm.getAllowedVBDD
 evice
+s
 (con
@@ -3247,22 +3247,25 @@
 urn(
-xen_user
+allowedVBD
 Device
+s
 );%0A%0A
@@ -3335,14 +3335,68 @@
 iceId));
+%0A%0A    verify(vm, times(1)).getAllowedVBDDevices(conn);
 %0A  %7D%0A%7D

Make tests cover the new code.