From 7149e05b6a0d1d98f96ccdc3acea35d2b2b4c199 Mon Sep 17 00:00:00 2001 From: Joss Armstrong Date: Tue, 12 Feb 2019 08:38:12 +0000 Subject: Test coverage in SshdDataAccessService Increase coverage from 45% to 93% Issue-ID: APPC-1416 Change-Id: Icd9560f279b6dcd3986fcf28b16796a68b2ccb4f Signed-off-by: Joss Armstrong --- .../adapter/ssh/sshd/SshdDataAccessService.java | 12 +-- .../ssh/sshd/SshdDataAccessServiceTest.java | 114 +++++++-------------- 2 files changed, 43 insertions(+), 83 deletions(-) (limited to 'appc-adapters/appc-ssh-adapter') diff --git a/appc-adapters/appc-ssh-adapter/appc-ssh-adapter-sshd/src/main/java/org/onap/appc/adapter/ssh/sshd/SshdDataAccessService.java b/appc-adapters/appc-ssh-adapter/appc-ssh-adapter-sshd/src/main/java/org/onap/appc/adapter/ssh/sshd/SshdDataAccessService.java index b0abcb4f7..63acab6fb 100644 --- a/appc-adapters/appc-ssh-adapter/appc-ssh-adapter-sshd/src/main/java/org/onap/appc/adapter/ssh/sshd/SshdDataAccessService.java +++ b/appc-adapters/appc-ssh-adapter/appc-ssh-adapter-sshd/src/main/java/org/onap/appc/adapter/ssh/sshd/SshdDataAccessService.java @@ -5,6 +5,8 @@ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Copyright (C) 2017 Amdocs + * ================================================================================ + * Modifications Copyright (C) 2019 Ericsson * ============================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,17 +25,15 @@ package org.onap.appc.adapter.ssh.sshd; +import java.sql.SQLException; +import java.util.ArrayList; import javax.sql.rowset.CachedRowSet; - import org.onap.appc.adapter.ssh.Constants; import org.onap.appc.adapter.ssh.SshConnectionDetails; import org.onap.appc.adapter.ssh.SshDataAccessException; import org.onap.appc.adapter.ssh.SshDataAccessService; import org.onap.ccsdk.sli.core.dblib.DbLibService; -import java.sql.SQLException; -import java.util.ArrayList; - public class SshdDataAccessService implements SshDataAccessService { private String schema = Constants.NETCONF_SCHEMA; @@ -62,8 +62,8 @@ public class SshdDataAccessService implements SshDataAccessService { boolean recordFound = false; - String queryString = "select " + Constants.USER_NAME_TABLE_FIELD_NAME + "," + Constants.PASSWORD_TABLE_FIELD_NAME + "," + Constants.PORT_NUMBER_TABLE_FIELD_NAME + " " + - "from " + Constants.DEVICE_AUTHENTICATION_TABLE_NAME + " " + + String queryString = "select " + Constants.USER_NAME_TABLE_FIELD_NAME + "," + Constants.PASSWORD_TABLE_FIELD_NAME + "," + + Constants.PORT_NUMBER_TABLE_FIELD_NAME + " " + "from " + Constants.DEVICE_AUTHENTICATION_TABLE_NAME + " " + "where " + Constants.VNF_TYPE_TABLE_FIELD_NAME + " = ?"; ArrayList argList = new ArrayList<>(); diff --git a/appc-adapters/appc-ssh-adapter/appc-ssh-adapter-sshd/src/test/java/org/onap/appc/adapter/ssh/sshd/SshdDataAccessServiceTest.java b/appc-adapters/appc-ssh-adapter/appc-ssh-adapter-sshd/src/test/java/org/onap/appc/adapter/ssh/sshd/SshdDataAccessServiceTest.java index 0739e679b..0d9fb3e82 100644 --- a/appc-adapters/appc-ssh-adapter/appc-ssh-adapter-sshd/src/test/java/org/onap/appc/adapter/ssh/sshd/SshdDataAccessServiceTest.java +++ b/appc-adapters/appc-ssh-adapter/appc-ssh-adapter-sshd/src/test/java/org/onap/appc/adapter/ssh/sshd/SshdDataAccessServiceTest.java @@ -1,88 +1,48 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2019 Ericsson + * ================================================================================ + * 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. + * + * ============LICENSE_END========================================================= + */ + package org.onap.appc.adapter.ssh.sshd; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import java.sql.SQLException; import org.junit.Before; import org.junit.Test; +import org.mockito.Mockito; import org.onap.appc.adapter.ssh.SshConnectionDetails; import org.onap.ccsdk.sli.core.dblib.DbLibService; - -import javax.sql.rowset.CachedRowSet; -import java.io.PrintWriter; -import java.sql.Connection; -import java.sql.SQLException; -import java.sql.SQLFeatureNotSupportedException; -import java.util.ArrayList; -import java.util.logging.Logger; - -import static org.junit.Assert.assertEquals; +import com.sun.rowset.CachedRowSetImpl; public class SshdDataAccessServiceTest { private SshdDataAccessService sshdDataAccessService; private DbLibService db; @Before - public void setUp() { + public void setUp() throws SQLException { sshdDataAccessService = new SshdDataAccessService(); - db = new DbLibService() { - @Override - public CachedRowSet getData(String s, ArrayList arrayList, String s1) throws SQLException { - return null; - } - - @Override - public boolean writeData(String s, ArrayList arrayList, String s1) throws SQLException { - return false; - } - - @Override - public boolean isActive() { - return false; - } - - @Override - public Connection getConnection() throws SQLException { - return null; - } - - @Override - public Connection getConnection(String username, String password) throws SQLException { - return null; - } - - @Override - public T unwrap(Class iface) throws SQLException { - return null; - } - - @Override - public boolean isWrapperFor(Class iface) throws SQLException { - return false; - } - - @Override - public PrintWriter getLogWriter() throws SQLException { - return null; - } - - @Override - public void setLogWriter(PrintWriter out) throws SQLException { - - } - - @Override - public void setLoginTimeout(int seconds) throws SQLException { - - } - - @Override - public int getLoginTimeout() throws SQLException { - return 0; - } - - @Override - public Logger getParentLogger() throws SQLFeatureNotSupportedException { - return null; - } - }; + db = Mockito.mock(DbLibService.class); + CachedRowSetImpl rowset = Mockito.mock(CachedRowSetImpl.class); + Mockito.when(rowset.first()).thenReturn(true); + Mockito.when(db.getData(Mockito.anyString(), Mockito.any(), Mockito.anyString())).thenReturn(rowset); + sshdDataAccessService.setDbLibService(db); } @Test @@ -97,15 +57,15 @@ public class SshdDataAccessServiceTest { assertEquals(false, sshdDataAccessService.getDbLibService().isActive()); } - @Test(expected = NullPointerException.class) + @Test public void testRetrieveConnectionDetails() { SshConnectionDetails connectionDetails = new SshConnectionDetails(); - sshdDataAccessService.retrieveConnectionDetails("test", connectionDetails); + sshdDataAccessService.setDbLibService(db); + assertTrue(sshdDataAccessService.retrieveConnectionDetails("test", connectionDetails)); } - @Test(expected = NullPointerException.class) + @Test public void testRetrieveConfigFileName() { - SshConnectionDetails connectionDetails = new SshConnectionDetails(); - sshdDataAccessService.retrieveConfigFileName("test"); + assertEquals(null, sshdDataAccessService.retrieveConfigFileName("test")); } } -- cgit 1.2.3-korg