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