@Test(expected = InternalException.class)
 public void lauchShouldThrowExceptionIfVlanIsNotExist() throws CloudException, InternalException {
   VMLaunchOptions vmLaunchOptions =
       VMLaunchOptions.getInstance(HWP_1_ID, VHD_1_ID, VM_1_NAME, VM_1_DESCRIPTION);
   vmLaunchOptions.inVlan(null, DATACENTER_ID, "not-exist-vlan-id");
   azurePackVirtualMachineSupport.launch(vmLaunchOptions);
 }
  @Test
  public void terminateShouldSendCorrectRequest() throws CloudException, InternalException {
    final AtomicInteger deleteCount = new AtomicInteger(0);
    new GetOrListVirtualMachinesRequestExecutorMockUp() {
      @Mock
      public void $init(
          CloudProvider provider,
          HttpClientBuilder clientBuilder,
          HttpUriRequest request,
          ResponseHandler handler) {
        String requestUri = request.getURI().toString();
        if (request.getMethod().equals("DELETE")
            && requestUri.equals(
                String.format(VM_RESOURCES, ENDPOINT, ACCOUNT_NO, DATACENTER_ID, VM_1_ID))) {
          requestResourceType = 11;
        } else {
          super.$init(provider, clientBuilder, request, handler);
        }
      }

      @Mock
      public Object execute() {
        if (requestResourceType == 11) {
          deleteCount.incrementAndGet();
          return "";
        } else {
          return super.execute();
        }
      }
    };
    azurePackVirtualMachineSupport.terminate(VM_1_ID, "no reason");
    assertEquals("terminate doesn't send DELETE request", 1, deleteCount.get());
  }
 @Test(expected = InternalException.class)
 public void lauchShouldThrowExceptionIfImageIsNotExist()
     throws CloudException, InternalException {
   VMLaunchOptions vmLaunchOptions =
       VMLaunchOptions.getInstance(HWP_1_ID, "not-exist-image-id", VM_1_NAME, VM_1_DESCRIPTION);
   azurePackVirtualMachineSupport.launch(vmLaunchOptions);
 }
 @Test
 public void listProductsShouldReturnDefaultTemplateProduct()
     throws CloudException, InternalException {
   new ListProductsRequestExecutorMockUp();
   List<VirtualMachineProduct> products =
       IteratorUtils.toList(
           azurePackVirtualMachineSupport
               .listProducts(TPL_1_ID, VirtualMachineProductFilterOptions.getInstance())
               .iterator());
   assertEquals("listProducts doesn't return correct result", 1, products.size());
   VirtualMachineProduct virtualMachineProduct = products.get(0);
   assertEquals(
       "listProducts doesn't return correct result", "Default", virtualMachineProduct.getName());
   assertEquals(
       "listProducts doesn't return correct result",
       "default",
       virtualMachineProduct.getProviderProductId());
   assertEquals(
       "listProducts doesn't return correct result",
       Integer.parseInt(TPL_CPU_COUNT),
       virtualMachineProduct.getCpuCount());
   assertEquals(
       "listProducts doesn't return correct result",
       Double.parseDouble(TPL_MEMORY),
       virtualMachineProduct.getRamSize().getQuantity());
   assertEquals(
       "listProducts doesn't return correct result",
       Storage.MEGABYTE,
       virtualMachineProduct.getRamSize().getUnitOfMeasure());
 }
 @Test
 public void stopShouldSendCorrectRequest() throws CloudException, InternalException {
   StartOrStopVirtualMachinesRequestExecutorMockUp mockUp =
       new StartOrStopVirtualMachinesRequestExecutorMockUp("Shutdown");
   azurePackVirtualMachineSupport.stop(VM_1_ID, false);
   assertEquals("stop doesn't send PUT request", 1, mockUp.putCount);
 }
 @Test(expected = InternalException.class)
 public void listProductsShouldThrowExceptionIfImageIsNotExist()
     throws CloudException, InternalException {
   new ListProductsRequestExecutorMockUp();
   azurePackVirtualMachineSupport.listProducts(
       "not-exist-image-id", VirtualMachineProductFilterOptions.getInstance());
 }
  @Test
  public void lauchTemplateVMShouldSendCorrectRequest() throws CloudException, InternalException {
    final AtomicInteger postCount = new AtomicInteger(0);
    new StartOrStopVirtualMachinesRequestExecutorMockUp("Start") {
      @Mock
      public void $init(
          CloudProvider provider,
          HttpClientBuilder clientBuilder,
          HttpUriRequest request,
          ResponseHandler handler) {
        String requestUri = request.getURI().toString();
        if (request.getMethod().equals("POST")
            && requestUri.equals(String.format(LIST_VM_RESOURCES, ENDPOINT, ACCOUNT_NO))) {
          requestResourceType = 21;
          WAPVirtualMachineModel wapVirtualMachineModel = new WAPVirtualMachineModel();
          wapVirtualMachineModel.setName(VM_1_NAME);
          wapVirtualMachineModel.setCloudId(REGION);
          wapVirtualMachineModel.setStampId(DATACENTER_ID);
          wapVirtualMachineModel.setVmTemplateId(TPL_1_ID);
          wapVirtualMachineModel.setProductKey(VM_1_WINDOWS_SERIAL_NUMBER);
          wapVirtualMachineModel.setLocalAdminUserName(VM_1_BOOTSTRAP_USER);
          wapVirtualMachineModel.setLocalAdminPassword(VM_1_BOOTSTRAP_PASSWORD);
          List<WAPNewAdapterModel> adapters = new ArrayList<>();
          WAPNewAdapterModel newAdapterModel = new WAPNewAdapterModel();
          newAdapterModel.setVmNetworkName(VM_1_NETWORK_NAME);
          adapters.add(newAdapterModel);
          wapVirtualMachineModel.setNewVirtualNetworkAdapterInput(adapters);

          assertPost(
              request,
              String.format(LIST_VM_RESOURCES, ENDPOINT, ACCOUNT_NO),
              new Header[0],
              wapVirtualMachineModel);
        } else {
          super.$init(provider, clientBuilder, request, handler);
        }
        responseHandler = handler;
      }

      @Mock
      public Object execute() {
        if (requestResourceType == 21) {
          postCount.incrementAndGet();
          return mapFromModel(this.responseHandler, createWAPVirtualMachineModel());
        } else {
          return super.execute();
        }
      }
    };

    VMLaunchOptions vmLaunchOptions =
        VMLaunchOptions.getInstance(HWP_1_ID, TPL_1_ID, VM_1_NAME, VM_1_DESCRIPTION);
    vmLaunchOptions.inVlan(null, DATACENTER_ID, VM_1_NETWORK_ID);
    vmLaunchOptions.withWinProductSerialNum(VM_1_WINDOWS_SERIAL_NUMBER);
    vmLaunchOptions.withBootstrapUser("dummy-user-name-to-be-replaced", VM_1_BOOTSTRAP_PASSWORD);
    VirtualMachine virtualMachine = azurePackVirtualMachineSupport.launch(vmLaunchOptions);
    assertEquals("terminate doesn't send DELETE request", 1, postCount.get());
    assertVirtualMachine(virtualMachine);
  }
  @Test
  public void getVirtualMachineShouldReturnCorrectResult()
      throws CloudException, InternalException {
    new GetOrListVirtualMachinesRequestExecutorMockUp();

    VirtualMachine virtualMachine = azurePackVirtualMachineSupport.getVirtualMachine(VM_1_ID);
    assertVirtualMachine(virtualMachine);
  }
 @Test
 public void listVirtualMachinesShouldReturnCorrectResult()
     throws CloudException, InternalException {
   new GetOrListVirtualMachinesRequestExecutorMockUp();
   List<VirtualMachine> virtualMachines =
       IteratorUtils.toList(azurePackVirtualMachineSupport.listVirtualMachines().iterator());
   assertEquals("listVirtualMachines doesn't return correct result", 1, virtualMachines.size());
   assertVirtualMachine(virtualMachines.get(0));
 }
 @Test
 public void listProductsShouldReturnCorrectHardwareProfileProducts()
     throws CloudException, InternalException {
   new ListProductsRequestExecutorMockUp();
   List<VirtualMachineProduct> products =
       IteratorUtils.toList(
           azurePackVirtualMachineSupport
               .listProducts(VHD_1_ID, VirtualMachineProductFilterOptions.getInstance())
               .iterator());
   assertHardwareProfileProducts(products);
 }
  @Test(expected = InternalException.class)
  public void lauchShouldThrowExceptionIfLaunchFromVHDWithDefaultProduct()
      throws CloudException, InternalException {
    final AtomicInteger postCount = new AtomicInteger(0);
    new StartOrStopVirtualMachinesRequestExecutorMockUp("Start") {
      @Mock
      public void $init(
          CloudProvider provider,
          HttpClientBuilder clientBuilder,
          HttpUriRequest request,
          ResponseHandler handler) {
        String requestUri = request.getURI().toString();
        if (request.getMethod().equals("POST")
            && requestUri.equals(String.format(LIST_VM_RESOURCES, ENDPOINT, ACCOUNT_NO))) {
          requestResourceType = 21;
          WAPVirtualMachineModel wapVirtualMachineModel = new WAPVirtualMachineModel();
          wapVirtualMachineModel.setName(VM_1_NAME);
          wapVirtualMachineModel.setCloudId(REGION);
          wapVirtualMachineModel.setStampId(DATACENTER_ID);
          wapVirtualMachineModel.setVirtualHardDiskId(VHD_1_ID);
          wapVirtualMachineModel.setHardwareProfileId(HWP_1_ID);

          List<WAPNewAdapterModel> adapters = new ArrayList<>();
          WAPNewAdapterModel newAdapterModel = new WAPNewAdapterModel();
          newAdapterModel.setVmNetworkName(VM_1_NETWORK_NAME);
          adapters.add(newAdapterModel);
          wapVirtualMachineModel.setNewVirtualNetworkAdapterInput(adapters);
        } else {
          super.$init(provider, clientBuilder, request, handler);
        }
        responseHandler = handler;
      }

      @Mock
      public Object execute() {
        if (requestResourceType == 21) {
          postCount.incrementAndGet();
          return mapFromModel(this.responseHandler, createWAPVirtualMachineModel());
        } else {
          return super.execute();
        }
      }
    };

    VMLaunchOptions vmLaunchOptions =
        VMLaunchOptions.getInstance("default", VHD_1_ID, VM_1_NAME, VM_1_DESCRIPTION);
    vmLaunchOptions.inVlan(null, DATACENTER_ID, VM_1_NETWORK_ID);
    azurePackVirtualMachineSupport.launch(vmLaunchOptions);
  }
  @Test
  public void listAllProductsShouldReturnCorrectHardwareProfileProducts()
      throws CloudException, InternalException {
    new ListProductsRequestExecutorMockUp();
    List<VirtualMachineProduct> products =
        IteratorUtils.toList(azurePackVirtualMachineSupport.listAllProducts().iterator());

    // listAllProducts returns all available hardware profile plus default product
    assertEquals("listProducts doesn't return correct result", 2, products.size());
    VirtualMachineProduct virtualMachineProduct = products.get(0);
    assertEquals(
        "listProducts doesn't return correct result", HWP_1_NAME, virtualMachineProduct.getName());
    assertEquals(
        "listProducts doesn't return correct result",
        HWP_1_ID,
        virtualMachineProduct.getProviderProductId());
    assertEquals(
        "listProducts doesn't return correct result",
        Integer.parseInt(HWP_1_CPU_COUNT),
        virtualMachineProduct.getCpuCount());
    assertEquals(
        "listProducts doesn't return correct result",
        Double.parseDouble(HWP_1_MEMORY),
        virtualMachineProduct.getRamSize().getQuantity());
    assertEquals(
        "listProducts doesn't return correct result",
        Storage.MEGABYTE,
        virtualMachineProduct.getRamSize().getUnitOfMeasure());

    // assert default product
    virtualMachineProduct = products.get(1);
    assertEquals(
        "listProducts doesn't return correct result",
        "default",
        virtualMachineProduct.getName().toLowerCase());
    assertEquals(
        "listProducts doesn't return correct result",
        "default",
        virtualMachineProduct.getProviderProductId().toLowerCase());
    assertEquals(
        "listProducts doesn't return correct result",
        "default",
        virtualMachineProduct.getDescription().toLowerCase());
  }
 @Test(expected = InternalException.class)
 public void getVirtualMachineShouldThrowExceptionIfVmIdIsNull()
     throws CloudException, InternalException {
   azurePackVirtualMachineSupport.getVirtualMachine(null);
 }
 @Test(expected = InternalException.class)
 public void listProductsShouldThrowExceptionIfFilterOptionIsNull()
     throws CloudException, InternalException {
   new ListProductsRequestExecutorMockUp();
   azurePackVirtualMachineSupport.listProducts(TPL_1_ID, null);
 }