/** * Delete enabled when Agent is not connected. */ @Test public void testAgentDelete() throws BusinessException { long platformId = 10L; PlatformIdent platformIdent = new PlatformIdent(); platformIdent.setId(platformId); when(platformIdentDao.load(Long.valueOf(platformId))).thenReturn(platformIdent); Map<Long, AgentStatusData> map = new HashMap<>(1); AgentStatusData agentStatusData = new AgentStatusData(AgentConnection.DISCONNECTED); map.put(platformId, agentStatusData); when(agentStatusProvider.getAgentStatusDataMap()).thenReturn(map); globalDataAccessService.deleteAgent(platformId); verify(platformIdentDao, times(1)).delete(platformIdent); verify(defaultDataDao, times(1)).deleteAll(platformId); ArgumentCaptor<ApplicationEvent> captor = ArgumentCaptor.forClass(ApplicationEvent.class); verify(eventPublisher, times(1)).publishEvent(captor.capture()); AgentDeletedEvent event = (AgentDeletedEvent) captor.getValue(); assertThat(event.getPlatformId(), is(platformId)); } }
/** * * {@inheritDoc} */ @Override @MethodLog public void deleteAgent(long platformId) throws BusinessException { PlatformIdent platformIdent = platformIdentDao.load(platformId); if (null != platformIdent) { AgentStatusData agentStatusData = agentStatusProvider.getAgentStatusDataMap().get(platformIdent.getId()); // delete is allowed only if agent is disconnected or was never connected if ((null != agentStatusData) && (agentStatusData.getAgentConnection() == AgentConnection.CONNECTED)) { throw new BusinessException("Delete the agent '" + platformIdent.getAgentName() + "'.", AgentManagementErrorCodeEnum.AGENT_CAN_NOT_BE_DELETED); } platformIdentDao.delete(platformIdent); defaultDataDao.deleteAll(platformIdent.getId()); AgentDeletedEvent event = new AgentDeletedEvent(this, platformIdent.getId()); eventPublisher.publishEvent(event); log.info("The Agent '" + platformIdent.getAgentName() + "' with the ID " + platformIdent.getId() + " was successfully deleted from the CMR."); } else { throw new BusinessException("Delete the agent with the ID=" + platformId + ".", AgentManagementErrorCodeEnum.AGENT_DOES_NOT_EXIST); } }