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