46c942c1f0c9a60bb8fef4044c3554f0a2edce8f
[oam/nf-oam-adopter.git] / ves-nf-oam-adopter / ves-nf-oam-adopter-pm-manager / src / test / java / org / o / ran / oam / nf / oam / adopter / pm / rest / manager / PerformanceManagementManagerTest.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  O-RAN-SC
4  *  ================================================================================
5  *  Copyright © 2021 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  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.o.ran.oam.nf.oam.adopter.pm.rest.manager;
21
22 import static java.lang.Thread.sleep;
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.junit.jupiter.api.Assertions.assertFalse;
25 import static org.junit.jupiter.api.Assertions.assertThrows;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27
28 import java.io.IOException;
29 import java.time.ZoneId;
30 import java.time.ZonedDateTime;
31 import java.time.format.DateTimeFormatter;
32 import java.util.List;
33 import java.util.concurrent.TimeUnit;
34 import org.junit.jupiter.api.AfterEach;
35 import org.junit.jupiter.api.BeforeEach;
36 import org.junit.jupiter.api.Test;
37 import org.junit.jupiter.api.Timeout;
38 import org.junit.jupiter.api.extension.ExtendWith;
39 import org.o.ran.oam.nf.oam.adopter.pm.rest.manager.api.PerformanceManagementAdaptersDeployer;
40 import org.o.ran.oam.nf.oam.adopter.pm.rest.manager.exceptions.AlreadyPresentException;
41 import org.o.ran.oam.nf.oam.adopter.pm.rest.manager.exceptions.NotFoundException;
42 import org.o.ran.oam.nf.oam.adopter.pm.rest.manager.mapper.PerformanceManagementFile2VesMapper;
43 import org.o.ran.oam.nf.oam.adopter.pm.rest.manager.properties.PerformanceManagementManagerProperties;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.beans.factory.annotation.Qualifier;
46 import org.springframework.boot.test.context.SpringBootTest;
47 import org.springframework.test.context.junit.jupiter.SpringExtension;
48
49 @ExtendWith(SpringExtension.class)
50 @SpringBootTest(classes = {VesEventNotifierMock.class, PerformanceManagementMapperConfigProvider.class,
51     PerformanceManagementFile2VesMapper.class, PerformanceManagementAdaptersDeployer.class})
52 class PerformanceManagementManagerTest {
53
54     @Autowired
55     @Qualifier("test")
56     private VesEventNotifierMock eventListener;
57     @Autowired
58     private PerformanceManagementFile2VesMapper fileMapper;
59     private PerformanceManagementAdaptersDeployer deployer;
60
61     /**
62      * Initialize test.
63      */
64     @BeforeEach
65     public void init() {
66         final HttpRestClientMock httpRestClientMock = new HttpRestClientMock();
67         final PerformanceManagementManagerProperties properties = new PerformanceManagementManagerProperties();
68         final ZonedDateTime now = ZonedDateTime.now(ZoneId.of("+02:00")).plusSeconds(5);
69         final String formattedString = now.format(DateTimeFormatter.ofPattern("HH:mm:ss"));
70         properties.setSynchronizationTimeStart(formattedString);
71         properties.setSynchronizationTimeFrequency(30);
72         deployer = new AdaptersDeployer(
73                 new PerformanceManagementRestAgentFactory(eventListener, fileMapper, properties, httpRestClientMock));
74     }
75
76     @Test
77     @Timeout(value = 10000, unit = TimeUnit.MILLISECONDS)
78     void testMapping() throws IOException, InterruptedException, AlreadyPresentException {
79         assertTrue(deployer.getAll().isEmpty());
80         deployer.create("172.0.10.2", "admin", "admin");
81         assertFalse(deployer.getAll().isEmpty());
82
83         final String expected = JsonUtils.readJson("/json/PMVESMessage.json");
84         final List<String> notifications = getVesNotification(eventListener, 2);
85         final String actual = notifications.get(0);
86         JsonUtils.compareResult(expected, actual);
87     }
88
89     @Test
90     void testDelete() throws AlreadyPresentException, NotFoundException {
91         assertTrue(deployer.getAll().isEmpty());
92         deployer.create("172.0.10.2", "admin", "admin");
93         assertFalse(deployer.getAll().isEmpty());
94
95         deployer.delete("172.0.10.2");
96         assertTrue(deployer.getAll().isEmpty());
97     }
98
99     @Test
100     void testAlreadyPresent() throws AlreadyPresentException {
101         assertTrue(deployer.getAll().isEmpty());
102         deployer.create("172.0.10.2", "admin", "admin");
103         assertFalse(deployer.getAll().isEmpty());
104
105         final Exception alreadyPresentException = assertThrows(AlreadyPresentException.class,
106                 () -> deployer.create("172.0.10.2", "admin", "admin"));
107         assertEquals("Adapter 172.0.10.2 already present.", alreadyPresentException.getMessage());
108     }
109
110     @Test
111     void testNotPresent() {
112         final Exception exception = assertThrows(NotFoundException.class, () -> deployer.delete("172.0.10.2"));
113         assertEquals("Adapter 172.0.10.2 is not present.", exception.getMessage());
114     }
115
116     @Test
117     void testTimeZone() throws AlreadyPresentException {
118         deployer.create("172.0.10.2", "admin", "admin");
119         assertEquals(deployer.getTimeZone("172.0.10.2"), ZoneId.of("+02:00"));
120     }
121
122     private static List<String> getVesNotification(final VesEventNotifierMock listener, final int expectedSize)
123             throws InterruptedException {
124         List<String> events = null;
125         for (int i = 0; i < 100000; i++) {
126             sleep(1000);
127             events = listener.getEvents();
128             if (events != null && !events.isEmpty() && events.size() == expectedSize) {
129                 break;
130             }
131         }
132         return events;
133     }
134
135     @AfterEach
136     public final void after() {
137         ((AdaptersDeployer) deployer).close();
138         assertTrue(deployer.getAll().isEmpty());
139     }
140 }