Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pro.siberian.passproofback.worklog
- import com.fasterxml.jackson.databind.ObjectMapper
- import com.github.tomakehurst.wiremock.client.WireMock.*
- import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo
- import com.github.tomakehurst.wiremock.junit5.WireMockTest
- import io.restassured.RestAssured.given
- import io.restassured.builder.RequestSpecBuilder
- import io.restassured.specification.RequestSpecification
- import org.assertj.core.api.Assertions.assertThat
- import org.hamcrest.CoreMatchers.containsString
- import org.junit.jupiter.api.BeforeEach
- import org.junit.jupiter.api.Test
- import org.junit.jupiter.api.extension.ExtendWith
- import org.springframework.http.HttpStatus
- import org.springframework.restdocs.RestDocumentationContextProvider
- import org.springframework.restdocs.RestDocumentationExtension
- import org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessResponse
- import org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint
- import org.springframework.restdocs.request.RequestDocumentation.*
- import org.springframework.restdocs.restassured3.RestAssuredRestDocumentation
- import pro.siberian.passproofback.PassproofTest
- import pro.siberian.passproofback.worklog.dto.WorklogDtoRequest
- import pro.siberian.passproofback.worklog.dto.WorklogDtoResponse
- import pro.siberian.passproofback.worklog.dto.FileResponseDto
- @ExtendWith(RestDocumentationExtension::class)
- @WireMockTest(httpPort = 8080)
- class WorklogApiTests : PassproofTest() {
- private lateinit var spec: RequestSpecification
- private val expectedTests = listOf(
- WorklogDtoResponse(
- testRun = "555",
- user = "student A",
- ),
- WorklogDtoResponse(
- testRun = "2",
- user = "user2",
- photo = listOf(FileResponseDto("localhost:8080/files/13")),
- comment = "https://jira.siberian.pro/browse/SWITIPS_JIRA_TEST-4\n" +
- "comment"
- )
- )
- @BeforeEach
- fun setup(restDocumentation: RestDocumentationContextProvider) {
- this.spec = RequestSpecBuilder()
- .addFilter(
- RestAssuredRestDocumentation.documentationConfiguration(restDocumentation).operationPreprocessors()
- )
- .build()
- }
- @Test
- fun `Get test list as HTML request returns 200`(wm: WireMockRuntimeInfo) {
- val wireMock = wm.wireMock
- wireMock.register(
- get(urlMatching("/tests"))
- .willReturn(
- aResponse().withStatus(HttpStatus.OK.value())
- )
- )
- given()
- .spec(spec)
- .filter(RestAssuredRestDocumentation.document("get-tests-html-ok"))
- .`when`()
- .get("/tests")
- .then()
- .statusCode(HttpStatus.OK.value())
- }
- @Test
- fun `Get test list as JSON request returns list of objects`(wm: WireMockRuntimeInfo) {
- val wireMock = wm.wireMock
- wireMock.register(
- get(urlMatching("/v1/tests"))
- .willReturn(
- aResponse()
- .withBody(ObjectMapper().writeValueAsBytes(expectedTests))
- .withHeader("content-type", "application/json")
- .withStatus(HttpStatus.OK.value())
- )
- )
- val receivedObjects = given()
- .spec(spec)
- .filter(
- RestAssuredRestDocumentation.document(
- "get-tests-json-ok",
- preprocessResponse(prettyPrint())
- )
- )
- .`when`()
- .get("/v1/tests")
- .then()
- .statusCode(HttpStatus.OK.value())
- .extract()
- .body().jsonPath().getList(".", WorklogDtoResponse::class.java)
- assertThat(receivedObjects).usingRecursiveComparison().ignoringFields("timestamp").isEqualTo(expectedTests)
- }
- @Test
- fun `Get test with empty scenario field request returns 500`(wm: WireMockRuntimeInfo) {
- val wireMock = wm.wireMock
- wireMock.register(
- get(urlMatching("/v1/test/"))
- .willReturn(
- aResponse()
- .withStatus(HttpStatus.INTERNAL_SERVER_ERROR.value())
- .withBody("List is empty")
- )
- )
- given()
- .spec(spec)
- .filter(RestAssuredRestDocumentation.document("get-empty-scenario-error"))
- .`when`()
- .get("/v1/test/")
- .then()
- .statusCode(HttpStatus.INTERNAL_SERVER_ERROR.value())
- .body(containsString("List is empty"))
- }
- @Test
- fun `Get test with non existing scenario field request returns 500`(wm: WireMockRuntimeInfo) {
- val wireMock = wm.wireMock
- wireMock.register(
- get(urlMatching("/v1/test/00000000"))
- .willReturn(
- aResponse()
- .withStatus(HttpStatus.INTERNAL_SERVER_ERROR.value())
- .withBody("List is empty")
- )
- )
- given()
- .spec(spec)
- .filter(
- RestAssuredRestDocumentation.document(
- "get-nonexisting-scenario-error",
- pathParameters(parameterWithName("scenario").description("Scenario of retrieved test"))
- )
- )
- .`when`()
- .get("/v1/test/{scenario}", "00000000")
- .then()
- .statusCode(HttpStatus.INTERNAL_SERVER_ERROR.value())
- .body(containsString("List is empty"))
- }
- @Test
- fun `Get test with existing scenario field request returns test object`(wm: WireMockRuntimeInfo) {
- val wireMock = wm.wireMock
- val testedWorklogDto = expectedTests[0]
- wireMock.register(
- get(urlMatching("/v1/test/${testedWorklogDto.scenario}"))
- .willReturn(
- aResponse()
- .withStatus(HttpStatus.OK.value())
- .withBody(ObjectMapper().writeValueAsBytes(testedWorklogDto))
- )
- )
- val receivedObject = given()
- .spec(spec)
- .filter(
- RestAssuredRestDocumentation.document(
- "get-existing-scenario-ok",
- pathParameters(parameterWithName("scenario").description("Scenario of retrieved test"))
- )
- )
- .`when`()
- .get("/v1/test/{scenario}", testedWorklogDto.scenario)
- .then()
- .statusCode(HttpStatus.OK.value())
- .extract()
- .body().jsonPath().getObject(".", WorklogDtoResponse::class.java)
- assertThat(receivedObject).usingRecursiveComparison().ignoringFields("timestamp").isEqualTo(testedWorklogDto)
- }
- @Test
- fun `Post fully configured test request returns 200 Ok`(wm: WireMockRuntimeInfo) {
- val wireMock = wm.wireMock
- val worklog = WorklogDtoRequest()
- wireMock.register(
- post(urlMatching("/v1/test.*"))
- .withQueryParam("user", equalTo(worklog.user))
- .withQueryParam("photo", equalTo(worklog.photo))
- .withQueryParam("testrun", equalTo(worklog.testRun))
- .withQueryParam("scenario", equalTo(worklog.scenario))
- .withQueryParam("comment", equalTo(worklog.comment))
- .withQueryParam("name", equalTo(worklog.name))
- .withQueryParam("status", equalTo(worklog.status))
- .withQueryParam("bug", equalTo(worklog.bug))
- .willReturn(
- aResponse()
- .withStatus(HttpStatus.OK.value())
- .withBody("Ok")
- )
- )
- given()
- .queryParam("user", worklog.user)
- .queryParam("photo", worklog.photo)
- .queryParam("testrun", worklog.testRun)
- .queryParam("scenario", worklog.scenario)
- .queryParam("comment", worklog.comment)
- .queryParam("name", worklog.name)
- .queryParam("status", worklog.status)
- .queryParam("bug", worklog.bug)
- .spec(spec)
- .filter(
- RestAssuredRestDocumentation.document(
- "post-valid-test-ok",
- requestParameters(
- parameterWithName("user").description("Author of test log"),
- parameterWithName("photo").description("Photos with confirmation of doing test"),
- parameterWithName("testrun").description("Testrun"),
- parameterWithName("scenario").description("Scenario of test"),
- parameterWithName("status").description("Status of test - optional"),
- parameterWithName("name").description("Name of test - optional"),
- parameterWithName("comment").description("Comment to test - optional"),
- parameterWithName("bug").description(
- "Bug associated with test - optional. " +
- "Can be positive number or JIRA link."
- )
- )
- )
- )
- .`when`()
- .post("/v1/test")
- .then()
- .statusCode(HttpStatus.OK.value())
- .body(containsString("Ok"))
- }
- @Test
- fun `Post test with empty required field request returns 500`(wm: WireMockRuntimeInfo) {
- val wireMock = wm.wireMock
- val worklog = WorklogDtoRequest()
- wireMock.register(
- post(urlMatching("/v1/test.*"))
- .withQueryParam("photo", equalTo(worklog.photo))
- .withQueryParam("testrun", equalTo(worklog.testRun))
- .withQueryParam("scenario", equalTo(worklog.scenario))
- .withQueryParam("comment", equalTo(worklog.comment))
- .withQueryParam("name", equalTo(worklog.name))
- .withQueryParam("status", equalTo(worklog.status))
- .willReturn(
- aResponse()
- .withStatus(HttpStatus.INTERNAL_SERVER_ERROR.value())
- .withBody("require user")
- )
- )
- given()
- .queryParam("photo", worklog.photo)
- .queryParam("testrun", worklog.testRun)
- .queryParam("scenario", worklog.scenario)
- .queryParam("comment", worklog.comment)
- .queryParam("name", worklog.name)
- .queryParam("status", worklog.status)
- .spec(spec)
- .filter(
- RestAssuredRestDocumentation.document(
- "post-test-without-user-error",
- requestParameters(
- parameterWithName("photo").description("Photos with confirmation of doing test"),
- parameterWithName("testrun").description("Testrun"),
- parameterWithName("scenario").description("Scenario of test"),
- parameterWithName("status").description("Status of test - optional"),
- parameterWithName("name").description("Name of test - optional"),
- parameterWithName("comment").description("Comment to test - optional"),
- )
- )
- )
- .`when`()
- .post("/v1/test")
- .then()
- .statusCode(HttpStatus.INTERNAL_SERVER_ERROR.value())
- .body(containsString("require user"))
- }
- @Test
- fun `Patch test request returns 200 Ok`(wm: WireMockRuntimeInfo) {
- val wireMock = wm.wireMock
- val worklog = WorklogDtoRequest()
- wireMock.register(
- patch(urlMatching("/v1/test/${worklog.scenario}.*"))
- .withQueryParam("bug", equalTo(worklog.bug))
- .withQueryParam("photo", equalTo(worklog.photo))
- .withQueryParam("comment", equalTo(worklog.comment))
- .withQueryParam("status", equalTo(worklog.status))
- .willReturn(
- aResponse()
- .withStatus(HttpStatus.OK.value())
- )
- )
- given()
- .queryParam("bug", worklog.bug)
- .queryParam("photo", worklog.photo)
- .queryParam("comment", worklog.comment)
- .queryParam("status", worklog.status)
- .spec(spec)
- .filter(
- RestAssuredRestDocumentation.document(
- "patch-test-ok-200",
- requestParameters(
- parameterWithName("bug").description("Bug associated with test - optional"),
- parameterWithName("photo").description("Photos with confirmation of doing test"),
- parameterWithName("status").description("Status of test - optional"),
- parameterWithName("comment").description("Comment to test - optional")
- ),
- pathParameters(
- parameterWithName("scenario_id").description("Scenario id of retrieved test")
- )
- )
- )
- .`when`()
- .patch("/v1/test/{scenario_id}", worklog.scenario)
- .then()
- .statusCode(HttpStatus.OK.value())
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement