|
| 1 | +package org.evomaster.e2etests.spring.openapi.v3.jsonpatchobjectvalue |
| 2 | + |
| 3 | +import com.foo.rest.examples.spring.openapi.v3.jsonpatchobjectvalue.JsonPatchObjectValueController |
| 4 | +import org.evomaster.core.problem.rest.data.HttpVerb |
| 5 | +import org.evomaster.e2etests.spring.openapi.v3.SpringTestBase |
| 6 | +import org.junit.jupiter.api.Assertions |
| 7 | +import org.junit.jupiter.api.BeforeAll |
| 8 | +import org.junit.jupiter.api.Test |
| 9 | + |
| 10 | +/** |
| 11 | + * E2E test: JSON Patch DTO with a NON-PRIMITIVE value. |
| 12 | + * |
| 13 | + * What is being tested: |
| 14 | + * When `dtoForRequestPayload` is enabled and the SUT has a JSON Patch endpoint whose resource |
| 15 | + * exposes an array-of-objects field, EvoMaster must generate: |
| 16 | + * 1. the DTO that represents the JSON Patch operations (the shared `JsonPatchOperation` DTO), and |
| 17 | + * 2. the nested DTO for the NON-PRIMITIVE `value` of those operations (the `Item` object DTO). |
| 18 | + * |
| 19 | + * Why the assertion is unambiguous: |
| 20 | + * The SUT only exposes a GET (no request body -> no DTO) and a PATCH (json-patch). Therefore the |
| 21 | + * only DTOs EvoMaster can generate are the JSON Patch ones. Reflectively loading |
| 22 | + * `org.foo.dto.JsonPatchOperation` and `org.foo.dto.Item` after the generated suite is compiled |
| 23 | + * proves that both were generated, i.e. that the value DTO of the JSON Patch operations exists. |
| 24 | + * |
| 25 | + * This mirrors the reflective-assert approach of |
| 26 | + * [org.evomaster.e2etests.spring.openapi.v3.dtoreflectiveassert.DtoReflectiveAssertEMTest]. |
| 27 | + */ |
| 28 | +class JsonPatchObjectValueDtoEMTest : SpringTestBase() { |
| 29 | + |
| 30 | + companion object { |
| 31 | + @BeforeAll |
| 32 | + @JvmStatic |
| 33 | + fun init() { |
| 34 | + initClass(JsonPatchObjectValueController()) |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + fun testRunEM() { |
| 40 | + runTestHandlingFlakyAndCompilation( |
| 41 | + "JsonPatchObjectValueDtoEM", |
| 42 | + "org.foo.JsonPatchObjectValueDtoEM", |
| 43 | + 100, |
| 44 | + ) { args: MutableList<String> -> |
| 45 | + |
| 46 | + setOption(args, "dtoForRequestPayload", "true") |
| 47 | + // This SUT intentionally has no authentication, and the PATCH returns 2xx, so the |
| 48 | + // security oracle would flag a fault 208 (Anonymous Modifications). It is irrelevant to |
| 49 | + // what we assert here (JSON Patch DTO generation), so we disable it to keep the suite clean. |
| 50 | + setOption(args, "security", "false") |
| 51 | + |
| 52 | + val solution = initAndRun(args) |
| 53 | + |
| 54 | + Assertions.assertTrue(solution.individuals.size >= 1) |
| 55 | + assertHasAtLeastOne(solution, HttpVerb.PATCH, 200, "/resource/{id}", "OK") |
| 56 | + } |
| 57 | + |
| 58 | + assertJsonPatchOperationDtoCreated() |
| 59 | + assertNonPrimitiveValueDtoCreated() |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * The shared JsonPatchOperation DTO must exist with the RFC 6902 fields (op, path, from, value). |
| 64 | + */ |
| 65 | + private fun assertJsonPatchOperationDtoCreated() { |
| 66 | + val (klass, instance) = initDtoClass("JsonPatchOperation") |
| 67 | + assertProperty(klass, instance, "op", "add") |
| 68 | + assertProperty(klass, instance, "path", "/items") |
| 69 | + assertProperty(klass, instance, "from", "/items") |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * The nested DTO for the non-primitive `value` of the operations must exist. Its name comes |
| 74 | + * from the OpenAPI component `Item`, with fields label (String) and quantity (Int). |
| 75 | + */ |
| 76 | + private fun assertNonPrimitiveValueDtoCreated() { |
| 77 | + val (klass, instance) = initDtoClass("Item") |
| 78 | + assertProperty(klass, instance, "label", "a label") |
| 79 | + assertProperty(klass, instance, "quantity", 7) |
| 80 | + } |
| 81 | +} |
0 commit comments