a7ee7f5ac72d53deffb3b531a79a47dfbb3d7906
[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.openecomp.mso.adapters.catalogdb.catalogrest;
22
23 import java.io.StringReader;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28
29 import javax.json.Json;
30 import javax.json.JsonArray;
31 import javax.json.JsonObject;
32 import javax.json.JsonReader;
33
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mockito;
37 import org.mockito.runners.MockitoJUnitRunner;
38 import org.openecomp.mso.db.catalog.beans.VnfResourceCustomization;
39
40 import static org.junit.Assert.*;
41
42
43 @RunWith(MockitoJUnitRunner.class)
44 public class CatalogQueryTest {
45
46
47     @Test
48     public void catalogQuerySetTemplateImpl_Test() {
49         CatalogQuery mockCatalogQuery = Mockito.mock(CatalogQuery.class);
50         Mockito.doCallRealMethod().when(mockCatalogQuery).setTemplate(Mockito.anyString(), Mockito.anyMapOf(String.class, String.class));
51
52         Map<String, String> valueMap = new HashMap<>();
53         valueMap.put("somekey", "somevalue");
54
55         String ret = mockCatalogQuery.setTemplate("<somekey>", valueMap);
56
57         assertTrue(ret.equalsIgnoreCase("somevalue"));
58     }
59
60     @Test
61     public void smartToJson_Test() {
62         List<VnfResourceCustomization> paramList = new ArrayList<>();
63         VnfResourceCustomization d1 = new VnfResourceCustomization();
64         d1.setModelCustomizationUuid("16ea3e56-a8ce-4ad7-8edd-4d2eae095391");
65         d1.setModelInstanceName("RG_6-26_mog11 0");
66         d1.setVersion("v1");
67         paramList.add(d1);
68
69         QueryServiceVnfs qryResp = new QueryServiceVnfs(paramList);
70         QueryServiceVnfs mockCatalogQuery = Mockito.spy(qryResp);
71         Mockito.doCallRealMethod().when(mockCatalogQuery).smartToJSON();
72         String ret = qryResp.smartToJSON();
73         // System.out.println(ret);
74
75         JsonReader reader = Json.createReader(new StringReader(ret.replaceAll("\r?\n", "")));
76         JsonObject respObj = reader.readObject();
77         reader.close();
78         JsonArray jArray = respObj.getJsonArray("serviceVnfs");
79         assertEquals(jArray.size(), 1);
80         assertEquals(jArray.getJsonObject(0).getString("modelCustomizationUuid"), "16ea3e56-a8ce-4ad7-8edd-4d2eae095391");
81     }
82
83     @Test
84     public void toJsonString_Test() {
85         CatalogQueryExtendedTest mockCatalogQuery = Mockito.mock(CatalogQueryExtendedTest.class);
86         Mockito.doCallRealMethod().when(mockCatalogQuery).JSON2(Mockito.anyBoolean(), Mockito.anyBoolean());
87         String ret = mockCatalogQuery.JSON2(true, true);
88
89         // System.out.println(ret);
90
91         JsonReader reader = Json.createReader(new StringReader(ret.replaceAll("\r?\n", "")));
92         JsonObject respObj = reader.readObject();
93         reader.close();
94         JsonArray jArray = respObj.getJsonArray("serviceVnfs");
95         assertEquals(jArray.size(), 1);
96         assertEquals(jArray.getJsonObject(0).getString("modelCustomizationUuid"), "16ea3e56-a8ce-4ad7-8edd-4d2eae095391");
97         assertEquals(jArray.getJsonObject(0).getString("version"), "v2");
98     }
99
100     class CatalogQueryExtendedTest extends CatalogQuery {
101         @Override
102         public String JSON2(boolean isArray, boolean isEmbed) {
103             return "{\"serviceVnfs\":[{\"version\":\"v2\",\"modelCustomizationUuid\":\"16ea3e56-a8ce-4ad7-8edd-4d2eae095391\",\"modelInstanceName\":\"ciVFOnboarded-FNAT-aab06c41 1\",\"created\":null,\"vnfResourceModelUuid\":null,\"vnfResourceModelUUID\":null,\"minInstances\":null,\"maxInstances\":null,\"availabilityZoneMaxCount\":null,\"vnfResource\":null,\"nfFunction\":null,\"nfType\":null,\"nfRole\":null,\"nfNamingCode\":null,\"multiStageDesign\":null,\"vfModuleCustomizations\":null,\"serviceResourceCustomizations\":null,\"creationTimestamp\":null}]}";
104         }
105
106     }
107
108 }
109
110