InventoryUtil { public static boolean removeServer( final String inventoryFilePath, final String hostId ) throws HmsException, JsonGenerationException, JsonMappingException, IOException { Map<String, Object[]> inventory = InventoryUtil.initializeInventory( inventoryFilePath ); if ( inventory == null || !inventory.containsKey( Constants.HOSTS ) ) { logger.error( "In removeServer, invalid inventory configuration in the file {}.", inventoryFilePath ); return false; } ObjectMapper objectMapper = new ObjectMapper(); Object[] hosts = inventory.get( Constants.HOSTS ); if ( hosts != null && hosts.length > 0 ) { List<ServerNode> serverNodesList = objectMapper.convertValue( hosts, new TypeReference<List<ServerNode>>() { } ); ServerNode serverNode = null; for ( Iterator<ServerNode> iterator = serverNodesList.iterator(); iterator.hasNext(); ) { serverNode = iterator.next(); if ( StringUtils.equals( hostId, serverNode.getNodeID() ) ) { iterator.remove(); logger.debug( "In removeServer, removed hostId '{}' from the inventory '{}'.", hostId, inventoryFilePath ); break; } } inventory.put( Constants.HOSTS, serverNodesList.toArray() ); } String inventoryFileContent = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString( inventory ); return InventoryUtil.createOrUpdateInventoryFile( inventoryFilePath, inventoryFileContent, true ); }  static boolean initializeInventory( String inventoryFilePath, String path ); static Map<String, Object[]> initializeInventory( String inventoryFilePath ); static ResponseEntity<HashMap<String, Object[]>> getInventoryOOB( String path, String contentType ); static void putInventoryOOB( String path, String contentType, HmsInventoryConfiguration conf ); static HashMap<String, List<HmsApi>> getOOBSupportedOperations( String path, String contentType ); @SuppressWarnings( "unchecked" ) static Map<String, Object> getUpdatedServerInfos( Object[] hostsToBeUpdated,
                                                             List<ServerInfo> requestServerInfoList ); @SuppressWarnings( "unchecked" ) static List<String> getInvalidHostsinRequest( Object[] inventoryHosts,
                                                         List<ServerInfo> requestServerInfoList ); static boolean createOrUpdateInventoryFile( final String inventoryFilePath,
                                                       final String inventoryFileContent ); static boolean createOrUpdateInventoryFile( final String inventoryFilePath,
                                                       final String inventoryFileContent, final boolean createBackup ); static boolean removeServer( final String inventoryFilePath, final String hostId ); static boolean updateInventoryNodeMap( final String inventoryFileAbsPath ); static boolean isOobAgentInventoryLoaded(); static boolean isInventoryLoaded(); static boolean refreshInventoryOnOutOfBand(); static final String NODE_ID; static final String IB_IP_ADDRESS; static final String USERNAME; static final String PASSWORD; }
@Test public void testRemoveServer() throws HmsException, JsonGenerationException, JsonMappingException, IOException { Map<String, Object[]> inventory = new HashMap<String, Object[]>(); String nodeId = "N" + TestUtil.getDateTimeStamp(); ServerNode serverNode = TestUtil.getServerNode( nodeId ); List<ServerNode> serverNodes = new ArrayList<ServerNode>(); serverNodes.add( serverNode ); String nodeId2 = "N2" + TestUtil.getDateTimeStamp(); ServerNode serverNode2 = TestUtil.getServerNode( nodeId2 ); serverNodes.add( serverNode2 ); inventory.put( Constants.HOSTS, serverNodes.toArray( new ServerNode[serverNodes.size()] ) ); String tempDir = TestUtil.getTemporaryDirectory(); String invFileName = FilenameUtils.concat( tempDir, TestUtil.getDateTimeStamp() + ".json" ); String invContent = TestUtil.getValueAsString( inventory ); boolean createdOrUpdated = InventoryUtil.createOrUpdateInventoryFile( invFileName, invContent ); assertTrue( createdOrUpdated ); boolean removed = InventoryUtil.removeServer( invFileName, nodeId ); assertTrue( removed ); Map<String, Object[]> invConfig = InventoryUtil.initializeInventory( invFileName ); assertNotNull( invConfig ); assertTrue( invConfig.containsKey( Constants.HOSTS ) ); Object[] hosts = invConfig.get( Constants.HOSTS ); assertNotNull( hosts ); assertTrue( hosts.length == 1 ); ObjectMapper objectMapper = new ObjectMapper(); ServerNode[] serverNodes1 = objectMapper.convertValue( hosts, new TypeReference<ServerNode[]>() { } ); assertNotNull( serverNodes1 ); assertTrue( serverNodes1.length == 1 ); ServerNode serverNode3 = serverNodes1[0]; assertTrue( StringUtils.equals( nodeId2, serverNode3.getNodeID() ) ); assertFalse( StringUtils.equals( nodeId, serverNode3.getNodeID() ) ); assertTrue( FileUtil.deleteDirectory( tempDir ) ); }