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===================================
29 "github.com/stretchr/testify/mock"
30 "github.com/stretchr/testify/require"
31 "oransc.org/nonrtric/dmaapmediatorproducer/mocks/httpclient"
34 func TestRegisterTypes(t *testing.T) {
35 assertions := require.New(t)
37 clientMock := httpclient.HTTPClient{}
39 clientMock.On("Do", mock.Anything).Return(&http.Response{
40 StatusCode: http.StatusCreated,
46 "additionalProperties": false
48 var schemaObj interface{}
49 json.Unmarshal([]byte(schemaString), &schemaObj)
51 type1 := TypeDefinition{
53 TypeSchema: schemaObj,
55 types := []TypeDefinition{type1}
57 r := NewRegistratorImpl("http://localhost:9990", &clientMock)
58 err := r.RegisterTypes(types)
61 var actualRequest *http.Request
62 clientMock.AssertCalled(t, "Do", mock.MatchedBy(func(req *http.Request) bool {
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)
77 func TestRegisterProducer(t *testing.T) {
78 assertions := require.New(t)
80 clientMock := httpclient.HTTPClient{}
82 clientMock.On("Do", mock.Anything).Return(&http.Response{
83 StatusCode: http.StatusCreated,
86 producer := ProducerRegistrationInfo{
87 InfoProducerSupervisionCallbackUrl: "supervisionCallbackUrl",
88 SupportedInfoTypes: []string{"type1"},
89 InfoJobCallbackUrl: "jobCallbackUrl",
92 r := NewRegistratorImpl("http://localhost:9990", &clientMock)
93 err := r.RegisterProducer("Producer1", &producer)
96 var actualRequest *http.Request
97 clientMock.AssertCalled(t, "Do", mock.MatchedBy(func(req *http.Request) bool {
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)