elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 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 | |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 21 | package jobs |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 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 | |
elinuxhenrik | a77cd65 | 2021-09-06 10:56:21 +0200 | [diff] [blame] | 33 | func TestGetTypes_filesOkShouldReturnSliceOfTypesAndProvideSupportedTypes(t *testing.T) { |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 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 | } |
elinuxhenrik | b1fb1d8 | 2021-09-07 02:58:52 +0200 | [diff] [blame] | 39 | t.Cleanup(func() { |
| 40 | os.RemoveAll(typesDir) |
| 41 | clearAll() |
| 42 | }) |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 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{ |
elinuxhenrik | a77cd65 | 2021-09-06 10:56:21 +0200 | [diff] [blame] | 50 | TypeId: "type1", |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 51 | Schema: type1Schema, |
| 52 | } |
| 53 | wantedTypes := []*Type{&wantedType} |
| 54 | assertions.EqualValues(wantedTypes, types) |
| 55 | assertions.Nil(err) |
elinuxhenrik | a77cd65 | 2021-09-06 10:56:21 +0200 | [diff] [blame] | 56 | |
| 57 | supportedTypes := GetSupportedTypes() |
| 58 | assertions.EqualValues([]string{"type1"}, supportedTypes) |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 59 | } |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 60 | |
elinuxhenrik | b1fb1d8 | 2021-09-07 02:58:52 +0200 | [diff] [blame] | 61 | func TestAddJobWhenTypeIsSupported_shouldAddJobToAllJobsMap(t *testing.T) { |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 62 | assertions := require.New(t) |
| 63 | allJobs["type1"] = make(map[string]JobInfo) |
elinuxhenrik | b1fb1d8 | 2021-09-07 02:58:52 +0200 | [diff] [blame] | 64 | t.Cleanup(func() { |
| 65 | clearAll() |
| 66 | }) |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 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"])) |
elinuxhenrik | b1fb1d8 | 2021-09-07 02:58:52 +0200 | [diff] [blame] | 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() |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 120 | } |