Skip to content

Commit

Permalink
tests and review fixies
Browse files Browse the repository at this point in the history
  • Loading branch information
treblereel authored and Dmitrii Tikhomirov committed Nov 3, 2024
1 parent 715f374 commit 52a50ec
Show file tree
Hide file tree
Showing 16 changed files with 752 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow/pkg/common"
"github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow/pkg/metadata"
"github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow/pkg/specs"
apimetadata "github.com/apache/incubator-kie-tools/packages/sonataflow-operator/api/metadata"
"github.com/apache/incubator-kie-tools/packages/sonataflow-operator/workflowproj"
)
Expand Down Expand Up @@ -122,7 +123,7 @@ func generateManifests(cfg *DeployUndeployCmdConfig) error {
supportFileExtensions := []string{metadata.JSONExtension, metadata.YAMLExtension, metadata.YMLExtension}

fmt.Println("🔍 Looking for specs files...")
minifiedfiles, err := common.NewMinifier(&common.OpenApiMinifierParams{
minifiedfiles, err := specs.NewMinifier(&specs.OpenApiMinifierOpts{
SpecsDir: cfg.SpecsDir,
SubflowsDir: cfg.SubflowsDir,
}).Minify()
Expand Down
46 changes: 46 additions & 0 deletions packages/kn-plugin-workflow/pkg/command/specs/minify_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package specs

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestMinifyCommand(t *testing.T) {
cmd := minifyCommand()

assert.NotNil(t, cmd)

assert.Equal(t, "minify", cmd.Use)

subcommands := cmd.Commands()
assert.NotEmpty(t, subcommands)
assert.Equal(t, 1, len(subcommands))

found := false
for _, c := range subcommands {
if c.Name() == "openapi" {
found = true
break
}
}
assert.True(t, found, "minify subcommand not found")
}
5 changes: 3 additions & 2 deletions packages/kn-plugin-workflow/pkg/command/specs/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package specs
import (
"fmt"
"github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow/pkg/common"
"github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow/pkg/specs"
"github.com/ory/viper"
"github.com/spf13/cobra"
"os"
Expand Down Expand Up @@ -64,7 +65,7 @@ func minifyOpenApi() *cobra.Command {

func runMinifyOpenApi() error {

var cfg = &common.OpenApiMinifierParams{
var cfg = &specs.OpenApiMinifierOpts{
SpecsDir: viper.GetString("specs-dir"),
SubflowsDir: viper.GetString("subflows-dir"),
}
Expand All @@ -85,7 +86,7 @@ func runMinifyOpenApi() error {
}
}

minifier := common.NewMinifier(cfg)
minifier := specs.NewMinifier(cfg)
_, err := minifier.Minify()
return err
}
44 changes: 44 additions & 0 deletions packages/kn-plugin-workflow/pkg/command/specs/openapi_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package specs

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestOpenApiCommand(t *testing.T) {
cmd := minifyOpenApi()

assert.NotNil(t, cmd)

assert.Equal(t, "openapi", cmd.Use)

subcommands := cmd.Commands()
assert.Empty(t, subcommands)

specsDirFlag := cmd.Flags().Lookup("specs-dir")
assert.NotNil(t, specsDirFlag)
assert.Equal(t, "p", specsDirFlag.Shorthand)

subflowsDirFlag := cmd.Flags().Lookup("subflows-dir")
assert.NotNil(t, subflowsDirFlag)
assert.Equal(t, "s", subflowsDirFlag.Shorthand)
}
46 changes: 46 additions & 0 deletions packages/kn-plugin-workflow/pkg/command/specs/specs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package specs

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestSpecsCommand(t *testing.T) {
cmd := SpecsCommand()

assert.NotNil(t, cmd)

assert.Equal(t, "specs", cmd.Use)

subcommands := cmd.Commands()
assert.NotEmpty(t, subcommands)
assert.Equal(t, 1, len(subcommands))

found := false
for _, c := range subcommands {
if c.Name() == "minify" {
found = true
break
}
}
assert.True(t, found, "minify subcommand not found")
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

package common
package specs

import (
"encoding/json"
Expand All @@ -27,6 +27,7 @@ import (
"path/filepath"
"strings"

"github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow/pkg/common"
"github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow/pkg/metadata"
"github.com/apache/incubator-kie-tools/packages/sonataflow-operator/api/v1alpha08"
"github.com/getkin/kin-openapi/openapi3"
Expand All @@ -37,11 +38,11 @@ import (

type OpenApiMinifier struct {
workflows []string
params *OpenApiMinifierParams
params *OpenApiMinifierOpts
operations map[string]sets.Set[string]
}

type OpenApiMinifierParams struct {
type OpenApiMinifierOpts struct {
RootPath string
SpecsDir string
SubflowsDir string
Expand All @@ -54,7 +55,7 @@ var minifiedExtensionsType = []string{metadata.YAMLExtension, metadata.YMLExtens
// k8sFileSizeLimit defines the maximum file size allowed (e.g., Kubernetes ConfigMap size limit is 1MB)
const k8sFileSizeLimit = 3145728 // 3MB

func NewMinifier(params *OpenApiMinifierParams) *OpenApiMinifier {
func NewMinifier(params *OpenApiMinifierOpts) *OpenApiMinifier {
return &OpenApiMinifier{params: params, operations: make(map[string]sets.Set[string]), workflows: []string{}}
}

Expand Down Expand Up @@ -110,7 +111,13 @@ func (m *OpenApiMinifier) processFunction(workflowFile string) error {
for _, function := range workflow.Functions {
if strings.HasPrefix(function.Operation, relativePath) {
trimmedPrefix := strings.TrimPrefix(function.Operation, relativePath+"/")
parts := strings.Split(trimmedPrefix, "#")
if !strings.Contains(trimmedPrefix, "#") {
return fmt.Errorf("Invalid operation format in function: %s", function.Operation)
}
parts := strings.SplitN(trimmedPrefix, "#", 2)
if len(parts) != 2 {
return fmt.Errorf("❌ ERROR: Invalid operation format: %s", function.Operation)
}
apiFileName := parts[0]
operation := parts[1]

Expand Down Expand Up @@ -174,7 +181,7 @@ func (m *OpenApiMinifier) minifySpecsFile(specFileName string, operations sets.S
if err != nil {
return "", fmt.Errorf("❌ ERROR: failed to write minified file of %s : %w", specFile, err)
}
finalSize, err := ValidateSpecsFileSize(minifiedFile)
finalSize, err := validateSpecsFileSize(minifiedFile)
if err != nil {
return "", fmt.Errorf("❌ ERROR: Minification of %s failed: %w", specFile, err)
}
Expand All @@ -189,16 +196,16 @@ func (m *OpenApiMinifier) minifySpecsFile(specFileName string, operations sets.S
}

func (m *OpenApiMinifier) findWorkflowFile() error {
file, err := FindSonataFlowFile(workflowExtensionsType)
file, err := common.FindSonataFlowFile(workflowExtensionsType)
if err != nil {
return err
}
m.workflows = append(m.workflows, file)
return nil
}

func (m *OpenApiMinifier) findSubflowsFiles(cfg *OpenApiMinifierParams) {
files := FindSonataFlowFiles(cfg.SubflowsDir, workflowExtensionsType)
func (m *OpenApiMinifier) findSubflowsFiles(cfg *OpenApiMinifierOpts) {
files := common.FindSonataFlowFiles(cfg.SubflowsDir, workflowExtensionsType)
m.workflows = append(m.workflows, files...)
}

Expand Down Expand Up @@ -246,7 +253,7 @@ func (m *OpenApiMinifier) writeMinifiedFileToDisk(specFile string, doc *openapi3
return minifiedSpecFile, nil
}

func ValidateSpecsFileSize(specFile string) (int64, error) {
func validateSpecsFileSize(specFile string) (int64, error) {
file, err := os.Stat(specFile)
if err != nil {
return -1, fmt.Errorf("❌ ERROR: failed to get file info: %w", err)
Expand Down
Loading

0 comments on commit 52a50ec

Please sign in to comment.