@SuppressWarnings({"rawtypes", "unchecked"}) private void mockApiClient(boolean isSuccess) throws Throwable { ApiClient apiClient = mock(ApiClient.class); DeploymentApi deploymentApi = mock(DeploymentApi.class); VmApi vmApi = mock(VmApi.class); TasksApi tasksApi = mock(TasksApi.class); Deployment deployment = new Deployment(); deployment.setId("deploymentId1"); deployment.setAuth(new AuthInfo()); final ResourceList<Deployment> deploymentResourceList = new ResourceList<>(Arrays.asList(deployment)); Vm vm1 = new Vm(); vm1.setId("vm1"); Map<String, String> metadata = new HashMap<String, String>(); metadata.put("key1", ContainersConfig.ContainerType.Zookeeper.name()); vm1.setMetadata(metadata); final ResourceList<Vm> vmList = new ResourceList<>(Arrays.asList(vm1)); NetworkConnection networkConnection = new NetworkConnection(); networkConnection.setNetwork("VM VLAN"); networkConnection.setIpAddress("127.0.0.1"); VmNetworks vmNetworks = new VmNetworks(); vmNetworks.setNetworkConnections(Collections.singleton(networkConnection)); final Task getNetworksTaskResult = new Task(); getNetworksTaskResult.setId("taskId"); getNetworksTaskResult.setState("COMPLETED"); getNetworksTaskResult.setResourceProperties(vmNetworks); final Task taskReturnedByPauseSystem = TestHelper.createCompletedApifeTask("PAUSE_SYSTEM"); if (isSuccess) { // List all deployments doAnswer( new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { ((FutureCallback<ResourceList<Deployment>>) invocation.getArguments()[0]) .onSuccess(deploymentResourceList); return null; } }) .when(deploymentApi) .listAllAsync(any(FutureCallback.class)); // Pause system doAnswer( new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { ((FutureCallback<Task>) invocation.getArguments()[1]) .onSuccess(taskReturnedByPauseSystem); return null; } }) .when(deploymentApi) .pauseSystemAsync(any(String.class), any(FutureCallback.class)); // List all vms doAnswer( new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { ((FutureCallback<ResourceList<Vm>>) invocation.getArguments()[1]) .onSuccess(vmList); return null; } }) .when(deploymentApi) .getAllDeploymentVmsAsync(anyString(), any(FutureCallback.class)); // Get vm networks doAnswer( new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { ((FutureCallback<Task>) invocation.getArguments()[1]) .onSuccess(getNetworksTaskResult); return null; } }) .when(vmApi) .getNetworksAsync(any(String.class), any(FutureCallback.class)); } else { doAnswer( new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { ((FutureCallback<ResourceList<Deployment>>) invocation.getArguments()[0]) .onFailure(new Exception("failed!")); return null; } }) .when(deploymentApi) .listAllAsync(any(FutureCallback.class)); } doReturn(deploymentApi).when(apiClient).getDeploymentApi(); doReturn(vmApi).when(apiClient).getVmApi(); doReturn(tasksApi).when(apiClient).getTasksApi(); doReturn(apiClient).when(apiClientFactory).create(); doReturn(apiClient).when(apiClientFactory).create(any(String.class)); }