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