Revise mock config classes to autowire properties
[portal/ric-dashboard.git] / dashboard / webapp-backend / src / test / java / org / oransc / ric / portal / dashboard / config / AdminMockConfiguration.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.config;
21
22 import java.io.IOException;
23 import java.lang.invoke.MethodHandles;
24 import java.util.HashSet;
25 import java.util.Set;
26
27 import org.onap.portalsdk.core.onboarding.exception.PortalAPIException;
28 import org.onap.portalsdk.core.restful.domain.EcompRole;
29 import org.onap.portalsdk.core.restful.domain.EcompUser;
30 import org.oransc.ric.portal.dashboard.AppStatsManager;
31 import org.oransc.ric.portal.dashboard.DashboardUserManager;
32 import org.oransc.ric.portal.dashboard.exception.StatsManagerException;
33 import org.oransc.ric.portal.dashboard.model.StatsDetailsTransport;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.beans.factory.annotation.Value;
38 import org.springframework.context.annotation.Bean;
39 import org.springframework.context.annotation.Configuration;
40 import org.springframework.context.annotation.Profile;
41
42 /**
43  * Creates user manager and stats manager with mock data.
44  */
45 @Configuration
46 @Profile("test")
47 public class AdminMockConfiguration {
48
49         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
50
51         // Autowire all the properties required by the real class
52         // (even tho not used here) as a test of the properties.
53         @Autowired
54         public AdminMockConfiguration(@Value("${userfile}") final String userfile,
55                         @Value("${statsfile}") final String statsfile) {
56                 logger.info("ctor userfile {} statsfile {}", userfile, statsfile);
57         }
58
59         @Bean
60         // The bean (method) name must be globally unique
61         public DashboardUserManager userManager() throws IOException, PortalAPIException {
62                 logger.debug("userManager: adding mock data");
63                 DashboardUserManager mgr = new DashboardUserManager(true);
64                 String[] firsts = { "John", "Alice", "Pierce", "Paul", "Jack" };
65                 String[] lasts = { "Doe", "Nolan", "King", "Smith", "Reacher" };
66                 String[] logins = { "jdoe", "anolan", "pking", "psmith", "jreacher" };
67                 boolean[] actives = { true, true, false, false, true };
68                 EcompRole role = new EcompRole();
69                 role.setName("view");
70                 Set<EcompRole> roles = new HashSet<>();
71                 roles.add(role);
72                 for (int i = 0; i < firsts.length; ++i) {
73                         EcompUser eu = new EcompUser();
74                         eu.setFirstName(firsts[i]);
75                         eu.setLastName(lasts[i]);
76                         eu.setLoginId(logins[i]);
77                         eu.setActive(actives[i]);
78                         eu.setRoles(roles);
79                         mgr.createUser(eu);
80                 }
81                 return mgr;
82         }
83
84         @Bean
85         // The bean (method) name must be globally unique
86         public AppStatsManager statsManager() throws IOException, StatsManagerException {
87                 logger.debug("statsManager: adding mock data");
88                 AppStatsManager mgr = new AppStatsManager(true);
89                 String instanceKey = RICInstanceMockConfiguration.INSTANCE_KEY_1;
90                 StatsDetailsTransport statsDetails = new StatsDetailsTransport();
91                 statsDetails.setAppId(0);
92                 statsDetails.setAppName("MachLearn");
93                 statsDetails.setMetricUrl("https://www.example.com");
94                 mgr.createStats(instanceKey, statsDetails);
95                 return mgr;
96         }
97
98 }