2 * Copyright 2016-2017, Nokia Corporation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 import com.nokia.cbam.lcm.v32.ApiClient;
18 import com.nokia.cbam.lcm.v32.model.*;
19 import okhttp3.Headers;
20 import okhttp3.RequestBody;
21 import okhttp3.ResponseBody;
22 import okhttp3.internal.http.RealResponseBody;
24 import okio.BufferedSource;
25 import org.junit.Test;
27 import java.io.ByteArrayOutputStream;
28 import java.io.IOException;
29 import java.lang.annotation.Annotation;
31 import static junit.framework.TestCase.assertEquals;
32 import static junit.framework.TestCase.assertTrue;
34 public class TestInhertence {
37 * test OpenStack v2 inheritence handling in serialization and deserialization
40 public void testOpenStackV2() throws IOException{
41 InstantiateVnfRequest req = new InstantiateVnfRequest();
42 OPENSTACKV2INFO vim = new OPENSTACKV2INFO();
43 req.getVims().add(vim);
44 vim.setVimInfoType(VimInfo.VimInfoTypeEnum.OPENSTACK_V2_INFO);
45 OpenStackAccessInfoV2 accessInfo = new OpenStackAccessInfoV2();
46 accessInfo.setPassword("myPassword");
47 vim.setAccessInfo(accessInfo);
48 Annotation[] x = new Annotation[0];
49 RequestBody requestBody = new ApiClient().getAdapterBuilder().build().requestBodyConverter(InstantiateVnfRequest.class, x, new Annotation[0]).convert(req);
50 assertTrue(getContent(requestBody).contains("myPassword"));
51 ResponseBody responseBody = toResponse(requestBody);
52 InstantiateVnfRequest deserialize = (InstantiateVnfRequest) new ApiClient().getAdapterBuilder().build().responseBodyConverter(InstantiateVnfRequest.class, new Annotation[0]).convert(responseBody);
53 assertEquals(1, deserialize.getVims().size());
54 OPENSTACKV2INFO deserializedVim = (OPENSTACKV2INFO) deserialize.getVims().get(0);
55 assertEquals("myPassword", deserializedVim.getAccessInfo().getPassword());
58 private ResponseBody toResponse(RequestBody convert) throws IOException {
59 Headers headers = new Headers.Builder().build();
60 Buffer buffer = new Buffer();
61 convert.writeTo(buffer);
62 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
63 buffer.copyTo(byteArrayOutputStream);
64 BufferedSource response = buffer;
65 return new RealResponseBody(headers, response);
68 private String getContent(RequestBody requestBody) throws IOException {
69 Buffer buffer = new Buffer();
70 requestBody.writeTo(buffer);
71 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
72 buffer.copyTo(byteArrayOutputStream);
73 return new String(byteArrayOutputStream.toByteArray());