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