@Test
 public void shouldUnderstandFilteringAgentListBasedOnUuid() {
   AgentInstance instance1 =
       AgentInstance.createFromLiveAgent(
           AgentRuntimeInfo.fromServer(
               new AgentConfig("uuid-1", "host-1", "192.168.1.2"),
               true,
               "/foo/bar",
               100l,
               "linux"),
           new SystemEnvironment());
   AgentInstance instance3 =
       AgentInstance.createFromLiveAgent(
           AgentRuntimeInfo.fromServer(
               new AgentConfig("uuid-3", "host-3", "192.168.1.4"),
               true,
               "/baz/quux",
               300l,
               "linux"),
           new SystemEnvironment());
   when(agentInstances.filter(Arrays.asList("uuid-1", "uuid-3")))
       .thenReturn(Arrays.asList(instance1, instance3));
   AgentsViewModel agents = agentService.filter(Arrays.asList("uuid-1", "uuid-3"));
   AgentViewModel view1 = new AgentViewModel(instance1);
   AgentViewModel view2 = new AgentViewModel(instance3);
   assertThat(agents, is(new AgentsViewModel(view1, view2)));
   verify(agentInstances).filter(Arrays.asList("uuid-1", "uuid-3"));
 }
 @Before
 public void setUp() {
   agentInstances = mock(AgentInstances.class);
   config = new AgentConfig("uuid", "host", "192.168.1.1");
   when(agentInstances.findAgentAndRefreshStatus("uuid"))
       .thenReturn(AgentInstance.createFromConfig(config, new SystemEnvironment()));
   agentDao = mock(AgentDao.class);
   uuidGenerator = mock(UuidGenerator.class);
   agentService =
       new AgentService(
           mock(AgentConfigService.class),
           new SystemEnvironment(),
           agentInstances,
           mock(EnvironmentConfigService.class),
           mock(GoConfigService.class),
           mock(SecurityService.class),
           agentDao,
           uuidGenerator,
           serverHealthService = mock(ServerHealthService.class));
   logFixture = LogFixture.startListening();
   agentIdentifier = config.getAgentIdentifier();
   when(agentDao.cookieFor(agentIdentifier)).thenReturn("cookie");
 }
  @Test
  public void shouldFailWhenDeleteIsNotSuccessful() throws Exception {
    SecurityService securityService = mock(SecurityService.class);
    AgentConfigService agentConfigService = mock(AgentConfigService.class);
    AgentInstance agentInstance = mock(AgentInstance.class);
    String uuid = "1234";
    Username username = new Username(new CaseInsensitiveString("test"));
    HttpOperationResult operationResult = mock(HttpOperationResult.class);

    when(securityService.hasOperatePermissionForAgents(username)).thenReturn(true);

    when(agentInstance.isNullAgent()).thenReturn(false);
    when(agentInstance.isDisabled()).thenReturn(true);
    when(agentInstance.isBuilding()).thenReturn(false);
    when(agentInstance.isCancelled()).thenReturn(false);

    doThrow(new RuntimeException()).when(agentConfigService).deleteAgents(agentInstance);

    when(agentInstances.findAgent(uuid)).thenReturn(agentInstance);

    AgentService agentService =
        new AgentService(
            agentConfigService,
            new SystemEnvironment(),
            agentInstances,
            mock(EnvironmentConfigService.class),
            mock(GoConfigService.class),
            securityService,
            agentDao,
            uuidGenerator,
            serverHealthService = mock(ServerHealthService.class));

    agentService.deleteAgents(username, operationResult, Arrays.asList(uuid));

    verify(operationResult).internalServerError(any(String.class), any(HealthStateType.class));
  }