@Test public void testPathParamValidatorMissingParams() { EndpointDefinition.Builder definition = EndpointDefinition.builder() .endpointName(ENDPOINT_NAME) .httpMethod(HttpMethod.GET) .httpPath(HttpPath.of("/a/path/{paramName}")); assertThatThrownBy(() -> EndpointDefinitionValidator.validateAll(definition.build(), emptyDealiasingVisitor)) .isInstanceOf(IllegalStateException.class) .hasMessage("Path parameters defined path template but not present in endpoint: [paramName]"); }
@Test public void testNoGetBodyValidator() { EndpointDefinition.Builder definition = EndpointDefinition.builder() .args(BODY_ARG_BUILDER.argName(ArgumentName.of("bodyArg")).build()) .endpointName(ENDPOINT_NAME) .httpMethod(HttpMethod.GET) .httpPath(HttpPath.of("/a/path")); assertThatThrownBy(() -> EndpointDefinitionValidator.validateAll(definition.build(), emptyDealiasingVisitor)) .isInstanceOf(IllegalStateException.class) .hasMessage(String.format( "Endpoint cannot be a GET and contain a body: method: %s, path: %s", HttpMethod.GET, "/a/path")); }
private static EndpointDefinition parseEndpoint( String name, com.palantir.conjure.parser.services.EndpointDefinition def, PathString basePath, Optional<AuthType> defaultAuth, ReferenceTypeResolver typeResolver, DealiasingTypeVisitor dealiasingVisitor) { HttpPath httpPath = parseHttpPath(def, basePath); EndpointDefinition endpoint = EndpointDefinition.builder() .endpointName(EndpointName.of(name)) .httpMethod(HttpMethod.valueOf(def.http().method())) .httpPath(httpPath) .auth(def.auth().map(ConjureParserUtils::parseAuthType).orElse(defaultAuth)) .args(parseArgs(def.args(), httpPath, typeResolver)) .markers(parseMarkers(def.markers(), typeResolver)) .returns(def.returns().map(t -> t.visit(new ConjureTypeParserVisitor(typeResolver)))) .docs(def.docs().map(Documentation::of)) .deprecated(def.deprecated().map(Documentation::of)) .build(); EndpointDefinitionValidator.validateAll(endpoint, dealiasingVisitor); return endpoint; }
@Test public void testSingleBodyParamValidator() { EndpointDefinition.Builder definition = EndpointDefinition.builder() .args(BODY_ARG_BUILDER.argName(ArgumentName.of("bodyArg1")).build()) .args(BODY_ARG_BUILDER.argName(ArgumentName.of("bodyArg2")).build()) .endpointName(ENDPOINT_NAME) .httpMethod(HttpMethod.GET) .httpPath(HttpPath.of("/a/path")); assertThatThrownBy(() -> EndpointDefinitionValidator.validateAll(definition.build(), emptyDealiasingVisitor)) .isInstanceOf(IllegalStateException.class) .hasMessage("Endpoint cannot have multiple body parameters: [bodyArg1, bodyArg2]"); }
@Test public void testArgumentTypeValidator() { EndpointDefinition.Builder definition = EndpointDefinition.builder() .args(ImmutableList.of(ArgumentDefinition.builder() .argName(ArgumentName.of("testArg")) .type(Type.primitive(PrimitiveType.BINARY)) .paramType(ParameterType.header(HeaderParameterType.of(ParameterId.of("testArg")))) .build())) .endpointName(ENDPOINT_NAME) .httpMethod(HttpMethod.GET) .httpPath(HttpPath.of("/a/path")); assertThatThrownBy(() -> EndpointDefinitionValidator.validateAll(definition.build(), emptyDealiasingVisitor)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("Non body parameters cannot be of the 'binary' type: 'testArg' is not allowed"); }
@Test public void testComplexHeaderObject() { TypeName typeName = TypeName.of("SomeObject", "com.palantir.foo"); EndpointDefinition.Builder definition = EndpointDefinition.builder() .args(ArgumentDefinition.builder() .argName(ArgumentName.of("someName")) .type(Type.reference(typeName)) .paramType(ParameterType.header(HeaderParameterType.of(ParameterId.of("SomeId")))) .build()) .endpointName(ENDPOINT_NAME) .httpMethod(HttpMethod.GET) .httpPath(HttpPath.of("/a/path")); DealiasingTypeVisitor dealiasingVisitor = new DealiasingTypeVisitor(ImmutableMap.of( typeName, TypeDefinition.object(ObjectDefinition.of(typeName, ImmutableList.of(), Documentation.of(""))) )); assertThatThrownBy(() -> EndpointDefinitionValidator.validateAll(definition.build(), dealiasingVisitor)) .isInstanceOf(IllegalStateException.class) .hasMessage("Header parameters must be enums, primitives, aliases or optional primitive:" + " \"someName\" is not allowed"); } }
private EndpointDefinition.Builder createEndpoint(String paramName) { ArgumentDefinition arg = ArgumentDefinition.builder() .paramType(ParameterType.body(BodyParameterType.of())) .type(Type.primitive(PrimitiveType.STRING)) .argName(ArgumentName.of(paramName)) .build(); return EndpointDefinition.builder() .httpMethod(HttpMethod.POST) .httpPath(HttpPath.of("/a/path")) .args(ImmutableList.of(arg)) .endpointName(EndpointName.of("test")); } }
@Test public void testPathParamValidatorExtraParams() { ArgumentDefinition paramDefinition = ArgumentDefinition.builder() .type(Type.primitive(PrimitiveType.STRING)) .argName(ArgumentName.of("paramName")) .paramType(ParameterType.path(PathParameterType.of())) .build(); EndpointDefinition.Builder definition = EndpointDefinition.builder() .args(paramDefinition) .endpointName(ENDPOINT_NAME) .httpMethod(HttpMethod.GET) .httpPath(HttpPath.of("/a/path")); assertThatThrownBy(() -> EndpointDefinitionValidator.validateAll(definition.build(), emptyDealiasingVisitor)) .isInstanceOf(IllegalStateException.class) .hasMessageContaining( "Path parameters defined in endpoint but not present in path template: [paramName]"); }
@Test public void testComplexHeader() { EndpointDefinition.Builder definition = EndpointDefinition.builder() .args(ArgumentDefinition.builder() .argName(ArgumentName.of("someName")) .type(Type.list(ListType.builder().itemType(Type.primitive(PrimitiveType.STRING)).build())) .paramType(ParameterType.header(HeaderParameterType.of(ParameterId.of("someId")))) .build()) .endpointName(ENDPOINT_NAME) .httpMethod(HttpMethod.GET) .httpPath(HttpPath.of("/a/path")); assertThatThrownBy(() -> EndpointDefinitionValidator.validateAll(definition.build(), emptyDealiasingVisitor)) .isInstanceOf(IllegalStateException.class) .hasMessage("Header parameters must be enums, primitives, aliases or optional primitive:" + " \"someName\" is not allowed"); }
@Test public void testPathParamValidatorUniquePathParams() { ArgumentDefinition paramDefinition1 = ArgumentDefinition.builder() .argName(ArgumentName.of("paramName")) .type(Type.primitive(PrimitiveType.STRING)) .paramType(ParameterType.path(PathParameterType.of())) .build(); ArgumentDefinition paramDefinition2 = ArgumentDefinition.builder() .argName(ArgumentName.of("paramName")) .type(Type.primitive(PrimitiveType.STRING)) .paramType(ParameterType.path(PathParameterType.of())) .build(); EndpointDefinition.Builder definition = EndpointDefinition.builder() .args(ImmutableList.of(paramDefinition1, paramDefinition2)) .endpointName(ENDPOINT_NAME) .httpMethod(HttpMethod.GET) .httpPath(HttpPath.of("/a/path")); assertThatThrownBy(() -> EndpointDefinitionValidator.validateAll(definition.build(), emptyDealiasingVisitor)) .isInstanceOf(IllegalStateException.class) .hasMessage("Path parameter with identifier \"paramName\" is defined multiple times for endpoint"); }
@Test @SuppressWarnings("CheckReturnValue") public void testArgumentBodyTypeValidator() { EndpointDefinition.Builder definition = EndpointDefinition.builder() .args(ImmutableList.of(ArgumentDefinition.builder() .argName(ArgumentName.of("testArg")) .type(Type.primitive(PrimitiveType.BINARY)) .paramType(ParameterType.body(BodyParameterType.of())) .build())) .endpointName(ENDPOINT_NAME) .httpMethod(HttpMethod.POST) .httpPath(HttpPath.of("/a/path")); // Should not throw exception EndpointDefinitionValidator.validateAll(definition.build(), emptyDealiasingVisitor); }
private EndpointDefinition createEndpoint(ParameterType paramType) { ArgumentDefinition arg = ArgumentDefinition.builder() .argName(PARAMETER_NAME) .paramType(paramType) .type(Type.primitive(PrimitiveType.INTEGER)) .build(); EndpointDefinition definition = EndpointDefinition.builder() .httpMethod(HttpMethod.POST) .httpPath(HttpPath.of("/a/path")) .args(arg) .endpointName(EndpointName.of("test")) .build(); EndpointDefinitionValidator.validateAll(definition, dealiasingVisitor); return definition; } }