2 * ============LICENSE_START=======================================================
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
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=========================================================
20 package org.o.ran.oam.nf.oam.adopter.pm.rest.manager;
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;
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;
49 @ExtendWith(SpringExtension.class)
50 @SpringBootTest(classes = {VesEventNotifierMock.class, PerformanceManagementMapperConfigProvider.class,
51 PerformanceManagementFile2VesMapper.class, PerformanceManagementAdaptersDeployer.class})
52 public class PerformanceManagementManagerTest {
56 private VesEventNotifierMock eventListener;
58 private PerformanceManagementFile2VesMapper fileMapper;
59 private PerformanceManagementAdaptersDeployer deployer;
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));
77 @Timeout(value = 10000, unit = TimeUnit.MILLISECONDS)
78 public 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());
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);
90 public void testDelete() throws AlreadyPresentException, NotFoundException {
91 assertTrue(deployer.getAll().isEmpty());
92 deployer.create("172.0.10.2", "admin", "admin");
93 assertFalse(deployer.getAll().isEmpty());
95 deployer.delete("172.0.10.2");
96 assertTrue(deployer.getAll().isEmpty());
100 public void testAlreadyPresent() throws AlreadyPresentException {
101 assertTrue(deployer.getAll().isEmpty());
102 deployer.create("172.0.10.2", "admin", "admin");
103 assertFalse(deployer.getAll().isEmpty());
105 final Exception alreadyPresentException = assertThrows(AlreadyPresentException.class,
106 () -> deployer.create("172.0.10.2", "admin", "admin"));
107 assertEquals(alreadyPresentException.getMessage(), "Adapter 172.0.10.2 already present.");
111 public void testNotPresent() {
112 final Exception exception = assertThrows(NotFoundException.class, () -> deployer.delete("172.0.10.2"));
113 assertEquals(exception.getMessage(), "Adapter 172.0.10.2 is not present.");
117 public 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"));
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++) {
127 events = listener.getEvents();
128 if (events != null && !events.isEmpty() && events.size() == expectedSize) {
136 public final void after() {
137 ((AdaptersDeployer) deployer).close();
138 assertTrue(deployer.getAll().isEmpty());