Services { public static Optional<ServicePort> findPort(Service service, int port) { if (service == null || service.getSpec() == null || service.getSpec().getPorts() == null) { return Optional.empty(); } return service .getSpec() .getPorts() .stream() .filter(p -> p.getPort() != null && p.getPort() == port) .findFirst(); }  static Optional<ServicePort> findPort(Service service, int port); static Optional<Service> findServiceWithPort(Collection<Service> services, int port);  }
@Test(dataProvider = "nullService") public void testFindPortShouldReturnEmptyWhenSomethingIsNull(Service service) { assertEquals(Services.findPort(service, 1), Optional.empty()); }
@Test public void testFindPortWhenExists() { final int PORT = 1234; Service service = new Service(); ServiceSpec spec = new ServiceSpec(); ServicePort port = new ServicePort(); port.setPort(PORT); spec.setPorts(Arrays.asList(port, new ServicePort())); service.setSpec(spec); assertEquals(Services.findPort(service, PORT).get(), port); }