[PORTAL-16 PORTAL-18] Widget ms; staging
[portal.git] / ecomp-portal-BE-common-test / src / main / java / org / openecomp / portalapp / portal / test / framework / ApplicationCommonContextTestSuite.java
1 /*-
2  * ================================================================================
3  * ECOMP Portal
4  * ================================================================================
5  * Copyright (C) 2017 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  * ================================================================================
19  */
20 package org.openecomp.portalapp.portal.test.framework;
21
22 import java.io.IOException;
23
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.openecomp.portalsdk.core.conf.AppConfig;
28 //import org.openecomp.portalapp.conf.ExternalAppConfig;
29 //import org.openecomp.portalapp.conf.HibernateMappingLocations;
30 import org.openecomp.portalsdk.core.conf.HibernateConfiguration;
31 import org.openecomp.portalsdk.core.conf.HibernateMappingLocatable;
32 import org.openecomp.portalsdk.core.objectcache.AbstractCacheManager;
33 import org.openecomp.portalsdk.core.util.CacheManager;
34 import org.openecomp.portalsdk.core.util.SystemProperties;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.context.annotation.Bean;
37 import org.springframework.context.annotation.ComponentScan;
38 import org.springframework.context.annotation.Configuration;
39 import org.springframework.context.annotation.FilterType;
40 import org.springframework.context.annotation.Profile;
41 import org.springframework.core.io.ClassPathResource;
42 import org.springframework.core.io.Resource;
43 import org.springframework.test.context.ActiveProfiles;
44 import org.springframework.test.context.ContextConfiguration;
45 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
46 import org.springframework.test.context.transaction.TransactionConfiguration;
47 import org.springframework.test.context.web.AnnotationConfigWebContextLoader;
48 import org.springframework.test.context.web.WebAppConfiguration;
49 import org.springframework.test.web.servlet.MockMvc;
50 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
51 import org.springframework.transaction.annotation.Transactional;
52 import org.springframework.web.context.WebApplicationContext;
53 import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
54
55 /**
56  * 
57  * In order to write a unit test, 1. inherit this class 2. place the "war"
58  * folder on your test class's classpath 3. run the test with the following VM
59  * argument; This is important because when starting the application from
60  * Container, the System Properties file (SystemProperties.java) can have the
61  * direct path but, when running from the Mock Junit container, the path should
62  * be prefixed with "classpath" to enable the mock container to search for the
63  * file in the classpath -Dcontainer.classpath="classpath:"
64  */
65
66 @SuppressWarnings("deprecation")
67 @RunWith(SpringJUnit4ClassRunner.class)
68 @WebAppConfiguration
69 @ContextConfiguration(loader = AnnotationConfigWebContextLoader.class, classes = { MockAppConfig.class })
70 @ActiveProfiles(value = "test")
71 public class ApplicationCommonContextTestSuite {
72
73         @Autowired
74         public WebApplicationContext wac;
75
76         private MockMvc mockMvc;
77
78         @Before
79         public void setup() {
80                 if (mockMvc == null) {
81                         this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
82
83                 }
84         }
85
86         public Object getBean(String name) {
87                 return this.wac.getBean(name);
88         }
89
90         public MockMvc getMockMvc() {
91                 return mockMvc;
92         }
93
94         public void setMockMvc(MockMvc mockMvc) {
95                 this.mockMvc = mockMvc;
96         }
97
98         public WebApplicationContext getWebApplicationContext() {
99                 return wac;
100         }
101
102 }
103
104 @Configuration
105 @ComponentScan(basePackages = "org.openecomp", excludeFilters = {
106                 // the following source configurations should not be scanned; instead of
107                 // using Exclusion filter, we can use the @Profile annotation to exclude
108                 // them
109                 @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.openecomp.portalsdk.core.controller.LogoutController*"),
110                 @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.openecomp.portalsdk.core.controller.SDKLoginController*"),
111                 @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.openecomp.portalapp.conf.ExternalAppConfig*"),
112                 @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.openecomp.*.*InitUebHandler*") })
113 @Profile("test")
114 class MockAppConfig extends AppConfig {
115
116         @Bean
117         HibernateMappingLocatable locatable() {
118                 return new MockHibernateMappingLocations();
119         }
120
121         @Bean
122         HibernateConfiguration hibConfiguration() {
123                 return new HibernateConfiguration();
124         }
125
126         @Bean
127         public SystemProperties systemProperties() {
128                 return new MockSystemProperties();
129         }
130
131         @Bean
132         public AbstractCacheManager cacheManager() {
133                 return new CacheManager() {
134
135                         public void configure() throws IOException {
136
137                         }
138                 };
139         }
140
141         public String[] tileDefinitions() {
142                 return new String[] { "classpath:/WEB-INF/fusion/defs/definitions.xml",
143                                 "classpath:/WEB-INF/defs/definitions.xml" };
144         }
145
146         public void addInterceptors(InterceptorRegistry registry) {
147                 // registry.addInterceptor(new
148                 // SessionTimeoutInterceptor()).excludePathPatterns(getExcludeUrlPathsForSessionTimeout());
149                 // registry.addInterceptor(resourceInterceptor());
150         }
151
152         public static class MockSystemProperties extends SystemProperties {
153
154                 public MockSystemProperties() {
155                 }
156
157         }
158
159 }
160
161 @Profile("test")
162 class MockHibernateMappingLocations implements HibernateMappingLocatable {
163
164         public Resource[] getMappingLocations() {
165                 return new Resource[] { new ClassPathResource("WEB-INF/fusion/orm/Fusion.hbm.xml"),
166                                 new ClassPathResource("WEB-INF/fusion/orm/EP.hbm.xml"),
167                                 new ClassPathResource("WEB-INF/fusion/orm/Workflow.hbm.xml") };
168
169         }
170
171         public String[] getPackagesToScan() {
172                 return new String[] { "org.openecomp", "src" };
173         }
174
175 }