Upgrade sonar plugin
[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 import org.testng.annotations.Test;\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 @Test(enabled=false)\r
65 public class MockApplicationContextTestSuite {\r
66                 \r
67             /** The wac. */\r
68         @Autowired\r
69             public WebApplicationContext wac;\r
70 \r
71             /** The mock mvc. */\r
72         private MockMvc mockMvc;\r
73 \r
74             /**\r
75          * Setup.\r
76          */\r
77         @Before\r
78             public void setup() {\r
79                 if(mockMvc == null) {\r
80                         this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();\r
81                         \r
82                 }\r
83             }\r
84             \r
85             /**\r
86          * Gets the bean.\r
87          *\r
88          * @param name the name\r
89          * @return the bean\r
90          */\r
91         public Object getBean(String name) {\r
92                         return this.wac.getBean(name);\r
93                 }\r
94 \r
95 \r
96                 /**\r
97                  * Gets the mock mvc.\r
98                  *\r
99                  * @return the mock mvc\r
100                  */\r
101                 public MockMvc getMockMvc() {\r
102                         return mockMvc;\r
103                 }\r
104 \r
105                 /**\r
106                  * Sets the mock mvc.\r
107                  *\r
108                  * @param mockMvc the new mock mvc\r
109                  */\r
110                 public void setMockMvc(MockMvc mockMvc) {\r
111                         this.mockMvc = mockMvc;\r
112                 }\r
113                 \r
114                 /**\r
115                  * Gets the web application context.\r
116                  *\r
117                  * @return the web application context\r
118                  */\r
119                 public WebApplicationContext getWebApplicationContext() {\r
120                         return wac;\r
121                 }\r
122                 \r
123                 \r
124                 \r
125                 \r
126 }\r
127                 \r
128 \r
129                 @Configuration\r
130                 @ComponentScan(basePackages = "org.openecomp", \r
131                                  excludeFilters = {\r
132                                                                         // the following source configurations should not be scanned; instead of using Exclusion filter, we can use the @Profile annotation to exclude them\r
133                                                                         // see AppConfig class\r
134                                                                         //@ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.openecomp.portalsdk.core.*AppConfig*")//,\r
135                                                                         //@ComponentScan.Filter(type = FilterType.REGEX, pattern = org.openecomp.*.*AppConfig*")\r
136                                                                   }\r
137                         )\r
138                 @Profile("test")\r
139                 class MockAppConfig extends AppConfig {\r
140                         \r
141                         @Bean \r
142                     public SystemProperties systemProperties(){\r
143                         return new MockSystemProperties();\r
144                     }\r
145                         \r
146                         @Bean\r
147                     public AbstractCacheManager cacheManager() {\r
148                         return new CacheManager() {\r
149                                 \r
150                                 public void configure() throws IOException {\r
151                                          \r
152                                 }\r
153                         };\r
154                     }\r
155                         \r
156                         protected String[] tileDefinitions() {\r
157                                 return new String[] {"classpath:/WEB-INF/fusion/defs/definitions.xml", "classpath:/WEB-INF/defs/definitions.xml"};\r
158                         }\r
159                         \r
160                          @Override\r
161                         public void addInterceptors(InterceptorRegistry registry) {\r
162                             //registry.addInterceptor(new SessionTimeoutInterceptor()).excludePathPatterns(getExcludeUrlPathsForSessionTimeout());\r
163                             //registry.addInterceptor(resourceInterceptor());\r
164                         }\r
165                          \r
166                          public static class MockSystemProperties extends SystemProperties {\r
167                                         \r
168                                         public MockSystemProperties() {\r
169                                         }\r
170                                         \r
171                                 }\r
172                                         \r
173                 }\r
174                 \r
175                 \r
176 \r
177 \r