913c3b65a9cf78e23584d6eb52865c962bcbfae9
[oam/nf-oam-adopter.git] / ves-nf-oam-adopter / ves-nf-oam-adopter-snmp-manager / src / test / java / org / o / ran / oam / nf / oam / adopter / snmp / manager / SnmpManagerTest.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.snmp.manager;
21
22 import static org.junit.jupiter.api.Assertions.assertNotNull;
23 import static org.mockito.ArgumentMatchers.anyString;
24 import static org.mockito.Mockito.when;
25
26 import java.time.ZoneId;
27 import org.junit.jupiter.api.BeforeEach;
28 import org.junit.jupiter.api.Test;
29 import org.junit.jupiter.api.extension.ExtendWith;
30 import org.o.ran.oam.nf.oam.adopter.snmp.manager.api.TimeZoneOffsetService;
31 import org.o.ran.oam.nf.oam.adopter.snmp.manager.configurations.SnmpManagerConfig;
32 import org.o.ran.oam.nf.oam.adopter.snmp.manager.mapper.SnmpMapperImpl;
33 import org.o.ran.oam.nf.oam.adopter.snmp.manager.properties.SnmpManagerProperties;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.beans.factory.annotation.Qualifier;
36 import org.springframework.boot.test.context.SpringBootTest;
37 import org.springframework.boot.test.mock.mockito.MockBean;
38 import org.springframework.test.context.junit.jupiter.SpringExtension;
39
40 @ExtendWith(SpringExtension.class)
41 @SpringBootTest(classes = {VesEventNotifierMock.class, SnmpMappingConfigurationProvider.class,
42     SnmpMapperImpl.class, SnmpManagerProperties.class, SnmpManagerConfig.class})
43 class SnmpManagerTest {
44     @Autowired
45     @Qualifier("test")
46     private VesEventNotifierMock listener;
47     @Autowired
48     private SnmpManagerProperties snmpManagerProperties;
49     @Autowired
50     private SnmpManagerImpl snmpManager;
51     @MockBean
52     private TimeZoneOffsetService timeZoneOffsetService;
53
54     @BeforeEach
55     public void init() {
56         when(timeZoneOffsetService.getTimeZone(anyString())).thenReturn(ZoneId.of("+02:00"));
57     }
58
59     private String getVesNotification(final VesEventNotifierMock notificationProvider) throws InterruptedException {
60         synchronized (notificationProvider) {
61             for (int i = 0; i < 1000; i++) {
62                 notificationProvider.wait(200);
63                 if (notificationProvider.getEvent() != null) {
64                     break;
65                 }
66             }
67         }
68         final String event = notificationProvider.getEvent();
69         assertNotNull(event);
70         notificationProvider.clear();
71         return event;
72     }
73
74     @Test
75     void testDefaultTrap() throws Exception {
76         SnmpTestUtil
77                 .sendDefaultTrapV2(snmpManagerProperties.getHost(), Integer.toString(snmpManagerProperties.getPort()));
78         final String expected = JsonUtils.readJson("/json/VESMessageDefaultTrap.json");
79         final String actual = getVesNotification(listener);
80         JsonUtils.compareResultSkipEpoch(expected, actual);
81     }
82
83     @Test
84     void testBoxDown() throws Exception {
85         SnmpTestUtil.sendPortDownTrapV2(snmpManagerProperties.getHost(),
86                 Integer.toString(snmpManagerProperties.getPort()));
87         final String expected = JsonUtils.readJson("/json/PortDOWN.json");
88         final String actual = getVesNotification(listener);
89         JsonUtils.compareResult(expected, actual);
90     }
91 }