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