### Question:
GatewayTopologyV4RequestValidator implements Validator<GatewayTopologyV4Request> { @Override public ValidationResult validate(GatewayTopologyV4Request subject) { ValidationResult topologyNameResult = ValidationResult.builder() .ifError(() -> StringUtils.isBlank(subject.getTopologyName()), "topologyName must be set in gateway topology.") .build(); return Optional.ofNullable(subject.getExposedServices()) .map(exposedServiceListValidator::validate) .map(vr -> vr.merge(topologyNameResult)) .orElse(topologyNameResult); } GatewayTopologyV4RequestValidator(ExposedServiceListValidator exposedServiceListValidator); @Override ValidationResult validate(GatewayTopologyV4Request subject);  }### Answer:
@Test public void testWithNoTopologyName() { GatewayTopologyV4Request gatewayTopologyJson = new GatewayTopologyV4Request(); ValidationResult result = underTest.validate(gatewayTopologyJson); assertEquals(1L, result.getErrors().size()); assertTrue(result.getErrors().get(0).contains("topologyName must be set in gateway topology.")); }
@Test public void testWithTopologyNameButNoServices() { GatewayTopologyV4Request gatewayTopologyJson = new GatewayTopologyV4Request(); gatewayTopologyJson.setTopologyName("topology"); ValidationResult result = underTest.validate(gatewayTopologyJson); assertEquals(State.VALID, result.getState()); }
@Test public void testWithKnoxServiceButNoTopologyName() { GatewayTopologyV4Request gatewayTopologyJson = new GatewayTopologyV4Request(); gatewayTopologyJson.setExposedServices(Collections.singletonList(exposedService("CLOUDERA_MANAGER_UI").getKnoxService())); ValidationResult result = underTest.validate(gatewayTopologyJson); assertEquals(1L, result.getErrors().size()); assertTrue(result.getErrors().get(0).contains("topologyName must be set in gateway topology.")); }
@Test public void testWithKnoxServiceAndTopologyName() { GatewayTopologyV4Request gatewayTopologyJson = new GatewayTopologyV4Request(); gatewayTopologyJson.setTopologyName("topology"); gatewayTopologyJson.setExposedServices(Collections.singletonList(exposedService("CLOUDERA_MANAGER_UI").getKnoxService())); ValidationResult result = underTest.validate(gatewayTopologyJson); assertEquals(State.VALID, result.getState()); }
@Test public void testWithInvalidKnoxService() { String invalidService = "INVALID_SERVICE"; GatewayTopologyV4Request gatewayTopologyJson = new GatewayTopologyV4Request(); gatewayTopologyJson.setTopologyName("topology"); gatewayTopologyJson.setExposedServices(Collections.singletonList(invalidService)); when(exposedServiceListValidator.validate(anyList())).thenReturn(new ValidationResultBuilder().error(invalidService).build()); ValidationResult result = underTest.validate(gatewayTopologyJson); assertEquals(State.ERROR, result.getState()); assertEquals(1L, result.getErrors().size()); assertTrue(result.getErrors().get(0).contains(invalidService)); }
@Test public void testWithInvalidKnoxServiceAndNoTopologyName() { String invalidService = "INVALID_SERVICE"; GatewayTopologyV4Request gatewayTopologyJson = new GatewayTopologyV4Request(); gatewayTopologyJson.setExposedServices(Collections.singletonList(invalidService)); when(exposedServiceListValidator.validate(anyList())).thenReturn(new ValidationResultBuilder().error(invalidService).build()); ValidationResult result = underTest.validate(gatewayTopologyJson); assertEquals(State.ERROR, result.getState()); assertEquals(2L, result.getErrors().size()); assertTrue(result.getErrors().get(0).contains(invalidService)); assertTrue(result.getErrors().get(1).contains("topologyName must be set in gateway topology.")); }