Advertisement
marrruuuuuuuu

WorklogAPITests(to devide MR)

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