blob: ff1aa03e1e8651cf4bfc2f8f6049c7bb171b38ab [file] [log] [blame]
Andrea Campanella58454b92016-04-01 15:19:00 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Andrea Campanella58454b92016-04-01 15:19:00 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.snmp.ctl;
18
19import com.btisystems.pronx.ems.core.snmp.ISnmpConfiguration;
20import com.btisystems.pronx.ems.core.snmp.ISnmpConfigurationFactory;
21import com.btisystems.pronx.ems.core.snmp.ISnmpSession;
22import com.btisystems.pronx.ems.core.snmp.ISnmpSessionFactory;
23import org.junit.Before;
24import org.junit.Test;
25import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
26import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm;
27
28import java.io.IOException;
29
30import static org.junit.Assert.*;
31
32/**
33 * DefaultSnmpController test class.
34 */
35public class DefaultSnmpControllerTest {
36
37 ISnmpSessionFactory mockSnmpSessionFactory = new MockISnmpSessionFactory();
38
39 DefaultSnmpController snmpController = new DefaultSnmpController();
40
41 DefaultSnmpDevice device = new DefaultSnmpDevice("1.1.1.1", 1, "test", "test");
42
43 ISnmpSession snmpSession = new ISnmpSessionAdapter();
44
45 DefaultAlarm alarm = new DefaultAlarm.Builder(
46 device.deviceId(), "SNMP alarm retrieval failed",
47 Alarm.SeverityLevel.CRITICAL,
48 System.currentTimeMillis()).build();
49
50 @Before
51 public void setUp() {
52 snmpController.sessionFactory = mockSnmpSessionFactory;
53 }
54
55 @Test
56 public void testActivate() {
57 snmpController.activate(null);
58 assertNotNull("Incorrect sessionFactory", snmpController.sessionFactory);
59 }
60
61 @Test
62 public void testDeactivate() {
63 snmpController.deactivate();
64 assertEquals("Device map should be clear", 0, snmpController.getDevices().size());
65 assertEquals("Session map should be clear", 0, snmpController.sessionMap.size());
66 }
67
68 @Test
69 public void addDevice() {
70 snmpController.addDevice(device.deviceId(), device);
71 assertEquals("Controller should contain device", device, snmpController.getDevice(device.deviceId()));
72 }
73
74 /**
75 * tests session creation and get from map if already exists.
76 */
77 @Test
78 public void getNotExistingSession() throws Exception {
79 addDevice();
80 assertEquals("Session should be created", snmpSession, snmpController.getSession(device.deviceId()));
81 assertEquals("Map should contain session", 1, snmpController.snmpDeviceMap.size());
82 assertEquals("Session should be fetched from map", snmpSession, snmpController.getSession(device.deviceId()));
83 }
84
85 @Test
86 public void removeDevice() {
87 addDevice();
88 snmpController.removeDevice(device.deviceId());
89 assertNull("Device shoudl not be present", snmpController.getDevice(device.deviceId()));
90 }
91
92 @Test
93 public void walkFailedAlarm() {
94 assertEquals("Alarms should be equals", alarm, snmpController.buildWalkFailedAlarm(device.deviceId()));
95 }
96
97 public class MockISnmpSessionFactory implements ISnmpSessionFactory {
98
99 @Override
100 public ISnmpSession createSession(ISnmpConfiguration configuration, String ipAddress) throws IOException {
101 new ISnmpSessionAdapter();
102 return snmpSession;
103 }
104
105 @Override
106 public ISnmpSession createSession(String ipAddress, String community)
107 throws IOException {
108 return snmpSession;
109 }
110
111 @Override
112 public ISnmpSession createSession(String ipAddress, String community,
113 String factoryName,
114 ISnmpConfigurationFactory.AccessType accessType)
115 throws IOException {
116 return snmpSession;
117 }
118 }
119}