Increase test coverage of AppStatsManager 62/3362/4
authorLott, Christopher (cl778h) <cl778h@att.com>
Tue, 21 Apr 2020 15:10:03 +0000 (11:10 -0400)
committerLott, Christopher (cl778h) <cl778h@att.com>
Tue, 21 Apr 2020 16:06:21 +0000 (12:06 -0400)
Also repair logger argument problem flagged by Sonar.
No functional change to app behavior.

Change-Id: If1c94d961c28eb4a60d0570fa2f5bd3c8fb3ea74
Signed-off-by: Lott, Christopher (cl778h) <cl778h@att.com>
dashboard/webapp-backend/src/main/java/org/oransc/ric/portal/dashboard/controller/CustomResponseEntityExceptionHandler.java
dashboard/webapp-backend/src/main/java/org/oransc/ric/portal/dashboard/model/AppStats.java
dashboard/webapp-backend/src/main/java/org/oransc/ric/portal/dashboard/model/StatsDetailsTransport.java
dashboard/webapp-backend/src/test/java/org/oransc/ric/portal/dashboard/AppStatsManagerTest.java [new file with mode: 0644]
dashboard/webapp-backend/src/test/java/org/oransc/ric/portal/dashboard/controller/AdminControllerTest.java
dashboard/webapp-backend/src/test/java/org/oransc/ric/portal/dashboard/controller/DefaultContextTest.java
dashboard/webapp-backend/src/test/java/org/oransc/ric/portal/dashboard/controller/E2ManagerControllerTest.java

index 4c800d9..a328e47 100644 (file)
@@ -83,7 +83,8 @@ public class CustomResponseEntityExceptionHandler extends ResponseEntityExceptio
        @ExceptionHandler({ RestClientResponseException.class })
        public final ResponseEntity<String> handleProxyMethodException(Exception ex, WebRequest request) {
                // Capture the full stack trace in the log.
-               log.error("handleProxyMethodException: request {}, exception {}", request.getDescription(false), ex);
+               if (log.isErrorEnabled())
+                       log.error("handleProxyMethodException: request " + request.getDescription(false), ex);
                if (ex instanceof HttpStatusCodeException) {
                        HttpStatusCodeException hsce = (HttpStatusCodeException) ex;
                        return ResponseEntity.status(HttpStatus.BAD_GATEWAY).body(hsce.getResponseBodyAsString());
index 6710cf6..1bf6b97 100644 (file)
@@ -19,6 +19,8 @@
  */
 package org.oransc.ric.portal.dashboard.model;
 
+import java.util.Objects;
+
 public class AppStats implements IDashboardResponse {
        private String instanceKey;
        private StatsDetailsTransport statsDetails;
@@ -54,4 +56,16 @@ public class AppStats implements IDashboardResponse {
                return this.getClass().getSimpleName() + "[instance=" + instanceKey + ", statsDetails=" + statsDetails + "]";
        }
 
+       @Override
+       public boolean equals(Object obj) {
+               if (this == obj)
+                       return true;
+               if (obj == null)
+                       return false;
+               if (getClass() != obj.getClass())
+                       return false;
+               AppStats other = (AppStats) obj;
+               return Objects.equals(instanceKey, other.instanceKey) && Objects.equals(statsDetails, other.statsDetails);
+       }
+
 }
index d79644e..847fdcb 100644 (file)
@@ -42,12 +42,6 @@ public class StatsDetailsTransport implements IDashboardResponse {
        public StatsDetailsTransport() {
        }
 
-       @Override
-       public String toString() {
-               return this.getClass().getName() + "[appId=" + getAppId() + ", appName=" + getAppName() + ", metricUrl="
-                               + getMetricUrl() + "]";
-       }
-
        public String getAppName() {
                return appName;
        }
@@ -63,4 +57,22 @@ public class StatsDetailsTransport implements IDashboardResponse {
        public void setMetricUrl(String metricUrl) {
                this.metricUrl = metricUrl;
        }
+
+       @Override
+       public String toString() {
+               return this.getClass().getName() + "[appId=" + getAppId() + ", appName=" + getAppName() + ", metricUrl="
+                               + getMetricUrl() + "]";
+       }
+
+       @Override
+       public boolean equals(Object obj) {
+               if (this == obj)
+                       return true;
+               if (obj == null)
+                       return false;
+               if (getClass() != obj.getClass())
+                       return false;
+               StatsDetailsTransport other = (StatsDetailsTransport) obj;
+               return appId == other.appId;
+       }
 }
diff --git a/dashboard/webapp-backend/src/test/java/org/oransc/ric/portal/dashboard/AppStatsManagerTest.java b/dashboard/webapp-backend/src/test/java/org/oransc/ric/portal/dashboard/AppStatsManagerTest.java
new file mode 100644 (file)
index 0000000..5aaace2
--- /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;
+
+import java.lang.invoke.MethodHandles;
+import java.util.List;
+
+import org.junit.Assert;
+import org.junit.jupiter.api.Test;
+import org.oransc.ric.portal.dashboard.config.RICInstanceMockConfiguration;
+import org.oransc.ric.portal.dashboard.exception.StatsManagerException;
+import org.oransc.ric.portal.dashboard.model.AppStats;
+import org.oransc.ric.portal.dashboard.model.StatsDetailsTransport;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.test.context.ActiveProfiles;
+
+@ActiveProfiles("test")
+public class AppStatsManagerTest {
+
+       private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+
+       @Test
+       public void testStatsMgr() throws Exception {
+               new AppStatsManager("file.json");
+               try {
+                       new AppStatsManager(null);
+                       throw new Exception("Unexpected success");
+               } catch (IllegalArgumentException ex) {
+                       logger.info("caught expected exception: {}", ex.toString());
+               }
+               AppStatsManager mgr = new AppStatsManager(true);
+               AppStats as1 = mgr.createStats(RICInstanceMockConfiguration.INSTANCE_KEY_1,
+                               new StatsDetailsTransport(-1, "app1 name", "http://app1"));
+               logger.info("Created stats {}", as1);
+               AppStats as2 = mgr.createStats(RICInstanceMockConfiguration.INSTANCE_KEY_1,
+                               new StatsDetailsTransport(-1, "app2 name", "http://app2"));
+               logger.info("Created stats {}", as2);
+               List<AppStats> all = mgr.getStats();
+               Assert.assertFalse(all.isEmpty());
+               List<AppStats> byInstance = mgr.getStatsByInstance(RICInstanceMockConfiguration.INSTANCE_KEY_1);
+               Assert.assertFalse(byInstance.isEmpty());
+               mgr = new AppStatsManager(false);
+               AppStats as3 = mgr.createStats(RICInstanceMockConfiguration.INSTANCE_KEY_1,
+                               new StatsDetailsTransport(-1, "app3 name", "http://app3"));
+               Assert.assertNotEquals(as2.getStatsDetails().getAppId(), as3.getStatsDetails().getAppId());
+               try {
+                       mgr.createStats(as3.getInstanceKey(), as3.getStatsDetails());
+                       throw new Exception("Unexpected success");
+               } catch (StatsManagerException ex) {
+                       logger.info("caught expected exception: {}", ex.toString());
+               }
+               AppStats as = mgr.getStatsById(as1.getInstanceKey(), as1.getStatsDetails().getAppId());
+               Assert.assertEquals(as1, as);
+               AppStats notFound = mgr.getStatsById("bogus", 12345);
+               Assert.assertNull(notFound);
+               as1.getStatsDetails().setMetricUrl("http://other");
+               mgr.updateStats(as1.getInstanceKey(), as1.getStatsDetails());
+               mgr.deleteStats(as1.getInstanceKey(), as1.getStatsDetails().getAppId());
+               try {
+                       mgr.updateStats("bogus", as1.getStatsDetails());
+                       throw new Exception("Unexpected success");
+               } catch (StatsManagerException ex) {
+                       logger.info("caught expected exception: {}", ex.toString());
+               }
+               try {
+                       mgr.deleteStats("bogus", 999);
+                       throw new Exception("Unexpected success");
+               } catch (StatsManagerException ex) {
+                       logger.info("caught expected exception: {}", ex.toString());
+               }
+       }
+
+}
index 4deafe7..37ab484 100644 (file)
@@ -97,14 +97,30 @@ public class AdminControllerTest extends AbstractControllerTest {
        @Order(1)
        @Test
        public void getAppStatsTest() {
+               // Get all
                URI uri = buildUri(null, AdminController.CONTROLLER_PATH, DashboardConstants.RIC_INSTANCE_KEY, "i1",
                                AdminController.STATAPPMETRIC_METHOD);
-               logger.info("Invoking uri {}", uri);
-               ResponseEntity<List<AppStats>> response = testRestTemplateAdminRole().exchange(uri, HttpMethod.GET, null,
+               logger.info("getAppStatsTest: uri {}", uri);
+               ResponseEntity<List<AppStats>> list = testRestTemplateStandardRole().exchange(uri, HttpMethod.GET, null,
                                new ParameterizedTypeReference<List<AppStats>>() {
                                });
-               Assertions.assertFalse(response.getBody().isEmpty());
-               Assertions.assertNotEquals(-1, response.getBody().get(0).getStatsDetails().getAppId());
+               Assertions.assertFalse(list.getBody().isEmpty());
+               Assertions.assertNotEquals(-1, list.getBody().get(0).getStatsDetails().getAppId());
+
+               // Get one by ID
+               int appId = list.getBody().get(0).getStatsDetails().getAppId();
+               uri = buildUri(null, AdminController.CONTROLLER_PATH, DashboardConstants.RIC_INSTANCE_KEY, "i1",
+                               AdminController.STATAPPMETRIC_METHOD, DashboardConstants.APP_ID, Integer.toString(appId));
+               logger.info("getAppStatsTest: uri {}", uri);
+               AppStats stats = testRestTemplateStandardRole().getForObject(uri, AppStats.class);
+               Assertions.assertEquals(appId, stats.getStatsDetails().getAppId());
+
+               // Fail to get one by ID
+               uri = buildUri(null, AdminController.CONTROLLER_PATH, DashboardConstants.RIC_INSTANCE_KEY, "i1",
+                               AdminController.STATAPPMETRIC_METHOD, DashboardConstants.APP_ID, "987654321");
+               logger.info("getAppStatsTest: uri {}", uri);
+               stats = testRestTemplateStandardRole().getForObject(uri, AppStats.class);
+               Assert.assertNull(stats);
        }
 
        @Order(2)
index dba0a32..b233eaf 100644 (file)
@@ -34,7 +34,7 @@ import org.springframework.test.context.junit.jupiter.SpringExtension;
  * Tests whether the default (not mock) configuration classes run to completion.
  */
 @ExtendWith(SpringExtension.class)
-// This way of setting the active profile should not be necessary.  See:
+// This way of setting the active profile should not be necessary. See:
 // https://github.com/spring-projects/spring-boot/issues/19788
 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = "spring.profiles.active:default")
 public class DefaultContextTest {
@@ -53,11 +53,9 @@ public class DefaultContextTest {
         * <LI>{@link PortalRestCentralServiceTest#createUserTest()}
         * <LI>{@link PortalRestCentralServiceTest#updateUserTest()}
         * </UL>
-        * Maybe: 
-        *
-        * I worked around the problem by using the application.yaml credentials.
-        * I also annotated this class above trying to limit the active profile,
-        * but I'm not confident it is working nor that it's needed.
+        * I worked around the problem by using the application.yaml credentials. I also
+        * annotated this class above trying to limit the active profile, but I'm not
+        * confident it is working nor that it's needed.
         */
        @Test
        public void contextLoads() {
index fb86630..844e7e2 100644 (file)
@@ -112,7 +112,8 @@ public class E2ManagerControllerTest extends AbstractControllerTest {
                logger.info("Invoking {}", uri);
                ResetRequest reset = new ResetRequest();
                HttpEntity<ResetRequest> entity = new HttpEntity<>(reset);
-               ResponseEntity<Void> voidResponse = testRestTemplateAdminRole().exchange(uri, HttpMethod.PUT, entity, Void.class);
+               ResponseEntity<Void> voidResponse = testRestTemplateAdminRole().exchange(uri, HttpMethod.PUT, entity,
+                               Void.class);
                logger.debug("resetTest: response {}", voidResponse);
                Assertions.assertTrue(voidResponse.getStatusCode().is2xxSuccessful());
        }