324aed0c56228a5398ec32b63fafc29e26b4e70f
[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/mocks/httpclient"
31 )
32
33 func TestRegisterTypes(t *testing.T) {
34         assertions := require.New(t)
35
36         clientMock := httpclient.HTTPClient{}
37
38         clientMock.On("Do", mock.Anything).Return(&http.Response{
39                 StatusCode: http.StatusCreated,
40         }, nil)
41
42         type1 := TypeDefinition{
43                 Id: "Type1",
44         }
45         types := []TypeDefinition{type1}
46
47         r := NewRegistratorImpl("http://localhost:9990", &clientMock)
48         err := r.RegisterTypes(types)
49
50         assertions.Nil(err)
51         var actualRequest *http.Request
52         clientMock.AssertCalled(t, "Do", mock.MatchedBy(func(req *http.Request) bool {
53                 actualRequest = req
54                 return true
55         }))
56         assertions.Equal(http.MethodPut, actualRequest.Method)
57         assertions.Equal("http", actualRequest.URL.Scheme)
58         assertions.Equal("localhost:9990", actualRequest.URL.Host)
59         assertions.Equal("/data-producer/v1/info-types/Type1", actualRequest.URL.Path)
60         assertions.Equal("application/json", actualRequest.Header.Get("Content-Type"))
61         body, _ := ioutil.ReadAll(actualRequest.Body)
62         expectedBody := []byte(`{"info_job_data_schema": {"type": "object","properties": {},"additionalProperties": false}}`)
63         assertions.Equal(expectedBody, body)
64         clientMock.AssertNumberOfCalls(t, "Do", 1)
65 }
66
67 func TestRegisterProducer(t *testing.T) {
68         assertions := require.New(t)
69
70         clientMock := httpclient.HTTPClient{}
71
72         clientMock.On("Do", mock.Anything).Return(&http.Response{
73                 StatusCode: http.StatusCreated,
74         }, nil)
75
76         producer := ProducerRegistrationInfo{
77                 InfoProducerSupervisionCallbackUrl: "supervisionCallbackUrl",
78                 SupportedInfoTypes:                 []string{"type1"},
79                 InfoJobCallbackUrl:                 "jobCallbackUrl",
80         }
81
82         r := NewRegistratorImpl("http://localhost:9990", &clientMock)
83         err := r.RegisterProducer("Producer1", &producer)
84
85         assertions.Nil(err)
86         var actualRequest *http.Request
87         clientMock.AssertCalled(t, "Do", mock.MatchedBy(func(req *http.Request) bool {
88                 actualRequest = req
89                 return true
90         }))
91         assertions.Equal(http.MethodPut, actualRequest.Method)
92         assertions.Equal("http", actualRequest.URL.Scheme)
93         assertions.Equal("localhost:9990", actualRequest.URL.Host)
94         assertions.Equal("/data-producer/v1/info-producers/Producer1", actualRequest.URL.Path)
95         assertions.Equal("application/json", actualRequest.Header.Get("Content-Type"))
96         body, _ := ioutil.ReadAll(actualRequest.Body)
97         expectedBody := []byte(`{"info_producer_supervision_callback_url":"supervisionCallbackUrl","supported_info_types":["type1"],"info_job_callback_url":"jobCallbackUrl"}`)
98         assertions.Equal(expectedBody, body)
99         clientMock.AssertNumberOfCalls(t, "Do", 1)
100 }