2 // ========================LICENSE_START=================================
5 // Copyright (C) 2021: Nordix Foundation
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 // ========================LICENSE_END===================================
28 "github.com/stretchr/testify/require"
31 const type1Schema = `{"title": "Type 1"}`
33 func TestGetTypes_filesOkShouldReturnSliceOfTypesAndProvideSupportedTypes(t *testing.T) {
34 assertions := require.New(t)
35 typesDir, err := os.MkdirTemp("", "configs")
37 t.Errorf("Unable to create temporary directory for types due to: %v", err)
40 os.RemoveAll(typesDir)
44 fname := filepath.Join(typesDir, "type1.json")
45 if err = os.WriteFile(fname, []byte(type1Schema), 0666); err != nil {
46 t.Errorf("Unable to create temporary files for types due to: %v", err)
48 types, err := GetTypes()
53 wantedTypes := []*Type{&wantedType}
54 assertions.EqualValues(wantedTypes, types)
57 supportedTypes := GetSupportedTypes()
58 assertions.EqualValues([]string{"type1"}, supportedTypes)
61 func TestAddJobWhenTypeIsSupported_shouldAddJobToAllJobsMap(t *testing.T) {
62 assertions := require.New(t)
63 allJobs["type1"] = make(map[string]JobInfo)
70 InfoJobIdentity: "job1",
73 InfoTypeIdentity: "type1",
76 err := AddJob(jobInfo)
78 assertions.Equal(1, len(allJobs["type1"]))
79 assertions.Equal(jobInfo, allJobs["type1"]["job1"])
82 func TestAddJobWhenTypeIsNotSupported_shouldReturnError(t *testing.T) {
83 assertions := require.New(t)
85 InfoTypeIdentity: "type1",
88 err := AddJob(jobInfo)
89 assertions.NotNil(err)
90 assertions.Equal("type not supported: type1", err.Error())
93 func TestAddJobWhenJobIdMissing_shouldReturnError(t *testing.T) {
94 assertions := require.New(t)
95 allJobs["type1"] = make(map[string]JobInfo)
100 InfoTypeIdentity: "type1",
103 err := AddJob(jobInfo)
104 assertions.NotNil(err)
105 assertions.Equal("missing required job identity: { type1}", err.Error())
108 func TestAddJobWhenTargetUriMissing_shouldReturnError(t *testing.T) {
109 assertions := require.New(t)
110 allJobs["type1"] = make(map[string]JobInfo)
112 InfoTypeIdentity: "type1",
113 InfoJobIdentity: "job1",
116 err := AddJob(jobInfo)
117 assertions.NotNil(err)
118 assertions.Equal("missing required target URI: { job1 type1}", err.Error())