a7271f526bd00d67a9f2da23a75f3697f12017d3
[ui/dmaapbc.git] /
1 package org.onap.fusion.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  * In order to write a unit test, 1. inherit this class - See SanityTest.java 2.
28  * place the "war" folder on your test class's classpath 3. run the test with
29  * the following VM argument; This is important because when starting the
30  * application from Container, the System Properties file
31  * (SystemProperties.java) can have the direct path but, when running from the
32  * Mock Junit container, the path should be prefixed with "classpath" to enable
33  * the mock container to search for the file in the classpath
34  * -Dcontainer.classpath="classpath:"
35  *
36  */
37
38 @RunWith(SpringJUnit4ClassRunner.class)
39 @WebAppConfiguration
40 @ContextConfiguration(loader = AnnotationConfigWebContextLoader.class, classes = { MockAppConfig.class })
41 @ActiveProfiles(value = "test")
42 public class MockApplicationContextTestSuite {
43
44         @Autowired
45         public WebApplicationContext wac;
46
47         private MockMvc mockMvc;
48
49         @Before
50         public void setup() {
51                 if (mockMvc == null) {
52                         this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
53
54                 }
55         }
56
57         public Object getBean(String name) {
58                 return this.wac.getBean(name);
59         }
60
61         public MockMvc getMockMvc() {
62                 return mockMvc;
63         }
64
65         public void setMockMvc(MockMvc mockMvc) {
66                 this.mockMvc = mockMvc;
67         }
68
69         public WebApplicationContext getWebApplicationContext() {
70                 return wac;
71         }
72
73 }
74
75 @Configuration
76 @ComponentScan(basePackages = "org.onap", excludeFilters = {
77                 // see AppConfig class
78 })
79 @Profile("test")
80 class MockAppConfig extends AppConfig {
81
82         @Bean
83         public SystemProperties systemProperties() {
84                 return new MockSystemProperties();
85         }
86
87         @Bean
88         public AbstractCacheManager cacheManager() {
89                 return new CacheManager() {
90
91                         public void configure() throws IOException {
92
93                         }
94                 };
95         }
96
97         protected String[] tileDefinitions() {
98                 return new String[] { "classpath:/WEB-INF/fusion/defs/definitions.xml",
99                                 "classpath:/WEB-INF/defs/definitions.xml" };
100         }
101
102         @Override
103         public void addInterceptors(InterceptorRegistry registry) {
104                 // registry.addInterceptor(new
105                 // SessionTimeoutInterceptor()).excludePathPatterns(getExcludeUrlPathsForSessionTimeout());
106                 // registry.addInterceptor(resourceInterceptor());
107         }
108
109         public static class MockSystemProperties extends SystemProperties {
110
111                 public MockSystemProperties() {
112                 }
113
114         }
115
116 }