98414964be87712810dd37eec3ff52894cb98384
[appc.git] / appc-inbound / appc-design-services / provider / src / test / java / org / onap / appc / design / validator / TestDesignDBServices.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2018 IBM
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  *
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.appc.design.validator;
23
24 import static org.junit.Assert.*;
25
26 import java.sql.ResultSet;
27 import java.util.HashMap;
28 import java.util.Map;
29
30 import org.junit.Before;
31 import org.junit.Ignore;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.Mock;
35 import org.mockito.Mockito;
36 import static org.mockito.Mockito.when;
37 import org.mockito.runners.MockitoJUnitRunner;
38 import org.onap.appc.design.dbervices.DbService;
39 import org.onap.appc.design.dbervices.DesignDBService;
40 import org.onap.appc.design.services.util.ArtifactHandlerClient;
41 import org.powermock.core.classloader.annotations.PrepareForTest;
42 import org.powermock.reflect.Whitebox;
43
44 @RunWith(MockitoJUnitRunner.class)
45 @PrepareForTest(DesignDBService.class)
46 public class TestDesignDBServices {
47
48     private DesignDBService designDbService;
49
50     private DesignDBService spyDesign;
51
52     @Mock
53     private DbService dbservice;
54
55     @Mock
56     private ResultSet rs;
57
58     @Mock
59     private ArtifactHandlerClient artifactHandlerClient;
60
61     @Before
62     public void setUp() throws Exception {
63         Map<String, String> outputMessage = new HashMap<>();
64         designDbService = DesignDBService.initialise();
65         Whitebox.setInternalState(designDbService, "dbservice", dbservice);
66         // Whitebox.setInternalState(designDbService, "ac",
67         // artifactHandlerClient);
68         Mockito.doReturn(outputMessage).when(artifactHandlerClient).execute(Mockito.anyString(), Mockito.anyString());
69         when(dbservice.getDBData(Mockito.anyString(), Mockito.anyList())).thenReturn(rs);
70         when(rs.next()).thenReturn(true, true, false);
71         Mockito.doReturn(true).when(dbservice).updateDBData(Mockito.anyString(), Mockito.anyList());
72
73         spyDesign = Mockito.spy(designDbService);
74     }
75
76     @Test
77     public void testSetStatus() throws Exception {
78         String payload = "{\"userID\": \"1234\", \"vnf-type\" : \"DesigTest-VNF\",\"artifact_status\":\"TestArtifactStatus\",\"action_status\":\"TestAction\",\"vnfc-type\":\"TestVnfc\" }";
79         String json = Whitebox.invokeMethod(spyDesign, "setStatus", payload, "1234");
80         assertEquals(true, json.contains("1234"));
81     }
82
83     @Test
84     public void testGetDesigns() throws Exception {
85         String payload = "{\"userID\": \"0000\", \"vnf-type\" : \"DesigTest-VNF\",\"artifact_status\":\"TestArtifactStatus\",\"action_status\":\"TestAction\" }";
86         String result = Whitebox.invokeMethod(spyDesign, "getDesigns", payload, "1234");
87         assertEquals(true, result.contains("0000"));
88     }
89
90     @Test
91     public void testGetDesignsWithFilter() throws Exception {
92         String payload = "{\"userID\": \"0000\", \"vnf-type\" : \"DesigTest-VNF\",\"filter\":\"TestFilter\" }";
93         String result = Whitebox.invokeMethod(spyDesign, "getDesigns", payload, "1234");
94         assertEquals(true, result.contains("0000"));
95     }
96
97     @Test
98     public void testSetInCartr() throws Exception {
99         String payload = "{\"userID\": \"0000\", \"vnf-type\" : \"DesigTest-VNF\",\"inCart\":\"TestInCart\",\"vnfc-type\":\"TestVnfc\"}";
100         String result = Whitebox.invokeMethod(spyDesign, "setInCart", payload, "1234");
101         assertEquals(true, result.contains("success"));
102     }
103
104     @Test
105     public void testSetProtocolReference() throws Exception {
106         String payload = "{\"userID\": \"0000\", \"vnf-type\" : \"DesigTest-VNF\",\"action\":\"TestAction\",\"action-level\":\"TestLevel\",\"protocol\":\"TestProtocol\",\"vnfc-type\":\"TestVnfc\",\"template\":\"TestTemplate\"}";
107         String result = Whitebox.invokeMethod(spyDesign, "setProtocolReference", payload, "1234");
108         assertEquals(true, result.contains("success"));
109     }
110
111     @Ignore
112     @Test(expected = ExceptionInInitializerError.class)
113     public void TestUploadArtifact() throws Exception {
114         String payload = " { \"userID\": \"00000\", \"vnf-type\" : \"DesigTest-VNF\", \"action\" : \"Configure\", \"artifact-name\":\"DesignRestArtifact_reference\",\"artifact-version\" :\"0.01\",\"artifact-type\" :\"DESIGNTOOL-TEST\",\"artifact-contents\":  \"TestContents\"} ";
115         Whitebox.invokeMethod(spyDesign, "uploadArtifact", payload, "1234");
116     }
117
118     @Test
119     public void testLinkstatusRelationShip() throws Exception {
120         String payload = "{\"userID\": \"0000\", \"vnf-type\" : \"DesigTest-VNF\",\"action\":\"TestAction\",\"action-level\":\"TestLevel\",\"protocol\":\"TestProtocol\",\"vnfc-type\":\"TestVnfc\",\"template\":\"TestTemplate\"}";
121         String result = Whitebox.invokeMethod(spyDesign, "linkstatusRelationShip", 0, 0, payload);
122         assertEquals(null, result);
123     }
124
125     @Test
126     public void testGetSDCReferenceID() throws Exception {
127         String payload = "{\"userID\": \"0000\", \"vnf-type\" : \"DesigTest-VNF\",\"action\":\"TestAction\",\"action-level\":\"TestLevel\",\"protocol\":\"TestProtocol\",\"vnfc-type\":\"TestVnfc\",\"artifact-type\" :\"DESIGNTOOL-TEST\", \"artifact-name\":\"DesignRestArtifact_reference\"}";
128         int result = Whitebox.invokeMethod(spyDesign, "getSDCReferenceID", payload);
129         assertEquals(0, result);
130     }
131
132     @Test
133     public void testgetDataFromActionStatus() throws Exception {
134         String payload = "{\"userID\": \"0000\", \"vnf-type\" : \"DesigTest-VNF\",\"action\":\"TestAction\",\"action-level\":\"TestLevel\",\"protocol\":\"TestProtocol\",\"vnfc-type\":\"TestVnfc\",\"artifact-type\" :\"DESIGNTOOL-TEST\", \"artifact-name\":\"DesignRestArtifact_reference\"}";
135         String result = Whitebox.invokeMethod(spyDesign, "getDataFromActionStatus", payload, "Data");
136         assertEquals(null, result);
137     }
138
139     @Test
140     public void testsetActionStatus() throws Exception {
141         String payload = "{\"userID\": \"0000\", \"vnf-type\" : \"DesigTest-VNF\",\"action\":\"TestAction\",\"action-level\":\"TestLevel\",\"protocol\":\"TestProtocol\",\"vnfc-type\":\"TestVnfc\",\"template\":\"TestTemplate\"}";
142         String result = Whitebox.invokeMethod(spyDesign, "setActionStatus", payload, "Status");
143         assertEquals(null, result);
144     }
145
146     @Test
147     public void testcreateArtifactTrackingRecord() throws Exception {
148         String payload = "{\"userID\": \"0000\", \"vnf-type\" : \"DesigTest-VNF\",\"action\":\"TestAction\",\"technology\":\"TestTech\",\"action-level\":\"TestLevel\",\"protocol\":\"TestProtocol\",\"vnfc-type\":\"TestVnfc\",\"template\":\"TestTemplate\"}";
149         String result = Whitebox.invokeMethod(spyDesign, "createArtifactTrackingRecord", payload, "1234", 0, 0);
150         assertEquals(null, result);
151     }
152
153     @Test
154     public void testgetSDCArtifactIDbyRequestID() throws Exception {
155         String payload = "{\"userID\": \"0000\", \"vnf-type\" : \"DesigTest-VNF\",\"action\":\"TestAction\",\"action-level\":\"TestLevel\",\"protocol\":\"TestProtocol\",\"vnfc-type\":\"TestVnfc\",\"artifact-type\" :\"DESIGNTOOL-TEST\", \"artifact-name\":\"DesignRestArtifact_reference\"}";
156         int result = Whitebox.invokeMethod(spyDesign, "getSDCArtifactIDbyRequestID", payload);
157         assertEquals(0, result);
158     }
159
160     @Test(expected = Exception.class)
161     public void testgetArtifact() throws Exception {
162         String payload = "{\"userID\": \"0000\", \"vnf-type\" : \"DesigTest-VNF\",\"action\":\"TestAction\",\"action-level\":\"TestLevel\",\"protocol\":\"TestProtocol\",\"vnfc-type\":\"TestVnfc\",\"artifact-type\" :\"DESIGNTOOL-TEST\", \"artifact-name\":\"DesignRestArtifact_reference\"}";
163         String result = Whitebox.invokeMethod(spyDesign, "getArtifact", payload, "1234");
164     }
165
166     @Test
167     public void testgetStatus() throws Exception {
168         String payload = "{\"userID\": \"0000\", \"vnf-type\" : \"DesigTest-VNF\",\"action\":\"TestAction\",\"action-level\":\"TestLevel\",\"protocol\":\"TestProtocol\",\"vnfc-type\":\"TestVnfc\",\"artifact-type\" :\"DESIGNTOOL-TEST\", \"artifact-name\":\"DesignRestArtifact_reference\"}";
169         String result = Whitebox.invokeMethod(spyDesign, "getStatus", payload, "1234");
170         assertEquals(true, result.contains("0000"));
171     }
172
173     @Test
174     public void testGetAppcTimestampUTC() throws Exception {
175              String requestId = "1234";
176              DesignDBService design = DesignDBService.initialise();
177              String result =  Whitebox.invokeMethod(design, "getAppcTimestampUTC",requestId);
178              assertTrue(result.endsWith("Z"));
179     }
180 }