1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.module.googlesitemap;
35
36 import static org.mockito.Mockito.mock;
37 import static org.mockito.Mockito.when;
38 import info.magnolia.cms.beans.config.ServerConfiguration;
39 import info.magnolia.cms.core.MetaData;
40 import info.magnolia.cms.core.MgnlNodeType;
41 import info.magnolia.cms.i18n.DefaultI18nContentSupport;
42 import info.magnolia.cms.i18n.I18nContentSupport;
43 import info.magnolia.cms.security.MgnlUser;
44 import info.magnolia.cms.security.User;
45 import info.magnolia.context.MgnlContext;
46 import info.magnolia.jcr.util.MetaDataUtil;
47 import info.magnolia.module.googlesitemap.bean.SiteMapEntry;
48 import info.magnolia.module.googlesitemap.service.SiteMapService;
49 import info.magnolia.module.googlesitemap.service.query.QueryUtil;
50 import info.magnolia.module.templatingkit.sites.Site;
51 import info.magnolia.module.templatingkit.sites.SiteManager;
52 import info.magnolia.test.ComponentsTestUtil;
53 import info.magnolia.test.mock.MockWebContext;
54 import info.magnolia.test.mock.jcr.MockNode;
55 import info.magnolia.test.mock.jcr.MockSession;
56
57 import java.util.ArrayList;
58 import java.util.Collections;
59 import java.util.HashMap;
60 import java.util.Iterator;
61 import java.util.List;
62
63 import javax.jcr.Node;
64 import javax.jcr.RepositoryException;
65
66 import org.junit.After;
67 import org.junit.Before;
68 import org.mockito.Answers;
69
70
71
72
73
74
75
76 public class SiteMapTestUtil {
77
78 protected static final String CONTEXT_PATH = "/manual_set_context_path";
79 protected static final String DEFAULT_URL = "http://localhost:8080";
80 protected static final String ROOT_PAGE = "rootPage";
81 protected static final String PAGE_DEEP_1_1 = "page_1_1";
82 protected static final String PAGE_DEEP_1_1_1 = "page_1_1_1";
83 protected static final String PAGE_DEEP_1_2 = "page_1_2";
84 protected static final String PAGE_DEEP_1_2_1 = "page_1_2_1";
85 protected static final String VIRTUALURI_DEEP_2_1 = "virtualUri_2_1";
86 protected static final String VIRTUALURI_DEEP_2_2 = "virtualUri_2_2";
87
88 protected MockNode rootWebsite;
89 protected Node rootPageWebsite;
90 protected Node page_1_1;
91 protected Node page_1_1_1;
92 protected Node page_1_2;
93 protected Node page_1_2_1;
94 protected MockNode rootConfig;
95 protected Node rootPageConfig;
96 protected Node virtualURIMapping;
97 protected Node virtualUri_2_1;
98 protected Node virtualUri_2_2;
99
100
101 protected MockWebContext ctx ;
102 protected SiteMapService service;
103
104 @Before
105 public void setUp() throws RepositoryException{
106
107 ctx = new MockWebContext();
108 ctx.setContextPath(CONTEXT_PATH);
109 User user = new MgnlUser("userName", "userRealName", new ArrayList<String>() , new ArrayList<String>(), new HashMap<String, String>());
110 ctx.setUser(user);
111 MgnlContext.setInstance(ctx);
112
113
114 ServerConfiguration sc = new ServerConfiguration();
115 sc.setDefaultBaseUrl(DEFAULT_URL);
116 ComponentsTestUtil.setInstance(ServerConfiguration.class, sc);
117
118
119 MockSession sessionWebsite = new MockSession("website");
120 ctx.addSession("website", sessionWebsite);
121 rootWebsite = (MockNode) sessionWebsite.getRootNode();
122 rootPageWebsite = rootWebsite.addNode(ROOT_PAGE, MgnlNodeType.NT_CONTENT);
123 page_1_1 = rootPageWebsite.addNode(PAGE_DEEP_1_1, MgnlNodeType.NT_CONTENT);
124 page_1_1_1 = page_1_1.addNode(PAGE_DEEP_1_1_1, MgnlNodeType.NT_CONTENT);
125 page_1_2 = rootPageWebsite.addNode(PAGE_DEEP_1_2, MgnlNodeType.NT_CONTENT);
126 page_1_2_1 = page_1_2.addNode(PAGE_DEEP_1_2_1, MgnlNodeType.NT_FILE);
127
128
129 MockSession sessionConfig = new MockSession("config");
130 ctx.addSession("config", sessionConfig);
131 rootConfig = (MockNode) sessionConfig.getRootNode();
132 rootPageConfig = rootConfig.addNode(ROOT_PAGE, MgnlNodeType.NT_CONTENT);
133 virtualURIMapping = rootPageConfig.addNode("virtualURIMapping", MgnlNodeType.NT_CONTENT);
134 virtualUri_2_1 = virtualURIMapping.addNode(VIRTUALURI_DEEP_2_1, MgnlNodeType.NT_CONTENTNODE);
135 virtualUri_2_2 = virtualURIMapping.addNode(VIRTUALURI_DEEP_2_2, MgnlNodeType.NT_CONTENTNODE);
136 }
137
138
139
140 @After
141 public void tearDown() {
142 ComponentsTestUtil.clear();
143 MgnlContext.setInstance(null);
144 service = null;
145 }
146
147
148
149
150
151
152 public void initService() throws RepositoryException {
153 QueryUtil queryUtil = mock(QueryUtil.class, Answers.RETURNS_SMART_NULLS.toString());
154 String xpath = "//virtualURIMapping//element(*,"+MgnlNodeType.NT_CONTENTNODE+")";
155 when(queryUtil.query("config", xpath, "xpath", MgnlNodeType.NT_CONTENTNODE)).thenReturn(virtualURIMapping.getNodes());
156 GoogleSiteMapConfiguration configuration = new GoogleSiteMapConfiguration();
157
158 SiteManager siteManager = mock(SiteManager.class, Answers.RETURNS_SMART_NULLS.toString());
159 Site site = new Site();
160 site.setEnabled(true);
161 when(siteManager.getAssignedSite(page_1_1)).thenReturn(site);
162 when(siteManager.getAssignedSite(page_1_2)).thenReturn(site);
163 when(siteManager.getAssignedSite(page_1_1_1)).thenReturn(site);
164 when(siteManager.getAssignedSite(page_1_2_1)).thenReturn(site);
165
166 ComponentsTestUtil.setInstance(I18nContentSupport.class, new DefaultI18nContentSupport());
167
168 service = new SiteMapService(siteManager, configuration, queryUtil);
169 }
170
171
172
173
174
175 public void setMetaData(Node node, String templateName) throws RepositoryException {
176 node.addNode(MetaData.DEFAULT_META_NODE, MgnlNodeType.NT_METADATA);
177 MetaData metaData = MetaDataUtil.getMetaData(node);
178 metaData.setCreationDate();
179 metaData.setModificationDate();
180 if(templateName != null) {
181 metaData.setTemplate(templateName);
182 }
183 }
184
185
186 public List<SiteMapEntry> fromIteratorToList(Iterator< SiteMapEntry> iterator) {
187 List<SiteMapEntry> res = new ArrayList<SiteMapEntry>();
188 while(iterator.hasNext()) {
189 res.add(iterator.next());
190 }
191 Collections.sort(res);
192 return res;
193 }
194
195 public Node initSiteComponentNode(Node parent, String nodeName) throws RepositoryException {
196 Node res = parent.addNode(nodeName, MgnlNodeType.NT_COMPONENT);
197 setMetaData(res, "google-sitemap:components/content/siteComponent");
198 return res;
199 }
200
201 public Node initVirtualUriComponentNode(Node parent, String nodeName) throws RepositoryException {
202 Node res = parent.addNode(nodeName, MgnlNodeType.NT_COMPONENT);
203 setMetaData(res, "google-sitemap:components/content/virtualUriComponent");
204 return res;
205 }
206
207 }