1bfa78ad6d98d2a99ac577cda5c812b1d64f1ffb
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / client / sdnc / mapper / NetworkTopologyOperationRequestMapperTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T 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.so.client.sdnc.mapper;
22
23 import static com.shazam.shazamcrest.MatcherAssert.assertThat;
24 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28
29 import java.io.IOException;
30 import java.nio.file.Files;
31 import java.nio.file.Paths;
32 import java.util.HashMap;
33 import java.util.Map;
34
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.InjectMocks;
39 import org.mockito.Spy;
40 import org.mockito.junit.MockitoJUnitRunner;
41 import org.onap.sdnc.northbound.client.model.GenericResourceApiNetworkOperationInformation;
42 import org.onap.sdnc.northbound.client.model.GenericResourceApiRequestActionEnumeration;
43 import org.onap.so.bpmn.servicedecomposition.bbobjects.CloudRegion;
44 import org.onap.so.bpmn.servicedecomposition.bbobjects.Collection;
45 import org.onap.so.bpmn.servicedecomposition.bbobjects.Customer;
46 import org.onap.so.bpmn.servicedecomposition.bbobjects.InstanceGroup;
47 import org.onap.so.bpmn.servicedecomposition.bbobjects.L3Network;
48 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
49 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceSubscription;
50 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestContext;
51 import org.onap.so.bpmn.servicedecomposition.modelinfo.ModelInfoNetwork;
52 import org.onap.so.bpmn.servicedecomposition.modelinfo.ModelInfoServiceInstance;
53 import org.onap.so.client.sdnc.beans.SDNCSvcAction;
54 import org.onap.so.client.sdnc.beans.SDNCSvcOperation;
55
56 import com.fasterxml.jackson.databind.ObjectMapper;
57
58 @RunWith(MockitoJUnitRunner.class)
59 public class NetworkTopologyOperationRequestMapperTest {
60
61         private final static String JSON_FILE_LOCATION = "src/test/resources/__files/BuildingBlocks/";
62
63         private ServiceInstance serviceInstance;
64         private ServiceInstance serviceInstanceNoCollection;
65         private Customer customer;
66         private RequestContext requestContext;
67         private L3Network network;
68         private CloudRegion cloudRegion;
69         
70         @Spy
71         private GeneralTopologyObjectMapper generalTopologyObjectMapper;
72         
73         @InjectMocks
74         private NetworkTopologyOperationRequestMapper mapper = new NetworkTopologyOperationRequestMapper();
75
76         @Before
77         public void before() {
78                 // prepare and set service instance
79                 serviceInstance = new ServiceInstance();
80                 serviceInstanceNoCollection = new ServiceInstance();
81                 ModelInfoServiceInstance modelInfoServiceInstance = new ModelInfoServiceInstance();
82                 modelInfoServiceInstance.setModelInvariantUuid("modelInvariantUuid");
83                 modelInfoServiceInstance.setModelName("modelName");
84                 modelInfoServiceInstance.setModelUuid("modelUuid");
85                 modelInfoServiceInstance.setModelVersion("modelVersion");
86                 serviceInstance.setModelInfoServiceInstance(modelInfoServiceInstance);
87                 serviceInstanceNoCollection.setModelInfoServiceInstance(modelInfoServiceInstance);
88                 // prepare Customer object
89                 customer = new Customer();
90                 customer.setGlobalCustomerId("globalCustomerId");
91                 //serviceInstance.setCustomer(customer);
92                 // set Customer on service instance
93                 ServiceSubscription serviceSubscription = new ServiceSubscription();
94                 serviceSubscription.setServiceType("productFamilyId");
95                 customer.setServiceSubscription(serviceSubscription);
96
97                 // set Customer on service instance
98                 customer.getServiceSubscription().getServiceInstances().add(serviceInstance);
99                 customer.getServiceSubscription().getServiceInstances().add(serviceInstanceNoCollection);
100                 //
101                 InstanceGroup networkInstanceGroup = new InstanceGroup();
102                 networkInstanceGroup.setId("networkInstanceGroupId");
103                 networkInstanceGroup.setInstanceGroupFunction("instanceGroupFunction");
104                 Collection networkCollection = new Collection();
105                 networkCollection.setInstanceGroup(networkInstanceGroup);
106                 serviceInstance.setCollection(networkCollection);
107                 //
108                 requestContext = new RequestContext();
109                 Map<String, Object> userParams = new HashMap<>();
110                 userParams.put("key1", "value1");
111                 requestContext.setUserParams(userParams);
112                 requestContext.setProductFamilyId("productFamilyId");
113                 requestContext.setMsoRequestId("MsoRequestId");
114
115                 network = new L3Network();
116                 network.setNetworkId("TEST_NETWORK_ID");
117                 network.setNetworkName("TEST_NETWORK_NAME");
118                 ModelInfoNetwork modelInfoNetwork = new ModelInfoNetwork();
119                 modelInfoNetwork.setModelInvariantUUID("modelInvariantUuid");
120                 modelInfoNetwork.setModelName("modelName");
121                 modelInfoNetwork.setModelVersion("modelVersion");
122                 modelInfoNetwork.setModelUUID("modelUuid");
123                 modelInfoNetwork.setModelCustomizationUUID("modelCustomizationUUID");
124                 network.setModelInfoNetwork(modelInfoNetwork);
125
126                 cloudRegion = new CloudRegion();
127         }
128
129         @Test
130         public void createGenericResourceApiNetworkOperationInformationTest() throws Exception {
131
132                 GenericResourceApiNetworkOperationInformation networkSDNCrequest = mapper.reqMapper(
133                                 SDNCSvcOperation.NETWORK_TOPOLOGY_OPERATION, SDNCSvcAction.ASSIGN, GenericResourceApiRequestActionEnumeration.CREATENETWORKINSTANCE, network, serviceInstance, customer,
134                                 requestContext, cloudRegion);
135
136                 ObjectMapper omapper = new ObjectMapper();
137                 GenericResourceApiNetworkOperationInformation reqMapper1 = omapper.readValue(
138                                 getJson("genericResourceApiNetworkOperationInformation.json"),
139                                 GenericResourceApiNetworkOperationInformation.class);
140
141                 assertThat(networkSDNCrequest, sameBeanAs(reqMapper1).ignoring("sdncRequestHeader.svcRequestId")
142                                 .ignoring("requestInformation.requestId"));
143                 assertEquals("MsoRequestId", networkSDNCrequest.getRequestInformation().getRequestId());
144         }
145         
146         @Test
147         public void createGenericResourceApiNetworkOperationInformationReqContextNullTest() throws Exception {
148
149                 RequestContext rc = new RequestContext();
150                 rc.setMsoRequestId(null);
151                 GenericResourceApiNetworkOperationInformation networkSDNCrequest = mapper.reqMapper(
152                                 SDNCSvcOperation.NETWORK_TOPOLOGY_OPERATION, SDNCSvcAction.ASSIGN, GenericResourceApiRequestActionEnumeration.CREATENETWORKINSTANCE, network, serviceInstance, customer,
153                                 rc, cloudRegion);
154                 assertNotNull(networkSDNCrequest.getRequestInformation().getRequestId());
155                 GenericResourceApiNetworkOperationInformation networkSDNCrequest2 = mapper.reqMapper(
156                                 SDNCSvcOperation.NETWORK_TOPOLOGY_OPERATION, SDNCSvcAction.ASSIGN, GenericResourceApiRequestActionEnumeration.CREATENETWORKINSTANCE, network, serviceInstance, customer,
157                                 null, cloudRegion);
158                 assertNotNull(networkSDNCrequest2.getRequestInformation().getRequestId());
159         }
160         
161         @Test
162         public void reqMapperTest() throws Exception {
163
164                 GenericResourceApiNetworkOperationInformation networkSDNCrequest = mapper.reqMapper(
165                                 SDNCSvcOperation.NETWORK_TOPOLOGY_OPERATION, SDNCSvcAction.ASSIGN, GenericResourceApiRequestActionEnumeration.CREATENETWORKINSTANCE, network, serviceInstance, customer,
166                                 requestContext, cloudRegion);
167
168                 assertNull(networkSDNCrequest.getServiceInformation().getOnapModelInformation().getModelCustomizationUuid());
169                 assertEquals("modelCustomizationUUID", networkSDNCrequest.getNetworkInformation().getOnapModelInformation().getModelCustomizationUuid());
170         }
171
172         @Test
173         public void reqMapperNoCollectionTest() throws Exception {
174                 GenericResourceApiNetworkOperationInformation networkSDNCrequest = mapper.reqMapper(
175                                 SDNCSvcOperation.NETWORK_TOPOLOGY_OPERATION, SDNCSvcAction.ASSIGN, GenericResourceApiRequestActionEnumeration.CREATENETWORKINSTANCE, network, serviceInstanceNoCollection, customer,
176                                 requestContext, cloudRegion);
177
178                 assertNull(networkSDNCrequest.getServiceInformation().getOnapModelInformation().getModelCustomizationUuid());
179                 assertEquals("modelCustomizationUUID", networkSDNCrequest.getNetworkInformation().getOnapModelInformation().getModelCustomizationUuid());
180         }
181         @Test
182         public void createGenericResourceApiNetworkOperationInformation_UnassignTest() throws Exception {
183
184                 NetworkTopologyOperationRequestMapper mapperUnassign = mapper;
185                 GenericResourceApiNetworkOperationInformation networkSDNCrequestUnassign = mapperUnassign.reqMapper(
186                                 SDNCSvcOperation.NETWORK_TOPOLOGY_OPERATION, SDNCSvcAction.UNASSIGN, GenericResourceApiRequestActionEnumeration.DELETENETWORKINSTANCE, network, serviceInstance, customer,
187                                 requestContext, cloudRegion);
188
189                 ObjectMapper omapperUnassign = new ObjectMapper();
190                 GenericResourceApiNetworkOperationInformation reqMapperUnassign = omapperUnassign.readValue(
191                                 getJson("genericResourceApiNetworkOperationInformationUnAssign.json"),
192                                 GenericResourceApiNetworkOperationInformation.class);
193
194                 assertThat(reqMapperUnassign, sameBeanAs(networkSDNCrequestUnassign).ignoring("sdncRequestHeader.svcRequestId")
195                                 .ignoring("requestInformation.requestId"));
196                 assertEquals("MsoRequestId", networkSDNCrequestUnassign.getRequestInformation().getRequestId());
197                 
198         }
199         
200         @Test
201         public void createGenericResourceApiNetworkOperationInformationNoNetworkNameTest() throws Exception {
202
203                 //set network name NULL
204                 network.setNetworkName(null);
205                 GenericResourceApiNetworkOperationInformation networkSDNCrequest = mapper.reqMapper(
206                                 SDNCSvcOperation.NETWORK_TOPOLOGY_OPERATION, SDNCSvcAction.ASSIGN, GenericResourceApiRequestActionEnumeration.CREATENETWORKINSTANCE, network, serviceInstance, customer,
207                                 requestContext, cloudRegion);
208
209                 ObjectMapper omapper = new ObjectMapper();
210                 GenericResourceApiNetworkOperationInformation reqMapper1 = omapper.readValue(
211                                 getJson("genericResourceApiNetworkOperationInformationNoNetworkName.json"),
212                                 GenericResourceApiNetworkOperationInformation.class);
213
214                 assertThat(reqMapper1, sameBeanAs(networkSDNCrequest).ignoring("sdncRequestHeader.svcRequestId")
215                                 .ignoring("requestInformation.requestId"));
216         }
217
218         /*
219          * Helper method to load JSON data
220          */
221         private String getJson(String filename) throws IOException {
222                 return new String(Files.readAllBytes(Paths.get(JSON_FILE_LOCATION + filename)));
223         }
224
225 }