Services { public static Optional<Service> findServiceWithPort(Collection<Service> services, int port) { if (services == null) { return Optional.empty(); } return services.stream().filter(s -> Services.findPort(s, port).isPresent()).findFirst(); }  static Optional<ServicePort> findPort(Service service, int port); static Optional<Service> findServiceWithPort(Collection<Service> services, int port);  }
@Test(dataProvider = "nullServices") public void testFindServiceWithPortShouldReturnEmptyWhenSomethingIsNull( Collection<Service> services) { assertEquals(Services.findServiceWithPort(services, 1), Optional.empty()); }
@Test public void testFindServiceWhenExists() { final int PORT = 1234; Service service = new Service(); ServiceSpec spec = new ServiceSpec(); ServicePort port = new ServicePort(); port.setPort(PORT); spec.setPorts(Collections.singletonList(port)); service.setSpec(spec); assertEquals( Services.findServiceWithPort(Arrays.asList(service, new Service()), PORT).get(), service); }