2 * ============LICENSE_START=======================================================
3 * BBS-RELOCATION-CPE-AUTHENTICATION-HANDLER
4 * ================================================================================
5 * Copyright (C) 2019 NOKIA Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.bbs.event.processor.model;
23 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import com.google.gson.Gson;
26 import com.google.gson.GsonBuilder;
27 import com.google.gson.TypeAdapterFactory;
29 import java.util.Arrays;
30 import java.util.Collections;
31 import java.util.LinkedHashMap;
33 import java.util.ServiceLoader;
35 import org.junit.jupiter.api.Test;
36 import org.onap.bbs.event.processor.utilities.ControlLoopJsonBodyBuilder;
37 import org.onap.bbs.event.processor.utilities.CpeAuthenticationJsonBodyBuilder;
38 import org.onap.bbs.event.processor.utilities.ReRegistrationJsonBodyBuilder;
40 class GsonSerializationsTest {
43 void creatingReRegistrationJsonBody_returnsJsonInString() {
45 String correlationId = "NokiaCorrelationId";
46 String attachmentPoint = "olt2/1/1";
47 String remoteId = "RemoteId";
48 String cvlan = "1005";
52 + "\"correlationId\":\"%s\","
53 + "\"attachment-point\":\"%s\","
54 + "\"remote-id\":\"%s\","
59 ReRegistrationConsumerDmaapModel model = ImmutableReRegistrationConsumerDmaapModel.builder()
60 .correlationId(correlationId)
61 .attachmentPoint(attachmentPoint)
68 String expectedResult = String.format(template, correlationId, attachmentPoint, remoteId, cvlan, svlan);
70 assertEquals(expectedResult, new ReRegistrationJsonBodyBuilder().createJsonBody(model));
74 void creatingCpeAuthenticationJsonBody_returnsJsonInString() {
76 String correlationId = "NokiaCorrelationID";
77 AuthenticationState oldAuthenticationState = AuthenticationState.IN_SERVICE;
78 AuthenticationState newAuthenticationState = AuthenticationState.OUT_OF_SERVICE;
79 String stateInterface = "stateInterface";
80 String rgwMacAddress = "00:0a:95:8d:78:16";
81 String swVersion = "1.2";
84 + "\"correlationId\":\"%s\","
85 + "\"old-authentication-state\":\"%s\","
86 + "\"new-authentication-state\":\"%s\","
87 + "\"state-interface\":\"%s\","
88 + "\"rgw-mac-address\":\"%s\","
89 + "\"sw-version\":\"%s\""
92 CpeAuthenticationConsumerDmaapModel model = ImmutableCpeAuthenticationConsumerDmaapModel.builder()
93 .correlationId(correlationId)
94 .oldAuthenticationState(oldAuthenticationState.getNameInOnap())
95 .newAuthenticationState(newAuthenticationState.getNameInOnap())
96 .stateInterface(stateInterface)
97 .rgwMacAddress(rgwMacAddress)
102 String expectedResult = String.format(template, correlationId, oldAuthenticationState.getNameInOnap(),
103 newAuthenticationState.getNameInOnap(), stateInterface, rgwMacAddress, swVersion);
105 assertEquals(expectedResult, new CpeAuthenticationJsonBodyBuilder().createJsonBody(model));
109 void creatingDcaeControlLoopJsonBody_returnsJsonInString() {
111 String closedLoopEventClient = "DCAE.BBS_mSInstance";
112 String policyVersion = "1.0.0.5";
113 String policyName = "CPE_Authentication";
115 "service=HSIAService,type=SampleType,"
116 + "closedLoopControlName=CL-CPE_A-d925ed73-8231-4d02-9545-db4e101f88f8";
117 String targetType = "VM";
118 long closedLoopAlarmStart = 1484677482204798L;
119 String closedLoopEventStatus = "ONSET";
120 String closedLoopControlName = "ControlLoop-CPE_A-2179b738-fd36-4843-a71a-a8c24c70c88b";
121 String version = "1.0.2";
122 String target = "vserver.vserver-name";
123 String requestId = "97964e10-686e-4790-8c45-bdfa61df770f";
124 String from = "DCAE";
126 Map<String, String> aaiEnrichmentData = new LinkedHashMap<>();
127 aaiEnrichmentData.put("service-information.service-instance-id", "service-instance-id-example");
128 aaiEnrichmentData.put("cvlan-id", "example cvlan-id");
129 aaiEnrichmentData.put("svlan-id", "example svlan-id");
131 String template = "{"
132 + "\"closedLoopEventClient\":\"%s\","
133 + "\"policyVersion\":\"%s\","
134 + "\"policyName\":\"%s\","
135 + "\"policyScope\":\"%s\","
136 + "\"target_type\":\"%s\","
138 + "\"service-information.service-instance-id\":\"service-instance-id-example\","
139 + "\"cvlan-id\":\"example cvlan-id\","
140 + "\"svlan-id\":\"example svlan-id\""
142 + "\"closedLoopAlarmStart\":%s,"
143 + "\"closedLoopEventStatus\":\"%s\","
144 + "\"closedLoopControlName\":\"%s\","
145 + "\"version\":\"%s\","
146 + "\"target\":\"%s\","
147 + "\"requestID\":\"%s\","
152 ControlLoopPublisherDmaapModel model = ImmutableControlLoopPublisherDmaapModel.builder()
153 .closedLoopEventClient(closedLoopEventClient)
154 .policyVersion(policyVersion)
155 .policyName(policyName)
156 .policyScope(policyScope)
157 .targetType(targetType)
158 .aaiEnrichmentData(aaiEnrichmentData)
159 .closedLoopAlarmStart(closedLoopAlarmStart)
160 .closedLoopEventStatus(closedLoopEventStatus)
161 .closedLoopControlName(closedLoopControlName)
164 .requestId(requestId)
168 String expectedResult = String.format(template,
169 closedLoopEventClient,
174 closedLoopAlarmStart,
175 closedLoopEventStatus,
176 closedLoopControlName,
182 assertEquals(expectedResult, new ControlLoopJsonBodyBuilder().createJsonBody(model));
186 void pnfAaiObject_IsSerializedSuccessfully() {
188 GsonBuilder gsonBuilder = new GsonBuilder();
189 ServiceLoader.load(TypeAdapterFactory.class).forEach(gsonBuilder::registerTypeAdapterFactory);
190 Gson gson = gsonBuilder.create();
192 String pnfName = "NokiaCorrelationID";
193 String swVersion = "1.2";
195 String template = "{"
196 + "\"pnf-name\":\"%s\","
197 + "\"in-maint\":true,"
198 + "\"sw-version\":\"%s\","
199 + "\"relationship-list\":{"
200 + "\"relationship\":["
202 + "\"related-to\":\"service-instance\","
203 + "\"relationship-label\":\"org.onap.relationships.inventory.ComposedOf\","
204 + "\"related-link\":\"/aai/v14/business/customers/customer/Demonstration/service-subscriptions"
205 + "/service-subscription/BBS/service-instances/service-instance/84003b26-6b76-4c75-b805-7b14ab4ffaef\","
206 + "\"relationship-data\":["
208 + "\"relationship-key\":\"customer.global-customer-id\","
209 + "\"relationship-value\":\"Demonstration\""
212 + "\"relationship-key\":\"service-subscription.service-type\","
213 + "\"relationship-value\":\"BBS\""
216 + "\"relationship-key\":\"service-instance.service-instance-id\","
217 + "\"relationship-value\":\"84003b26-6b76-4c75-b805-7b14ab4ffaef\""
220 + "\"related-to-property\":["
222 + "\"property-key\":\"service-instance.service-instance-name\","
223 + "\"property-value\":\"bbs-instance\""
228 + "\"related-to\":\"platform\","
229 + "\"relationship-label\":\"org.onap.relationships.inventory.Uses\","
230 + "\"related-link\":\"/aai/v14/business/platforms/platform/bbs-platform\","
231 + "\"relationship-data\":["
233 + "\"relationship-key\":\"platform.platform-name\","
234 + "\"relationship-value\":\"bbs-platform\""
242 // Build Relationship Data
243 RelationshipListAaiObject.RelationshipEntryAaiObject firstRelationshipEntry =
244 ImmutableRelationshipEntryAaiObject.builder()
245 .relatedTo("service-instance")
246 .relatedLink("/aai/v14/business/customers/customer/Demonstration/service-subscriptions"
247 + "/service-subscription/BBS/service-instances"
248 + "/service-instance/84003b26-6b76-4c75-b805-7b14ab4ffaef")
249 .relationshipLabel("org.onap.relationships.inventory.ComposedOf")
250 .relationshipData(Arrays.asList(
251 ImmutableRelationshipDataEntryAaiObject.builder()
252 .relationshipKey("customer.global-customer-id")
253 .relationshipValue("Demonstration").build(),
254 ImmutableRelationshipDataEntryAaiObject.builder()
255 .relationshipKey("service-subscription.service-type")
256 .relationshipValue("BBS").build(),
257 ImmutableRelationshipDataEntryAaiObject.builder()
258 .relationshipKey("service-instance.service-instance-id")
259 .relationshipValue("84003b26-6b76-4c75-b805-7b14ab4ffaef").build())
261 .relatedToProperties(Collections.singletonList(
262 ImmutablePropertyAaiObject.builder()
263 .propertyKey("service-instance.service-instance-name")
264 .propertyValue("bbs-instance").build())
268 RelationshipListAaiObject.RelationshipEntryAaiObject secondRelationshipEntry =
269 ImmutableRelationshipEntryAaiObject.builder()
270 .relatedTo("platform")
271 .relatedLink("/aai/v14/business/platforms/platform/bbs-platform")
272 .relationshipLabel("org.onap.relationships.inventory.Uses")
273 .relationshipData(Collections.singletonList(ImmutableRelationshipDataEntryAaiObject.builder()
274 .relationshipKey("platform.platform-name")
275 .relationshipValue("bbs-platform").build()))
278 RelationshipListAaiObject relationshipListAaiObject = ImmutableRelationshipListAaiObject.builder()
279 .relationshipEntries(Arrays.asList(firstRelationshipEntry, secondRelationshipEntry))
282 // Finally construct PNF object data
283 PnfAaiObject pnfAaiObject = ImmutablePnfAaiObject.builder()
285 .isInMaintenance(true)
286 .swVersion(swVersion)
287 .relationshipListAaiObject(relationshipListAaiObject)
291 String jsonPnfObject = String.format(template, pnfName, swVersion);
293 assertEquals(jsonPnfObject, gson.toJson(pnfAaiObject));
294 assertEquals(pnfAaiObject, gson.fromJson(jsonPnfObject, ImmutablePnfAaiObject.class));
298 void serviceInstanceAaiObject_IsSerializedSuccessfully() {
300 GsonBuilder gsonBuilder = new GsonBuilder();
301 ServiceLoader.load(TypeAdapterFactory.class).forEach(gsonBuilder::registerTypeAdapterFactory);
302 Gson gson = gsonBuilder.create();
304 String serviceInstanceId = "84003b26-6b76-4c75-b805-7b14ab4ffaef";
305 String orchestrationStatus = "active";
307 String template = "{"
308 + "\"service-instance-id\":\"%s\","
309 + "\"orchestration-status\":\"%s\","
310 + "\"relationship-list\":{"
311 + "\"relationship\":["
313 + "\"related-to\":\"service-instance\","
314 + "\"relationship-label\":\"org.onap.relationships.inventory.ComposedOf\","
315 + "\"related-link\":\"/aai/v14/business/customers/customer/Demonstration/service-subscriptions"
316 + "/service-subscription/BBS-CFS"
317 + "/service-instances/service-instance/bb374844-44e4-488f-8381-fb5a0e3e6989\","
318 + "\"relationship-data\":["
320 + "\"relationship-key\":\"service-instance.service-instance-id\","
321 + "\"relationship-value\":\"bb374844-44e4-488f-8381-fb5a0e3e6989\""
330 + "\"metaname\":\"cvlan\","
331 + "\"metaval\":\"1005\""
337 // Build Relationship Data
338 RelationshipListAaiObject.RelationshipEntryAaiObject relationshipEntry =
339 ImmutableRelationshipEntryAaiObject.builder()
340 .relatedTo("service-instance")
341 .relatedLink("/aai/v14/business/customers/customer/Demonstration/service-subscriptions"
342 + "/service-subscription/BBS-CFS/service-instances"
343 + "/service-instance/bb374844-44e4-488f-8381-fb5a0e3e6989")
344 .relationshipLabel("org.onap.relationships.inventory.ComposedOf")
345 .relationshipData(Collections.singletonList(ImmutableRelationshipDataEntryAaiObject.builder()
346 .relationshipKey("service-instance.service-instance-id")
347 .relationshipValue("bb374844-44e4-488f-8381-fb5a0e3e6989").build()))
350 RelationshipListAaiObject relationshipListAaiObject = ImmutableRelationshipListAaiObject.builder()
351 .relationshipEntries(Collections.singletonList(relationshipEntry))
354 MetadataListAaiObject.MetadataEntryAaiObject metadataEntry =
355 ImmutableMetadataEntryAaiObject.builder()
360 MetadataListAaiObject metadataListAaiObject = ImmutableMetadataListAaiObject.builder()
361 .metadataEntries(Collections.singletonList(metadataEntry))
364 // Finally construct Service Instance object data
365 ServiceInstanceAaiObject serviceInstanceAaiObject = ImmutableServiceInstanceAaiObject.builder()
366 .serviceInstanceId(serviceInstanceId)
367 .orchestrationStatus(orchestrationStatus)
368 .relationshipListAaiObject(relationshipListAaiObject)
369 .metadataListAaiObject(metadataListAaiObject)
373 String jsonServiceInstanceObject = String.format(template, serviceInstanceId, orchestrationStatus);
375 assertEquals(jsonServiceInstanceObject, gson.toJson(serviceInstanceAaiObject));
376 assertEquals(serviceInstanceAaiObject, gson.fromJson(jsonServiceInstanceObject,
377 ImmutableServiceInstanceAaiObject.class));