2 * ============LICENSE_START=======================================================
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
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=========================================================
21 package org.onap.so.adapters.requestsdb;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
26 import java.util.ArrayList;
27 import java.util.Calendar;
28 import java.util.Date;
29 import java.util.List;
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;
46 @RunWith(SpringRunner.class)
47 @SpringBootTest(classes = MSORequestDBApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
48 @ActiveProfiles("test")
50 public class ArchiveInfraRequestsSchedulerTest {
53 private ArchiveInfraRequestsScheduler scheduler;
56 private InfraActiveRequestsRepository iarRepo;
59 private ArchivedInfraRequestsRepository archivedRepo;
61 @Value("${mso.infra-requests.archived.period}")
62 private int archivedPeriod;
66 public void testArchiveInfraRequests() {
67 String requestId1 = "requestId1";
68 String requestId2 = "requestId2";
70 InfraActiveRequests iar1 = new InfraActiveRequests();
71 iar1.setRequestId(requestId1);
72 iar1.setAction("action1");
74 InfraActiveRequests iar2 = new InfraActiveRequests();
75 iar2.setRequestId(requestId2);
76 iar2.setAction("action2");
78 List<InfraActiveRequests> requests = new ArrayList<>();
81 iarRepo.save(requests);
83 scheduler.archiveInfraRequests(requests);
85 assertEquals(2, archivedRepo.count());
86 assertEquals(requestId1, archivedRepo.findOne(requestId1).getRequestId());
87 assertEquals(requestId2, archivedRepo.findOne(requestId2).getRequestId());
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();
99 List<InfraActiveRequests> requests = iarRepo.findByEndTimeLessThan(archivingDate, new PageRequest(0, 100));
100 List<InfraActiveRequests> requests2 = iarRepo.findByStartTimeLessThanAndEndTime(archivingDate, null, new PageRequest(0, 100));
102 int total = requests.size() + requests2.size();
104 scheduler.infraRequestsScheduledTask();
106 assertTrue(archivedRepo.count() >= total);
107 assertTrue(iarRepo.count() < total);