blob: 0941033897d2b0893cfa79771d24363863d59e4c [file] [log] [blame]
elinuxhenrikcce95ff2021-09-05 17:27:02 +02001// -
2// ========================LICENSE_START=================================
3// O-RAN-SC
4// %%
5// Copyright (C) 2021: Nordix Foundation
6// %%
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
10//
11// http://www.apache.org/licenses/LICENSE-2.0
12//
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===================================
19//
20
elinuxhenrik63a42ca2021-09-06 22:16:24 +020021package jobs
elinuxhenrikcce95ff2021-09-05 17:27:02 +020022
23import (
24 "os"
25 "path/filepath"
26 "testing"
27
28 "github.com/stretchr/testify/require"
29)
30
31const type1Schema = `{"title": "Type 1"}`
32
elinuxhenrika77cd652021-09-06 10:56:21 +020033func TestGetTypes_filesOkShouldReturnSliceOfTypesAndProvideSupportedTypes(t *testing.T) {
elinuxhenrikcce95ff2021-09-05 17:27:02 +020034 assertions := require.New(t)
35 typesDir, err := os.MkdirTemp("", "configs")
36 if err != nil {
37 t.Errorf("Unable to create temporary directory for types due to: %v", err)
38 }
elinuxhenrikb1fb1d82021-09-07 02:58:52 +020039 t.Cleanup(func() {
40 os.RemoveAll(typesDir)
41 clearAll()
42 })
elinuxhenrikcce95ff2021-09-05 17:27:02 +020043 typeDir = 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)
47 }
48 types, err := GetTypes()
49 wantedType := Type{
elinuxhenrika77cd652021-09-06 10:56:21 +020050 TypeId: "type1",
elinuxhenrikcce95ff2021-09-05 17:27:02 +020051 Schema: type1Schema,
52 }
53 wantedTypes := []*Type{&wantedType}
54 assertions.EqualValues(wantedTypes, types)
55 assertions.Nil(err)
elinuxhenrika77cd652021-09-06 10:56:21 +020056
57 supportedTypes := GetSupportedTypes()
58 assertions.EqualValues([]string{"type1"}, supportedTypes)
elinuxhenrikcce95ff2021-09-05 17:27:02 +020059}
elinuxhenrik63a42ca2021-09-06 22:16:24 +020060
elinuxhenrikb1fb1d82021-09-07 02:58:52 +020061func TestAddJobWhenTypeIsSupported_shouldAddJobToAllJobsMap(t *testing.T) {
elinuxhenrik63a42ca2021-09-06 22:16:24 +020062 assertions := require.New(t)
63 allJobs["type1"] = make(map[string]JobInfo)
elinuxhenrikb1fb1d82021-09-07 02:58:52 +020064 t.Cleanup(func() {
65 clearAll()
66 })
elinuxhenrik63a42ca2021-09-06 22:16:24 +020067 jobInfo := JobInfo{
68 Owner: "owner",
69 LastUpdated: "now",
70 InfoJobIdentity: "job1",
71 TargetUri: "target",
72 InfoJobData: "{}",
73 InfoTypeIdentity: "type1",
74 }
75
76 err := AddJob(jobInfo)
77 assertions.Nil(err)
78 assertions.Equal(1, len(allJobs["type1"]))
elinuxhenrikb1fb1d82021-09-07 02:58:52 +020079 assertions.Equal(jobInfo, allJobs["type1"]["job1"])
80}
81
82func TestAddJobWhenTypeIsNotSupported_shouldReturnError(t *testing.T) {
83 assertions := require.New(t)
84 jobInfo := JobInfo{
85 InfoTypeIdentity: "type1",
86 }
87
88 err := AddJob(jobInfo)
89 assertions.NotNil(err)
90 assertions.Equal("type not supported: type1", err.Error())
91}
92
93func TestAddJobWhenJobIdMissing_shouldReturnError(t *testing.T) {
94 assertions := require.New(t)
95 allJobs["type1"] = make(map[string]JobInfo)
96 t.Cleanup(func() {
97 clearAll()
98 })
99 jobInfo := JobInfo{
100 InfoTypeIdentity: "type1",
101 }
102
103 err := AddJob(jobInfo)
104 assertions.NotNil(err)
105 assertions.Equal("missing required job identity: { type1}", err.Error())
106}
107
108func TestAddJobWhenTargetUriMissing_shouldReturnError(t *testing.T) {
109 assertions := require.New(t)
110 allJobs["type1"] = make(map[string]JobInfo)
111 jobInfo := JobInfo{
112 InfoTypeIdentity: "type1",
113 InfoJobIdentity: "job1",
114 }
115
116 err := AddJob(jobInfo)
117 assertions.NotNil(err)
118 assertions.Equal("missing required target URI: { job1 type1}", err.Error())
119 clearAll()
elinuxhenrik63a42ca2021-09-06 22:16:24 +0200120}