github.com/SAP/jenkins-library@v1.362.0/cmd/jsonApplyPatch_test.go (about)

     1  //go:build unit
     2  // +build unit
     3  
     4  package cmd
     5  
     6  import (
     7  	"github.com/SAP/jenkins-library/pkg/mock"
     8  	"github.com/stretchr/testify/assert"
     9  	"testing"
    10  )
    11  
    12  var schema = []byte(`
    13  {
    14      "$schema": "http://json-schema.org/draft-07/schema#",
    15      "title": "SAP Cloud SDK pipeline_config JSON schema",
    16      "type": "object",
    17      "properties": {
    18          "general": {
    19              "type": [
    20                  "object",
    21                  "null"
    22              ],
    23              "properties": {
    24                  "productiveBranch": {
    25                      "type": "string",
    26                      "default": "master"
    27                  }
    28              }
    29          }
    30      }
    31  }
    32  `)
    33  
    34  var patch = []byte(`
    35  [
    36      {
    37          "op": "add",
    38          "path": "/properties/general/properties/gitCredentialsId",
    39          "value": {
    40              "type": "string"
    41          }
    42      }
    43  ]
    44  `)
    45  
    46  var patchedSchema = []byte(`{
    47      "$schema": "http://json-schema.org/draft-07/schema#",
    48      "title": "SAP Cloud SDK pipeline_config JSON schema",
    49      "type": "object",
    50      "properties": {
    51          "general": {
    52              "type": [
    53                  "object",
    54                  "null"
    55              ],
    56              "properties": {
    57                  "productiveBranch": {
    58                      "type": "string",
    59                      "default": "master"
    60                  },
    61  		"gitCredentialsId": {
    62                      "type": "string"
    63                  }
    64              }
    65          }
    66      }
    67  }`)
    68  
    69  func TestSchemaPatch(t *testing.T) {
    70  	t.Run("default", func(t *testing.T) {
    71  		options := jsonApplyPatchOptions{
    72  			Input:  "schema.json",
    73  			Patch:  "patch.json",
    74  			Output: "output.json",
    75  		}
    76  		filesMock := mock.FilesMock{}
    77  		filesMock.AddFile("schema.json", schema)
    78  		filesMock.AddFile("patch.json", patch)
    79  		err := runJsonApplyPatch(&options, &filesMock)
    80  		assert.NoError(t, err)
    81  		patchedSchemaResult, err := filesMock.FileRead("output.json")
    82  		assert.NoError(t, err)
    83  		assert.JSONEq(t, string(patchedSchema), string(patchedSchemaResult))
    84  	})
    85  
    86  	t.Run("file does not exist", func(t *testing.T) {
    87  		options := jsonApplyPatchOptions{
    88  			Input:  "schema.json",
    89  			Patch:  "patch.json",
    90  			Output: "output.json",
    91  		}
    92  		filesMock := mock.FilesMock{}
    93  		err := runJsonApplyPatch(&options, &filesMock)
    94  		assert.Error(t, err)
    95  	})
    96  }