Merge "Test updates for ECS, PMS and SDNC"
[nonrtric.git] / dmaap-mediator-producer / internal / jobs / jobs_test.go
1 // -
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
21 package jobs
22
23 import (
24         "os"
25         "path/filepath"
26         "testing"
27
28         "github.com/stretchr/testify/require"
29 )
30
31 const type1Schema = `{"title": "Type 1"}`
32
33 func TestGetTypes_filesOkShouldReturnSliceOfTypesAndProvideSupportedTypes(t *testing.T) {
34         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         }
39         t.Cleanup(func() {
40                 os.RemoveAll(typesDir)
41                 clearAll()
42         })
43         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{
50                 TypeId: "type1",
51                 Schema: type1Schema,
52         }
53         wantedTypes := []*Type{&wantedType}
54         assertions.EqualValues(wantedTypes, types)
55         assertions.Nil(err)
56
57         supportedTypes := GetSupportedTypes()
58         assertions.EqualValues([]string{"type1"}, supportedTypes)
59 }
60
61 func TestAddJobWhenTypeIsSupported_shouldAddJobToAllJobsMap(t *testing.T) {
62         assertions := require.New(t)
63         allJobs["type1"] = make(map[string]JobInfo)
64         t.Cleanup(func() {
65                 clearAll()
66         })
67         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"]))
79         assertions.Equal(jobInfo, allJobs["type1"]["job1"])
80 }
81
82 func 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
93 func 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
108 func 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()
120 }