Skip to content

Commit

Permalink
feat(server): ApiViolationsController always responds including api_d…
Browse files Browse the repository at this point in the history
…efinition #593
  • Loading branch information
roxspring committed Apr 15, 2019
1 parent c5e29d2 commit d6a5227
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestHeader
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.ResponseBody
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.util.UriComponentsBuilder
Expand Down Expand Up @@ -62,12 +61,11 @@ class ApiViolationsController(
@ResponseBody
@GetMapping("/api-violations/{externalId}")
fun getExistingViolationResponse(
@PathVariable(value = "externalId") externalId: UUID,
@RequestParam(value = "include_api_definition", required = false, defaultValue = "false") includeApiDefinition: Boolean
@PathVariable(value = "externalId") externalId: UUID
): ApiDefinitionResponse {
val review = apiReviewRepository.findByExternalId(externalId) ?: throw ApiReviewNotFoundException()

return buildApiDefinitionResponse(review, includeApiDefinition)
return buildApiDefinitionResponse(review)
}

private fun retrieveRulesPolicy(request: ApiDefinitionRequest): RulesPolicy = request.ignoreRules
Expand All @@ -83,7 +81,7 @@ class ApiViolationsController(
throw e
}

private fun buildApiDefinitionResponse(review: ApiReview, includeApiDefinition: Boolean = false): ApiDefinitionResponse = ApiDefinitionResponse(
private fun buildApiDefinitionResponse(review: ApiReview): ApiDefinitionResponse = ApiDefinitionResponse(
externalId = review.externalId,
message = serverMessageService.serverMessage(review.userAgent),
violations = review.ruleViolations.orEmpty().map {
Expand All @@ -104,6 +102,6 @@ class ApiViolationsController(
Severity.MAY to review.mayViolations,
Severity.HINT to review.hintViolations
).map { it.first.name.toLowerCase() to it.second }.toMap(),
apiDefinition = if (includeApiDefinition) review.apiDefinition else null
apiDefinition = review.apiDefinition
)
}
8 changes: 0 additions & 8 deletions server/src/main/resources/api/zally-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -248,14 +248,6 @@ components:
schema:
type: string
format: uuid
IncludeApiDefinition:
name: include_api_definition
in: query
description: Whether to include the API definition in the response
required: false
schema:
type: boolean
default: false

schemas:
LintingRequest:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package de.zalando.zally.apireview

import de.zalando.zally.configuration.JacksonObjectMapperConfiguration
import de.zalando.zally.dto.ApiDefinitionRequest
import org.hamcrest.CoreMatchers.hasItem
import org.hamcrest.Matchers.containsString
import org.hamcrest.Matchers.notNullValue
import org.hamcrest.Matchers.nullValue
import org.intellij.lang.annotations.Language
import org.junit.Test
import org.junit.runner.RunWith
Expand All @@ -18,6 +18,7 @@ import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.content
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.header
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status

Expand All @@ -40,7 +41,13 @@ class ApiViolationsControllerTest {
.content("{\"api_definition_string\":\"\"}")
)
.andExpect(status().isOk)
.andExpect(header().exists("Location"))
.andExpect(content().contentType(APPLICATION_JSON_UTF8))
.andExpect(content().string(containsString("https://zalando.github.io/restful-api-guidelines")))
.andExpect(jsonPath("$.violations[*].rule_link", hasItem("https://zalando.github.io/restful-api-guidelines/#101")))
.andExpect(jsonPath("$.external_id", notNullValue()))
.andExpect(jsonPath("$.violations", notNullValue()))
.andExpect(jsonPath("$.api_definition", notNullValue()))
}

@Test
Expand All @@ -53,29 +60,7 @@ class ApiViolationsControllerTest {
}

@Test
fun `getExistingViolationResponse with existing responds Ok without api_definition`() {

val location = mvc.perform(
post("/api-violations")
.contentType("application/json")
.content("{\"api_definition_string\":\"\"}")
)
.andExpect(status().isOk)
.andReturn().response.getHeaderValue("Location")!!

mvc.perform(
get(location.toString())
.accept("application/json")
)
.andExpect(status().isOk)
.andExpect(content().contentType(APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$.external_id", notNullValue()))
.andExpect(jsonPath("$.violations", notNullValue()))
.andExpect(jsonPath("$.api_definition", nullValue()))
}

@Test
fun `getExistingViolationResponse with existing and include_api_definition responds Ok with api_definition`() {
fun `getExistingViolationResponse with existing responds Ok`() {

val location = mvc.perform(
post("/api-violations")
Expand All @@ -87,7 +72,6 @@ class ApiViolationsControllerTest {

mvc.perform(
get(location.toString())
.param("include_api_definition", "true")
.accept("application/json")
)
.andExpect(status().isOk)
Expand Down
2 changes: 1 addition & 1 deletion web-ui/src/client/app/services/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const RestService = {
.fetch(
`${
window.env.ZALLY_API_URL
}/api-violations/${externalId}?include_api_definition=true`,
}/api-violations/${externalId}`,
options
)
.then(response => response.json())
Expand Down

0 comments on commit d6a5227

Please sign in to comment.