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===================================
32 "github.com/stretchr/testify/mock"
33 "github.com/stretchr/testify/require"
34 "oransc.org/nonrtric/dmaapmediatorproducer/internal/restclient"
35 "oransc.org/nonrtric/dmaapmediatorproducer/mocks"
38 const typeDefinition = `{"id": "type1", "dmaapTopic": "unauthenticated.SEC_FAULT_OUTPUT", "schema": {"title": "Type 1"}}`
40 func TestGetTypes_filesOkShouldReturnSliceOfTypesAndProvideSupportedTypes(t *testing.T) {
41 assertions := require.New(t)
42 typesDir, err := os.MkdirTemp("", "configs")
44 t.Errorf("Unable to create temporary directory for types due to: %v", err)
47 os.RemoveAll(typesDir)
51 fname := filepath.Join(typesDir, "type1.json")
52 if err = os.WriteFile(fname, []byte(typeDefinition), 0666); err != nil {
53 t.Errorf("Unable to create temporary files for types due to: %v", err)
55 types, err := GetTypes()
58 DMaaPTopic: "unauthenticated.SEC_FAULT_OUTPUT",
59 Schema: `{"title":"Type 1"}`,
60 Jobs: make(map[string]JobInfo),
62 wantedTypes := []*Type{&wantedType}
63 assertions.EqualValues(wantedTypes, types)
66 supportedTypes := GetSupportedTypes()
67 assertions.EqualValues([]string{"type1"}, supportedTypes)
70 func TestAddJobWhenTypeIsSupported_shouldAddJobToAllJobsMap(t *testing.T) {
71 assertions := require.New(t)
75 InfoJobIdentity: "job1",
78 InfoTypeIdentity: "type1",
80 allJobs["type1"] = Type{
82 Jobs: map[string]JobInfo{"job1": wantedJob},
88 err := AddJob(wantedJob)
90 assertions.Equal(1, len(allJobs["type1"].Jobs))
91 assertions.Equal(wantedJob, allJobs["type1"].Jobs["job1"])
94 func TestAddJobWhenTypeIsNotSupported_shouldReturnError(t *testing.T) {
95 assertions := require.New(t)
97 InfoTypeIdentity: "type1",
100 err := AddJob(jobInfo)
101 assertions.NotNil(err)
102 assertions.Equal("type not supported: type1", err.Error())
105 func TestAddJobWhenJobIdMissing_shouldReturnError(t *testing.T) {
106 assertions := require.New(t)
107 allJobs["type1"] = Type{
114 InfoTypeIdentity: "type1",
117 err := AddJob(jobInfo)
118 assertions.NotNil(err)
119 assertions.Equal("missing required job identity: { type1}", err.Error())
122 func TestAddJobWhenTargetUriMissing_shouldReturnError(t *testing.T) {
123 assertions := require.New(t)
124 allJobs["type1"] = Type{
128 InfoTypeIdentity: "type1",
129 InfoJobIdentity: "job1",
132 err := AddJob(jobInfo)
133 assertions.NotNil(err)
134 assertions.Equal("missing required target URI: { job1 type1}", err.Error())
138 func TestPollAndDistributeMessages(t *testing.T) {
139 assertions := require.New(t)
141 InfoTypeIdentity: "type1",
142 InfoJobIdentity: "job1",
143 TargetUri: "http://consumerHost/target",
145 allJobs["type1"] = Type{
148 Jobs: map[string]JobInfo{"job1": jobInfo},
154 body := ioutil.NopCloser(bytes.NewReader([]byte(`[{"message": {"data": "data"}}]`)))
155 clientMock := mocks.HTTPClient{}
156 clientMock.On("Get", mock.Anything).Return(&http.Response{
157 StatusCode: http.StatusOK,
161 clientMock.On("Do", mock.Anything).Return(&http.Response{
162 StatusCode: http.StatusOK,
165 restclient.Client = &clientMock
167 pollAndDistributeMessages("http://mrAddr")
169 time.Sleep(100 * time.Millisecond)
171 var actualRequest *http.Request
172 clientMock.AssertCalled(t, "Get", "http://mrAddr/events/topic/users/dmaapmediatorproducer")
173 clientMock.AssertNumberOfCalls(t, "Get", 1)
175 clientMock.AssertCalled(t, "Do", mock.MatchedBy(func(req *http.Request) bool {
179 assertions.Equal(http.MethodPost, actualRequest.Method)
180 assertions.Equal("consumerHost", actualRequest.URL.Host)
181 assertions.Equal("/target", actualRequest.URL.Path)
182 assertions.Equal("application/json; charset=utf-8", actualRequest.Header.Get("Content-Type"))
183 actualBody, _ := ioutil.ReadAll(actualRequest.Body)
184 assertions.Equal([]byte(`[{"message": {"data": "data"}}]`), actualBody)
185 clientMock.AssertNumberOfCalls(t, "Do", 1)