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 com.nokia.cbam.lcm.v32.JSON;
20 import okhttp3.Headers;
21 import okhttp3.RequestBody;
22 import okhttp3.ResponseBody;
23 import okhttp3.internal.http.RealResponseBody;
25 import okio.BufferedSource;
26 import org.junit.Test;
28 import org.threeten.bp.OffsetDateTime;
29 import org.threeten.bp.ZoneOffset;
31 import java.io.ByteArrayOutputStream;
32 import java.io.IOException;
33 import java.lang.annotation.Annotation;
35 import static junit.framework.TestCase.assertEquals;
36 import static junit.framework.TestCase.assertNotNull;
37 import static junit.framework.TestCase.assertTrue;
39 public class TestInhertence {
42 * test OpenStack v2 inheritence handling in serialization and deserialization
45 public void testInheritence() throws IOException{
46 InstantiateVnfRequest req = new InstantiateVnfRequest();
47 OPENSTACKV2INFO vim = new OPENSTACKV2INFO();
48 req.getVims().add(vim);
49 vim.setVimInfoType(VimInfo.VimInfoTypeEnum.OPENSTACK_V2_INFO);
50 OpenStackAccessInfoV2 accessInfo = new OpenStackAccessInfoV2();
51 accessInfo.setPassword("myPassword");
52 vim.setAccessInfo(accessInfo);
53 Annotation[] x = new Annotation[0];
54 RequestBody requestBody = new ApiClient().getAdapterBuilder().build().requestBodyConverter(InstantiateVnfRequest.class, x, new Annotation[0]).convert(req);
55 assertTrue(getContent(requestBody).contains("myPassword"));
56 ResponseBody responseBody = toResponse(requestBody);
57 InstantiateVnfRequest deserialize = (InstantiateVnfRequest) new ApiClient().getAdapterBuilder().build().responseBodyConverter(InstantiateVnfRequest.class, new Annotation[0]).convert(responseBody);
58 assertEquals(1, deserialize.getVims().size());
59 OPENSTACKV2INFO deserializedVim = (OPENSTACKV2INFO) deserialize.getVims().get(0);
60 assertEquals("myPassword", deserializedVim.getAccessInfo().getPassword());
64 * Test how dates are handled
67 public void testDateHandling() throws Exception{
68 VnfLifecycleChangeNotification vnfLifecycleChangeNotification = new VnfLifecycleChangeNotification();
69 OffsetDateTime fixedTime = OffsetDateTime.of(1981, 7, 4, 1,2,3,4, ZoneOffset.ofHours(0));
70 vnfLifecycleChangeNotification.setTimestamp(fixedTime);
71 vnfLifecycleChangeNotification.setNotificationType(VnfNotificationType.VNFLIFECYCLECHANGENOTIFICATION);
72 vnfLifecycleChangeNotification.setVnfInstanceId("vnfId");
73 Annotation[] x = new Annotation[0];
74 RequestBody requestBody = new ApiClient().getAdapterBuilder().build().requestBodyConverter(VnfLifecycleChangeNotification.class, x, new Annotation[0]).convert(vnfLifecycleChangeNotification);
75 String content = getContent(requestBody);
76 assertTrue(content.contains("1981-07-04T01:02:03"));
77 ResponseBody responseBody = toResponse(requestBody);
78 VnfLifecycleChangeNotification deserialize = (VnfLifecycleChangeNotification) new ApiClient().getAdapterBuilder().build().responseBodyConverter(VnfLifecycleChangeNotification.class, new Annotation[0]).convert(responseBody);
79 assertEquals(fixedTime, deserialize.getTimestamp());
83 public void testRawDateHAndling()throws Exception{
84 String content= "{\n" +
85 "\t\"status\": \"STARTED\",\n" +
86 "\t\"vnfInstanceId\": \"CBAM-3f081d70e0da4a44bd8d7b52e4dddbeb\",\n" +
87 "\t\"timestamp\": \"2018-03-24T07:11:09.910335Z\",\n" +
88 "\t\"notificationType\": \"VnfLifecycleChangeNotification\",\n" +
89 "\t\"lifecycleOperationOccurrenceId\": \"CBAM-158182e3df6744109cd980d52f608698\",\n" +
90 "\t\"subscriptionId\": \"CBAM-190e87ba3c0d4348a8d5b0c21bccc11d\",\n" +
91 "\t\"operation\": \"TERMINATE\"\n" +
95 VnfLifecycleChangeNotification deserialized = new JSON().getGson().fromJson(content, VnfLifecycleChangeNotification.class);
96 assertNotNull(deserialized.getTimestamp());
99 private ResponseBody toResponse(RequestBody convert) throws IOException {
100 Headers headers = new Headers.Builder().build();
101 Buffer buffer = new Buffer();
102 convert.writeTo(buffer);
103 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
104 buffer.copyTo(byteArrayOutputStream);
105 BufferedSource response = buffer;
106 return new RealResponseBody(headers, response);
109 private String getContent(RequestBody requestBody) throws IOException {
110 Buffer buffer = new Buffer();
111 requestBody.writeTo(buffer);
112 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
113 buffer.copyTo(byteArrayOutputStream);
114 return new String(byteArrayOutputStream.toByteArray());