markusthoemmes commented on a change in pull request #3878: Validate the Controller Swagger spec matches the impl URL: https://github.com/apache/incubator-openwhisk/pull/3878#discussion_r202484697
########## File path: tests/src/test/scala/common/rest/SwaggerValidator.scala ########## @@ -0,0 +1,109 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package common.rest + +import scala.collection.JavaConverters._ + +import akka.http.scaladsl.model.HttpEntity +import akka.http.scaladsl.model.HttpRequest +import akka.http.scaladsl.model.HttpResponse +import akka.util.ByteString +import com.atlassian.oai.validator.SwaggerRequestResponseValidator +import com.atlassian.oai.validator.model.SimpleRequest +import com.atlassian.oai.validator.model.SimpleResponse +import com.atlassian.oai.validator.report.ValidationReport +import com.atlassian.oai.validator.whitelist.ValidationErrorsWhitelist +import com.atlassian.oai.validator.whitelist.rule.WhitelistRules + +trait SwaggerValidator { + private val specWhitelist = ValidationErrorsWhitelist + .create() + .withRule( + "Ignore action and trigger payloads", + WhitelistRules.allOf( + WhitelistRules.messageContains("Object instance has properties which are not allowed by the schema"), + WhitelistRules.anyOf( + WhitelistRules.pathContains("/web/"), + WhitelistRules.pathContains("/actions/"), + WhitelistRules.pathContains("/triggers/")), + WhitelistRules.methodIs(io.swagger.models.HttpMethod.POST))) + .withRule( + "Ignore invalid action kinds", + WhitelistRules.allOf( + WhitelistRules.messageContains("kind"), + WhitelistRules.messageContains("Instance value"), + WhitelistRules.messageContains("not found"), + WhitelistRules.pathContains("/actions/"), + WhitelistRules.methodIs(io.swagger.models.HttpMethod.PUT))) + .withRule( + "Ignore tests that check for invalid DELETEs and PUTs on actions", + WhitelistRules.anyOf( + WhitelistRules.messageContains("DELETE operation not allowed on path '/api/v1/namespaces/_/actions/'"), + WhitelistRules.messageContains("PUT operation not allowed on path '/api/v1/namespaces/_/actions/'"))) + + private val specValidator = SwaggerRequestResponseValidator + .createFor("apiv1swagger.json") + .withWhitelist(specWhitelist) + .build() + + /** + * Validate a HTTP request and response against the Swagger spec. Request + * and response bodies are passed separately so that this validation + * does not have to consume the body content directly from the request + * and response, which would prevent callers from later consuming it. + * + * @param request the HttpRequest + * @param requestBody the request body + * @param response the HttpResponse + * @param responseBody the response body + * @return The list of validation error messages, if any + */ + def validateRequestAndResponse(request: HttpRequest, + requestBody: Option[String], + response: HttpResponse, + responseBody: String): Seq[String] = { + var specRequestBuilder = new SimpleRequest.Builder(request.method.value, request.uri.path.toString()) + for (header <- request.headers) { + specRequestBuilder = specRequestBuilder.withHeader(header.name, header.value) + } + for ((key, value) <- request.uri.query().toMap) { + specRequestBuilder = specRequestBuilder.withQueryParam(key, value) + } Review comment: Scala tip: You can get around the variable here by using `foldLeft`, like: ```scala val specRequest = { val builder = new SimpleRequest.Builder(request.method.value, request.uri.path.toString) val withHeaders = request.headers.foldLeft(builder)((b, header) => b.withHeader(header.name, header.value)) val andQuery = request.uri.query().foldLeft(withHeaders) { case (b, (key, value)) => b.withQueryParam(key, value) } val andBody = requestBody .map(builder.withBody(_).withHeader("Content-Type", request.entity.contentType.value)) .getOrElse(andQuery) andBody.build() } ``` `foldLeft` runs over a list and applies each element to a seed value that you pass to it. This also gets around using `Option.get`, which can throw an exception. Using combinators on Option (like map and getOrElse) you get a fully compiletime checked version of all the types without needing to wrap them in if/else statements. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
