2 * ================================================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property
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 * ================================================================================
20 package org.openecomp.portalapp.test.framework;
22 import java.io.IOException;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.openecomp.portalsdk.core.conf.AppConfig;
28 //import org.openecomp.portalapp.conf.ExternalAppConfig;
29 //import org.openecomp.portalapp.conf.HibernateMappingLocations;
30 import org.openecomp.portalsdk.core.conf.HibernateConfiguration;
31 import org.openecomp.portalsdk.core.conf.HibernateMappingLocatable;
32 import org.openecomp.portalsdk.core.objectcache.AbstractCacheManager;
33 import org.openecomp.portalsdk.core.util.CacheManager;
34 import org.openecomp.portalsdk.core.util.SystemProperties;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.context.annotation.Bean;
37 import org.springframework.context.annotation.ComponentScan;
38 import org.springframework.context.annotation.Configuration;
39 import org.springframework.context.annotation.FilterType;
40 import org.springframework.context.annotation.Profile;
41 import org.springframework.core.io.ClassPathResource;
42 import org.springframework.core.io.Resource;
43 import org.springframework.test.context.ActiveProfiles;
44 import org.springframework.test.context.ContextConfiguration;
45 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
46 import org.springframework.test.context.transaction.TransactionConfiguration;
47 import org.springframework.test.context.web.AnnotationConfigWebContextLoader;
48 import org.springframework.test.context.web.WebAppConfiguration;
49 import org.springframework.test.web.servlet.MockMvc;
50 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
51 import org.springframework.transaction.annotation.Transactional;
52 import org.springframework.web.context.WebApplicationContext;
53 import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
57 * In order to write a unit test, 1. inherit this class 2. place the "war"
58 * folder on your test class's classpath 3. run the test with the following VM
59 * argument; This is important because when starting the application from
60 * Container, the System Properties file (SystemProperties.java) can have the
61 * direct path but, when running from the Mock Junit container, the path should
62 * be prefixed with "classpath" to enable the mock container to search for the
63 * file in the classpath -Dcontainer.classpath="classpath:"
66 @SuppressWarnings("deprecation")
67 @RunWith(SpringJUnit4ClassRunner.class)
69 @ContextConfiguration(loader = AnnotationConfigWebContextLoader.class, classes = { MockAppConfig.class })
70 @ActiveProfiles(value = "test")
71 public class ApplicationCommonContextTestSuite {
74 public WebApplicationContext wac;
76 private MockMvc mockMvc;
80 if (mockMvc == null) {
81 this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
86 public Object getBean(String name) {
87 return this.wac.getBean(name);
90 public MockMvc getMockMvc() {
94 public void setMockMvc(MockMvc mockMvc) {
95 this.mockMvc = mockMvc;
98 public WebApplicationContext getWebApplicationContext() {
105 @ComponentScan(basePackages = "org.openecomp", excludeFilters = {
106 // the following source configurations should not be scanned; instead of
107 // using Exclusion filter, we can use the @Profile annotation to exclude
109 @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.openecomp.portalsdk.core.controller.LogoutController*"),
110 @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.openecomp.portalsdk.core.controller.SDKLoginController*"),
111 @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.openecomp.portalapp.conf.ExternalAppConfig*"),
112 @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.openecomp.*.*InitUebHandler*") })
114 class MockAppConfig extends AppConfig {
117 HibernateMappingLocatable locatable() {
118 return new MockHibernateMappingLocations();
122 HibernateConfiguration hibConfiguration() {
123 return new HibernateConfiguration();
127 public SystemProperties systemProperties() {
128 return new MockSystemProperties();
132 public AbstractCacheManager cacheManager() {
133 return new CacheManager() {
135 public void configure() throws IOException {
141 public String[] tileDefinitions() {
142 return new String[] { "classpath:/WEB-INF/fusion/defs/definitions.xml",
143 "classpath:/WEB-INF/defs/definitions.xml" };
146 public void addInterceptors(InterceptorRegistry registry) {
147 // registry.addInterceptor(new
148 // SessionTimeoutInterceptor()).excludePathPatterns(getExcludeUrlPathsForSessionTimeout());
149 // registry.addInterceptor(resourceInterceptor());
152 public static class MockSystemProperties extends SystemProperties {
154 public MockSystemProperties() {
162 class MockHibernateMappingLocations implements HibernateMappingLocatable {
164 public Resource[] getMappingLocations() {
165 return new Resource[] { new ClassPathResource("WEB-INF/fusion/orm/Fusion.hbm.xml"),
166 new ClassPathResource("WEB-INF/fusion/orm/EP.hbm.xml"),
167 new ClassPathResource("WEB-INF/fusion/orm/Workflow.hbm.xml") };
171 public String[] getPackagesToScan() {
172 return new String[] { "org.openecomp", "src" };