Advertisement
marrruuuuuuuu

WorklogAPITests

Jul 4th, 2022
1,232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 14.99 KB | None | 0 0
  1. package pro.siberian.passproofback.worklog
  2.  
  3. import com.fasterxml.jackson.databind.ObjectMapper
  4. import com.github.tomakehurst.wiremock.client.WireMock.*
  5. import com.github.tomakehurst.wiremock.http.QueryParameter.queryParam
  6. import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo
  7. import com.github.tomakehurst.wiremock.junit5.WireMockTest
  8. import io.restassured.RestAssured.given
  9. import io.restassured.builder.RequestSpecBuilder
  10. import io.restassured.specification.RequestSpecification
  11. import org.assertj.core.api.Assertions.assertThat
  12. import org.hamcrest.CoreMatchers.containsString
  13. import org.junit.jupiter.api.BeforeEach
  14. import org.junit.jupiter.api.Test
  15. import org.junit.jupiter.api.extension.ExtendWith
  16. import org.mockito.BDDMockito.willReturn
  17. import org.springframework.boot.test.context.SpringBootTest
  18. import org.springframework.http.HttpStatus
  19. import org.springframework.restdocs.RestDocumentationContextProvider
  20. import org.springframework.restdocs.RestDocumentationExtension
  21. import org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessResponse
  22. import org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint
  23. import org.springframework.restdocs.request.RequestDocumentation.*
  24. import org.springframework.restdocs.restassured3.RestAssuredRestDocumentation
  25. import org.springframework.test.context.ActiveProfiles
  26. import org.springframework.test.context.DynamicPropertyRegistry
  27. import org.springframework.test.context.DynamicPropertySource
  28. import org.testcontainers.containers.PostgreSQLContainer
  29. import org.testcontainers.junit.jupiter.Container
  30. import org.testcontainers.junit.jupiter.Testcontainers
  31. import pro.siberian.passproofback.worklog.dto.MilestoneDto
  32. import pro.siberian.passproofback.worklog.dto.RunDto
  33. import pro.siberian.passproofback.worklog.dto.TestDto
  34. import pro.siberian.passproofback.worklog.dto.WorklogDtoRequest
  35. import pro.siberian.passproofback.worklog.dto.WorklogDtoResponse
  36. import pro.siberian.passproofback.worklog.dto.FileMetaDto
  37. import pro.siberian.passproofback.worklog.dto.FileResponseDto
  38.  
  39. @ExtendWith(RestDocumentationExtension::class)
  40. @SpringBootTest
  41. @ActiveProfiles("test")
  42. @WireMockTest(httpPort = 8080)
  43. @Testcontainers
  44. class WorklogApiTests {
  45.  
  46.     private lateinit var spec: RequestSpecification
  47.  
  48.     private val uploadedTests = listOf(
  49.         WorklogDtoRequest(
  50.             testRun = "1",
  51.             user = "user1",
  52.         ),
  53.         WorklogDtoRequest(
  54.             testRun = "2",
  55.             user = "user2",
  56.             photo = "localhost:8080/files/1",
  57.             bug = "4"
  58.         )
  59.     )
  60.  
  61.     private val expectedTests = listOf(
  62.         WorklogDtoResponse(
  63.             testRun = "555",
  64.             user = "student A",
  65.         ),
  66.         WorklogDtoResponse(
  67.             testRun = "2",
  68.             user = "user2",
  69.             photo = listOf(FileResponseDto("localhost:8080/files/13")),
  70.             comment = "https://jira.siberian.pro/browse/SWITIPS_JIRA_TEST-4\n" +
  71.                     "comment"
  72.         )
  73.     )
  74.  
  75.     companion object {
  76.         @Container
  77.         val container: PostgreSQLContainer<Nothing> = PostgreSQLContainer<Nothing>("postgres:latest").withReuse(true)
  78.  
  79.         @JvmStatic
  80.         @DynamicPropertySource
  81.         fun datasourceconfig(registry: DynamicPropertyRegistry) {
  82.             registry.add("spring.datasource.url", container::getJdbcUrl)
  83.             registry.add("spring.datasource.password", container::getPassword)
  84.             registry.add("spring.datasource.username", container::getUsername)
  85.         }
  86.     }
  87.  
  88.     @BeforeEach
  89.     fun setup(restDocumentation: RestDocumentationContextProvider) {
  90.         this.spec = RequestSpecBuilder()
  91.             .addFilter(
  92.                 RestAssuredRestDocumentation.documentationConfiguration(restDocumentation).operationPreprocessors()
  93.             )
  94.             .build()
  95.     }
  96.  
  97.  
  98.     @Test
  99.     fun `Get test list as HTML request returns 200`(wm: WireMockRuntimeInfo) {
  100.         val wireMock = wm.wireMock
  101.         wireMock.register(
  102.             get(urlMatching("/tests"))
  103.                 .willReturn(
  104.                     aResponse().withStatus(HttpStatus.OK.value())
  105.                 )
  106.         )
  107.  
  108.         given()
  109.             .spec(spec)
  110.             .filter(RestAssuredRestDocumentation.document("get-tests-html-ok"))
  111.             .`when`()
  112.             .get("/tests")
  113.             .then()
  114.             .statusCode(HttpStatus.OK.value())
  115.     }
  116.  
  117.     @Test
  118.     fun `Get test list as JSON request returns list of objects`(wm: WireMockRuntimeInfo) {
  119.         val wireMock = wm.wireMock
  120.         wireMock.register(
  121.             get(urlMatching("/testrail/get_test/.*"))
  122.                 .willReturn(
  123.                     aResponse()
  124.                         .withBody(ObjectMapper().writeValueAsBytes(TestDto()))
  125.                         .withHeader("content-type", "application/json")
  126.                         .withStatus(HttpStatus.OK.value())
  127.                 )
  128.         )
  129.         wireMock.register(
  130.             get(urlMatching("/testrail/get_run/.*"))
  131.                 .willReturn(
  132.                     aResponse()
  133.                         .withBody(ObjectMapper().writeValueAsBytes(RunDto()))
  134.                         .withHeader("content-type", "application/json")
  135.                         .withStatus(HttpStatus.OK.value())
  136.                 )
  137.         )
  138.         wireMock.register(
  139.             get(urlMatching("/testrail/get_milestone/.*"))
  140.                 .willReturn(
  141.                     aResponse()
  142.                         .withBody(ObjectMapper().writeValueAsBytes(MilestoneDto()))
  143.                         .withHeader("content-type", "application/json")
  144.                         .withStatus(HttpStatus.OK.value())
  145.                 )
  146.         )
  147.  
  148.         val receivedObjects: List<WorklogDtoResponse> = given()
  149.             .spec(spec)
  150.             .filter(
  151.                 RestAssuredRestDocumentation.document(
  152.                     "get-tests-json-ok",
  153.                     preprocessResponse(prettyPrint())
  154.                 )
  155.             )
  156.             .`when`()
  157.             .get("/v1/tests")
  158.             .then()
  159.             .statusCode(HttpStatus.OK.value())
  160.             .extract()
  161.             .body().jsonPath().getList(".", WorklogDtoResponse::class.java)
  162.  
  163.         assertThat(receivedObjects).usingRecursiveComparison().ignoringFields("timestamp").isEqualTo(expectedTests)
  164.     }
  165.  
  166.     @Test
  167.     fun `Get test with empty scenario field request returns 500`(wm: WireMockRuntimeInfo) {
  168.         val wireMock = wm.wireMock
  169.         wireMock.register(
  170.             get(urlMatching("/v1/test/"))
  171.                 .willReturn(
  172.                     aResponse()
  173.                         .withStatus(HttpStatus.INTERNAL_SERVER_ERROR.value())
  174.                         .withBody("List is empty")
  175.                 )
  176.         )
  177.  
  178.         given()
  179.             .spec(spec)
  180.             .filter(RestAssuredRestDocumentation.document("get-empty-scenario-error"))
  181.             .`when`()
  182.             .get("/v1/test/")
  183.             .then()
  184.             .statusCode(HttpStatus.INTERNAL_SERVER_ERROR.value())
  185.             .body(containsString("List is empty"))
  186.     }
  187.  
  188.     @Test
  189.     fun `Get test with non existing scenario field request returns 500`(wm: WireMockRuntimeInfo) {
  190.         val wireMock = wm.wireMock
  191.         wireMock.register(
  192.             get(urlMatching("/v1/test/00000000"))
  193.                 .willReturn(
  194.                     aResponse()
  195.                         .withStatus(HttpStatus.INTERNAL_SERVER_ERROR.value())
  196.                         .withBody("List is empty")
  197.                 )
  198.         )
  199.  
  200.         given()
  201.             .spec(spec)
  202.             .filter(
  203.                 RestAssuredRestDocumentation.document(
  204.                     "get-nonexisting-scenario-error",
  205.                     pathParameters(parameterWithName("scenario").description("Scenario of retrieved test"))
  206.                 )
  207.             )
  208.             .`when`()
  209.             .get("/v1/test/00000000")
  210.             .then()
  211.             .statusCode(HttpStatus.INTERNAL_SERVER_ERROR.value())
  212.             .body(containsString("List is empty"))
  213.     }
  214.  
  215.     @Test
  216.     fun `Get test with existing scenario field request returns test object`(wm: WireMockRuntimeInfo) {
  217.         val wireMock = wm.wireMock
  218.         val testedWorklogDto = expectedTests[0]
  219.         wireMock.register(
  220.             get(urlMatching("/v1/test/${testedWorklogDto.scenario}"))
  221.                 .willReturn(
  222.                     aResponse()
  223.                         .withStatus(HttpStatus.OK.value())
  224.                         .withBody(ObjectMapper().writeValueAsBytes(testedWorklogDto))
  225.                 )
  226.         )
  227.  
  228.         val receivedObject = given()
  229.             .spec(spec)
  230.             .filter(
  231.                 RestAssuredRestDocumentation.document(
  232.                     "get-existing-scenario-ok",
  233.                     pathParameters(parameterWithName("scenario").description("Scenario of retrieved test"))
  234.                 )
  235.             )
  236.             .`when`()
  237.             .get("/v1/test/${testedWorklogDto.scenario}")
  238.             .then()
  239.             .statusCode(HttpStatus.OK.value())
  240.             .extract()
  241.             .body().jsonPath().getList(".", WorklogDtoResponse::class.java)
  242.  
  243.         assertThat(receivedObject).usingRecursiveComparison().ignoringFields("timestamp").isEqualTo(testedWorklogDto)
  244.     }
  245.  
  246.     @Test
  247.     fun `Post fully configured test request returns 200 Ok`(wm: WireMockRuntimeInfo) {
  248.         val wireMock = wm.wireMock
  249.         val worklog = WorklogDtoRequest()
  250.         wireMock.register(
  251.             post(urlMatching("/v1/test"))
  252.                 .withQueryParam("user", equalTo(worklog.user))
  253.                 .withQueryParam("photo", equalTo(worklog.photo))
  254.                 .withQueryParam("testrun", equalTo(worklog.testRun))
  255.                 .withQueryParam("scenario", equalTo(worklog.scenario))
  256.                 .withQueryParam("comment", equalTo(worklog.comment))
  257.                 .withQueryParam("name", equalTo(worklog.name))
  258.                 .withQueryParam("status", equalTo(worklog.status))
  259.                 .withQueryParam("bug", equalTo(worklog.bug))
  260.                 .willReturn(
  261.                     aResponse()
  262.                         .withStatus(HttpStatus.OK.value())
  263.                         .withBody("Ok")
  264.                 )
  265.         )
  266.  
  267.         given()
  268.             .spec(spec)
  269.             .filter(
  270.                 RestAssuredRestDocumentation.document(
  271.                     "post-valid-test-ok",
  272.                     requestParameters(
  273.                         parameterWithName("user").description("Author of test log"),
  274.                         parameterWithName("photo").description("Photos with confirmation of doing test"),
  275.                         parameterWithName("testrun").description("Testrun"),
  276.                         parameterWithName("scenario").description("Scenario of test"),
  277.                         parameterWithName("status").description("Status of test - optional"),
  278.                         parameterWithName("name").description("Name of test - optional"),
  279.                         parameterWithName("comment").description("Comment to test - optional"),
  280.                         parameterWithName("bug").description(
  281.                             "Bug associated with test - optional. " +
  282.                                     "Can be positive number or JIRA link."
  283.                         )
  284.                     )
  285.                 )
  286.             )
  287.             .`when`()
  288.             .post("/v1/test")
  289.             .then()
  290.             .statusCode(HttpStatus.OK.value())
  291.             .body(containsString("Ok"))
  292.     }
  293.  
  294.     @Test
  295.     fun `Post test with empty required field request returns 500`(wm: WireMockRuntimeInfo) {
  296.         val wireMock = wm.wireMock
  297.         val worklog = WorklogDtoRequest()
  298.         wireMock.register(
  299.             post(urlMatching("/v1/test"))
  300.                 .withQueryParam("photo", equalTo(worklog.photo))
  301.                 .withQueryParam("testrun", equalTo(worklog.testRun))
  302.                 .withQueryParam("scenario", equalTo(worklog.scenario))
  303.                 .withQueryParam("comment", equalTo(worklog.comment))
  304.                 .withQueryParam("name", equalTo(worklog.name))
  305.                 .withQueryParam("status", equalTo(worklog.status))
  306.                 .willReturn(
  307.                     aResponse()
  308.                         .withStatus(HttpStatus.INTERNAL_SERVER_ERROR.value())
  309.                         .withBody("require user")
  310.                 )
  311.         )
  312.  
  313.         given()
  314.             .spec(spec)
  315.             .filter(
  316.                 RestAssuredRestDocumentation.document(
  317.                     "post-test-without-user-error",
  318.                     requestParameters(
  319.                         parameterWithName("photo").description("Photos with confirmation of doing test"),
  320.                         parameterWithName("testrun").description("Testrun"),
  321.                         parameterWithName("scenario").description("Scenario of test"),
  322.                         parameterWithName("status").description("Status of test - optional"),
  323.                         parameterWithName("name").description("Name of test - optional"),
  324.                         parameterWithName("comment").description("Comment to test - optional"),
  325.                     )
  326.                 )
  327.             )
  328.             .`when`()
  329.             .post("/v1/test")
  330.             .then()
  331.             .statusCode(HttpStatus.INTERNAL_SERVER_ERROR.value())
  332.             .body(containsString("require user"))
  333.     }
  334.  
  335.     @Test
  336.     fun `Patch test request returns 200 Ok`(wm: WireMockRuntimeInfo) {
  337.         val wireMock = wm.wireMock
  338.         val worklog = WorklogDtoRequest()
  339.         wireMock.register(
  340.             patch(urlMatching("/v1/test/${worklog.scenario}"))
  341.                 .withQueryParam("bug", equalTo(worklog.bug))
  342.                 .withQueryParam("photo", equalTo(worklog.photo))
  343.                 .withQueryParam("comment", equalTo(worklog.comment))
  344.                 .withQueryParam("status", equalTo(worklog.status))
  345.                 .willReturn(
  346.                     aResponse()
  347.                         .withStatus(HttpStatus.OK.value())
  348.                 )
  349.         )
  350.  
  351.         given()
  352.             .spec(spec)
  353.             .filter(
  354.                 RestAssuredRestDocumentation.document(
  355.                     "patch-test-ok-200",
  356.                     requestParameters(
  357.                         parameterWithName("bug").description("Bug associated with test - optional"),
  358.                         parameterWithName("photo").description("Photos with confirmation of doing test"),
  359.                         parameterWithName("status").description("Status of test - optional"),
  360.                         parameterWithName("comment").description("Comment to test - optional")
  361.                     ),
  362.                     pathParameters(
  363.                         parameterWithName("scenario_id").description("Scenario id of retrieved test")
  364.                     )
  365.                 )
  366.             )
  367.             .`when`()
  368.             .post("/v1/test/${worklog.scenario}")
  369.             .then()
  370.             .statusCode(HttpStatus.OK.value())
  371.     }
  372.  
  373. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement