Fix Sonar complains
[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 org.junit.jupiter.api.Assertions.assertEquals;
23 import static org.junit.jupiter.api.Assertions.assertFalse;
24 import static org.junit.jupiter.api.Assertions.assertThrows;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
26
27 import java.io.IOException;
28 import java.time.ZoneId;
29 import java.time.ZonedDateTime;
30 import java.time.format.DateTimeFormatter;
31 import java.util.List;
32 import java.util.concurrent.TimeUnit;
33 import org.junit.jupiter.api.AfterEach;
34 import org.junit.jupiter.api.BeforeEach;
35 import org.junit.jupiter.api.Test;
36 import org.junit.jupiter.api.Timeout;
37 import org.junit.jupiter.api.extension.ExtendWith;
38 import org.o.ran.oam.nf.oam.adopter.pm.rest.manager.api.PerformanceManagementAdaptersDeployer;
39 import org.o.ran.oam.nf.oam.adopter.pm.rest.manager.exceptions.AlreadyPresentException;
40 import org.o.ran.oam.nf.oam.adopter.pm.rest.manager.exceptions.NotFoundException;
41 import org.o.ran.oam.nf.oam.adopter.pm.rest.manager.mapper.PerformanceManagementFile2VesMapper;
42 import org.o.ran.oam.nf.oam.adopter.pm.rest.manager.properties.PerformanceManagementManagerProperties;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.beans.factory.annotation.Qualifier;
45 import org.springframework.boot.test.context.SpringBootTest;
46 import org.springframework.test.context.junit.jupiter.SpringExtension;
47
48 @ExtendWith(SpringExtension.class)
49 @SpringBootTest(classes = {VesEventNotifierMock.class, PerformanceManagementMapperConfigProvider.class,
50     PerformanceManagementFile2VesMapper.class, PerformanceManagementAdaptersDeployer.class})
51 class PerformanceManagementManagerTest {
52
53     @Autowired
54     @Qualifier("test")
55     private VesEventNotifierMock eventListener;
56     @Autowired
57     private PerformanceManagementFile2VesMapper fileMapper;
58     private PerformanceManagementAdaptersDeployer deployer;
59
60     /**
61      * Initialize test.
62      */
63     @BeforeEach
64     public void init() {
65         final HttpRestClientMock httpRestClientMock = new HttpRestClientMock();
66         final PerformanceManagementManagerProperties properties = new PerformanceManagementManagerProperties();
67         final ZonedDateTime now = ZonedDateTime.now(ZoneId.of("+02:00")).plusSeconds(5);
68         final String formattedString = now.format(DateTimeFormatter.ofPattern("HH:mm:ss"));
69         properties.setSynchronizationTimeStart(formattedString);
70         properties.setSynchronizationTimeFrequency(30);
71         deployer = new AdaptersDeployer(
72                 new PerformanceManagementRestAgentFactory(eventListener, fileMapper, properties, httpRestClientMock));
73     }
74
75     @Test
76     @Timeout(value = 10000, unit = TimeUnit.MILLISECONDS)
77     void testMapping() throws IOException, InterruptedException, AlreadyPresentException {
78         assertTrue(deployer.getAll().isEmpty());
79         deployer.create("172.0.10.2", "admin", "admin");
80         assertFalse(deployer.getAll().isEmpty());
81
82         final String expected = JsonUtils.readJson("/json/PMVESMessage.json");
83         final List<String> notifications = getVesNotification(eventListener, 2);
84         final String actual = notifications.get(0);
85         JsonUtils.compareResult(expected, actual);
86     }
87
88     @Test
89     void testDelete() throws AlreadyPresentException, NotFoundException {
90         assertTrue(deployer.getAll().isEmpty());
91         deployer.create("172.0.10.2", "admin", "admin");
92         assertFalse(deployer.getAll().isEmpty());
93
94         deployer.delete("172.0.10.2");
95         assertTrue(deployer.getAll().isEmpty());
96     }
97
98     @Test
99     void testAlreadyPresent() throws AlreadyPresentException {
100         assertTrue(deployer.getAll().isEmpty());
101         deployer.create("172.0.10.2", "admin", "admin");
102         assertFalse(deployer.getAll().isEmpty());
103
104         final Exception alreadyPresentException = assertThrows(AlreadyPresentException.class,
105                 () -> deployer.create("172.0.10.2", "admin", "admin"));
106         assertEquals("Adapter 172.0.10.2 already present.", alreadyPresentException.getMessage());
107     }
108
109     @Test
110     void testNotPresent() {
111         final Exception exception = assertThrows(NotFoundException.class, () -> deployer.delete("172.0.10.2"));
112         assertEquals("Adapter 172.0.10.2 is not present.", exception.getMessage());
113     }
114
115     @Test
116     void testTimeZone() throws AlreadyPresentException {
117         deployer.create("172.0.10.2", "admin", "admin");
118         assertEquals(deployer.getTimeZone("172.0.10.2"), ZoneId.of("+02:00"));
119     }
120
121     private static List<String> getVesNotification(final VesEventNotifierMock listener, final int expectedSize)
122             throws InterruptedException {
123         List<String> events = null;
124         synchronized (listener) {
125             for (int i = 0; i < 100000; i++) {
126                 listener.wait(1000);
127                 events = listener.getEvents();
128                 if (events != null && !events.isEmpty() && events.size() == expectedSize) {
129                     break;
130                 }
131             }
132         }
133         return events;
134     }
135
136     @AfterEach
137     public final void after() {
138         ((AdaptersDeployer) deployer).close();
139         assertTrue(deployer.getAll().isEmpty());
140     }
141 }