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
48 Schema: `{"title": "Type 1"}`,
50 types := []*jobs.Type{&type1}
52 r := NewRegistratorImpl("http://localhost:9990")
53 err := r.RegisterTypes(types)
56 var actualRequest *http.Request
57 clientMock.AssertCalled(t, "Do", mock.MatchedBy(func(req *http.Request) bool {
61 assertions.Equal(http.MethodPut, actualRequest.Method)
62 assertions.Equal("http", actualRequest.URL.Scheme)
63 assertions.Equal("localhost:9990", actualRequest.URL.Host)
64 assertions.Equal("/data-producer/v1/info-types/Type1", actualRequest.URL.Path)
65 assertions.Equal("application/json; charset=utf-8", actualRequest.Header.Get("Content-Type"))
66 body, _ := ioutil.ReadAll(actualRequest.Body)
67 expectedBody := []byte(`{"info_job_data_schema": {"title": "Type 1"}}`)
68 assertions.Equal(expectedBody, body)
69 clientMock.AssertNumberOfCalls(t, "Do", 1)
72 func TestRegisterProducer(t *testing.T) {
73 assertions := require.New(t)
75 clientMock := mocks.HTTPClient{}
77 clientMock.On("Do", mock.Anything).Return(&http.Response{
78 StatusCode: http.StatusCreated,
81 restclient.Client = &clientMock
83 producer := ProducerRegistrationInfo{
84 InfoProducerSupervisionCallbackUrl: "supervisionCallbackUrl",
85 SupportedInfoTypes: []string{"type1"},
86 InfoJobCallbackUrl: "jobCallbackUrl",
89 r := NewRegistratorImpl("http://localhost:9990")
90 err := r.RegisterProducer("Producer1", &producer)
93 var actualRequest *http.Request
94 clientMock.AssertCalled(t, "Do", mock.MatchedBy(func(req *http.Request) bool {
98 assertions.Equal(http.MethodPut, actualRequest.Method)
99 assertions.Equal("http", actualRequest.URL.Scheme)
100 assertions.Equal("localhost:9990", actualRequest.URL.Host)
101 assertions.Equal("/data-producer/v1/info-producers/Producer1", actualRequest.URL.Path)
102 assertions.Equal("application/json; charset=utf-8", actualRequest.Header.Get("Content-Type"))
103 body, _ := ioutil.ReadAll(actualRequest.Body)
104 expectedBody := []byte(`{"info_producer_supervision_callback_url":"supervisionCallbackUrl","supported_info_types":["type1"],"info_job_callback_url":"jobCallbackUrl"}`)
105 assertions.Equal(expectedBody, body)
106 clientMock.AssertNumberOfCalls(t, "Do", 1)