|
| 1 | +package io.swagger.codegen; |
| 2 | + |
| 3 | +import io.swagger.models.Operation; |
| 4 | +import io.swagger.models.Swagger; |
| 5 | +import io.swagger.models.properties.Property; |
| 6 | +import io.swagger.parser.SwaggerParser; |
| 7 | + |
| 8 | +import org.testng.Assert; |
| 9 | +import org.testng.annotations.Test; |
| 10 | + |
| 11 | +import java.util.List; |
| 12 | + |
| 13 | +public class CodegenTest { |
| 14 | + |
| 15 | + @Test(description = "read a file upload param from a 2.0 spec") |
| 16 | + public void fileUploadParamTest() { |
| 17 | + final Swagger model = new SwaggerParser().read("src/test/resources/2_0/petstore.json"); |
| 18 | + final DefaultCodegen codegen = new DefaultCodegen(); |
| 19 | + final String path = "/pet/{petId}/uploadImage"; |
| 20 | + final Operation p = model.getPaths().get(path).getPost(); |
| 21 | + final CodegenOperation op = codegen.fromOperation(path, "post", p, model.getDefinitions()); |
| 22 | + |
| 23 | + Assert.assertEquals(op.operationId, "uploadFile"); |
| 24 | + Assert.assertEquals(op.httpMethod, "POST"); |
| 25 | + Assert.assertTrue(op.hasConsumes); |
| 26 | + Assert.assertEquals(op.consumes.size(), 1); |
| 27 | + Assert.assertEquals(op.consumes.get(0).get("mediaType"), "multipart/form-data"); |
| 28 | + Assert.assertTrue(op.hasProduces); |
| 29 | + Assert.assertEquals(op.allParams.size(), 3); |
| 30 | + Assert.assertEquals(op.formParams.size(), 2); |
| 31 | + |
| 32 | + final CodegenParameter file = op.formParams.get(1); |
| 33 | + Assert.assertTrue(file.isFormParam); |
| 34 | + Assert.assertEquals(file.dataType, "file"); |
| 35 | + Assert.assertNull(file.required); |
| 36 | + Assert.assertTrue(file.isFile); |
| 37 | + Assert.assertNull(file.hasMore); |
| 38 | + } |
| 39 | + |
| 40 | + @Test(description = "read formParam values from a 2.0 spec") |
| 41 | + public void formParamTest() { |
| 42 | + final Swagger model = new SwaggerParser().read("src/test/resources/2_0/petstore.json"); |
| 43 | + final DefaultCodegen codegen = new DefaultCodegen(); |
| 44 | + final String path = "/pet/{petId}"; |
| 45 | + final Operation p = model.getPaths().get(path).getPost(); |
| 46 | + final CodegenOperation op = codegen.fromOperation(path, "post", p, model.getDefinitions()); |
| 47 | + |
| 48 | + Assert.assertEquals(op.operationId, "updatePetWithForm"); |
| 49 | + Assert.assertEquals(op.httpMethod, "POST"); |
| 50 | + Assert.assertTrue(op.hasConsumes); |
| 51 | + Assert.assertEquals(op.consumes.size(), 1); |
| 52 | + Assert.assertEquals(op.consumes.get(0).get("mediaType"), "application/x-www-form-urlencoded"); |
| 53 | + Assert.assertTrue(op.hasProduces); |
| 54 | + Assert.assertEquals(op.produces.size(), 2); |
| 55 | + Assert.assertEquals(op.produces.get(0).get("mediaType"), "application/json"); |
| 56 | + Assert.assertEquals(op.produces.get(0).get("hasMore"), "true"); |
| 57 | + Assert.assertEquals(op.produces.get(1).get("mediaType"), "application/xml"); |
| 58 | + Assert.assertEquals(op.pathParams.size(), 1); |
| 59 | + |
| 60 | + final CodegenParameter idParam = op.pathParams.get(0); |
| 61 | + Assert.assertTrue(idParam.isPathParam); |
| 62 | + Assert.assertEquals(idParam.dataType, "String"); |
| 63 | + Assert.assertTrue(idParam.required); |
| 64 | + Assert.assertNull(idParam.hasMore); |
| 65 | + |
| 66 | + Assert.assertEquals(op.allParams.size(), 3); |
| 67 | + Assert.assertEquals(op.formParams.size(), 2); |
| 68 | + |
| 69 | + final CodegenParameter nameParam = op.formParams.get(0); |
| 70 | + Assert.assertTrue(nameParam.isFormParam); |
| 71 | + Assert.assertTrue(nameParam.notFile); |
| 72 | + Assert.assertEquals(nameParam.dataType, "String"); |
| 73 | + Assert.assertNull(nameParam.required); |
| 74 | + Assert.assertTrue(nameParam.hasMore); |
| 75 | + |
| 76 | + final CodegenParameter statusParam = op.formParams.get(1); |
| 77 | + Assert.assertTrue(statusParam.isFormParam); |
| 78 | + Assert.assertTrue(statusParam.notFile); |
| 79 | + Assert.assertEquals(statusParam.dataType, "String"); |
| 80 | + Assert.assertNull(statusParam.required); |
| 81 | + Assert.assertNull(statusParam.hasMore); |
| 82 | + } |
| 83 | + |
| 84 | + @Test(description = "handle required parameters from a 2.0 spec as required when figuring out Swagger types") |
| 85 | + public void requiredParametersTest() { |
| 86 | + final Swagger model = new SwaggerParser().read("src/test/resources/2_0/requiredTest.json"); |
| 87 | + |
| 88 | + final DefaultCodegen codegen = new DefaultCodegen() { |
| 89 | + public String getSwaggerType(Property p) { |
| 90 | + if (p != null && !p.getRequired()) { |
| 91 | + return "Optional<" + super.getSwaggerType(p) + ">"; |
| 92 | + } |
| 93 | + return super.getSwaggerType(p); |
| 94 | + } |
| 95 | + }; |
| 96 | + final String path = "/tests/requiredParams"; |
| 97 | + final Operation p = model.getPaths().get(path).getGet(); |
| 98 | + final CodegenOperation op = codegen.fromOperation(path, "get", p, model.getDefinitions()); |
| 99 | + |
| 100 | + final List<CodegenParameter> formParams = op.formParams; |
| 101 | + Assert.assertEquals(formParams.size(), 2); |
| 102 | + Assert.assertEquals(formParams.get(0).dataType, "Long"); |
| 103 | + Assert.assertEquals(formParams.get(1).dataType, "Optional<string>"); |
| 104 | + Assert.assertEquals(op.returnType, "Long"); |
| 105 | + } |
| 106 | + |
| 107 | + @Test(description = "select main response from a 2.0 spec using the lowest 2XX code") |
| 108 | + public void responseSelectionTest1() { |
| 109 | + final Swagger model = new SwaggerParser().read("src/test/resources/2_0/responseSelectionTest.json"); |
| 110 | + final DefaultCodegen codegen = new DefaultCodegen(); |
| 111 | + final String path = "/tests/withTwoHundredAndDefault"; |
| 112 | + final Operation p = model.getPaths().get(path).getGet(); |
| 113 | + final CodegenOperation op = codegen.fromOperation(path, "get", p, model.getDefinitions()); |
| 114 | + |
| 115 | + Assert.assertEquals(op.returnType, "String"); |
| 116 | + } |
| 117 | + |
| 118 | + @Test(description = "select main response from a 2.0 spec using the default keyword when no 2XX code") |
| 119 | + public void responseSelectionTest2() { |
| 120 | + final Swagger model = new SwaggerParser().read("src/test/resources/2_0/responseSelectionTest.json"); |
| 121 | + final DefaultCodegen codegen = new DefaultCodegen(); |
| 122 | + final String path = "/tests/withoutTwoHundredButDefault"; |
| 123 | + final Operation p = model.getPaths().get(path).getGet(); |
| 124 | + final CodegenOperation op = codegen.fromOperation(path, "get", p, model.getDefinitions()); |
| 125 | + |
| 126 | + Assert.assertEquals(op.returnType, "String"); |
| 127 | + } |
| 128 | + |
| 129 | + @Test(description = "return byte array when response format is byte") |
| 130 | + public void binaryDataTest() { |
| 131 | + final Swagger model = new SwaggerParser().read("src/test/resources/2_0/binaryDataTest.json"); |
| 132 | + final DefaultCodegen codegen = new DefaultCodegen(); |
| 133 | + final String path = "/tests/binaryResponse"; |
| 134 | + final Operation p = model.getPaths().get(path).getPost(); |
| 135 | + final CodegenOperation op = codegen.fromOperation(path, "post", p, model.getDefinitions()); |
| 136 | + |
| 137 | + Assert.assertEquals(op.returnType, "byte[]"); |
| 138 | + Assert.assertEquals(op.bodyParam.dataType, "byte[]"); |
| 139 | + Assert.assertTrue(op.bodyParam.isBinary); |
| 140 | + Assert.assertTrue(op.responses.get(0).isBinary); |
| 141 | + } |
| 142 | +} |
0 commit comments