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{
45 types := []TypeDefinition{type1}
47 r := NewRegistratorImpl("http://localhost:9990", &clientMock)
48 err := r.RegisterTypes(types)
51 var actualRequest *http.Request
52 clientMock.AssertCalled(t, "Do", mock.MatchedBy(func(req *http.Request) bool {
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)
67 func TestRegisterProducer(t *testing.T) {
68 assertions := require.New(t)
70 clientMock := httpclient.HTTPClient{}
72 clientMock.On("Do", mock.Anything).Return(&http.Response{
73 StatusCode: http.StatusCreated,
76 producer := ProducerRegistrationInfo{
77 InfoProducerSupervisionCallbackUrl: "supervisionCallbackUrl",
78 SupportedInfoTypes: []string{"type1"},
79 InfoJobCallbackUrl: "jobCallbackUrl",
82 r := NewRegistratorImpl("http://localhost:9990", &clientMock)
83 err := r.RegisterProducer("Producer1", &producer)
86 var actualRequest *http.Request
87 clientMock.AssertCalled(t, "Do", mock.MatchedBy(func(req *http.Request) bool {
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)