f05a301caadfaba172e729b57a31cf5c53f31d39
[so.git] / adapters / mso-requests-db-adapter / src / test / java / org / onap / so / adapters / requestsdb / ArchiveInfraRequestsSchedulerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2018 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.onap.so.adapters.requestsdb;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25
26 import java.util.ArrayList;
27 import java.util.Calendar;
28 import java.util.Date;
29 import java.util.List;
30
31 import org.junit.Ignore;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.onap.so.adapters.requestsdb.application.MSORequestDBApplication;
35 import org.onap.so.db.request.beans.InfraActiveRequests;
36 import org.onap.so.db.request.data.repository.ArchivedInfraRequestsRepository;
37 import org.onap.so.db.request.data.repository.InfraActiveRequestsRepository;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.beans.factory.annotation.Value;
40 import org.springframework.boot.test.context.SpringBootTest;
41 import org.springframework.data.domain.PageRequest;
42 import org.springframework.test.context.ActiveProfiles;
43 import org.springframework.test.context.junit4.SpringRunner;
44 import org.springframework.transaction.annotation.Transactional;
45
46 @RunWith(SpringRunner.class)
47 @SpringBootTest(classes = MSORequestDBApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
48 @ActiveProfiles("test")
49 @Transactional
50 public class ArchiveInfraRequestsSchedulerTest {
51         
52         @Autowired
53         private ArchiveInfraRequestsScheduler scheduler;
54         
55         @Autowired
56         private InfraActiveRequestsRepository iarRepo;
57         
58         @Autowired
59         private ArchivedInfraRequestsRepository archivedRepo;
60         
61         @Value("${mso.infra-requests.archived.period}")
62         private int archivedPeriod;
63         
64         @Test 
65         @Transactional
66         public void testArchiveInfraRequests() {
67                 String requestId1 = "requestId1";
68                 String requestId2 = "requestId2";
69                 
70                 InfraActiveRequests iar1 = new InfraActiveRequests();
71                 iar1.setRequestId(requestId1);
72                 iar1.setAction("action1");
73                 
74                 InfraActiveRequests iar2 = new InfraActiveRequests();
75                 iar2.setRequestId(requestId2);
76                 iar2.setAction("action2");
77                 
78                 List<InfraActiveRequests> requests = new ArrayList<>();
79                 requests.add(iar1);
80                 requests.add(iar2);
81                 iarRepo.save(requests);
82                 
83                 scheduler.archiveInfraRequests(requests);
84                 
85                 assertEquals(2, archivedRepo.count());
86                 assertEquals(requestId1, archivedRepo.findOne(requestId1).getRequestId());
87                 assertEquals(requestId2, archivedRepo.findOne(requestId2).getRequestId());
88         }
89
90         @Test   
91         @Ignore
92         public void testInfraRequestsScheduledTask() {
93                 Date currentDate= new Date();
94                 Calendar calendar = Calendar.getInstance();
95                 calendar.setTime(currentDate);
96                 calendar.add(Calendar.DATE, -archivedPeriod);
97                 Date archivingDate = calendar.getTime();
98                 
99                 List<InfraActiveRequests> requests = iarRepo.findByEndTimeLessThan(archivingDate, new PageRequest(0, 100));
100                 List<InfraActiveRequests> requests2 = iarRepo.findByStartTimeLessThanAndEndTime(archivingDate, null, new PageRequest(0, 100));
101                 
102                 int total = requests.size() + requests2.size();
103                 
104                 scheduler.infraRequestsScheduledTask();
105                 
106                 assertTrue(archivedRepo.count() >= total);
107                 assertTrue(iarRepo.count() < total);
108         }
109 }