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