2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.fusion.core;
23 import java.io.IOException;
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.onap.portalsdk.core.conf.AppConfig;
43 import org.onap.portalsdk.core.objectcache.AbstractCacheManager;
44 import org.onap.portalsdk.core.util.SystemProperties;
45 import org.onap.portalsdk.core.util.CacheManager;
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:"
60 @RunWith(SpringJUnit4ClassRunner.class)
62 @ContextConfiguration(loader = AnnotationConfigWebContextLoader.class, classes = {MockAppConfig.class})
63 @ActiveProfiles(value="test")
65 public class MockApplicationContextTestSuite {
69 public WebApplicationContext wac;
72 private MockMvc mockMvc;
80 this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
88 * @param name the name
91 public Object getBean(String name) {
92 return this.wac.getBean(name);
99 * @return the mock mvc
101 public MockMvc getMockMvc() {
108 * @param mockMvc the new mock mvc
110 public void setMockMvc(MockMvc mockMvc) {
111 this.mockMvc = mockMvc;
115 * Gets the web application context.
117 * @return the web application context
119 public WebApplicationContext getWebApplicationContext() {
130 @ComponentScan(basePackages = "org.onap",
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*")
139 class MockAppConfig extends AppConfig {
142 public SystemProperties systemProperties(){
143 return new MockSystemProperties();
147 public AbstractCacheManager cacheManager() {
148 return new CacheManager() {
150 public void configure() throws IOException {
156 protected String[] tileDefinitions() {
157 return new String[] {"classpath:/WEB-INF/fusion/defs/definitions.xml", "classpath:/WEB-INF/defs/definitions.xml"};
161 public void addInterceptors(InterceptorRegistry registry) {
162 //registry.addInterceptor(new SessionTimeoutInterceptor()).excludePathPatterns(getExcludeUrlPathsForSessionTimeout());
163 //registry.addInterceptor(resourceInterceptor());
166 public static class MockSystemProperties extends SystemProperties {
168 public MockSystemProperties() {