Catalog DB Adapter test cases
[so.git] / adapters / mso-catalog-db-adapter / src / test / java / org / openecomp / mso / adapters / catalogdb / catalogrest / CatalogQueryTest.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.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<String,String>();
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         {
63         List<VnfResourceCustomization> paramList = new ArrayList<VnfResourceCustomization>();
64         VnfResourceCustomization d1 = new VnfResourceCustomization();
65         d1.setModelCustomizationUuid("16ea3e56-a8ce-4ad7-8edd-4d2eae095391");
66         d1.setModelInstanceName("RG_6-26_mog11 0");
67         d1.setVersion("v1");
68         paramList.add(d1);
69         
70         QueryServiceVnfs qryResp = new QueryServiceVnfs(paramList);
71         QueryServiceVnfs mockCatalogQuery = Mockito.spy(qryResp);
72         Mockito.doCallRealMethod().when(mockCatalogQuery).smartToJSON();
73                 String ret = qryResp.smartToJSON();
74                 // System.out.println(ret);
75                 
76         JsonReader reader = Json.createReader(new StringReader(ret.replaceAll("\r?\n", "")));
77         JsonObject respObj = reader.readObject();
78         reader.close();
79         JsonArray jArray = respObj.getJsonArray("serviceVnfs");
80                 assertEquals(jArray.size(), 1);
81                 assertEquals(jArray.getJsonObject(0).getString("modelCustomizationUuid"), "16ea3e56-a8ce-4ad7-8edd-4d2eae095391");
82         }       
83         
84         @Test
85         public void toJsonString_Test()
86         {
87                 CatalogQueryExtendedTest mockCatalogQuery = Mockito.mock(CatalogQueryExtendedTest.class);
88                 Mockito.doCallRealMethod().when(mockCatalogQuery).JSON2(Mockito.anyBoolean(), Mockito.anyBoolean());
89                 String ret = mockCatalogQuery.JSON2(true, true);
90                 
91                 // System.out.println(ret);
92                 
93         JsonReader reader = Json.createReader(new StringReader(ret.replaceAll("\r?\n", "")));
94         JsonObject respObj = reader.readObject();
95         reader.close();
96         JsonArray jArray = respObj.getJsonArray("serviceVnfs");
97                 assertEquals(jArray.size(), 1);
98                 assertEquals(jArray.getJsonObject(0).getString("modelCustomizationUuid"), "16ea3e56-a8ce-4ad7-8edd-4d2eae095391");
99                 assertEquals(jArray.getJsonObject(0).getString("version"), "v2");
100         }       
101         
102         class CatalogQueryExtendedTest extends CatalogQuery{
103                 @Override
104                 public String JSON2(boolean isArray, boolean isEmbed) {
105                         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}]}";
106                 }
107                 
108         }       
109         
110 }
111
112