|
| 1 | +package org.acme.read.crud; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertNotNull; |
| 5 | +import static org.junit.Assert.assertTrue; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | +import java.io.InputStream; |
| 9 | +import java.util.HashSet; |
| 10 | +import java.util.Optional; |
| 11 | +import java.util.Set; |
| 12 | +import java.util.Map.Entry; |
| 13 | +import java.util.stream.Stream; |
| 14 | + |
| 15 | +import org.acme.read.ModelReader; |
| 16 | +import org.eclipse.microprofile.config.ConfigProvider; |
| 17 | +import org.eclipse.microprofile.openapi.models.OpenAPI; |
| 18 | +import org.eclipse.microprofile.openapi.models.PathItem; |
| 19 | +import org.eclipse.microprofile.openapi.models.media.Schema; |
| 20 | +import org.junit.jupiter.api.BeforeAll; |
| 21 | +import org.junit.jupiter.api.Disabled; |
| 22 | +import org.junit.jupiter.params.ParameterizedTest; |
| 23 | +import org.junit.jupiter.params.provider.MethodSource; |
| 24 | + |
| 25 | +import io.quarkus.smallrye.openapi.runtime.OpenApiConstants; |
| 26 | +import io.smallrye.openapi.api.OpenApiConfig; |
| 27 | +import io.smallrye.openapi.api.OpenApiConfigImpl; |
| 28 | +import io.smallrye.openapi.runtime.OpenApiProcessor; |
| 29 | +import io.smallrye.openapi.runtime.OpenApiStaticFile; |
| 30 | +import io.smallrye.openapi.runtime.io.Format; |
| 31 | + |
| 32 | +@Disabled |
| 33 | +public class ResponseTypeMapperApicurioTest { |
| 34 | + private static OpenAPI model; |
| 35 | + private static ModelReader reader; |
| 36 | + private static final Set<String> EXCLUDED_RESPONSES = Set.of("conflict", "empty", "error", "forbidden", |
| 37 | + "invalidTopicsError", "notFound", "parameterBodies", "redirect", "string", "validationError", |
| 38 | + "EmptyRepository" /* Only used for 409 response */, |
| 39 | + "FileDeleteResponse" /* Used for single operation to delete a file in a repository */, |
| 40 | + "FileResponse" /* Used for single operation to update a file in a repository */, |
| 41 | + "FilesResponse" /* Modify multiple files in a repository */ |
| 42 | + ); |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | + @BeforeAll |
| 47 | + public static void setUp() { |
| 48 | + try (InputStream is = ResponseTypeMapperGiteaTest.class.getClassLoader().getResourceAsStream("apicurio.json")) { |
| 49 | + try (OpenApiStaticFile staticFile = new OpenApiStaticFile(is, Format.JSON)) { |
| 50 | + OpenApiConfig openApiConfig = new OpenApiConfigImpl(ConfigProvider.getConfig()); |
| 51 | + model = OpenApiProcessor.modelFromStaticFile(openApiConfig, staticFile); |
| 52 | + reader = new ModelReader(model); |
| 53 | + } |
| 54 | + } catch (IOException ex) { |
| 55 | + throw new RuntimeException("Could not find [" + OpenApiConstants.BASE_NAME + Format.JSON + "]"); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @ParameterizedTest |
| 60 | + @MethodSource("responseTypesWithGetById") |
| 61 | + void getByIdPath(String modelName) { |
| 62 | + ResponseTypeMapper analyzer = new ResponseTypeMapper(model, modelName); |
| 63 | + if (!analyzer.isArrayType()) { |
| 64 | + Optional<Entry<String, PathItem>> byIdPath = analyzer.getByIdPath(); |
| 65 | + assertNotNull(byIdPath); |
| 66 | + assertTrue(byIdPath.isPresent()); |
| 67 | + assertNotNull(byIdPath.get().getValue().getGET()); |
| 68 | + System.out.println("GetById path for " + modelName + " is " + byIdPath.get().getKey()); |
| 69 | + Schema schema = byIdPath.get().getValue().getGET().getResponses().getAPIResponse("200").getContent().getMediaType(analyzer.getResponseMediaType()).getSchema(); |
| 70 | + assertEquals(analyzer.getByIdSchemaName(), ResponseTypeMapper.resolveSchemaName(schema)); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + @ParameterizedTest |
| 75 | + @MethodSource("responseTypesWithDelete") |
| 76 | + void deletePath(String modelName) { |
| 77 | + ResponseTypeMapper analyzer = new ResponseTypeMapper(model, modelName); |
| 78 | + if (!analyzer.isArrayType()) { |
| 79 | + Optional<Entry<String, PathItem>> deletePath = analyzer.deletePath(); |
| 80 | + assertNotNull(deletePath); |
| 81 | + assertTrue(deletePath.isPresent()); |
| 82 | + assertNotNull(deletePath.get().getValue().getDELETE()); |
| 83 | + System.out.println("Delete path for " + modelName + " is " + deletePath.get().getKey()); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @ParameterizedTest |
| 88 | + @MethodSource("responseTypesWithPatch") |
| 89 | + void putPath(String modelName) { |
| 90 | + ResponseTypeMapper analyzer = new ResponseTypeMapper(model, modelName); |
| 91 | + if (!analyzer.isArrayType()) { |
| 92 | + Optional<Entry<String, PathItem>> putPath = analyzer.putPath(); |
| 93 | + assertNotNull(putPath); |
| 94 | + assertTrue(putPath.isPresent()); |
| 95 | + assertNotNull(putPath.get().getValue().getPUT()); |
| 96 | + System.out.println("Patch path for " + modelName + " is " + putPath.get().getKey()); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + @ParameterizedTest |
| 101 | + @MethodSource("responseTypesWithCreate") |
| 102 | + void createPath(String modelName) { |
| 103 | + ResponseTypeMapper analyzer = new ResponseTypeMapper(model, modelName); |
| 104 | + if (!analyzer.isArrayType()) { |
| 105 | + Optional<Entry<String, PathItem>> createPath = analyzer.createPath(); |
| 106 | + assertNotNull(createPath); |
| 107 | + assertTrue(createPath.isPresent()); |
| 108 | + assertNotNull(createPath.get().getValue().getPOST()); |
| 109 | + System.out.println("Create path for " + modelName + " is " + createPath.get().getKey()); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private static Stream<String> responseTypesWithGetById() { |
| 114 | + Set<String> noFindById = new HashSet<String>(EXCLUDED_RESPONSES); |
| 115 | + return reader.getResponseTypeOrSchemaNames(e -> !noFindById.contains(e.getKey())); |
| 116 | + } |
| 117 | + |
| 118 | + private static Stream<String> responseTypesWithPatch() { |
| 119 | + Set<String> noPatch = new HashSet<String>(EXCLUDED_RESPONSES); |
| 120 | + return reader.getResponseTypeOrSchemaNames(e -> !noPatch.contains(e.getKey())); |
| 121 | + } |
| 122 | + |
| 123 | + private static Stream<String> responseTypesWithCreate() { |
| 124 | + Set<String> noCreate = new HashSet<String>(EXCLUDED_RESPONSES); |
| 125 | + return reader.getResponseTypeOrSchemaNames(e -> !noCreate.contains(e.getKey())); |
| 126 | + } |
| 127 | + |
| 128 | + private static Stream<String> responseTypesWithDelete() { |
| 129 | + Set<String> noDelete = new HashSet<String>(EXCLUDED_RESPONSES); |
| 130 | + return reader.getResponseTypeOrSchemaNames(e -> !noDelete.contains(e.getKey())); |
| 131 | + } |
| 132 | +} |
0 commit comments