Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-sdk-app / src / test / java / org / openecomp / portalsdk / core / MockApplicationContextTestSuite.java
1 package org.openecomp.portalsdk.core;
2
3 import java.io.IOException;
4
5 import org.junit.Before;
6 import org.junit.runner.RunWith;
7 import org.openecomp.portalsdk.core.conf.AppConfig;
8 import org.openecomp.portalsdk.core.objectcache.AbstractCacheManager;
9 import org.openecomp.portalsdk.core.util.CacheManager;
10 import org.openecomp.portalsdk.core.util.SystemProperties;
11 import org.springframework.beans.factory.annotation.Autowired;
12 import org.springframework.context.annotation.Bean;
13 import org.springframework.context.annotation.ComponentScan;
14 import org.springframework.context.annotation.Configuration;
15 import org.springframework.context.annotation.Profile;
16 import org.springframework.test.context.ActiveProfiles;
17 import org.springframework.test.context.ContextConfiguration;
18 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
19 import org.springframework.test.context.web.AnnotationConfigWebContextLoader;
20 import org.springframework.test.context.web.WebAppConfiguration;
21 import org.springframework.test.web.servlet.MockMvc;
22 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
23 import org.springframework.web.context.WebApplicationContext;
24 import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
25
26 /**
27  * 
28  * 
29  * 
30  * In order to write a unit test, 
31  * 1. inherit this class - See SanityTest.java
32  * 2. place the "war" folder on your test class's classpath
33  * 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
34  *    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  
35  *    -Dcontainer.classpath="classpath:"
36  *
37  */
38
39 @RunWith(SpringJUnit4ClassRunner.class)
40 @WebAppConfiguration
41 @ContextConfiguration(loader = AnnotationConfigWebContextLoader.class, classes = {MockAppConfig.class})
42 @ActiveProfiles(value="test")
43 public class MockApplicationContextTestSuite {
44                 
45             @Autowired
46             public WebApplicationContext wac;
47
48             private MockMvc mockMvc;
49
50             @Before
51             public void setup() {
52                 if(mockMvc == null) {
53                         this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
54                         
55                 }
56             }
57             
58             public Object getBean(String name) {
59                         return this.wac.getBean(name);
60                 }
61
62
63                 public MockMvc getMockMvc() {
64                         return mockMvc;
65                 }
66
67                 public void setMockMvc(MockMvc mockMvc) {
68                         this.mockMvc = mockMvc;
69                 }
70                 
71                 public WebApplicationContext getWebApplicationContext() {
72                         return wac;
73                 }
74                 
75                 
76                 
77                 
78 }
79                 
80
81                 @Configuration
82                 @ComponentScan(basePackages = "org.openecomp", 
83                                  excludeFilters = {
84                                                                         
85                                                                   }
86                         )
87                 @Profile("test")
88                 class MockAppConfig extends AppConfig {
89                         
90                         @Bean 
91                     public SystemProperties systemProperties(){
92                         return new MockSystemProperties();
93                     }
94                         
95                         @Bean
96                     public AbstractCacheManager cacheManager() {
97                         return new CacheManager() {
98                                 
99                                 public void configure() throws IOException {
100                                          
101                                 }
102                         };
103                     }
104                         
105                         protected String[] tileDefinitions() {
106                                 return new String[] {"classpath:/WEB-INF/fusion/defs/definitions.xml", "classpath:/WEB-INF/defs/definitions.xml"};
107                         }
108                         
109                          @Override
110                         public void addInterceptors(InterceptorRegistry registry) {
111                             //registry.addInterceptor(new SessionTimeoutInterceptor()).excludePathPatterns(getExcludeUrlPathsForSessionTimeout());
112                             //registry.addInterceptor(resourceInterceptor());
113                         }
114                          
115                          public static class MockSystemProperties extends SystemProperties {
116                                         
117                                         public MockSystemProperties() {
118                                         }
119                                         
120                                 }
121                                         
122                 }
123                 
124                 
125
126