Improve coverage and silence sonar warnings 53/1953/1
authorLott, Christopher (cl778h) <cl778h@att.com>
Thu, 5 Dec 2019 19:25:14 +0000 (14:25 -0500)
committerLott, Christopher (cl778h) <cl778h@att.com>
Thu, 5 Dec 2019 19:32:58 +0000 (14:32 -0500)
Change-Id: I30464956c7b2b0ccb57d1f9cebd5e111381cc3cc
Signed-off-by: Lott, Christopher (cl778h) <cl778h@att.com>
13 files changed:
webapp-backend/src/main/java/org/oransc/ric/portal/dashboard/model/AppTransport.java
webapp-backend/src/main/java/org/oransc/ric/portal/dashboard/model/EcompUserDetails.java
webapp-backend/src/main/java/org/oransc/ric/portal/dashboard/model/InstanceTransport.java
webapp-backend/src/main/java/org/oransc/ric/portal/dashboard/util/HttpsURLConnectionUtils.java
webapp-backend/src/test/java/org/oransc/ric/portal/dashboard/DashboardUserManagerTest.java
webapp-backend/src/test/java/org/oransc/ric/portal/dashboard/config/CaasIngressMockConfiguration.java
webapp-backend/src/test/java/org/oransc/ric/portal/dashboard/config/PortalApiMockConfiguration.java [moved from webapp-backend/src/test/java/org/oransc/ric/portal/dashboard/config/PortalApIMockConfiguration.java with 94% similarity]
webapp-backend/src/test/java/org/oransc/ric/portal/dashboard/controller/CaasIngressControllerTest.java
webapp-backend/src/test/java/org/oransc/ric/portal/dashboard/controller/PortalRestCentralServiceTest.java
webapp-backend/src/test/java/org/oransc/ric/portal/dashboard/k8sapi/CaasIngressTest.java
webapp-backend/src/test/java/org/oransc/ric/portal/dashboard/model/AbstractModelTest.java [new file with mode: 0644]
webapp-backend/src/test/java/org/oransc/ric/portal/dashboard/model/EcompUserDetailsTest.java [deleted file]
webapp-backend/src/test/java/org/oransc/ric/portal/dashboard/model/ModelTest.java [new file with mode: 0644]

index ea04067..abe026b 100644 (file)
@@ -39,11 +39,11 @@ public class AppTransport implements IDashboardResponse {
        /**
         * Builds an object with the specified value.
         * 
-        * @param s
-        *              value to transport.
+        * @param n
+        *              Name to transport
         */
-       public AppTransport(String s) {
-               this.name = s;
+       public AppTransport(String n) {
+               this.name = n;
        }
 
        public String getName() {
index c81a66b..30cab78 100644 (file)
@@ -34,7 +34,7 @@ public class EcompUserDetails implements UserDetails {
 
        private static final long serialVersionUID = 1L;
        // use transient per Sonar warning
-       private transient final EcompUser ecompUser;
+       private final transient EcompUser ecompUser;
 
        // This is the default Spring role-name prefix.
        private static final String ROLEP = "ROLE_";
index 9f0ad2b..df36438 100644 (file)
@@ -28,6 +28,26 @@ public class InstanceTransport implements IDashboardResponse {
        private String key;
        private String name;
 
+       /**
+        * Builds an empty object.
+        */
+       public InstanceTransport() {
+               // no-arg constructor
+       }
+
+       /**
+        * Convenience constructor for minimal value set.
+        * 
+        * @param key
+        *                 Key
+        * @param name
+        *                 Name
+        */
+       public InstanceTransport(String key, String name) {
+               this.key = key;
+               this.name = name;
+       }
+
        public String getKey() {
                return key;
        }
index a27f956..c4cb055 100644 (file)
@@ -46,7 +46,7 @@ public final class HttpsURLConnectionUtils {
 
        private static final TrustManager[] UNQUESTIONING_TRUST_MANAGER = new TrustManager[] { new X509TrustManager() {
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
-                       return null;
+                       return new java.security.cert.X509Certificate[0];
                }
 
                public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException {
index a8879fc..553865e 100644 (file)
@@ -25,6 +25,7 @@ import java.util.Set;
 
 import org.junit.Assert;
 import org.junit.jupiter.api.Test;
+import org.onap.portalsdk.core.onboarding.exception.PortalAPIException;
 import org.onap.portalsdk.core.restful.domain.EcompRole;
 import org.onap.portalsdk.core.restful.domain.EcompUser;
 import org.slf4j.Logger;
@@ -55,7 +56,13 @@ public class DashboardUserManagerTest {
                DashboardUserManager dum = new DashboardUserManager(true);
                EcompUser user = createEcompUser(loginId);
                dum.createUser(user);
-               logger.debug("Created user {}", user);
+               logger.info("Created user {}", user);
+               try {
+                       dum.createUser(user);
+                       throw new Exception("Unexpected success");
+               } catch (PortalAPIException ex) {
+                       logger.info("caught expected exception: {}", ex.toString());
+               }
                Assert.assertFalse(dum.getUsers().isEmpty());
                EcompUser fetched = dum.getUser(loginId);
                Assert.assertEquals(fetched, user);
@@ -63,6 +70,12 @@ public class DashboardUserManagerTest {
                dum.updateUser(loginId, fetched);
                EcompUser missing = dum.getUser("foo");
                Assert.assertNull(missing);
+               EcompUser unk = createEcompUser("unknown");
+               try {
+                       dum.updateUser("unk", unk);
+               } catch (PortalAPIException ex) {
+                       logger.info("caught expected exception: {}", ex.toString());
+               }
        }
 
 }
index 6b12493..bd94c2e 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.oransc.ric.portal.dashboard.config;
 
+import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.mock;
 
@@ -93,9 +94,13 @@ public class CaasIngressMockConfiguration {
        public SimpleKubernetesClient ciPltApi() throws IOException {
                SimpleKubernetesClient mockClient = mock(SimpleKubernetesClient.class);
                doAnswer(inv -> {
-                       logger.debug("listPods for plt");
-                       return pltPods;
-               }).when(mockClient).listPods("ricplt");
+                       String ns = inv.<String>getArgument(0);
+                       logger.debug("listPods for namespace {}", ns);
+                       if ("ricplt".equals(ns))
+                               return pltPods;
+                       else
+                               throw new Exception("Fake server failure");
+               }).when(mockClient).listPods(any(String.class));
                return mockClient;
        }
 
@@ -41,7 +41,7 @@ import org.springframework.context.annotation.Profile;
 
 @Configuration
 @Profile("test")
-public class PortalApIMockConfiguration {
+public class PortalApiMockConfiguration {
 
        private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
 
@@ -73,10 +73,6 @@ public class PortalApIMockConfiguration {
                        logger.debug("getUserId");
                        return "userId";
                }).when(mockManager).validateEcompSso(any(HttpServletRequest.class));
-               doAnswer(inv -> {
-                       logger.debug("getAppCredentials");
-                       return credentialsMap;
-               }).when(mockManager).getAppCredentials();
                return mockManager;
        }
 
index 40ee7f6..2292b9c 100644 (file)
@@ -22,6 +22,7 @@ package org.oransc.ric.portal.dashboard.controller;
 import java.lang.invoke.MethodHandles;
 import java.net.URI;
 
+import org.junit.Assert;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 import org.slf4j.Logger;
@@ -55,4 +56,27 @@ public class CaasIngressControllerTest extends AbstractControllerTest {
                Assertions.assertTrue(s.contains(nsPlt));
        }
 
+       @Test
+       public void unknownClusterTest() {
+               final String nsPlt = "ricplt";
+               URI uri = buildUri(null, CaasIngressController.CONTROLLER_PATH, CaasIngressController.PODS_METHOD,
+                               CaasIngressController.PP_CLUSTER, "cluster", CaasIngressController.PP_NAMESPACE, nsPlt);
+               logger.info("Invoking {}", uri);
+               String s = testRestTemplateStandardRole().getForObject(uri, String.class);
+               Assert.assertNull(s);
+       }
+
+       // Unknown namespace triggers a controller exception
+       @Test
+       public void bogusNsTest() {
+               final String ns = "unknown";
+               URI uri = buildUri(null, CaasIngressController.CONTROLLER_PATH, CaasIngressController.PODS_METHOD,
+                               CaasIngressController.PP_CLUSTER, CaasIngressController.CLUSTER_PLT, CaasIngressController.PP_NAMESPACE,
+                               ns);
+               logger.info("Invoking {}", uri);
+               String s = testRestTemplateStandardRole().getForObject(uri, String.class);
+               Assertions.assertFalse(s.isEmpty());
+               Assertions.assertTrue(s.contains("RIC Dashboard Error"));
+       }
+
 }
index 1124090..03f7b25 100644 (file)
@@ -31,7 +31,7 @@ import org.onap.portalsdk.core.onboarding.util.PortalApiConstants;
 import org.onap.portalsdk.core.restful.domain.EcompRole;
 import org.onap.portalsdk.core.restful.domain.EcompUser;
 import org.oransc.ric.portal.dashboard.DashboardConstants;
-import org.oransc.ric.portal.dashboard.config.PortalApIMockConfiguration;
+import org.oransc.ric.portal.dashboard.config.PortalApiMockConfiguration;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.http.HttpEntity;
@@ -65,10 +65,10 @@ public class PortalRestCentralServiceTest extends AbstractControllerTest {
 
        private HttpEntity<Object> getEntityWithHeaders(Object body) {
                HttpHeaders headers = new HttpHeaders();
-               headers.set(PortalApIMockConfiguration.PORTAL_USERNAME_HEADER_KEY,
-                               PortalApIMockConfiguration.PORTAL_USERNAME_HEADER_KEY);
-               headers.set(PortalApIMockConfiguration.PORTAL_PASSWORD_HEADER_KEY,
-                               PortalApIMockConfiguration.PORTAL_PASSWORD_HEADER_KEY);
+               headers.set(PortalApiMockConfiguration.PORTAL_USERNAME_HEADER_KEY,
+                               PortalApiMockConfiguration.PORTAL_USERNAME_HEADER_KEY);
+               headers.set(PortalApiMockConfiguration.PORTAL_PASSWORD_HEADER_KEY,
+                               PortalApiMockConfiguration.PORTAL_PASSWORD_HEADER_KEY);
                HttpEntity<Object> entity = new HttpEntity<>(body, headers);
                return entity;
        }
index f882c14..f28494d 100644 (file)
@@ -21,11 +21,11 @@ package org.oransc.ric.portal.dashboard.k8sapi;
 
 import java.lang.invoke.MethodHandles;
 
+import org.junit.Assert;
 import org.junit.jupiter.api.Test;
 import org.oransc.ric.portal.dashboard.util.HttpsURLConnectionUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.springframework.http.ResponseEntity;
 import org.springframework.web.client.RestTemplate;
 
 public class CaasIngressTest {
@@ -33,15 +33,16 @@ public class CaasIngressTest {
        private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
 
        @Test
-       public void test() throws Exception {
+       public void coverHttpsUtils() throws Exception {
                HttpsURLConnectionUtils.turnOffSslChecking();
                // Get IP address from REC deployment team for testing
                final String podsUrl = "https://localhost:16443/api/v1/namespaces/ricaux/pods";
                RestTemplate rt = new RestTemplate();
                try {
-                       ResponseEntity<String> podsResponse = rt.getForEntity(podsUrl, String.class);
-                       logger.info(podsResponse.getBody());
+                       rt.getForEntity(podsUrl, String.class);
+                       Assert.assertTrue(false);
                } catch (Exception ex) {
+                       Assert.assertNotNull(ex);
                        logger.warn("Failed as expected");
                }
                HttpsURLConnectionUtils.turnOnSslChecking();
diff --git a/webapp-backend/src/test/java/org/oransc/ric/portal/dashboard/model/AbstractModelTest.java b/webapp-backend/src/test/java/org/oransc/ric/portal/dashboard/model/AbstractModelTest.java
new file mode 100644 (file)
index 0000000..d2566ac
--- /dev/null
@@ -0,0 +1,61 @@
+/*-
+ * ========================LICENSE_START=================================
+ * O-RAN-SC
+ * %%
+ * Copyright (C) 2019 AT&T Intellectual Property
+ * %%
+ * 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.oransc.ric.portal.dashboard.model;
+
+import java.time.Instant;
+
+public abstract class AbstractModelTest {
+
+       // Values for properties
+       final boolean b1 = true;
+       final boolean b2 = false;
+       final byte[] by1 = { 0, 1, 2, 3 };
+       final Instant t1 = Instant.now().plusSeconds(1);
+       final Instant t2 = Instant.now().plusSeconds(2);
+       final Instant t3 = Instant.now().plusSeconds(3);
+       final Instant t4 = Instant.now().plusSeconds(4);
+       final Instant t5 = Instant.now().plusSeconds(5);
+       final Instant t6 = Instant.now().plusSeconds(6);
+       final Integer i1 = 1;
+       final Integer i2 = 2;
+       final Integer i3 = 3;
+       final Integer i4 = 4;
+       final Integer i5 = 5;
+       final Long l1 = 1L;
+       final Long l2 = 2L;
+       final Long l3 = 3L;
+       final Long l4 = 4L;
+       final String s1 = "string1";
+       final String s2 = "string2";
+       final String s3 = "string3";
+       final String s4 = "string4";
+       final String s5 = "string5";
+       final String s6 = "string6";
+       final String s7 = "string7";
+       final String s8 = "string8";
+       final String s9 = "string9";
+       final String s10 = "string10";
+       final String s11 = "string11";
+       final String s12 = "string12";
+       final String s13 = "string13";
+       final String u1 = "http://foo.com";
+       final String u2 = "http://bar.com";
+
+}
diff --git a/webapp-backend/src/test/java/org/oransc/ric/portal/dashboard/model/EcompUserDetailsTest.java b/webapp-backend/src/test/java/org/oransc/ric/portal/dashboard/model/EcompUserDetailsTest.java
deleted file mode 100644 (file)
index f0d145e..0000000
+++ /dev/null
@@ -1,47 +0,0 @@
-/*-
- * ========================LICENSE_START=================================
- * O-RAN-SC
- * %%
- * Copyright (C) 2019 AT&T Intellectual Property
- * %%
- * 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.oransc.ric.portal.dashboard.model;
-
-import java.lang.invoke.MethodHandles;
-
-import org.junit.Assert;
-import org.junit.jupiter.api.Test;
-import org.onap.portalsdk.core.restful.domain.EcompUser;
-import org.oransc.ric.portal.dashboard.DashboardUserManagerTest;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-public class EcompUserDetailsTest {
-
-       private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
-
-       @Test
-       public void testEcompUserDetails() {
-               EcompUser eu = DashboardUserManagerTest.createEcompUser("lgid");
-               logger.info("EcompUser {}", eu);
-               EcompUserDetails eud = new EcompUserDetails(eu);
-               Assert.assertNotNull(eud.getAuthorities());
-               Assert.assertNull(eud.getPassword());
-               Assert.assertNotNull(eud.getUsername());
-               Assert.assertTrue(eud.isAccountNonExpired());
-               Assert.assertTrue(eud.isAccountNonLocked());
-       }
-
-}
diff --git a/webapp-backend/src/test/java/org/oransc/ric/portal/dashboard/model/ModelTest.java b/webapp-backend/src/test/java/org/oransc/ric/portal/dashboard/model/ModelTest.java
new file mode 100644 (file)
index 0000000..8aab066
--- /dev/null
@@ -0,0 +1,127 @@
+/*-
+ * ========================LICENSE_START=================================
+ * O-RAN-SC
+ * %%
+ * Copyright (C) 2019 AT&T Intellectual Property
+ * %%
+ * 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.oransc.ric.portal.dashboard.model;
+
+import java.lang.invoke.MethodHandles;
+
+import org.junit.Assert;
+import org.junit.jupiter.api.Test;
+import org.onap.portalsdk.core.restful.domain.EcompUser;
+import org.oransc.ric.e2mgr.client.model.GetNodebResponse;
+import org.oransc.ric.e2mgr.client.model.NodebIdentity;
+import org.oransc.ric.portal.dashboard.DashboardUserManagerTest;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class ModelTest extends AbstractModelTest {
+
+       private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+
+       private void checkAppTransport(AppTransport m) {
+               Assert.assertEquals(s1, m.getName());
+               Assert.assertEquals(s2, m.getVersion());
+       }
+
+       @Test
+       public void testAppTransport() {
+               AppTransport m = new AppTransport(s1);
+               m = new AppTransport();
+               m.setName(s1);
+               m.setVersion(s2);
+               checkAppTransport(m);
+               logger.info(m.toString());
+       }
+
+       @Test
+       public void testEcompUserDetails() {
+               EcompUser eu = DashboardUserManagerTest.createEcompUser("lgid");
+               logger.info("EcompUser {}", eu);
+               EcompUserDetails eud = new EcompUserDetails(eu);
+               Assert.assertNotNull(eud.getAuthorities());
+               Assert.assertNull(eud.getPassword());
+               Assert.assertNotNull(eud.getUsername());
+               Assert.assertTrue(eud.isAccountNonExpired());
+               Assert.assertTrue(eud.isAccountNonLocked());
+       }
+
+       private void checkErrorTransport(ErrorTransport m) {
+               Assert.assertEquals(s1, m.getError());
+               Assert.assertEquals(s2, m.getMessage());
+               Assert.assertEquals(s3, m.getPath());
+               Assert.assertEquals(i1, m.getStatus());
+               Assert.assertEquals(t1, m.getTimestamp());
+       }
+
+       @Test
+       public void testErrorTransport() {
+               ErrorTransport m = new ErrorTransport(i1, s1);
+               m = new ErrorTransport(i1, new Exception());
+               m = new ErrorTransport(i1, s1, s2, s3);
+               m = new ErrorTransport();
+               m.setError(s1);
+               m.setMessage(s2);
+               m.setPath(s3);
+               m.setStatus(i1);
+               m.setTimestamp(t1);
+               checkErrorTransport(m);
+               logger.info(m.toString());
+       }
+
+       private void checkInstanceTransport(InstanceTransport m) {
+               Assert.assertEquals(s1, m.getKey());
+               Assert.assertEquals(s2, m.getName());
+       }
+
+       @Test
+       public void testInstanceTransport() {
+               InstanceTransport m = new InstanceTransport(s1, s1);
+               m = new InstanceTransport();
+               m.setKey(s1);
+               m.setName(s2);
+               checkInstanceTransport(m);
+               logger.info(m.toString());
+       }
+
+       @Test
+       public void testRanDetailsTransport() {
+               RanDetailsTransport m = new RanDetailsTransport();
+               NodebIdentity nodebIdentity = new NodebIdentity();
+               GetNodebResponse nodebResponse = new GetNodebResponse();
+               m = new RanDetailsTransport(nodebIdentity, nodebResponse);
+               Assert.assertEquals(m.getNodebIdentity(), nodebIdentity);
+               Assert.assertEquals(m.getNodebStatus(), nodebResponse);
+       }
+
+       private void checkSuccessTransport(SuccessTransport m) {
+               Assert.assertEquals(s1, m.getData());
+               Assert.assertEquals(1, m.getStatus());
+       }
+
+       @Test
+       public void testSuccessTransport() {
+               SuccessTransport m = new SuccessTransport(1, s1);
+               m = new SuccessTransport();
+               m.setData(s1);
+               m.setStatus(1);
+               checkSuccessTransport(m);
+               logger.info(m.toString());
+       }
+
+}