Junits for design services
[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.AfterClass;
31 import org.junit.Before;
32 import org.junit.BeforeClass;
33 import org.junit.Ignore;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mock;
37 import org.mockito.Mockito;
38 import static org.mockito.Mockito.when;
39 import org.mockito.runners.MockitoJUnitRunner;
40 import org.onap.appc.design.dbervices.DbService;
41 import org.onap.appc.design.dbervices.DesignDBService;
42 import org.onap.appc.design.dbervices.DesignDBService.ArtifactHandlerFactory;
43 import org.onap.appc.design.services.util.ArtifactHandlerClient;
44 import org.powermock.api.mockito.PowerMockito;
45 import org.powermock.core.classloader.annotations.PrepareForTest;
46 import org.powermock.modules.junit4.PowerMockRunner;
47 import org.powermock.reflect.Whitebox;
48
49 @RunWith(PowerMockRunner.class)
50 public class TestDesignDBServices {
51
52     private DesignDBService designDbService;
53
54     private DesignDBService spyDesign;
55
56     @Mock
57     private DbService dbservice;
58
59     @Mock
60     private ResultSet rs;
61
62     @Mock
63     private ArtifactHandlerClient artifactHandlerClient;
64     
65     private static ArtifactHandlerFactory saveFactory;
66         
67     private static ArtifactHandlerFactory factory;
68     
69     @BeforeClass
70     public static void setUpBeforeClass() {
71         saveFactory = Whitebox.getInternalState(DesignDBService.class, "artifactHandlerFactory");
72     }
73     
74     @AfterClass
75     public static void tearDownAfterClass() {
76         Whitebox.setInternalState(DesignDBService.class, "artifactHandlerFactory", saveFactory);
77     }
78
79
80     @Before
81     public void setUp() throws Exception {
82         factory = Mockito.mock(ArtifactHandlerFactory.class);
83         Whitebox.setInternalState(DesignDBService.class, "artifactHandlerFactory", factory);
84         Map<String, String> outputMessage = new HashMap<>();
85         designDbService = DesignDBService.initialise();
86         Whitebox.setInternalState(designDbService, "dbservice", dbservice);
87         //Whitebox.setInternalState(designDbService, "ac",artifactHandlerClient);
88         //Mockito.doReturn(outputMessage).when(artifactHandlerClient).execute(Mockito.anyString(), Mockito.anyString());
89         when(factory.ahi()).thenReturn(artifactHandlerClient);
90         when(dbservice.getDBData(Mockito.anyString(), Mockito.anyList())).thenReturn(rs);
91         when(rs.next()).thenReturn(true, true, false);
92         Mockito.doReturn(true).when(dbservice).updateDBData(Mockito.anyString(), Mockito.anyList());
93
94         spyDesign = PowerMockito.spy(designDbService);
95     }
96      
97     @Test
98     public void testSetStatus() throws Exception {
99         String payload = "{\"userID\": \"1234\", \"vnf-type\" : \"DesigTest-VNF\",\"artifact_status\":\"TestArtifactStatus\",\"action_status\":\"TestAction\",\"vnfc-type\":\"TestVnfc\" }";
100         String json = Whitebox.invokeMethod(spyDesign, "setStatus", payload, "1234");
101         assertEquals(true, json.contains("1234"));
102     }
103
104     @Test
105     public void testGetDesigns() throws Exception {
106         String payload = "{\"userID\": \"0000\", \"vnf-type\" : \"DesigTest-VNF\",\"artifact_status\":\"TestArtifactStatus\",\"action_status\":\"TestAction\" }";
107         String result = Whitebox.invokeMethod(spyDesign, "getDesigns", payload, "1234");
108         assertEquals(true, result.contains("0000"));
109     }
110
111     @Test
112     public void testGetDesignsWithFilter() throws Exception {
113         String payload = "{\"userID\": \"0000\", \"vnf-type\" : \"DesigTest-VNF\",\"filter\":\"TestFilter\" }";
114         String result = Whitebox.invokeMethod(spyDesign, "getDesigns", payload, "1234");
115         assertEquals(true, result.contains("0000"));
116     }
117
118     @Test
119     public void testSetInCartr() throws Exception {
120         String payload = "{\"userID\": \"0000\", \"vnf-type\" : \"DesigTest-VNF\",\"inCart\":\"TestInCart\",\"vnfc-type\":\"TestVnfc\"}";
121         String result = Whitebox.invokeMethod(spyDesign, "setInCart", payload, "1234");
122         assertEquals(true, result.contains("success"));
123     }
124
125     @Test
126     public void testSetProtocolReference() throws Exception {
127         String payload = "{\"userID\": \"0000\", \"vnf-type\" : \"DesigTest-VNF\",\"action\":\"TestAction\",\"action-level\":\"TestLevel\",\"protocol\":\"TestProtocol\",\"vnfc-type\":\"TestVnfc\",\"template\":\"TestTemplate\"}";
128         String result = Whitebox.invokeMethod(spyDesign, "setProtocolReference", payload, "1234");
129         assertEquals(true, result.contains("success"));
130     }
131
132     
133     @Test
134     public void TestUploadArtifact() throws Exception {
135         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\"} ";
136         String result = Whitebox.invokeMethod(spyDesign, "uploadArtifact", payload, "1234");
137         assertEquals(true, result.contains("success"));
138     }
139
140     @Test
141     public void testLinkstatusRelationShip() throws Exception {
142         String payload = "{\"userID\": \"0000\", \"vnf-type\" : \"DesigTest-VNF\",\"action\":\"TestAction\",\"action-level\":\"TestLevel\",\"protocol\":\"TestProtocol\",\"vnfc-type\":\"TestVnfc\",\"template\":\"TestTemplate\"}";
143         String result = Whitebox.invokeMethod(spyDesign, "linkstatusRelationShip", 0, 0, payload);
144         assertEquals(null, result);
145     }
146
147     @Test
148     public void testGetSDCReferenceID() throws Exception {
149         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\"}";
150         int result = Whitebox.invokeMethod(spyDesign, "getSDCReferenceID", payload);
151         assertEquals(0, result);
152     }
153
154     @Test
155     public void testgetDataFromActionStatus() throws Exception {
156         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\"}";
157         String result = Whitebox.invokeMethod(spyDesign, "getDataFromActionStatus", payload, "Data");
158         assertEquals(null, result);
159     }
160
161     @Test
162     public void testsetActionStatus() throws Exception {
163         String payload = "{\"userID\": \"0000\", \"vnf-type\" : \"DesigTest-VNF\",\"action\":\"TestAction\",\"action-level\":\"TestLevel\",\"protocol\":\"TestProtocol\",\"vnfc-type\":\"TestVnfc\",\"template\":\"TestTemplate\"}";
164         String result = Whitebox.invokeMethod(spyDesign, "setActionStatus", payload, "Status");
165         assertEquals(null, result);
166     }
167
168     @Test
169     public void testcreateArtifactTrackingRecord() throws Exception {
170         String payload = "{\"userID\": \"0000\", \"vnf-type\" : \"DesigTest-VNF\",\"action\":\"TestAction\",\"technology\":\"TestTech\",\"action-level\":\"TestLevel\",\"protocol\":\"TestProtocol\",\"vnfc-type\":\"TestVnfc\",\"template\":\"TestTemplate\"}";
171         String result = Whitebox.invokeMethod(spyDesign, "createArtifactTrackingRecord", payload, "1234", 0, 0);
172         assertEquals(null, result);
173     }
174
175     @Test
176     public void testgetSDCArtifactIDbyRequestID() throws Exception {
177         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\"}";
178         int result = Whitebox.invokeMethod(spyDesign, "getSDCArtifactIDbyRequestID", payload);
179         assertEquals(0, result);
180     }
181
182     @Test(expected = Exception.class)
183     public void testgetArtifact() throws Exception {
184         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\"}";
185         String result = Whitebox.invokeMethod(spyDesign, "getArtifact", payload, "1234");
186     }
187
188     @Test
189     public void testgetStatus() throws Exception {
190         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\"}";
191         String result = Whitebox.invokeMethod(spyDesign, "getStatus", payload, "1234");
192         assertEquals(true, result.contains("0000"));
193     }
194
195     @Test
196     public void testGetAppcTimestampUTC() throws Exception {
197              String requestId = "1234";
198              DesignDBService design = DesignDBService.initialise();
199              String result =  Whitebox.invokeMethod(design, "getAppcTimestampUTC",requestId);
200              assertTrue(result.endsWith("Z"));
201     }
202     
203    
204
205 }