Improve unit-test coverage 25/2225/2
authorLott, Christopher (cl778h) <cl778h@att.com>
Mon, 13 Jan 2020 21:08:06 +0000 (16:08 -0500)
committerLott, Christopher (cl778h) <cl778h@att.com>
Tue, 14 Jan 2020 16:02:53 +0000 (11:02 -0500)
Add tests of Portal API filter.
Extend model (POJO) tests.
No functional changes.

Change-Id: I8d3ea2fa68ef8cbeede1f80afe3eb1c990108595
Signed-off-by: Lott, Christopher (cl778h) <cl778h@att.com>
webapp-backend/src/test/java/org/oransc/ric/portal/dashboard/controller/AdminControllerExtension.java [moved from webapp-backend/src/test/java/org/oransc/ric/portal/dashboard/controller/AdminController2.java with 59% similarity]
webapp-backend/src/test/java/org/oransc/ric/portal/dashboard/controller/AdminControllerTest.java
webapp-backend/src/test/java/org/oransc/ric/portal/dashboard/controller/AppManagerControllerTest.java
webapp-backend/src/test/java/org/oransc/ric/portal/dashboard/model/ModelTest.java
webapp-backend/src/test/java/org/oransc/ric/portal/dashboard/portalapi/PortalAuthManagerTest.java [new file with mode: 0644]

@@ -23,31 +23,44 @@ import java.lang.invoke.MethodHandles;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.http.HttpStatus;
 import org.springframework.http.MediaType;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.client.HttpClientErrorException;
 import org.springframework.web.client.RestClientResponseException;
 
 /**
- * Provides methods that throw exceptions to support testing.
+ * Extends the Admin controller with methods that throw exceptions to support
+ * testing.
  */
 @RestController
 @RequestMapping(value = AdminController.CONTROLLER_PATH, produces = MediaType.APPLICATION_JSON_VALUE)
-public class AdminController2 {
+public class AdminControllerExtension {
+
+       public static final String HTTP_STATUS_CODE_EXCEPTION_METHOD = "hscexception";
+       public static final String REST_CLIENT_RESPONSE_EXCEPTION_METHOD = "rcrexception";
+       public static final String RUNTIME_EXCEPTION_METHOD = "rexception";
 
        private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
 
-       @GetMapping("throw1")
-       public void throw1() {
+       @GetMapping(HTTP_STATUS_CODE_EXCEPTION_METHOD)
+       public void throwHttpStatusCodeException() {
+               logger.warn("throwing HttpStatusCodeException");
+               throw new HttpClientErrorException(HttpStatus.CHECKPOINT, "simulate http status code exception");
+       }
+
+       @GetMapping(REST_CLIENT_RESPONSE_EXCEPTION_METHOD)
+       public void throwRestClientResponseException() {
                logger.warn("throwing RestClientResponseException");
-               throw new RestClientResponseException("foo", 0, "bar", null, null, null);
+               throw new RestClientResponseException("simulate remote client failure", 0, "bar", null, null, null);
        }
 
-       @GetMapping("throw2")
-       public void throw2() {
+       @GetMapping(RUNTIME_EXCEPTION_METHOD)
+       public void throwRuntimeException() {
                logger.warn("throwing RuntimeException");
-               throw new RuntimeException("throw2");
+               throw new RuntimeException("simulate runtime failure");
        }
 
 }
index d6f206e..02534fb 100644 (file)
@@ -90,13 +90,23 @@ public class AdminControllerTest extends AbstractControllerTest {
        @Test
        public void getxAppMetricsUrlTest() {
                Map<String, String> metricsQueryParms = new HashMap<String, String>();
+               URI uri;
+
+               metricsQueryParms.clear();
                metricsQueryParms.put("app", DashboardConstants.APP_NAME_AC);
-               URI uri = buildUri(metricsQueryParms, AdminController.CONTROLLER_PATH, AdminController.XAPPMETRICS_METHOD);
+               uri = buildUri(metricsQueryParms, AdminController.CONTROLLER_PATH, AdminController.XAPPMETRICS_METHOD);
                logger.debug("Invoking {}", uri);
                ResponseEntity<SuccessTransport> successResponse = testRestTemplateStandardRole().exchange(uri, HttpMethod.GET,
                                null, SuccessTransport.class);
                Assertions.assertFalse(successResponse.getBody().getData().toString().isEmpty());
                Assertions.assertTrue(successResponse.getStatusCode().is2xxSuccessful());
+
+               metricsQueryParms.clear();
+               metricsQueryParms.put("app", DashboardConstants.APP_NAME_MC);
+               logger.debug("Invoking {}", uri);
+               successResponse = testRestTemplateStandardRole().exchange(uri, HttpMethod.GET, null, SuccessTransport.class);
+               Assertions.assertFalse(successResponse.getBody().getData().toString().isEmpty());
+               Assertions.assertTrue(successResponse.getStatusCode().is2xxSuccessful());
        }
 
        @Test
@@ -113,4 +123,36 @@ public class AdminControllerTest extends AbstractControllerTest {
                Assertions.assertTrue(errorResponse.getStatusCode().is4xxClientError());
        }
 
+       @Test
+       public void throwHttpStatusCodeExceptionTest() {
+               URI uri = buildUri(null, AdminController.CONTROLLER_PATH,
+                               AdminControllerExtension.HTTP_STATUS_CODE_EXCEPTION_METHOD);
+               logger.debug("Invoking {}", uri);
+               ResponseEntity<ErrorTransport> errorResponse = testRestTemplateStandardRole().exchange(uri, HttpMethod.GET,
+                               null, ErrorTransport.class);
+               logger.debug("{}", errorResponse.getBody().getError().toString());
+               Assertions.assertTrue(errorResponse.getStatusCode().is5xxServerError());
+       }
+
+       @Test
+       public void throwRestClientResponseExceptionTest() {
+               URI uri = buildUri(null, AdminController.CONTROLLER_PATH,
+                               AdminControllerExtension.REST_CLIENT_RESPONSE_EXCEPTION_METHOD);
+               logger.debug("Invoking {}", uri);
+               ResponseEntity<ErrorTransport> errorResponse = testRestTemplateStandardRole().exchange(uri, HttpMethod.GET,
+                               null, ErrorTransport.class);
+               logger.debug("{}", errorResponse.getBody().getError().toString());
+               Assertions.assertTrue(errorResponse.getStatusCode().is5xxServerError());
+       }
+
+       @Test
+       public void throwRuntimeExceptionTest() {
+               URI uri = buildUri(null, AdminController.CONTROLLER_PATH, AdminControllerExtension.RUNTIME_EXCEPTION_METHOD);
+               logger.debug("Invoking {}", uri);
+               ResponseEntity<String> errorResponse = testRestTemplateStandardRole().exchange(uri, HttpMethod.GET, null,
+                               String.class);
+               logger.debug("{}", errorResponse.getBody());
+               Assertions.assertTrue(errorResponse.getStatusCode().is5xxServerError());
+       }
+
 }
index 961e693..fcc0566 100644 (file)
@@ -149,4 +149,13 @@ public class AppManagerControllerTest extends AbstractControllerTest {
                Assertions.assertTrue(voidResponse.getStatusCode().is2xxSuccessful());
        }
 
+       @Test
+       public void invalidInstanceTest() {
+               URI uri = buildUri(null, AppManagerController.CONTROLLER_PATH, DashboardConstants.RIC_INSTANCE_KEY, "bogus",
+                               AppManagerController.CONFIG_METHOD);
+               logger.info("Invoking {}", uri);
+               ResponseEntity<Void> voidResponse = testRestTemplateAdminRole().exchange(uri, HttpMethod.GET, null, Void.class);
+               Assertions.assertTrue(voidResponse.getStatusCode().is5xxServerError());
+       }
+
 }
index 3208fd4..5cb134b 100644 (file)
@@ -20,6 +20,8 @@
 package org.oransc.ric.portal.dashboard.model;
 
 import java.lang.invoke.MethodHandles;
+import java.util.ArrayList;
+import java.util.List;
 
 import org.junit.Assert;
 import org.junit.jupiter.api.Test;
@@ -59,6 +61,9 @@ public class ModelTest extends AbstractModelTest {
                Assert.assertNotNull(eud.getUsername());
                Assert.assertTrue(eud.isAccountNonExpired());
                Assert.assertTrue(eud.isAccountNonLocked());
+               Assert.assertTrue(eud.isCredentialsNonExpired());
+               Assert.assertTrue(eud.isEnabled());
+               logger.info(eud.toString());
        }
 
        private void checkErrorTransport(ErrorTransport m) {
@@ -110,6 +115,7 @@ public class ModelTest extends AbstractModelTest {
                m.nodebIdentity(nodebIdentity).nodebStatus(nodebResponse);
                Assert.assertEquals(m.getNodebIdentity(), nodebIdentity);
                Assert.assertEquals(m.getNodebStatus(), nodebResponse);
+               logger.debug(m.toString());
        }
 
        private void checkSuccessTransport(SuccessTransport m) {
@@ -139,7 +145,9 @@ public class ModelTest extends AbstractModelTest {
                m.setKey(s1);
                m.setName(s2);
                checkRicInstanceKeyName(m);
-               Assert.assertEquals(m, m);
+               Assert.assertTrue(m.equals(m));
+               Assert.assertFalse(m.equals(null));
+               Assert.assertFalse(m.equals(new RicInstanceKeyName()));
                Assert.assertNotEquals(1, m.hashCode());
                logger.info(m.toString());
        }
@@ -159,9 +167,26 @@ public class ModelTest extends AbstractModelTest {
                m.setKey(s3);
                m.setName(s4);
                checkRicInstance(m);
-               Assert.assertEquals(m, m);
+               Assert.assertTrue(m.equals(m));
+               Assert.assertFalse(m.equals(null));
+               Assert.assertFalse(m.equals(new RicInstance()));
                Assert.assertNotEquals(1, m.hashCode());
                logger.info(m.toString());
        }
 
+       @Test
+       public void testRicInstanceList() {
+               RicInstanceList m = new RicInstanceList();
+               List<RicInstance> list = new ArrayList<>();
+               m = new RicInstanceList(list);
+               m.getInstances();
+               m.getKeyNameList();
+               try {
+                       m.getInstance(s1);
+               } catch (Exception ex) {
+                       logger.info("failed as expected", ex);
+               }
+               logger.info(m.toString());
+       }
+
 }
diff --git a/webapp-backend/src/test/java/org/oransc/ric/portal/dashboard/portalapi/PortalAuthManagerTest.java b/webapp-backend/src/test/java/org/oransc/ric/portal/dashboard/portalapi/PortalAuthManagerTest.java
new file mode 100644 (file)
index 0000000..e5e2d7f
--- /dev/null
@@ -0,0 +1,91 @@
+/*-
+ * ========================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.portalapi;
+
+import java.io.IOException;
+import java.lang.invoke.MethodHandles;
+import java.lang.reflect.InvocationTargetException;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.Cookie;
+
+import org.junit.Assert;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.onap.portalsdk.core.onboarding.util.PortalApiConstants;
+import org.oransc.ric.portal.dashboard.DashboardUserManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
+import org.springframework.test.context.ActiveProfiles;
+import org.springframework.test.context.junit.jupiter.SpringExtension;
+
+@ExtendWith(SpringExtension.class)
+@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
+@ActiveProfiles("test")
+public class PortalAuthManagerTest {
+
+       @Value("${portalapi.decryptor}")
+       private String decryptor;
+
+       private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+
+       @Test
+       public void testPortalStuff() throws ClassNotFoundException, InstantiationException, IllegalAccessException,
+                       InvocationTargetException, NoSuchMethodException, IOException, ServletException {
+               PortalAuthManager m = new PortalAuthManager("app", "user", "secret", decryptor, "cookie");
+               Assert.assertNotNull(m.getAppCredentials());
+               String s = null;
+
+               MockHttpServletRequest request = new MockHttpServletRequest();
+               s = m.validateEcompSso(request);
+               logger.debug("validateEcompSso answers {}", s);
+               Assert.assertNull(s);
+
+               Cookie cookie = new Cookie(PortalApiConstants.EP_SERVICE, "bogus");
+               request.setCookies(cookie);
+               s = m.validateEcompSso(request);
+               logger.debug("validateEcompSso answers {}", s);
+               Assert.assertNull(s);
+
+               DashboardUserManager dum = new DashboardUserManager(true);
+               PortalAuthenticationFilter filter = new PortalAuthenticationFilter(false, m, dum);
+               filter.init(null);
+               filter.destroy();
+               MockHttpServletResponse response = new MockHttpServletResponse();
+               try {
+                       filter.doFilter(request, response, null);
+               } catch (NullPointerException ex) {
+                       logger.debug("chain is null");
+               }
+
+               filter = new PortalAuthenticationFilter(true, m, dum);
+               try {
+                       filter.doFilter(request, response, null);
+               } catch (NullPointerException ex) {
+                       logger.debug("chain is null");
+               }
+       }
+
+}
\ No newline at end of file