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