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>
@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());
*/
package org.oransc.ric.portal.dashboard.model;
+import java.util.Objects;
+
public class AppStats implements IDashboardResponse {
private String instanceKey;
private StatsDetailsTransport statsDetails;
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);
+ }
+
}
public StatsDetailsTransport() {
}
- @Override
- public String toString() {
- return this.getClass().getName() + "[appId=" + getAppId() + ", appName=" + getAppName() + ", metricUrl="
- + getMetricUrl() + "]";
- }
-
public String getAppName() {
return appName;
}
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;
+ }
}
--- /dev/null
+/*-
+ * ========================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());
+ }
+ }
+
+}
@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)
* 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 {
* <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() {
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());
}