Andrea Campanella | 58454b9 | 2016-04-01 15:19:00 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Foundation |
Andrea Campanella | 58454b9 | 2016-04-01 15:19:00 -0700 | [diff] [blame] | 3 | * |
| 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 | |
| 17 | package org.onosproject.snmp.ctl; |
| 18 | |
| 19 | import com.btisystems.pronx.ems.core.snmp.ISnmpConfiguration; |
| 20 | import com.btisystems.pronx.ems.core.snmp.ISnmpConfigurationFactory; |
| 21 | import com.btisystems.pronx.ems.core.snmp.ISnmpSession; |
| 22 | import com.btisystems.pronx.ems.core.snmp.ISnmpSessionFactory; |
| 23 | import org.junit.Before; |
| 24 | import org.junit.Test; |
| 25 | import org.onosproject.incubator.net.faultmanagement.alarm.Alarm; |
| 26 | import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm; |
| 27 | |
| 28 | import java.io.IOException; |
| 29 | |
| 30 | import static org.junit.Assert.*; |
| 31 | |
| 32 | /** |
| 33 | * DefaultSnmpController test class. |
| 34 | */ |
| 35 | public 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 | } |