2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package com.onap.sdnc.vnfbackupservice.service;
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;
27 import java.sql.Timestamp;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.List;
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;
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;
49 public class VnfbackupServiceImplTest {
52 VnfBackupServiceRepo vrepo;
55 private RestTemplate rTemplate;
58 private VnfRestClient vnfclient;
61 VnfbackupServiceImpl vnfserviceimpl;
64 public void setUp() throws Exception {
65 MockitoAnnotations.initMocks(this);
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" + " }";
77 String url = "http://localhost:8023/test/api/foos";
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";
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;
92 String vnfname = "vnfname";
95 public void parseConfigTest() {
96 VnfbackupServiceImpl vnfbackupServiceImpl = new VnfbackupServiceImpl();
97 vnfbackupServiceImpl.parseVnfConfigDetails(json);
102 public void backupVnfconfigTest()
104 HttpHeaders headers = new HttpHeaders();
105 HttpHeaders headers1= new HttpHeaders();
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));
113 when(vnfclient.generateHeaders(headers, userName, password)).thenReturn(headers1);
115 ResponseEntity<String> entity = new ResponseEntity<String>(HttpStatus.OK);
117 when(rTemplate.exchange(url, HttpMethod.GET, entity, String.class)).thenReturn(entity) ;
119 when(vnfclient.vnfRestClient(url, userName, password)).thenReturn(json);
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);
130 when(vrepo.getVnfDetail(vnfId)).thenReturn(vnfConfigDetailsDB);
132 List<VnfConfigDetailsDB> listvnfconfdb= new ArrayList<VnfConfigDetailsDB>();
134 when(vrepo.getVnfDetails(vnfId)).thenReturn(listvnfconfdb);
136 doNothing().when(vrepo).saveVnfDetails(configinfo, creationDate, lastupdated, status, vnfId, vnfname, vnfversion);
138 vnfserviceimpl.backupVnfconfig(vnfId);
140 assertEquals("success", vnfserviceimpl.backupVnfconfig(vnfId));
144 public void updatedBackuptimeTest() {
145 String sdtime="12:00";
146 when(vrepo.getvnfschedulertime()).thenReturn(sdtime);
147 vnfserviceimpl.updatedBackuptime();