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===================================
28 "github.com/stretchr/testify/mock"
29 "github.com/stretchr/testify/require"
30 "oransc.org/nonrtric/dmaapmediatorproducer/mocks/httpclient"
33 func TestRegisterTypes(t *testing.T) {
34 assertions := require.New(t)
36 clientMock := httpclient.HTTPClient{}
38 clientMock.On("Do", mock.Anything).Return(&http.Response{
39 StatusCode: http.StatusCreated,
42 type1 := TypeDefinition{
44 TypeSchema: `{"type": "object","properties": {},"additionalProperties": false}`,
46 types := []TypeDefinition{type1}
48 r := NewRegistratorImpl("http://localhost:9990", &clientMock)
49 err := r.RegisterTypes(types)
52 var actualRequest *http.Request
53 clientMock.AssertCalled(t, "Do", mock.MatchedBy(func(req *http.Request) bool {
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)
68 func TestRegisterProducer(t *testing.T) {
69 assertions := require.New(t)
71 clientMock := httpclient.HTTPClient{}
73 clientMock.On("Do", mock.Anything).Return(&http.Response{
74 StatusCode: http.StatusCreated,
77 producer := ProducerRegistrationInfo{
78 InfoProducerSupervisionCallbackUrl: "supervisionCallbackUrl",
79 SupportedInfoTypes: []string{"type1"},
80 InfoJobCallbackUrl: "jobCallbackUrl",
83 r := NewRegistratorImpl("http://localhost:9990", &clientMock)
84 err := r.RegisterProducer("Producer1", &producer)
87 var actualRequest *http.Request
88 clientMock.AssertCalled(t, "Do", mock.MatchedBy(func(req *http.Request) bool {
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)