NonRT-RIC A1 Northbound API
[nonrtric.git] / sdnc-a1-controller / oam / configbackuprestore / vnfconfigbackupservice / src / test / java / com / onap / sdnc / vnfbackupservice / service / VnfbackupServiceImplTest.java
1 /*
2 * ============LICENSE_START=======================================================
3 * ONAP : SDNC-FEATURES
4 * ================================================================================
5 * Copyright 2018 TechMahindra
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 package com.onap.sdnc.vnfbackupservice.service;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertTrue;
24 import static org.mockito.Mockito.doNothing;
25 import static org.mockito.Mockito.when;
26
27 import java.sql.Timestamp;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.List;
31
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.InjectMocks;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.springframework.http.HttpHeaders;
38 import org.springframework.http.HttpMethod;
39 import org.springframework.http.HttpStatus;
40 import org.springframework.http.MediaType;
41 import org.springframework.http.ResponseEntity;
42 import org.springframework.web.client.RestTemplate;
43
44 import com.onap.sdnc.vnfbackupservice.dao.VnfBackupServiceRepo;
45 import com.onap.sdnc.vnfbackupservice.model.VnfConfigDetailsDB;
46 import com.onap.sdnc.vnfbackupservice.scheduler.VnfRestClient;
47 import com.onap.sdnc.vnfbackupservice.service.VnfbackupServiceImpl;
48
49 public class VnfbackupServiceImplTest {
50         
51         @Mock
52         VnfBackupServiceRepo vrepo;
53         
54         @Mock
55         private RestTemplate rTemplate;
56         
57         @Mock
58     private VnfRestClient vnfclient;
59         
60         @InjectMocks
61         VnfbackupServiceImpl vnfserviceimpl;
62         
63         @Before
64         public void setUp() throws Exception {
65                 MockitoAnnotations.initMocks(this);
66         }
67
68         String json =     "{\r\n" + "  \"vnfs\": {\r\n" + "    \"vnf-list\": [\r\n" + "     {\r\n"
69                                         + "        \"vnf-id\": \"f24ae4f1-ed6b-4f8b-9ad6-4110a6fe26e7\",\r\n"
70                                         + "        \"service-data\": {\r\n" + "          \"vnf-request-information\": {\r\n"
71                                         + "            \"vnf-name\": \"vDNS-Techm_vIMS_vnf_1\"\r\n" + "          },\r\n"
72                                         + "        \"service-status\": {         \r\n" + "          \"response-code\": \"200\"\r\n"
73                                         + "        }},\r\n" + "       \"service-status\": {         \r\n"
74                                         + "          \"response-code\": \"200\"\r\n" + "        }\r\n" + "     }\r\n" + "      \r\n"
75                                         + "       ]\r\n" + "  }\r\n" + "        }";
76         
77         String url = "http://localhost:8023/test/api/foos";
78         String vnfId="vnfid";
79         String userName = "abc";
80         String password = "abc";
81         String configfile="config";     
82         String vnfversion = "Version-1";
83         String jsonInput="jsoninput";
84         String vnfName = "vnfname";
85         
86         long millis = System.currentTimeMillis();
87         Timestamp date = new java.sql.Timestamp(millis);
88         String configinfo =json;
89         Timestamp creationDate = date;
90         Timestamp lastupdated = date;
91         int status = 1;
92         String vnfname = "vnfname";
93         
94         @Test
95         public void parseConfigTest() { 
96                 VnfbackupServiceImpl vnfbackupServiceImpl = new VnfbackupServiceImpl();
97                 vnfbackupServiceImpl.parseVnfConfigDetails(json);
98                 assertTrue(true);
99         }                       
100         
101         @Test()
102         public void backupVnfconfigTest()
103         {               
104                 HttpHeaders headers = new HttpHeaders();
105                 HttpHeaders headers1= new HttpHeaders();
106                 
107                 headers1.setAccept(Arrays.asList(new MediaType[] { MediaType.APPLICATION_JSON }));
108                 headers1.setContentType(MediaType.APPLICATION_JSON);
109                 String base64Username = userName + ":" + password;
110                 byte[] message = base64Username.getBytes();
111                 headers1.set("Authorization", "Basic " + java.util.Base64.getEncoder().encodeToString(message));                
112
113                 when(vnfclient.generateHeaders(headers, userName, password)).thenReturn(headers1);
114                 
115                 ResponseEntity<String> entity = new ResponseEntity<String>(HttpStatus.OK);       
116         
117                 when(rTemplate.exchange(url, HttpMethod.GET, entity, String.class)).thenReturn(entity) ;
118                 
119                 when(vnfclient.vnfRestClient(url, userName, password)).thenReturn(json);                
120                                 
121                 VnfConfigDetailsDB vnfConfigDetailsDB = new VnfConfigDetailsDB();
122                 vnfConfigDetailsDB.setVnfid(vnfId);
123                 vnfConfigDetailsDB.setConfiginfo(configinfo);
124                 vnfConfigDetailsDB.setCreationdate(date);
125                 vnfConfigDetailsDB.setLastupdated(date);
126                 vnfConfigDetailsDB.setStatus(status);
127                 vnfConfigDetailsDB.setVnfname(vnfname);
128                 vnfConfigDetailsDB.setVnfversion(vnfversion);
129                         
130                 when(vrepo.getVnfDetail(vnfId)).thenReturn(vnfConfigDetailsDB);
131
132                 List<VnfConfigDetailsDB> listvnfconfdb= new ArrayList<VnfConfigDetailsDB>();
133                 
134                 when(vrepo.getVnfDetails(vnfId)).thenReturn(listvnfconfdb);
135                  
136                 doNothing().when(vrepo).saveVnfDetails(configinfo, creationDate, lastupdated, status, vnfId, vnfname, vnfversion);
137                                 
138                 vnfserviceimpl.backupVnfconfig(vnfId);
139                 
140                 assertEquals("success", vnfserviceimpl.backupVnfconfig(vnfId));                 
141         }       
142         
143         @Test
144         public void updatedBackuptimeTest() {
145                 String sdtime="12:00";
146                 when(vrepo.getvnfschedulertime()).thenReturn(sdtime);           
147                 vnfserviceimpl.updatedBackuptime();             
148         }
149         
150 }