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/internal/jobs"
31 "oransc.org/nonrtric/dmaapmediatorproducer/internal/restclient"
32 "oransc.org/nonrtric/dmaapmediatorproducer/mocks"
35 func TestRegisterTypes(t *testing.T) {
36 assertions := require.New(t)
38 clientMock := mocks.HTTPClient{}
40 clientMock.On("Do", mock.Anything).Return(&http.Response{
41 StatusCode: http.StatusCreated,
44 restclient.Client = &clientMock
46 type1 := jobs.TypeData{
49 types := []jobs.TypeData{type1}
51 r := NewRegistratorImpl("http://localhost:9990")
52 err := r.RegisterTypes(types)
55 var actualRequest *http.Request
56 clientMock.AssertCalled(t, "Do", mock.MatchedBy(func(req *http.Request) bool {
60 assertions.Equal(http.MethodPut, actualRequest.Method)
61 assertions.Equal("http", actualRequest.URL.Scheme)
62 assertions.Equal("localhost:9990", actualRequest.URL.Host)
63 assertions.Equal("/data-producer/v1/info-types/Type1", actualRequest.URL.Path)
64 assertions.Equal("application/json; charset=utf-8", actualRequest.Header.Get("Content-Type"))
65 body, _ := ioutil.ReadAll(actualRequest.Body)
66 expectedBody := []byte(`{"info_job_data_schema": {"type": "object","properties": {},"additionalProperties": false}}`)
67 assertions.Equal(expectedBody, body)
68 clientMock.AssertNumberOfCalls(t, "Do", 1)
71 func TestRegisterProducer(t *testing.T) {
72 assertions := require.New(t)
74 clientMock := mocks.HTTPClient{}
76 clientMock.On("Do", mock.Anything).Return(&http.Response{
77 StatusCode: http.StatusCreated,
80 restclient.Client = &clientMock
82 producer := ProducerRegistrationInfo{
83 InfoProducerSupervisionCallbackUrl: "supervisionCallbackUrl",
84 SupportedInfoTypes: []string{"type1"},
85 InfoJobCallbackUrl: "jobCallbackUrl",
88 r := NewRegistratorImpl("http://localhost:9990")
89 err := r.RegisterProducer("Producer1", &producer)
92 var actualRequest *http.Request
93 clientMock.AssertCalled(t, "Do", mock.MatchedBy(func(req *http.Request) bool {
97 assertions.Equal(http.MethodPut, actualRequest.Method)
98 assertions.Equal("http", actualRequest.URL.Scheme)
99 assertions.Equal("localhost:9990", actualRequest.URL.Host)
100 assertions.Equal("/data-producer/v1/info-producers/Producer1", actualRequest.URL.Path)
101 assertions.Equal("application/json; charset=utf-8", actualRequest.Header.Get("Content-Type"))
102 body, _ := ioutil.ReadAll(actualRequest.Body)
103 expectedBody := []byte(`{"info_producer_supervision_callback_url":"supervisionCallbackUrl","supported_info_types":["type1"],"info_job_callback_url":"jobCallbackUrl"}`)
104 assertions.Equal(expectedBody, body)
105 clientMock.AssertNumberOfCalls(t, "Do", 1)