|
| 1 | +/* |
| 2 | + * Copyright 2025 https://github.com/openapi-processor/openapi-processor-base |
| 3 | + * PDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.openapiprocessor.core.converter |
| 7 | + |
| 8 | +import io.kotest.core.spec.style.StringSpec |
| 9 | +import io.kotest.matchers.maps.shouldBeEmpty |
| 10 | +import io.kotest.matchers.maps.shouldHaveSize |
| 11 | +import io.kotest.matchers.nulls.shouldNotBeNull |
| 12 | +import io.mockk.mockk |
| 13 | +import io.openapiprocessor.core.model.Documentation |
| 14 | +import io.openapiprocessor.core.model.datatypes.DataTypeName |
| 15 | +import io.openapiprocessor.core.model.datatypes.ObjectDataType |
| 16 | +import io.openapiprocessor.core.model.datatypes.PropertyDataType |
| 17 | +import io.openapiprocessor.core.model.datatypes.StringDataType |
| 18 | +import io.openapiprocessor.core.parser.ContentType |
| 19 | +import io.openapiprocessor.core.parser.HttpMethod |
| 20 | +import io.openapiprocessor.core.parser.HttpStatus |
| 21 | +import io.openapiprocessor.core.parser.Response |
| 22 | +import io.openapiprocessor.core.model.Response as ModelResponse |
| 23 | + |
| 24 | + |
| 25 | +class ContentTypeInterfaceCollectorSpec: StringSpec({ |
| 26 | + |
| 27 | + val foo = ObjectDataType( |
| 28 | + DataTypeName("Foo"), |
| 29 | + "pkg", |
| 30 | + linkedMapOf( |
| 31 | + "foo" to PropertyDataType( |
| 32 | + readOnly = false, |
| 33 | + writeOnly = false, |
| 34 | + dataType = StringDataType(), |
| 35 | + documentation = Documentation()))) |
| 36 | + |
| 37 | + val oof = ObjectDataType( |
| 38 | + DataTypeName("Oof"), |
| 39 | + "pkg", |
| 40 | + linkedMapOf( |
| 41 | + "oof" to PropertyDataType( |
| 42 | + readOnly = false, |
| 43 | + writeOnly = false, |
| 44 | + dataType = StringDataType(), |
| 45 | + documentation = Documentation()))) |
| 46 | + |
| 47 | + val bar = ObjectDataType( |
| 48 | + DataTypeName("Bar"), |
| 49 | + "pkg", |
| 50 | + linkedMapOf( |
| 51 | + "bar" to PropertyDataType( |
| 52 | + readOnly = false, |
| 53 | + writeOnly = false, |
| 54 | + dataType = StringDataType(), |
| 55 | + documentation = Documentation()))) |
| 56 | + |
| 57 | + "returns empty result on empty inputs" { |
| 58 | + val collector = ContentTypeInterfaceCollector("/foo", HttpMethod.GET) |
| 59 | + |
| 60 | + val cti = collector.collectContentTypeInterfaces(emptyMap(), emptyMap()) |
| 61 | + |
| 62 | + cti.shouldBeEmpty() |
| 63 | + } |
| 64 | + |
| 65 | + "returns empty result with identical response data types" { |
| 66 | + val collector = ContentTypeInterfaceCollector("/foo", HttpMethod.GET) |
| 67 | + |
| 68 | + val ctr = mutableMapOf<ContentType, Map<HttpStatus, Response>>() |
| 69 | + ctr["application/json"] = mapOf( |
| 70 | + "200" to mockk<Response>(), |
| 71 | + "201" to mockk<Response>() |
| 72 | + ) |
| 73 | + |
| 74 | + val srr = mutableMapOf<HttpStatus, List<ModelResponse>>() |
| 75 | + srr["200"] = listOf(ModelResponse("application/json", foo)) |
| 76 | + srr["201"] = listOf(ModelResponse("application/json", foo)) |
| 77 | + |
| 78 | + val cti = collector.collectContentTypeInterfaces(ctr, srr) |
| 79 | + |
| 80 | + cti.shouldBeEmpty() |
| 81 | + } |
| 82 | + |
| 83 | + "returns interface result with different response data types" { |
| 84 | + val collector = ContentTypeInterfaceCollector("/foo", HttpMethod.GET) |
| 85 | + |
| 86 | + val ctr = mutableMapOf<ContentType, Map<HttpStatus, Response>>() |
| 87 | + ctr["application/json"] = mapOf( |
| 88 | + "200" to mockk<Response>(), |
| 89 | + "201" to mockk<Response>(), |
| 90 | + ) |
| 91 | + |
| 92 | + val srr = mutableMapOf<HttpStatus, List<ModelResponse>>() |
| 93 | + srr["200"] = listOf(ModelResponse("application/json", foo)) |
| 94 | + srr["201"] = listOf(ModelResponse("application/json", bar)) |
| 95 | + |
| 96 | + val cti = collector.collectContentTypeInterfaces(ctr, srr) |
| 97 | + |
| 98 | + cti shouldHaveSize 1 |
| 99 | + |
| 100 | + cti["application/json"].shouldNotBeNull() |
| 101 | + } |
| 102 | + |
| 103 | + "returns interface result with different response data types including errors" { |
| 104 | + val collector = ContentTypeInterfaceCollector("/foo", HttpMethod.GET) |
| 105 | + |
| 106 | + val ctr = mutableMapOf<ContentType, Map<HttpStatus, Response>>() |
| 107 | + ctr["application/json"] = mapOf( |
| 108 | + "200" to mockk<Response>(), |
| 109 | + "202" to mockk<Response>(), |
| 110 | + "400" to mockk<Response>(), |
| 111 | + "401" to mockk<Response>(), |
| 112 | + ) |
| 113 | + |
| 114 | + val srr = mutableMapOf<HttpStatus, List<ModelResponse>>() |
| 115 | + srr["200"] = listOf(ModelResponse("application/json", foo)) |
| 116 | + srr["201"] = listOf(ModelResponse("application/json", bar)) |
| 117 | + srr["400"] = listOf(ModelResponse("application/json", oof)) |
| 118 | + srr["401"] = listOf(ModelResponse("application/json", oof)) |
| 119 | + |
| 120 | + val cti = collector.collectContentTypeInterfaces(ctr, srr) |
| 121 | + |
| 122 | + cti shouldHaveSize 1 |
| 123 | + |
| 124 | + cti["application/json"].shouldNotBeNull() |
| 125 | + } |
| 126 | + |
| 127 | +}) |
0 commit comments