67a82c0aa6c6c76dad1f5c668ac2cdeb1ec1a9e5
[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.Assertions;
27 import org.junit.jupiter.api.Test;
28 import org.oransc.ric.portal.dashboard.config.RICInstanceMockConfiguration;
29 import org.oransc.ric.portal.dashboard.exception.StatsManagerException;
30 import org.oransc.ric.portal.dashboard.model.AppStats;
31 import org.oransc.ric.portal.dashboard.model.StatsDetailsTransport;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.test.context.ActiveProfiles;
35
36 @ActiveProfiles("test")
37 public class AppStatsManagerTest {
38
39         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
40
41         @Test
42         public void testStatsMgr() throws Exception {
43                 new AppStatsManager("file.json");
44                 Assertions.assertThrows(IllegalArgumentException.class, () -> {
45                         new AppStatsManager(null);
46                 });
47                 AppStatsManager mgr = new AppStatsManager(true);
48                 AppStats as1 = mgr.createStats(RICInstanceMockConfiguration.INSTANCE_KEY_1,
49                                 new StatsDetailsTransport(-1, "app1 name", "http://app1"));
50                 logger.info("Created stats {}", as1);
51                 AppStats as2 = mgr.createStats(RICInstanceMockConfiguration.INSTANCE_KEY_1,
52                                 new StatsDetailsTransport(-1, "app2 name", "http://app2"));
53                 logger.info("Created stats {}", as2);
54                 List<AppStats> all = mgr.getStats();
55                 Assert.assertFalse(all.isEmpty());
56                 List<AppStats> byInstance = mgr.getStatsByInstance(RICInstanceMockConfiguration.INSTANCE_KEY_1);
57                 Assert.assertFalse(byInstance.isEmpty());
58                 final AppStatsManager mgr2 = new AppStatsManager(false);
59                 AppStats as3 = mgr2.createStats(RICInstanceMockConfiguration.INSTANCE_KEY_1,
60                                 new StatsDetailsTransport(-1, "app3 name", "http://app3"));
61                 Assert.assertNotEquals(as2.getStatsDetails().getAppId(), as3.getStatsDetails().getAppId());
62                 Assertions.assertThrows(StatsManagerException.class, () -> {
63                         mgr2.createStats(as3.getInstanceKey(), as3.getStatsDetails());
64                 });
65                 AppStats as = mgr.getStatsById(as1.getInstanceKey(), as1.getStatsDetails().getAppId());
66                 Assert.assertEquals(as1, as);
67                 AppStats notFound = mgr.getStatsById("bogus", 12345);
68                 Assert.assertNull(notFound);
69                 as1.getStatsDetails().setMetricUrl("http://other");
70                 mgr.updateStats(as1.getInstanceKey(), as1.getStatsDetails());
71                 mgr.deleteStats(as1.getInstanceKey(), as1.getStatsDetails().getAppId());
72                 Assertions.assertThrows(StatsManagerException.class, () -> {
73                         mgr2.updateStats("bogus", as1.getStatsDetails());
74                 });
75                 Assertions.assertThrows(StatsManagerException.class, () -> {
76                         mgr2.deleteStats("bogus", 999);
77                 });
78         }
79
80 }