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