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.http.QueryParameter.queryParam
- 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.mockito.BDDMockito.willReturn
- import org.springframework.boot.test.context.SpringBootTest
- 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 org.springframework.test.context.ActiveProfiles
- import org.springframework.test.context.DynamicPropertyRegistry
- import org.springframework.test.context.DynamicPropertySource
- import org.testcontainers.containers.PostgreSQLContainer
- import org.testcontainers.junit.jupiter.Container
- import org.testcontainers.junit.jupiter.Testcontainers
- import pro.siberian.passproofback.worklog.dto.MilestoneDto
- import pro.siberian.passproofback.worklog.dto.RunDto
- import pro.siberian.passproofback.worklog.dto.TestDto
- import pro.siberian.passproofback.worklog.dto.WorklogDtoRequest
- import pro.siberian.passproofback.worklog.dto.WorklogDtoResponse
- import pro.siberian.passproofback.worklog.dto.FileMetaDto
- import pro.siberian.passproofback.worklog.dto.FileResponseDto
- @ExtendWith(RestDocumentationExtension::class)
- @SpringBootTest
- @ActiveProfiles("test")
- @WireMockTest(httpPort = 8080)
- @Testcontainers
- class WorklogApiTests {
- private lateinit var spec: RequestSpecification
- private val uploadedTests = listOf(
- WorklogDtoRequest(
- testRun = "1",
- user = "user1",
- ),
- WorklogDtoRequest(
- testRun = "2",
- user = "user2",
- photo = "localhost:8080/files/1",
- bug = "4"
- )
- )
- 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"
- )
- )
- companion object {
- @Container
- val container: PostgreSQLContainer<Nothing> = PostgreSQLContainer<Nothing>("postgres:latest").withReuse(true)
- @JvmStatic
- @DynamicPropertySource
- fun datasourceconfig(registry: DynamicPropertyRegistry) {
- registry.add("spring.datasource.url", container::getJdbcUrl)
- registry.add("spring.datasource.password", container::getPassword)
- registry.add("spring.datasource.username", container::getUsername)
- }
- }
- @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("/testrail/get_test/.*"))
- .willReturn(
- aResponse()
- .withBody(ObjectMapper().writeValueAsBytes(TestDto()))
- .withHeader("content-type", "application/json")
- .withStatus(HttpStatus.OK.value())
- )
- )
- wireMock.register(
- get(urlMatching("/testrail/get_run/.*"))
- .willReturn(
- aResponse()
- .withBody(ObjectMapper().writeValueAsBytes(RunDto()))
- .withHeader("content-type", "application/json")
- .withStatus(HttpStatus.OK.value())
- )
- )
- wireMock.register(
- get(urlMatching("/testrail/get_milestone/.*"))
- .willReturn(
- aResponse()
- .withBody(ObjectMapper().writeValueAsBytes(MilestoneDto()))
- .withHeader("content-type", "application/json")
- .withStatus(HttpStatus.OK.value())
- )
- )
- val receivedObjects: List<WorklogDtoResponse> = 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/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/${testedWorklogDto.scenario}")
- .then()
- .statusCode(HttpStatus.OK.value())
- .extract()
- .body().jsonPath().getList(".", 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()
- .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()
- .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()
- .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`()
- .post("/v1/test/${worklog.scenario}")
- .then()
- .statusCode(HttpStatus.OK.value())
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement