fd43b8bec2c786473c95430848383f753eb48a99
[dcaegen2/services.git] /
1 /*
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.bbs.event.processor.model;
22
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24
25 import com.google.gson.Gson;
26 import com.google.gson.GsonBuilder;
27 import com.google.gson.TypeAdapterFactory;
28
29 import java.util.Arrays;
30 import java.util.Collections;
31 import java.util.LinkedHashMap;
32 import java.util.Map;
33 import java.util.ServiceLoader;
34
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;
39
40 class GsonSerializationsTest {
41
42     @Test
43     void creatingReRegistrationJsonBody_returnsJsonInString() {
44
45         String correlationId = "NokiaCorrelationId";
46         String attachmentPoint = "olt2/1/1";
47         String remoteId = "RemoteId";
48         String cvlan = "1005";
49         String svlan = "100";
50
51         String template = "{"
52                 + "\"correlationId\":\"%s\","
53                 + "\"attachment-point\":\"%s\","
54                 + "\"remote-id\":\"%s\","
55                 + "\"cvlan\":\"%s\","
56                 + "\"svlan\":\"%s\""
57                 + "}";
58
59         ReRegistrationConsumerDmaapModel model = ImmutableReRegistrationConsumerDmaapModel.builder()
60                 .correlationId(correlationId)
61                 .attachmentPoint(attachmentPoint)
62                 .remoteId(remoteId)
63                 .cVlan(cvlan)
64                 .sVlan(svlan)
65                 .build();
66
67
68         String expectedResult = String.format(template, correlationId, attachmentPoint, remoteId, cvlan, svlan);
69
70         assertEquals(expectedResult, new ReRegistrationJsonBodyBuilder().createJsonBody(model));
71     }
72
73     @Test
74     void creatingCpeAuthenticationJsonBody_returnsJsonInString() {
75
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";
82
83         String template = "{"
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\""
90                 + "}";
91
92         CpeAuthenticationConsumerDmaapModel model = ImmutableCpeAuthenticationConsumerDmaapModel.builder()
93                 .correlationId(correlationId)
94                 .oldAuthenticationState(oldAuthenticationState.getNameInOnap())
95                 .newAuthenticationState(newAuthenticationState.getNameInOnap())
96                 .stateInterface(stateInterface)
97                 .rgwMacAddress(rgwMacAddress)
98                 .swVersion(swVersion)
99                 .build();
100
101
102         String expectedResult = String.format(template, correlationId, oldAuthenticationState.getNameInOnap(),
103                 newAuthenticationState.getNameInOnap(), stateInterface, rgwMacAddress, swVersion);
104
105         assertEquals(expectedResult, new CpeAuthenticationJsonBodyBuilder().createJsonBody(model));
106     }
107
108     @Test
109     void creatingDcaeControlLoopJsonBody_returnsJsonInString() {
110
111         String closedLoopEventClient = "DCAE.BBS_mSInstance";
112         String policyVersion = "1.0.0.5";
113         String policyName = "CPE_Authentication";
114         String policyScope =
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";
125
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");
130
131         String template = "{"
132                 + "\"closedLoopEventClient\":\"%s\","
133                 + "\"policyVersion\":\"%s\","
134                 + "\"policyName\":\"%s\","
135                 + "\"policyScope\":\"%s\","
136                 + "\"target_type\":\"%s\","
137                 + "\"AAI\":{"
138                 + "\"service-information.service-instance-id\":\"service-instance-id-example\","
139                 + "\"cvlan-id\":\"example cvlan-id\","
140                 + "\"svlan-id\":\"example svlan-id\""
141                 + "},"
142                 + "\"closedLoopAlarmStart\":%s,"
143                 + "\"closedLoopEventStatus\":\"%s\","
144                 + "\"closedLoopControlName\":\"%s\","
145                 + "\"version\":\"%s\","
146                 + "\"target\":\"%s\","
147                 + "\"requestID\":\"%s\","
148                 + "\"from\":\"%s\""
149                 + "}";
150
151
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)
162                 .version(version)
163                 .target(target)
164                 .requestId(requestId)
165                 .originator(from)
166                 .build();
167
168         String expectedResult = String.format(template,
169                 closedLoopEventClient,
170                 policyVersion,
171                 policyName,
172                 policyScope,
173                 targetType,
174                 closedLoopAlarmStart,
175                 closedLoopEventStatus,
176                 closedLoopControlName,
177                 version,
178                 target,
179                 requestId,
180                 from);
181
182         assertEquals(expectedResult, new ControlLoopJsonBodyBuilder().createJsonBody(model));
183     }
184
185     @Test
186     void pnfAaiObject_IsSerializedSuccessfully() {
187
188         GsonBuilder gsonBuilder = new GsonBuilder();
189         ServiceLoader.load(TypeAdapterFactory.class).forEach(gsonBuilder::registerTypeAdapterFactory);
190         Gson gson = gsonBuilder.create();
191
192         String pnfName = "NokiaCorrelationID";
193         String swVersion = "1.2";
194
195         String template = "{"
196                 + "\"pnf-name\":\"%s\","
197                 + "\"in-maint\":true,"
198                 + "\"sw-version\":\"%s\","
199                 + "\"relationship-list\":{"
200                 + "\"relationship\":["
201                 + "{"
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\":["
207                 + "{"
208                 + "\"relationship-key\":\"customer.global-customer-id\","
209                 + "\"relationship-value\":\"Demonstration\""
210                 + "},"
211                 + "{"
212                 + "\"relationship-key\":\"service-subscription.service-type\","
213                 + "\"relationship-value\":\"BBS\""
214                 + "},"
215                 + "{"
216                 + "\"relationship-key\":\"service-instance.service-instance-id\","
217                 + "\"relationship-value\":\"84003b26-6b76-4c75-b805-7b14ab4ffaef\""
218                 + "}"
219                 + "],"
220                 + "\"related-to-property\":["
221                 + "{"
222                 + "\"property-key\":\"service-instance.service-instance-name\","
223                 + "\"property-value\":\"bbs-instance\""
224                 + "}"
225                 + "]"
226                 + "},"
227                 + "{"
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\":["
232                 + "{"
233                 + "\"relationship-key\":\"platform.platform-name\","
234                 + "\"relationship-value\":\"bbs-platform\""
235                 + "}"
236                 + "]"
237                 + "}"
238                 + "]"
239                 + "}"
240                 + "}";
241
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())
260                 )
261                         .relatedToProperties(Collections.singletonList(
262                                 ImmutablePropertyAaiObject.builder()
263                                         .propertyKey("service-instance.service-instance-name")
264                                         .propertyValue("bbs-instance").build())
265                         )
266                 .build();
267
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()))
276                 .build();
277
278         RelationshipListAaiObject relationshipListAaiObject = ImmutableRelationshipListAaiObject.builder()
279                 .relationshipEntries(Arrays.asList(firstRelationshipEntry, secondRelationshipEntry))
280                 .build();
281
282         // Finally construct PNF object data
283         PnfAaiObject pnfAaiObject = ImmutablePnfAaiObject.builder()
284                 .pnfName(pnfName)
285                 .isInMaintenance(true)
286                 .swVersion(swVersion)
287                 .relationshipListAaiObject(relationshipListAaiObject)
288                 .build();
289
290
291         String jsonPnfObject = String.format(template, pnfName, swVersion);
292
293         assertEquals(jsonPnfObject, gson.toJson(pnfAaiObject));
294         assertEquals(pnfAaiObject, gson.fromJson(jsonPnfObject, ImmutablePnfAaiObject.class));
295     }
296
297     @Test
298     void serviceInstanceAaiObject_IsSerializedSuccessfully() {
299
300         GsonBuilder gsonBuilder = new GsonBuilder();
301         ServiceLoader.load(TypeAdapterFactory.class).forEach(gsonBuilder::registerTypeAdapterFactory);
302         Gson gson = gsonBuilder.create();
303
304         String serviceInstanceId = "84003b26-6b76-4c75-b805-7b14ab4ffaef";
305         String orchestrationStatus = "active";
306
307         String template = "{"
308                 + "\"service-instance-id\":\"%s\","
309                 + "\"orchestration-status\":\"%s\","
310                 + "\"relationship-list\":{"
311                 + "\"relationship\":["
312                 + "{"
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\":["
319                 + "{"
320                 + "\"relationship-key\":\"service-instance.service-instance-id\","
321                 + "\"relationship-value\":\"bb374844-44e4-488f-8381-fb5a0e3e6989\""
322                 + "}"
323                 + "]"
324                 + "}"
325                 + "]"
326                 + "},"
327                 + "\"metadata\":{"
328                 + "\"metadatum\":["
329                 + "{"
330                 + "\"metaname\":\"cvlan\","
331                 + "\"metaval\":\"1005\""
332                 + "}"
333                 + "]"
334                 + "}"
335                 + "}";
336
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()))
348                         .build();
349
350         RelationshipListAaiObject relationshipListAaiObject = ImmutableRelationshipListAaiObject.builder()
351                 .relationshipEntries(Collections.singletonList(relationshipEntry))
352                 .build();
353
354         MetadataListAaiObject.MetadataEntryAaiObject metadataEntry =
355                 ImmutableMetadataEntryAaiObject.builder()
356                 .metaname("cvlan")
357                 .metavalue("1005")
358                 .build();
359
360         MetadataListAaiObject metadataListAaiObject = ImmutableMetadataListAaiObject.builder()
361                 .metadataEntries(Collections.singletonList(metadataEntry))
362                 .build();
363
364         // Finally construct Service Instance object data
365         ServiceInstanceAaiObject serviceInstanceAaiObject = ImmutableServiceInstanceAaiObject.builder()
366                 .serviceInstanceId(serviceInstanceId)
367                 .orchestrationStatus(orchestrationStatus)
368                 .relationshipListAaiObject(relationshipListAaiObject)
369                 .metadataListAaiObject(metadataListAaiObject)
370                 .build();
371
372
373         String jsonServiceInstanceObject = String.format(template, serviceInstanceId, orchestrationStatus);
374
375         assertEquals(jsonServiceInstanceObject, gson.toJson(serviceInstanceAaiObject));
376         assertEquals(serviceInstanceAaiObject, gson.fromJson(jsonServiceInstanceObject,
377                 ImmutableServiceInstanceAaiObject.class));
378     }
379 }