ProxyValidator implements Validator { @Override public void validate(Api definition) { Proxy proxyDefinition = definition.getProxy(); if (proxyDefinition == null) { throw new ValidationException("An API must have a proxy part"); } if (proxyDefinition.getVirtualHosts() == null || proxyDefinition.getVirtualHosts().isEmpty()) { throw new ValidationException("An API must contain at least of virtual_host"); } for(VirtualHost host : proxyDefinition.getVirtualHosts()) { if (host.getPath() == null || host.getPath().matches(CONTEXT_PATH_PATTERN)) { throw new ValidationException("An API must have a valid context path"); } if (! host.getPath().startsWith("/")) { throw new ValidationException("An API must have a valid context-path starting with '/'"); } } }  @Override void validate(Api definition);  }
@Test(expected = ValidationException.class) public void validate_proxy_badContextPath() { Proxy proxyDefinition = new Proxy(); proxyDefinition.setVirtualHosts(Collections.singletonList(new VirtualHost("context-path"))); Api definition = new Api(); definition.setProxy(proxyDefinition); new ProxyValidator().validate(definition); }
@Test public void validate_proxy_correctContextPathAndTarget() { Proxy proxyDefinition = new Proxy(); proxyDefinition.setVirtualHosts(Collections.singletonList(new VirtualHost("/context-path"))); proxyDefinition.setGroups(Collections.singleton(new EndpointGroup())); proxyDefinition.getGroups().iterator().next().setEndpoints(Collections.singleton(new HttpEndpoint("name", "http: Api definition = new Api(); definition.setProxy(proxyDefinition); new ProxyValidator().validate(definition); }