Increase test coverage of AppStatsManager
[portal/ric-dashboard.git] / dashboard / webapp-backend / src / test / java / org / oransc / ric / portal / dashboard / AppStatsManagerTest.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property
6  * %%
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20 package org.oransc.ric.portal.dashboard;
21
22 import java.lang.invoke.MethodHandles;
23 import java.util.List;
24
25 import org.junit.Assert;
26 import org.junit.jupiter.api.Test;
27 import org.oransc.ric.portal.dashboard.config.RICInstanceMockConfiguration;
28 import org.oransc.ric.portal.dashboard.exception.StatsManagerException;
29 import org.oransc.ric.portal.dashboard.model.AppStats;
30 import org.oransc.ric.portal.dashboard.model.StatsDetailsTransport;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.test.context.ActiveProfiles;
34
35 @ActiveProfiles("test")
36 public class AppStatsManagerTest {
37
38         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
39
40         @Test
41         public void testStatsMgr() throws Exception {
42                 new AppStatsManager("file.json");
43                 try {
44                         new AppStatsManager(null);
45                         throw new Exception("Unexpected success");
46                 } catch (IllegalArgumentException ex) {
47                         logger.info("caught expected exception: {}", ex.toString());
48                 }
49                 AppStatsManager mgr = new AppStatsManager(true);
50                 AppStats as1 = mgr.createStats(RICInstanceMockConfiguration.INSTANCE_KEY_1,
51                                 new StatsDetailsTransport(-1, "app1 name", "http://app1"));
52                 logger.info("Created stats {}", as1);
53                 AppStats as2 = mgr.createStats(RICInstanceMockConfiguration.INSTANCE_KEY_1,
54                                 new StatsDetailsTransport(-1, "app2 name", "http://app2"));
55                 logger.info("Created stats {}", as2);
56                 List<AppStats> all = mgr.getStats();
57                 Assert.assertFalse(all.isEmpty());
58                 List<AppStats> byInstance = mgr.getStatsByInstance(RICInstanceMockConfiguration.INSTANCE_KEY_1);
59                 Assert.assertFalse(byInstance.isEmpty());
60                 mgr = new AppStatsManager(false);
61                 AppStats as3 = mgr.createStats(RICInstanceMockConfiguration.INSTANCE_KEY_1,
62                                 new StatsDetailsTransport(-1, "app3 name", "http://app3"));
63                 Assert.assertNotEquals(as2.getStatsDetails().getAppId(), as3.getStatsDetails().getAppId());
64                 try {
65                         mgr.createStats(as3.getInstanceKey(), as3.getStatsDetails());
66                         throw new Exception("Unexpected success");
67                 } catch (StatsManagerException ex) {
68                         logger.info("caught expected exception: {}", ex.toString());
69                 }
70                 AppStats as = mgr.getStatsById(as1.getInstanceKey(), as1.getStatsDetails().getAppId());
71                 Assert.assertEquals(as1, as);
72                 AppStats notFound = mgr.getStatsById("bogus", 12345);
73                 Assert.assertNull(notFound);
74                 as1.getStatsDetails().setMetricUrl("http://other");
75                 mgr.updateStats(as1.getInstanceKey(), as1.getStatsDetails());
76                 mgr.deleteStats(as1.getInstanceKey(), as1.getStatsDetails().getAppId());
77                 try {
78                         mgr.updateStats("bogus", as1.getStatsDetails());
79                         throw new Exception("Unexpected success");
80                 } catch (StatsManagerException ex) {
81                         logger.info("caught expected exception: {}", ex.toString());
82                 }
83                 try {
84                         mgr.deleteStats("bogus", 999);
85                         throw new Exception("Unexpected success");
86                 } catch (StatsManagerException ex) {
87                         logger.info("caught expected exception: {}", ex.toString());
88                 }
89         }
90
91 }