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