summaryrefslogtreecommitdiffstats
path: root/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/onap/core/TestMsbApiProvider.java
blob: a0565206cd37883eb95e5c01bbe0c8f4356165a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 * Copyright 2016-2017, Nokia Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core;

import io.reactivex.Observable;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.onap.msb.ApiClient;
import org.onap.msb.api.ServiceResourceApi;
import org.onap.msb.model.MicroServiceFullInfo;
import org.onap.msb.model.NodeInfo;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamTokenProvider;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase;
import org.springframework.core.env.Environment;

import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.fail;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.util.ReflectionTestUtils.setField;

public class TestMsbApiProvider extends TestBase {
    @Mock
    private Environment environment;
    @Mock
    private CbamTokenProvider cbamTokenProvider;
    private MicroServiceFullInfo microServiceInfo = new MicroServiceFullInfo();
    private List<NodeInfo> nodes = new ArrayList<>();
    private MsbApiProvider msbApiProvider;

    @Before
    public void init() {
        setField(MsbApiProvider.class, "logger", logger);
        msbApiProvider = new MsbApiProvider(environment);
        microServiceInfo.setNodes(nodes);
    }

    /**
     * test MSB client is created based on driver properties
     */
    @Test
    public void testMsbClient() {
        setFieldWithPropertyAnnotation(msbApiProvider, "${messageBusIp}", "mymessageBusIp");
        setFieldWithPropertyAnnotation(msbApiProvider, "${messageBusPort}", "123");
        //when
        ApiClient msbClient = msbApiProvider.buildApiClient();
        //verify
        assertEquals("http://mymessagebusip:123/api/msdiscover/v1/", msbClient.getAdapterBuilder().build().baseUrl().toString());
    }

    /**
     * error is propagated if no suitable micro service endpoint is found
     */
    @Test
    public void testNoSuitableMicroService() throws Exception {
        NodeInfo dockerAccessPoint = new NodeInfo();
        dockerAccessPoint.setIp("172.1.2.3");
        microServiceInfo.setServiceName("serviceName");
        microServiceInfo.setVersion("v1");
        microServiceInfo.setUrl("/lead/nslcm/v1");
        when(environment.getProperty(IpMappingProvider.IP_MAP, String.class, "")).thenReturn("");
        nodes.add(dockerAccessPoint);
        msbApiProvider = new MsbApiProvider(environment) {
            @Override
            public ServiceResourceApi getMsbApi() {
                return msbClient;
            }
        };
        when(msbClient.getMicroService_0("serviceName", "v1", null, null, null, null, null)).thenReturn(buildObservable(microServiceInfo));
        //when
        try {
            msbApiProvider.getMicroServiceUrl("serviceName", "v1");
            fail();
        } catch (Exception e) {
            String msg = "The serviceName service with v1 does not have any valid nodes[class NodeInfo {\n" +
                    "    ip: 172.1.2.3\n" +
                    "    port: null\n" +
                    "    lbServerParams: null\n" +
                    "    checkType: null\n" +
                    "    checkUrl: null\n" +
                    "    checkInterval: null\n" +
                    "    checkTimeOut: null\n" +
                    "    ttl: null\n" +
                    "    haRole: null\n" +
                    "    nodeId: null\n" +
                    "    status: null\n" +
                    "    expiration: null\n" +
                    "    createdAt: null\n" +
                    "    updatedAt: null\n" +
                    "}]";
            assertEquals(msg, e.getMessage());
            verify(logger).error(msg);
        }
    }

    /**
     * non Docker endpoint is selected
     */
    @Test
    public void testExistingValidEndpoint() throws Exception {
        NodeInfo nonDocker = new NodeInfo();
        nonDocker.setIp("173.1.2.3");
        nonDocker.setPort("234");
        microServiceInfo.setServiceName("serviceName");
        microServiceInfo.setVersion("v1");
        microServiceInfo.setUrl("/lead/nslcm/v1");
        when(environment.getProperty(IpMappingProvider.IP_MAP, String.class, "")).thenReturn("173.1.2.3->1.2.3.4");
        nodes.add(nonDocker);
        msbApiProvider = new MsbApiProvider(environment) {
            @Override
            public ServiceResourceApi getMsbApi() {
                return msbClient;
            }
        };
        when(msbClient.getMicroService_0("serviceName", "v1", null, null, null, null, null)).thenReturn(buildObservable(microServiceInfo));
        msbApiProvider.afterPropertiesSet();
        //when
        assertEquals("http://1.2.3.4:234/lead/nslcm/v1", msbApiProvider.getMicroServiceUrl("serviceName", "v1"));
    }


    /**
     * use HTTPS for known ports
     */
    @Test
    public void testMsbServiceOverssl() throws Exception {
        NodeInfo nonDocker = new NodeInfo();
        nonDocker.setIp("173.1.2.3");
        nonDocker.setPort("123");
        microServiceInfo.setServiceName("serviceName");
        microServiceInfo.setVersion("v1");
        microServiceInfo.setUrl("/lead/nslcm/v1");
        microServiceInfo.setEnableSsl(true);
        when(environment.getProperty(IpMappingProvider.IP_MAP, String.class, "")).thenReturn("173.1.2.3->1.2.3.4");
        nodes.add(nonDocker);
        msbApiProvider = new MsbApiProvider(environment) {
            @Override
            public ServiceResourceApi getMsbApi() {
                return msbClient;
            }
        };
        when(msbClient.getMicroService_0("serviceName", "v1", null, null, null, null, null)).thenReturn(buildObservable(microServiceInfo));
        msbApiProvider.afterPropertiesSet();
        //when
        assertEquals("https://1.2.3.4:123/lead/nslcm/v1", msbApiProvider.getMicroServiceUrl("serviceName", "v1"));
    }

    /**
     * if unable to get micro service info the error is propagated
     */
    @Test
    public void testUnableQueryMicroserviInfo() throws Exception {
        msbApiProvider = new MsbApiProvider(environment) {
            @Override
            public ServiceResourceApi getMsbApi() {
                return msbClient;
            }
        };
        RuntimeException expectedException = new RuntimeException();
        when(msbClient.getMicroService_0("serviceName", "v1", null, null, null, null, null)).thenReturn(Observable.error(expectedException));
        //when
        try {
            msbApiProvider.getMicroServiceUrl("serviceName", "v1");
            fail();
        } catch (Exception e) {
            assertEquals("Unable to get micro service URL for serviceName with version v1", e.getMessage());
            verify(logger).error("Unable to get micro service URL for serviceName with version v1", expectedException);
        }
    }

}