summaryrefslogtreecommitdiffstats
path: root/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/onap/core/TestMsbApiProvider.java
blob: e6d8ebb4c7a2ef702fb2779ef1a950354b5bbb15 (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
/*
 * 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 org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.onap.msb.sdk.discovery.common.RouteException;
import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo;
import org.onap.msb.sdk.discovery.entity.NodeInfo;
import org.onap.msb.sdk.httpclient.msb.MSBServiceClient;
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 java.util.HashSet;
import java.util.Set;

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 Set<NodeInfo> nodes = new HashSet<>();
    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
        MSBServiceClient msbClient = msbApiProvider.getMsbClient();
        //verify
        assertEquals("mymessageBusIp:123", msbClient.getMsbSvrAddress());
    }

    /**
     * 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 MSBServiceClient getMsbClient() {
                return msbClient;
            }
        };
        when(msbClient.queryMicroServiceInfo("serviceName", "v1")).thenReturn(microServiceInfo);
        //when
        try {
            msbApiProvider.getMicroServiceUrl("serviceName", "v1");
            fail();
        } catch (Exception e) {
            assertEquals("The serviceName service with v1 does not have any valid nodes[172.1.2.3:null  ttl:]", e.getMessage());
            verify(logger).error("The serviceName service with v1 does not have any valid nodes[172.1.2.3:null  ttl:]");
        }
    }

    /**
     * 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 MSBServiceClient getMsbClient() {
                return msbClient;
            }
        };
        when(msbClient.queryMicroServiceInfo("serviceName", "v1")).thenReturn(microServiceInfo);
        msbApiProvider.afterPropertiesSet();
        //when
        assertEquals("http://1.2.3.4:234/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 MSBServiceClient getMsbClient() {
                return msbClient;
            }
        };
        RouteException expectedException = new RouteException();
        when(msbClient.queryMicroServiceInfo("serviceName", "v1")).thenThrow(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);
        }
    }

}