|
| 1 | +/* |
| 2 | + * Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech) |
| 3 | + * Copyright 2018 SmartBear Software |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.openapitools.codegen.goserver; |
| 19 | + |
| 20 | +import org.openapitools.codegen.DefaultGenerator; |
| 21 | +import org.openapitools.codegen.TestUtils; |
| 22 | +import org.openapitools.codegen.config.CodegenConfigurator; |
| 23 | +import org.testng.Assert; |
| 24 | +import org.testng.annotations.Test; |
| 25 | + |
| 26 | +import java.io.File; |
| 27 | +import java.io.IOException; |
| 28 | +import java.nio.file.Files; |
| 29 | +import java.nio.file.Paths; |
| 30 | +import java.util.List; |
| 31 | + |
| 32 | +public class GoServerCodegenTest { |
| 33 | + |
| 34 | + @Test |
| 35 | + public void verifyGoMod() throws IOException { |
| 36 | + File output = Files.createTempDirectory("test").toFile(); |
| 37 | + output.deleteOnExit(); |
| 38 | + |
| 39 | + final CodegenConfigurator configurator = createDefaultCodegenConfigurator(output) |
| 40 | + .setInputSpec("src/test/resources/3_0/go-server/route-order.yaml"); |
| 41 | + |
| 42 | + DefaultGenerator generator = new DefaultGenerator(); |
| 43 | + List<File> files = generator.opts(configurator.toClientOptInput()).generate(); |
| 44 | + files.forEach(File::deleteOnExit); |
| 45 | + |
| 46 | + TestUtils.assertFileExists(Paths.get(output + "/go.mod")); |
| 47 | + TestUtils.assertFileContains(Paths.get(output + "/go.mod"), |
| 48 | + "module github.com/my-user/my-repo"); |
| 49 | + TestUtils.assertFileContains(Paths.get(output + "/go.mod"), |
| 50 | + "require github.com/gorilla/mux v1.8.0"); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void verifyOrder() throws IOException { |
| 55 | + File output = Files.createTempDirectory("test").toFile(); |
| 56 | + output.deleteOnExit(); |
| 57 | + |
| 58 | + final CodegenConfigurator configurator = createDefaultCodegenConfigurator(output) |
| 59 | + .setInputSpec("src/test/resources/3_0/go-server/route-order.yaml"); |
| 60 | + |
| 61 | + DefaultGenerator generator = new DefaultGenerator(); |
| 62 | + List<File> files = generator.opts(configurator.toClientOptInput()).generate(); |
| 63 | + files.forEach(File::deleteOnExit); |
| 64 | + |
| 65 | + TestUtils.assertFileExists(Paths.get(output + "/go/routers.go")); |
| 66 | + TestUtils.assertFileContains(Paths.get(output + "/go/routers.go"), |
| 67 | + "type Routes map[string]Route"); |
| 68 | + |
| 69 | + TestUtils.assertFileExists(Paths.get(output + "/go/api_dev.go")); |
| 70 | + // verify /getPath/latest is first route |
| 71 | + Assert.assertEquals(Files.readAllLines(Paths.get(output + "/go/api_dev.go")).get(52), "\t\t\"GetLatest\": Route{"); |
| 72 | + // verify /getPath/{id} is second route |
| 73 | + Assert.assertEquals(Files.readAllLines(Paths.get(output + "/go/api_dev.go")).get(57), "\t\t\"GetById\": Route{"); |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | + private static CodegenConfigurator createDefaultCodegenConfigurator(File output) { |
| 78 | + return new CodegenConfigurator() |
| 79 | + .setGeneratorName("go-server") |
| 80 | + .setGitUserId("my-user") |
| 81 | + .setGitRepoId("my-repo") |
| 82 | + .setPackageName("mypackage") |
| 83 | + .setOutputDir(output.getAbsolutePath().replace("\\", "/")); |
| 84 | + } |
| 85 | + |
| 86 | +} |
0 commit comments