summaryrefslogtreecommitdiffstats
path: root/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/onap/direct/TestAAIRestApiProvider.java
blob: 953f7da0a72df14deaa71a4a45437b8beace6832 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
 * 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.direct;

import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.onap.aai.domain.yang.v11.GenericVnf;
import org.onap.aai.domain.yang.v11.L3Network;
import org.onap.aai.domain.yang.v11.ObjectFactory;
import org.onap.aai.restclient.client.OperationResult;
import org.onap.aai.restclient.client.RestClient;
import org.onap.aai.restclient.enums.RestAuthenticationMode;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase;

import javax.xml.bind.JAXBContext;
import java.io.ByteArrayOutputStream;
import java.io.StringReader;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;

import static com.google.common.collect.Lists.newArrayList;
import static java.util.Base64.getEncoder;
import static javax.ws.rs.core.MediaType.APPLICATION_XML_TYPE;
import static junit.framework.TestCase.*;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.util.ReflectionTestUtils.setField;

public class TestAAIRestApiProvider extends TestBase {
    private ObjectFactory OBJECT_FACTORY = new ObjectFactory();
    @Mock
    private RestClient restClient;
    private AAIRestApiProvider aaiRestApiProvider;
    private ArgumentCaptor<Map> headers = ArgumentCaptor.forClass(Map.class);
    private ArgumentCaptor<String> payload = ArgumentCaptor.forClass(String.class);

    private OperationResult result = new OperationResult();

    public static String marshall(Object object) throws Exception {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        JAXBContext.newInstance(object.getClass()).createMarshaller().marshal(object, bos);
        return bos.toString();
    }

    public static <T> T unmarshal(String content, Class<T> clazz) {
        try {
            return (T) JAXBContext.newInstance(clazz).createUnmarshaller().unmarshal(new StringReader(content));
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }

    @Before
    public void init() {
        //MockitoAnnotations.initMocks(this);
        AAIRestApiProvider real = new AAIRestApiProvider(msbApiProvider);
        setField(AAIRestApiProvider.class, "logger", logger);
        setFieldWithPropertyAnnotation(real, "${aaiUsername}", "aaiUsername");
        setFieldWithPropertyAnnotation(real, "${aaiPassword}", "aaiPassword");
        aaiRestApiProvider = Mockito.spy(real);
        when(aaiRestApiProvider.buildRawClient()).thenReturn(restClient);
        when(restClient.basicAuthPassword("aaiPassword")).thenReturn(restClient);
        when(restClient.basicAuthUsername("aaiUsername")).thenReturn(restClient);
        when(restClient.authenticationMode(RestAuthenticationMode.SSL_BASIC)).thenReturn(restClient);
        when(msbApiProvider.getMicroServiceUrl(AAIRestApiProvider.AAIService.CLOUD.getServiceName(), "v11")).thenReturn("x://1.2.3.4:4/a");
        result.setResultCode(201);
    }

    /**
     * test HTTP GET success scenario
     */
    @Test
    public void testGetSuccess() throws Exception {
        GenericVnf vnf = OBJECT_FACTORY.createGenericVnf();
        vnf.setVnfId("myVnfId");
        when(restClient.get(eq("x://1.2.3.4:4/a/myurl"), headers.capture(), eq(APPLICATION_XML_TYPE))).thenReturn(result);
        result.setResult(marshall(vnf));
        //when
        GenericVnf actualVnf = aaiRestApiProvider.get(logger, AAIRestApiProvider.AAIService.CLOUD, "/myurl", GenericVnf.class);
        //verify
        assertEquals(vnf.getVnfId(), actualVnf.getVnfId());
        assertHeaders();
    }

    /**
     * HTTP GET on non existing resource results in {@link java.util.NoSuchElementException}
     */
    @Test
    public void testGetMissingResource() throws Exception {
        when(restClient.get(eq("x://1.2.3.4:4/a/myurl"), headers.capture(), eq(APPLICATION_XML_TYPE))).thenReturn(result);
        result.setResultCode(404);
        //when
        try {
            aaiRestApiProvider.get(logger, AAIRestApiProvider.AAIService.CLOUD, "/myurl", GenericVnf.class);
            fail();
        } catch (NoSuchElementException e) {
            verify(logger).debug("The resource at /myurl does not exists");
            assertEquals("The resource at /myurl does not exists", e.getMessage());
        }
    }

    /**
     * Non known HTTP response code is propagated
     */
    @Test
    public void testUnknownErroCode() throws Exception {
        when(restClient.get(eq("x://1.2.3.4:4/a/myurl"), headers.capture(), eq(APPLICATION_XML_TYPE))).thenReturn(result);
        result.setResultCode(502);
        result.setFailureCause("myFail");
        //when
        try {
            aaiRestApiProvider.get(logger, AAIRestApiProvider.AAIService.CLOUD, "/myurl", GenericVnf.class);
            fail();
        } catch (RuntimeException e) {
            verify(logger).error("Bad response. Code: 502 cause: myFail");
            assertEquals("Bad response. Code: 502 cause: myFail", e.getMessage());
        }
    }

    /**
     * response content is not used when not requesting result
     */
    @Test
    public void testNoResult() throws Exception {
        when(restClient.get(eq("x://1.2.3.4:4/a/myurl"), headers.capture(), eq(APPLICATION_XML_TYPE))).thenReturn(result);
        result.setResultCode(202);
        //when
        Void result = aaiRestApiProvider.get(logger, AAIRestApiProvider.AAIService.CLOUD, "/myurl", Void.class);
        //verify
        assertNull(result);
    }

    /**
     * test HTTP PUT success scenario
     */
    @Test
    public void putSuccess() throws Exception {
        when(restClient.put(eq("x://1.2.3.4:4/a/myurl"), payload.capture(), headers.capture(), eq(APPLICATION_XML_TYPE), eq(APPLICATION_XML_TYPE))).thenReturn(result);
        GenericVnf request = OBJECT_FACTORY.createGenericVnf();
        request.setVnfId("myVnfId");
        L3Network response = OBJECT_FACTORY.createL3Network();
        response.setNetworkId("myNetworkId");
        result.setResult(marshall(response));
        //when
        L3Network actualResponse = aaiRestApiProvider.put(logger, AAIRestApiProvider.AAIService.CLOUD, "/myurl", request, L3Network.class);
        //verify
        GenericVnf actualValue = unmarshal(payload.getValue(), GenericVnf.class);
        assertEquals("myVnfId", actualValue.getVnfId());
        assertEquals("myNetworkId", actualResponse.getNetworkId());
        assertHeaders();
    }

    /**
     * test HTTP delete success scenario
     */
    @Test
    public void deleteSuccess() throws Exception {
        when(restClient.delete(eq("x://1.2.3.4:4/a/myurl"), headers.capture(), eq(APPLICATION_XML_TYPE))).thenReturn(result);
        //when
        aaiRestApiProvider.delete(logger, AAIRestApiProvider.AAIService.CLOUD, "/myurl");
        //verify
        assertHeaders();
        //the when above is the verify
    }

    /**
     * invalid request content results in error
     */
    @Test
    public void testInvalidInput() throws Exception {
        when(restClient.put(eq("x://1.2.3.4:4/a/myurl"), payload.capture(), headers.capture(), eq(APPLICATION_XML_TYPE), eq(APPLICATION_XML_TYPE))).thenReturn(result);
        //when
        try {
            aaiRestApiProvider.put(logger, AAIRestApiProvider.AAIService.CLOUD, "/myurl", "Invalid content", L3Network.class);
            //verify
            fail();
        } catch (Exception e) {
            assertEquals("Unable to marshal content", e.getMessage());
            verify(logger).error("Unable to marshal content", e.getCause());
        }
    }

    /**
     * invalid response content results in error
     */
    @Test
    public void testInvalidResponse() throws Exception {
        when(restClient.put(eq("x://1.2.3.4:4/a/myurl"), payload.capture(), headers.capture(), eq(APPLICATION_XML_TYPE), eq(APPLICATION_XML_TYPE))).thenReturn(result);
        GenericVnf request = OBJECT_FACTORY.createGenericVnf();
        request.setVnfId("myVnfId");
        result.setResult("invalid");
        //when
        try {
            aaiRestApiProvider.put(logger, AAIRestApiProvider.AAIService.CLOUD, "/myurl", request, L3Network.class);
            //verify
            fail();
        } catch (Exception e) {
            assertEquals("Unable to unmarshal content", e.getMessage());
            verify(logger).error("Unable to unmarshal content", e.getCause());
        }
    }

    /**
     * test AAI service names in AAI
     */
    @Test
    public void testServiceNames() {
        //the names have been base64-ed to prevent "smart" IDEs (idea) to refactor the tests too for the otherwise known fix constants in external systems
        assertEquals("YWFpLWNsb3VkSW5mcmFzdHJ1Y3R1cmU=", getEncoder().encodeToString(AAIRestApiProvider.AAIService.CLOUD.getServiceName().getBytes()));
        assertEquals("YWFpLW5ldHdvcms=", getEncoder().encodeToString(AAIRestApiProvider.AAIService.NETWORK.getServiceName().getBytes()));
        assertEquals("YWFpLWV4dGVybmFsU3lzdGVt", getEncoder().encodeToString(AAIRestApiProvider.AAIService.ESR.getServiceName().getBytes()));
    }

    private void assertHeaders() {
        Map<String, List<String>> actualHeaders = headers.getValue();
        assertEquals(2, actualHeaders.size());
        assertEquals(newArrayList("NokiaSVNFM"), actualHeaders.get("X-FromAppId"));
        assertEquals(newArrayList("application/xml"), actualHeaders.get("Accept"));
    }
}