github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow@v0.28.1-0.20240311201729-34c6856b157f/e2e-tests/quarkus_convert_test.go (about)

     1  //go:build e2e_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 e2e_tests
    23  
    24  import (
    25  	"fmt"
    26  	"os"
    27  	"path/filepath"
    28  	"testing"
    29  
    30  	"github.com/stretchr/testify/require"
    31  
    32  	"github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow/pkg/command"
    33  	"github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow/pkg/command/quarkus"
    34  	"github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow/pkg/common"
    35  	"github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow/pkg/metadata"
    36  )
    37  
    38  var cfgTestInputCreateConvert = CfgTestInputCreate{
    39  	input: command.CreateCmdConfig{},
    40  }
    41  
    42  var cfgTestInputQuarkusCreateConvert = CfgTestInputQuarkusCreate{
    43  	input: quarkus.CreateQuarkusProjectConfig{},
    44  }
    45  
    46  type CfgTestInputQuarkusConvert struct {
    47  	input quarkus.CreateQuarkusProjectConfig
    48  }
    49  
    50  var cfgTestInputQuarkusConvert_Success = []CfgTestInputQuarkusConvert{
    51  	{input: quarkus.CreateQuarkusProjectConfig{}},
    52  	{input: quarkus.CreateQuarkusProjectConfig{
    53  		Extensions: "quarkus-jsonp,quarkus-smallrye-openapi",
    54  	}},
    55  	{input: quarkus.CreateQuarkusProjectConfig{
    56  		Extensions: "quarkus-jsonp,quarkus-smallrye-openapi",
    57  		DependenciesVersion: metadata.DependenciesVersion{
    58  			QuarkusPlatformGroupId: "io.quarkus.platform",
    59  			QuarkusVersion:         "3.2.9.Final",
    60  		},
    61  	}},
    62  }
    63  
    64  var cfgTestInputQuarkusConvert_Failed = []CfgTestInputQuarkusConvert{
    65  	{input: quarkus.CreateQuarkusProjectConfig{
    66  		Extensions: "nonexistent-extension",
    67  	}},
    68  }
    69  
    70  var cfgTestInputQuarkusConvert_FailedAlreadyQuarkus = []CfgTestInputQuarkusConvert{
    71  	{input: quarkus.CreateQuarkusProjectConfig{}},
    72  }
    73  
    74  func transformQuarkusConvertCmdCfgToArgs(t *testing.T, cfg quarkus.CreateQuarkusProjectConfig) []string {
    75  	args := []string{"convert"}
    76  	require.Empty(t, cfg.ProjectName, "The project name can not be set in the test of `quarkus convert`.")
    77  	if cfg.Extensions != "" {
    78  		args = append(args, "--extension", cfg.Extensions)
    79  	}
    80  	if cfg.DependenciesVersion.QuarkusPlatformGroupId != "" {
    81  		args = append(args, "--quarkus-platform-group-id", cfg.DependenciesVersion.QuarkusPlatformGroupId)
    82  	}
    83  	if cfg.DependenciesVersion.QuarkusVersion != "" {
    84  		args = append(args, "--quarkus-version", cfg.DependenciesVersion.QuarkusVersion)
    85  	}
    86  	return args
    87  }
    88  
    89  func TestQuarkusConvertProjectSuccess(t *testing.T) {
    90  	for testIndex, test := range cfgTestInputQuarkusConvert_Success {
    91  		t.Run(fmt.Sprintf("Test quarkus convert project success index: %d", testIndex), func(t *testing.T) {
    92  			defer CleanUpAndChdirTemp(t)
    93  			RunQuarkusConvertTest(t, cfgTestInputCreateConvert, test)
    94  		})
    95  	}
    96  }
    97  
    98  func RunQuarkusConvertTest(t *testing.T, cfgTestInputCreateConvert CfgTestInputCreate, test CfgTestInputQuarkusConvert) string {
    99  	var err error
   100  
   101  	projectName := GetCreateProjectName(t, cfgTestInputCreateConvert)
   102  	projectDir := filepath.Join(TempTestsPath, projectName)
   103  
   104  	// Create the project
   105  	RunCreateTest(t, cfgTestInputCreateConvert)
   106  
   107  	err = os.Chdir(projectDir)
   108  	require.NoErrorf(t, err, "Expected nil error, got %v", err)
   109  
   110  	// Run `quarkus convert` command
   111  	_, err = ExecuteKnWorkflowQuarkus(transformQuarkusConvertCmdCfgToArgs(t, test.input)...)
   112  	require.NoErrorf(t, err, "Expected nil error, got %v", err)
   113  
   114  	// Check if the expected directories and files are present
   115  	expectedDirectories := []string{
   116  		"src/main/java",
   117  		"src/main/resources",
   118  		"src/main/docker",
   119  		"src/main",
   120  		"src",
   121  		".mvn/wrapper",
   122  		".mvn",
   123  	}
   124  	VerifyDirectoriesExist(t, projectDir, expectedDirectories)
   125  	expectedFiles := []string{
   126  		"src/main/resources/application.properties",
   127  		"src/main/resources/workflow.sw.json",
   128  		"src/main/docker/Dockerfile.legacy-jar",
   129  		"src/main/docker/Dockerfile.jvm",
   130  		"src/main/docker/Dockerfile.native",
   131  		"src/main/docker/Dockerfile.native-micro",
   132  		".mvn/wrapper/.gitignore",
   133  		".mvn/wrapper/MavenWrapperDownloader.java",
   134  		".mvn/wrapper/maven-wrapper.properties",
   135  		".gitignore",
   136  		"pom.xml",
   137  		"README.md",
   138  		".dockerignore",
   139  		"mvnw.cmd",
   140  		"mvnw",
   141  	}
   142  	VerifyFilesExist(t, projectDir, expectedFiles)
   143  
   144  	// Verify the content of the file `workflow.sw.json`
   145  	workflowFilePath := filepath.Join(projectDir, "src/main/resources/workflow.sw.json")
   146  	workflowFileData, err := common.GetWorkflowTemplate(false)
   147  	require.NoErrorf(t, err, "Error reading workflow template: %v", err)
   148  	expectedFileContent := string(workflowFileData)
   149  	VerifyFileContent(t, workflowFilePath, expectedFileContent)
   150  
   151  	return projectName
   152  }
   153  
   154  func TestQuarkusConvertProjectFailed(t *testing.T) {
   155  	for testIndex, test := range cfgTestInputQuarkusConvert_Failed {
   156  		t.Run(fmt.Sprintf("Test quarkus convert project fail index: %d", testIndex), func(t *testing.T) {
   157  			defer CleanUpAndChdirTemp(t)
   158  
   159  			var err error
   160  			projectName := GetCreateProjectName(t, cfgTestInputCreateConvert)
   161  			projectDir := filepath.Join(TempTestsPath, projectName)
   162  
   163  			// Create the project
   164  			RunCreateTest(t, cfgTestInputCreateConvert)
   165  
   166  			err = os.Chdir(projectDir)
   167  			require.NoErrorf(t, err, "Expected nil error, got %v", err)
   168  
   169  			// Run `quarkus convert` command
   170  			_, err = ExecuteKnWorkflowQuarkus(transformQuarkusConvertCmdCfgToArgs(t, test.input)...)
   171  			require.Errorf(t, err, "Expected error, got nil")
   172  
   173  			common.DeleteFolderStructure(t, projectDir)
   174  		})
   175  	}
   176  }
   177  
   178  func TestQuarkusConvertProjectFailedAlreadyQuarkus(t *testing.T) {
   179  	for testIndex, test := range cfgTestInputQuarkusConvert_Failed {
   180  		t.Run(fmt.Sprintf("Test quarkus convert project fail already quarkus index: %d", testIndex), func(t *testing.T) {
   181  			defer CleanUpAndChdirTemp(t)
   182  
   183  			var err error
   184  			projectName := GetQuarkusCreateProjectName(t, cfgTestInputQuarkusCreateConvert)
   185  			projectDir := filepath.Join(TempTestsPath, projectName)
   186  
   187  			// Create the project
   188  			RunQuarkusCreateTest(t, cfgTestInputQuarkusCreateConvert)
   189  
   190  			err = os.Chdir(projectDir)
   191  			require.NoErrorf(t, err, "Expected nil error, got %v", err)
   192  
   193  			// Run `quarkus convert` command
   194  			_, err = ExecuteKnWorkflowQuarkus(transformQuarkusConvertCmdCfgToArgs(t, test.input)...)
   195  			require.Errorf(t, err, "Expected error, got nil")
   196  		})
   197  	}
   198  }