[ONOS-4621] Unit Tests for Snmp provider and protocol

Change-Id: If70c701478762201b3739d9bd017c9f7eec75db1
diff --git a/protocols/snmp/ctl/src/main/java/org/onosproject/snmp/ctl/DefaultSnmpController.java b/protocols/snmp/ctl/src/main/java/org/onosproject/snmp/ctl/DefaultSnmpController.java
index 35556cd..e82a583 100644
--- a/protocols/snmp/ctl/src/main/java/org/onosproject/snmp/ctl/DefaultSnmpController.java
+++ b/protocols/snmp/ctl/src/main/java/org/onosproject/snmp/ctl/DefaultSnmpController.java
@@ -52,10 +52,10 @@
     private final Logger log = LoggerFactory
             .getLogger(getClass());
 
-    private ISnmpSessionFactory sessionFactory;
+    protected ISnmpSessionFactory sessionFactory;
 
-    private final Map<DeviceId, ISnmpSession> sessionMap = new HashMap<>();
-    protected Map<DeviceId, SnmpDevice> snmpDeviceMap = new ConcurrentHashMap<>();
+    protected final Map<DeviceId, ISnmpSession> sessionMap = new HashMap<>();
+    protected final Map<DeviceId, SnmpDevice> snmpDeviceMap = new ConcurrentHashMap<>();
 
     @Activate
     public void activate(ComponentContext context) {
@@ -66,6 +66,7 @@
 
     @Deactivate
     public void deactivate() {
+        sessionMap.clear();
         snmpDeviceMap.clear();
         log.info("Stopped");
     }
diff --git a/protocols/snmp/ctl/src/test/java/org/onosproject/snmp/ctl/DefaultSnmpControllerTest.java b/protocols/snmp/ctl/src/test/java/org/onosproject/snmp/ctl/DefaultSnmpControllerTest.java
new file mode 100644
index 0000000..c785dd6
--- /dev/null
+++ b/protocols/snmp/ctl/src/test/java/org/onosproject/snmp/ctl/DefaultSnmpControllerTest.java
@@ -0,0 +1,119 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.snmp.ctl;
+
+import com.btisystems.pronx.ems.core.snmp.ISnmpConfiguration;
+import com.btisystems.pronx.ems.core.snmp.ISnmpConfigurationFactory;
+import com.btisystems.pronx.ems.core.snmp.ISnmpSession;
+import com.btisystems.pronx.ems.core.snmp.ISnmpSessionFactory;
+import org.junit.Before;
+import org.junit.Test;
+import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
+import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm;
+
+import java.io.IOException;
+
+import static org.junit.Assert.*;
+
+/**
+ * DefaultSnmpController test class.
+ */
+public class DefaultSnmpControllerTest {
+
+    ISnmpSessionFactory mockSnmpSessionFactory = new MockISnmpSessionFactory();
+
+    DefaultSnmpController snmpController = new DefaultSnmpController();
+
+    DefaultSnmpDevice device = new DefaultSnmpDevice("1.1.1.1", 1, "test", "test");
+
+    ISnmpSession snmpSession = new ISnmpSessionAdapter();
+
+    DefaultAlarm alarm = new DefaultAlarm.Builder(
+            device.deviceId(), "SNMP alarm retrieval failed",
+            Alarm.SeverityLevel.CRITICAL,
+            System.currentTimeMillis()).build();
+
+    @Before
+    public void setUp() {
+        snmpController.sessionFactory = mockSnmpSessionFactory;
+    }
+
+    @Test
+    public void testActivate() {
+        snmpController.activate(null);
+        assertNotNull("Incorrect sessionFactory", snmpController.sessionFactory);
+    }
+
+    @Test
+    public void testDeactivate() {
+        snmpController.deactivate();
+        assertEquals("Device map should be clear", 0, snmpController.getDevices().size());
+        assertEquals("Session map should be clear", 0, snmpController.sessionMap.size());
+    }
+
+    @Test
+    public void addDevice() {
+        snmpController.addDevice(device.deviceId(), device);
+        assertEquals("Controller should contain device", device, snmpController.getDevice(device.deviceId()));
+    }
+
+    /**
+     * tests session creation and get from map if already exists.
+     */
+    @Test
+    public void getNotExistingSession() throws Exception {
+        addDevice();
+        assertEquals("Session should be created", snmpSession, snmpController.getSession(device.deviceId()));
+        assertEquals("Map should contain session", 1, snmpController.snmpDeviceMap.size());
+        assertEquals("Session should be fetched from map", snmpSession, snmpController.getSession(device.deviceId()));
+    }
+
+    @Test
+    public void removeDevice() {
+        addDevice();
+        snmpController.removeDevice(device.deviceId());
+        assertNull("Device shoudl not be present", snmpController.getDevice(device.deviceId()));
+    }
+
+    @Test
+    public void walkFailedAlarm() {
+        assertEquals("Alarms should be equals", alarm, snmpController.buildWalkFailedAlarm(device.deviceId()));
+    }
+
+    public class MockISnmpSessionFactory implements ISnmpSessionFactory {
+
+        @Override
+        public ISnmpSession createSession(ISnmpConfiguration configuration, String ipAddress) throws IOException {
+            new ISnmpSessionAdapter();
+            return snmpSession;
+        }
+
+        @Override
+        public ISnmpSession createSession(String ipAddress, String community)
+                throws IOException {
+            return snmpSession;
+        }
+
+        @Override
+        public ISnmpSession createSession(String ipAddress, String community,
+                                          String factoryName,
+                                          ISnmpConfigurationFactory.AccessType accessType)
+                throws IOException {
+            return snmpSession;
+        }
+    }
+}
\ No newline at end of file
diff --git a/protocols/snmp/ctl/src/test/java/org/onosproject/snmp/ctl/DefaultSnmpDeviceTest.java b/protocols/snmp/ctl/src/test/java/org/onosproject/snmp/ctl/DefaultSnmpDeviceTest.java
new file mode 100644
index 0000000..d62b113
--- /dev/null
+++ b/protocols/snmp/ctl/src/test/java/org/onosproject/snmp/ctl/DefaultSnmpDeviceTest.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.snmp.ctl;
+
+import org.junit.Test;
+import org.onosproject.net.DeviceId;
+
+import static org.junit.Assert.*;
+
+/**
+ * Test class for DefaultSnmpDevice.
+ */
+public class DefaultSnmpDeviceTest {
+
+    private final String snmpHost = "1.1.1.1";
+    private int snmpPort = 1;
+    private final String username = "test";
+    private final String community = "test";
+    private final DeviceId deviceId = DeviceId.deviceId("snmp:1.1.1.1:1");
+    private final String deviceInfo = "host: 1.1.1.1. port: 1";
+
+    DefaultSnmpDevice device = new DefaultSnmpDevice("1.1.1.1", 1, "test", "test");
+
+    @Test
+    public void basics() throws Exception {
+        assertTrue("Device should be reachable", device.isReachable());
+        assertEquals("Incorrect host", snmpHost, device.getSnmpHost());
+        assertEquals("Incorrect port", snmpPort, device.getSnmpPort());
+        assertEquals("Incorrect username", username, device.getUsername());
+        assertEquals("Incorrect community", community, device.getCommunity());
+        assertEquals("Incorrect deviceID", deviceId, device.deviceId());
+        assertEquals("Incorrect deviceInfo", deviceInfo, device.deviceInfo());
+        device.disconnect();
+        assertFalse("Device should not be reachable", device.isReachable());
+
+    }
+}
diff --git a/protocols/snmp/ctl/src/test/java/org/onosproject/snmp/ctl/ISnmpSessionAdapter.java b/protocols/snmp/ctl/src/test/java/org/onosproject/snmp/ctl/ISnmpSessionAdapter.java
new file mode 100644
index 0000000..8bbb93d
--- /dev/null
+++ b/protocols/snmp/ctl/src/test/java/org/onosproject/snmp/ctl/ISnmpSessionAdapter.java
@@ -0,0 +1,83 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.snmp.ctl;
+
+import com.btisystems.pronx.ems.core.model.DeviceEntityDescription;
+import com.btisystems.pronx.ems.core.snmp.ISnmpSession;
+import com.btisystems.pronx.ems.core.snmp.IVariableBindingHandler;
+import com.btisystems.pronx.ems.core.snmp.SnmpIoException;
+import com.btisystems.pronx.ems.core.snmp.WalkResponse;
+import org.snmp4j.smi.OID;
+import org.snmp4j.smi.VariableBinding;
+
+import java.io.IOException;
+import java.net.InetAddress;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * AdapterClass for ISnmpSession.
+ */
+public class ISnmpSessionAdapter implements ISnmpSession {
+    @Override
+    public String identifyDevice() {
+        return null;
+    }
+
+    @Override
+    public String getVariable(String oid) {
+        return null;
+    }
+
+    @Override
+    public Integer getVariableAsInt(String oid) {
+        return null;
+    }
+
+    @Override
+    public WalkResponse walkDevice(IVariableBindingHandler networkDevice, List<OID> oids)
+            throws IOException {
+        return null;
+    }
+
+    @Override
+    public WalkResponse getTableRows(IVariableBindingHandler networkDevice,
+                                     Map<DeviceEntityDescription, List<OID>> tableIndexes)
+            throws IOException {
+        return null;
+    }
+
+    @Override
+    public InetAddress getAddress() {
+        return null;
+    }
+
+    @Override
+    public void close() throws IOException {
+
+    }
+
+    @Override
+    public void setVariables(VariableBinding[] bindings) {
+
+    }
+
+    @Override
+    public void checkErrorCodeAndDescription() throws SnmpIoException {
+
+    }
+}
\ No newline at end of file