github.com/kiegroup/kie-tools/packages/kn-plugin-workflow@v0.32.0/it-tests/quarkus_create_test.go (about) 1 //go:build it_tests 2 3 /* 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22 package it_tests 23 24 import ( 25 "fmt" 26 "path/filepath" 27 "testing" 28 29 "github.com/stretchr/testify/require" 30 31 "github.com/kiegroup/kie-tools/packages/kn-plugin-workflow/pkg/command/quarkus" 32 "github.com/kiegroup/kie-tools/packages/kn-plugin-workflow/pkg/common" 33 "github.com/kiegroup/kie-tools/packages/kn-plugin-workflow/pkg/metadata" 34 ) 35 36 type CfgTestInputQuarkusCreate struct { 37 input quarkus.CreateQuarkusProjectConfig 38 } 39 40 var cfgTestInputQuarkusCreate_Success = []CfgTestInputQuarkusCreate{ 41 {input: quarkus.CreateQuarkusProjectConfig{}}, 42 {input: quarkus.CreateQuarkusProjectConfig{ 43 ProjectName: "my-project", 44 }}, 45 {input: quarkus.CreateQuarkusProjectConfig{ 46 Extensions: "quarkus-jsonp,quarkus-smallrye-openapi", 47 }}, 48 {input: quarkus.CreateQuarkusProjectConfig{ 49 ProjectName: "serverless-workflow-hello-world", 50 Extensions: "quarkus-jsonp,quarkus-smallrye-openapi", 51 DependenciesVersion: metadata.DependenciesVersion{ 52 QuarkusPlatformGroupId: "io.quarkus.platform", 53 QuarkusVersion: "2.16.6.Final", 54 }, 55 }}, 56 } 57 58 var cfgTestInputQuarkusCreate_Fail = []CfgTestInputQuarkusCreate{ 59 {input: quarkus.CreateQuarkusProjectConfig{ 60 ProjectName: "wrong/project-name", 61 }}, 62 {input: quarkus.CreateQuarkusProjectConfig{ 63 Extensions: "nonexistent-extension", 64 }}, 65 } 66 67 func transformQuarkusCreateCmdCfgToArgs(cfg quarkus.CreateQuarkusProjectConfig) []string { 68 args := []string{"create"} 69 if cfg.ProjectName != "" { 70 args = append(args, "--name", cfg.ProjectName) 71 } 72 if cfg.Extensions != "" { 73 args = append(args, "--extension", cfg.Extensions) 74 } 75 if cfg.DependenciesVersion.QuarkusPlatformGroupId != "" { 76 args = append(args, "--quarkus-platform-group-id", cfg.DependenciesVersion.QuarkusPlatformGroupId) 77 } 78 if cfg.DependenciesVersion.QuarkusVersion != "" { 79 args = append(args, "--quarkus-version", cfg.DependenciesVersion.QuarkusVersion) 80 } 81 return args 82 } 83 84 func GetQuarkusCreateProjectName(t *testing.T, config CfgTestInputQuarkusCreate) string { 85 if config.input.ProjectName != "" { 86 return config.input.ProjectName 87 } else { 88 projectDefaultName, err := LookupFlagDefaultValue("name", quarkus.NewCreateCommand()) 89 require.NoErrorf(t, err, "Error: %v", err) 90 return projectDefaultName 91 } 92 } 93 94 func TestQuarkusCreateProjectSuccess(t *testing.T) { 95 for testIndex, test := range cfgTestInputQuarkusCreate_Success { 96 t.Run(fmt.Sprintf("Test quarkus create project success index: %d", testIndex), func(t *testing.T) { 97 defer CleanUpAndChdirTemp(t) 98 RunQuarkusCreateTest(t, test) 99 }) 100 } 101 } 102 103 func RunQuarkusCreateTest(t *testing.T, test CfgTestInputQuarkusCreate) string { 104 var err error 105 106 projectName := GetQuarkusCreateProjectName(t, test) 107 projectDir := filepath.Join(TempTestsPath, projectName) 108 109 // Run `quarkus create` command 110 _, err = ExecuteKnWorkflowQuarkus(transformQuarkusCreateCmdCfgToArgs(test.input)...) 111 require.NoErrorf(t, err, "Expected nil error, got: %v", err) 112 113 // Check if the project directory was created 114 require.DirExistsf(t, projectDir, "Expected project directory '%s' to be created", projectDir) 115 116 // Check if the expected directories and files are present 117 expectedDirectories := []string{ 118 "src/main/java", 119 "src/main/resources", 120 "src/main/docker", 121 "src/main", 122 "src", 123 ".mvn/wrapper", 124 ".mvn", 125 } 126 VerifyDirectoriesExist(t, projectDir, expectedDirectories) 127 expectedFiles := []string{ 128 "src/main/resources/application.properties", 129 "src/main/resources/workflow.sw.json", 130 "src/main/docker/Dockerfile.legacy-jar", 131 "src/main/docker/Dockerfile.jvm", 132 "src/main/docker/Dockerfile.native", 133 "src/main/docker/Dockerfile.native-micro", 134 ".mvn/wrapper/.gitignore", 135 ".mvn/wrapper/MavenWrapperDownloader.java", 136 ".mvn/wrapper/maven-wrapper.properties", 137 ".gitignore", 138 "pom.xml", 139 "README.md", 140 ".dockerignore", 141 "mvnw.cmd", 142 "mvnw", 143 } 144 VerifyFilesExist(t, projectDir, expectedFiles) 145 146 // Verify the content of the file `workflow.sw.json` 147 workflowFilePath := filepath.Join(projectDir, "src/main/resources/workflow.sw.json") 148 workflowFileData, err := common.GetWorkflowTemplate() 149 require.NoErrorf(t, err, "Error reading workflow template: %v", err) 150 expectedFileContent := string(workflowFileData) 151 VerifyFileContent(t, workflowFilePath, expectedFileContent) 152 153 return projectName 154 } 155 156 func TestQuarkusCreateProjectFail(t *testing.T) { 157 for testIndex, test := range cfgTestInputQuarkusCreate_Fail { 158 t.Run(fmt.Sprintf("Test quarkus create project fail index: %d", testIndex), func(t *testing.T) { 159 defer CleanUpAndChdirTemp(t) 160 161 projectName := GetQuarkusCreateProjectName(t, test) 162 projectDir := filepath.Join(TempTestsPath, projectName) 163 164 // Run `quarkus create` command 165 _, err := ExecuteKnWorkflowQuarkus(transformQuarkusCreateCmdCfgToArgs(test.input)...) 166 require.Errorf(t, err, "Expected error, got nil") 167 168 // Check if the project directory was not created 169 require.NoDirExistsf(t, projectDir, "Expected project directory '%s' not to be created", projectDir) 170 }) 171 } 172 }