Merge "Test updates for ECS, PMS and SDNC"
[nonrtric.git] / dmaap-mediator-producer / internal / config / registrator_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 config
22
23 import (
24         "io/ioutil"
25         "net/http"
26         "testing"
27
28         "github.com/stretchr/testify/mock"
29         "github.com/stretchr/testify/require"
30         "oransc.org/nonrtric/dmaapmediatorproducer/internal/jobs"
31         "oransc.org/nonrtric/dmaapmediatorproducer/internal/restclient"
32         "oransc.org/nonrtric/dmaapmediatorproducer/mocks"
33 )
34
35 func TestRegisterTypes(t *testing.T) {
36         assertions := require.New(t)
37
38         clientMock := mocks.HTTPClient{}
39
40         clientMock.On("Do", mock.Anything).Return(&http.Response{
41                 StatusCode: http.StatusCreated,
42         }, nil)
43
44         restclient.Client = &clientMock
45
46         type1 := jobs.Type{
47                 TypeId: "Type1",
48                 Schema: `{"title": "Type 1"}`,
49         }
50         types := []*jobs.Type{&type1}
51
52         r := NewRegistratorImpl("http://localhost:9990")
53         err := r.RegisterTypes(types)
54
55         assertions.Nil(err)
56         var actualRequest *http.Request
57         clientMock.AssertCalled(t, "Do", mock.MatchedBy(func(req *http.Request) bool {
58                 actualRequest = req
59                 return true
60         }))
61         assertions.Equal(http.MethodPut, actualRequest.Method)
62         assertions.Equal("http", actualRequest.URL.Scheme)
63         assertions.Equal("localhost:9990", actualRequest.URL.Host)
64         assertions.Equal("/data-producer/v1/info-types/Type1", actualRequest.URL.Path)
65         assertions.Equal("application/json; charset=utf-8", actualRequest.Header.Get("Content-Type"))
66         body, _ := ioutil.ReadAll(actualRequest.Body)
67         expectedBody := []byte(`{"info_job_data_schema": {"title": "Type 1"}}`)
68         assertions.Equal(expectedBody, body)
69         clientMock.AssertNumberOfCalls(t, "Do", 1)
70 }
71
72 func TestRegisterProducer(t *testing.T) {
73         assertions := require.New(t)
74
75         clientMock := mocks.HTTPClient{}
76
77         clientMock.On("Do", mock.Anything).Return(&http.Response{
78                 StatusCode: http.StatusCreated,
79         }, nil)
80
81         restclient.Client = &clientMock
82
83         producer := ProducerRegistrationInfo{
84                 InfoProducerSupervisionCallbackUrl: "supervisionCallbackUrl",
85                 SupportedInfoTypes:                 []string{"type1"},
86                 InfoJobCallbackUrl:                 "jobCallbackUrl",
87         }
88
89         r := NewRegistratorImpl("http://localhost:9990")
90         err := r.RegisterProducer("Producer1", &producer)
91
92         assertions.Nil(err)
93         var actualRequest *http.Request
94         clientMock.AssertCalled(t, "Do", mock.MatchedBy(func(req *http.Request) bool {
95                 actualRequest = req
96                 return true
97         }))
98         assertions.Equal(http.MethodPut, actualRequest.Method)
99         assertions.Equal("http", actualRequest.URL.Scheme)
100         assertions.Equal("localhost:9990", actualRequest.URL.Host)
101         assertions.Equal("/data-producer/v1/info-producers/Producer1", actualRequest.URL.Path)
102         assertions.Equal("application/json; charset=utf-8", actualRequest.Header.Get("Content-Type"))
103         body, _ := ioutil.ReadAll(actualRequest.Body)
104         expectedBody := []byte(`{"info_producer_supervision_callback_url":"supervisionCallbackUrl","supported_info_types":["type1"],"info_job_callback_url":"jobCallbackUrl"}`)
105         assertions.Equal(expectedBody, body)
106         clientMock.AssertNumberOfCalls(t, "Do", 1)
107 }