ServerPortRegistry { public static void addPort( ServerPort serverPort ) { if ( serverPorts.get( serverPort.getId() ) != null && serverPorts.get( serverPort.getId() ).getAssignedPort() != null ) { throw new IllegalStateException( "Another port has already been assigned with this ID." ); } serverPorts.put( serverPort.getId(), serverPort ); String serviceName = serverPort.getServiceName(); Service service = services.get( serviceName ); if ( service == null ) { logger.warn( "Server Port added with no service. Adding service without description" ); service = new Service( serverPort.getServiceName(), "Unknown Description" ); services.put( serviceName, service ); } Set<ServerPort> servicePorts = service.getServerPorts(); servicePorts.add( serverPort ); } private  ServerPortRegistry(); static void addPort( ServerPort serverPort ); static void addService( Service service ); static void removePort( ServerPort serverPort ); static void clear(); static Service getService( String serviceName ); static ServerPort getPort( String portId ); static Set<Service> getServices(); static Set<ServerPort> getPorts();  }
@Test public void addPortTest() { ServerPortRegistry.clear(); Service service = new Service( "foo", "foo description" ); ServerPortRegistry.addService( service ); ServerPort serverPort1 = new ServerPort( "id1", "description", 60001, "foo" ); ServerPort serverPort2 = new ServerPort( "id2", "description", 60101, "bla" ); ServerPort serverPort3 = new ServerPort( "id3", "description", 60201 ); ServerPortRegistry.addPort( serverPort1 ); ServerPortRegistry.addPort( serverPort2 ); ServerPortRegistry.addPort( serverPort3 ); assertEquals( serverPort1, ServerPortRegistry.getPort( "id1" ) ); assertEquals( serverPort2, ServerPortRegistry.getPort( "id2" ) ); assertEquals( serverPort3, ServerPortRegistry.getPort( "id3" ) ); assertEquals( "Unknown Description", ServerPortRegistry.getService( ServerPortRegistry.getPort( "id2" ).getServiceName() ).getServiceDescription() ); assertEquals( "", ServerPortRegistry.getService( ServerPortRegistry.getPort( "id3" ).getServiceName() ) .getServiceName() ); assertEquals( "Unknown Description", ServerPortRegistry.getService( ServerPortRegistry.getPort( "id3" ).getServiceName() ).getServiceDescription() ); }