1   package com.germinus.merlin.mock;
2   
3   import javax.portlet.RenderRequest;
4   import javax.portlet.RenderResponse;
5   
6   import org.easymock.EasyMock;
7   import org.springframework.mock.web.MockHttpServletRequest;
8   import org.springframework.mock.web.MockHttpServletResponse;
9   import org.springframework.mock.web.MockServletContext;
10  import org.springframework.mock.web.portlet.MockPortletSession;
11  import org.springframework.mock.web.portlet.MockRenderRequest;
12  
13  import com.germinus.merlin.manager.layout.ILayoutManager;
14  import com.germinus.merlin.manager.layout.ILayoutManagerFactory;
15  import com.germinus.merlin.page.PagePortletConfiguration;
16  import com.germinus.merlin.util.IMerlinUtil;
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.SystemException;
19  
20  public class MockMerlinFactory extends MockLiferayFactory implements IMockMerlinFactory {
21  
22  	private PagePortletConfiguration blogPageConfiguration;
23  	private PagePortletConfiguration documentPageConfiguration;
24  	private PagePortletConfiguration forumPageConfiguration;
25  	
26  	public MockRenderRequest getRenderRequest() {
27  		
28  		MockRenderRequest request = super.getRenderRequest();
29  		
30  		request.setParameter("folderId", 
31  				new Long(MockMerlinKeys.DEFAULT_FOLDER_ID).toString());
32  		request.setParameter("folderId", 
33  				new Long(MockMerlinKeys.DEFAULT_PARENT_FOLDER_ID).toString());
34  		
35  		 MockRenderRequest renderRequest = new MockRenderRequest();
36  		 renderRequest.addParameter("viewDetails", "true");
37  		 renderRequest.setSession(new MockPortletSession(null));
38  		 renderRequest.setContextPath("assignments/");		
39  		return renderRequest;
40  	}
41  	
42  	public MockMerlinFactory(){
43  		
44  	}
45  	
46  	private ILayoutManager buildLayoutManager(PagePortletConfiguration pagePortletConfiguration, long pageId)
47  	{
48  		ILayoutManager layoutManager =
49  			EasyMock.createNiceMock(ILayoutManager.class);
50  		
51  		try {
52  			EasyMock.expect(layoutManager.addPage(
53  					getRenderRequest(), getRenderResponse(), pagePortletConfiguration))
54  				.andStubReturn(pageId);
55  			EasyMock.expect(layoutManager.addSubPage(
56  					getRenderRequest(), getRenderResponse(), pagePortletConfiguration))
57  				.andStubReturn(pageId);
58  			EasyMock.expect(layoutManager.addPortlet(getRenderRequest(), 
59  				getRenderResponse(), MockMerlinKeys.DEFAULT_PORTLET_ID))
60  				.andStubReturn(MockMerlinKeys.DEFAULT_PORTLET_ID);
61  			EasyMock.expect(layoutManager.deletePage(MockMerlinKeys.DEFAULT_LAYOUT_ID))
62  				.andStubReturn(pageId);
63  			EasyMock.expect(layoutManager.getPageProperties())
64  				.andStubReturn(pagePortletConfiguration);
65  			
66  			//mock implemementation
67  			EasyMock.replay(layoutManager);
68  		} catch (Exception e) {
69  			e.printStackTrace();
70  		}
71  		return layoutManager;
72  	}
73  	
74  	/* (non-Javadoc)
75  	 * @see com.germinus.merlin.mock.IMockMerlinFactory#getBlogPageConfiguration()
76  	 */
77  	public PagePortletConfiguration getBlogPageConfiguration() {
78  		return blogPageConfiguration;
79  	}
80  	
81  	/* (non-Javadoc)
82  	 * @see com.germinus.merlin.mock.IMockMerlinFactory#getDocumentPageConfiguration()
83  	 */
84  	public PagePortletConfiguration getDocumentPageConfiguration() {
85  		return documentPageConfiguration;
86  	}
87  	
88  	/* (non-Javadoc)
89  	 * @see com.germinus.merlin.mock.IMockMerlinFactory#getForumPageConfiguration()
90  	 */
91  	public PagePortletConfiguration getForumPageConfiguration() {
92  		return forumPageConfiguration;
93  	}
94  	
95  	/* (non-Javadoc)
96  	 * @see com.germinus.merlin.mock.IMockMerlinFactory#getLayoutManager(int)
97  	 */
98  	public ILayoutManager getLayoutManager(int type)
99  	{
100 		if(type == ILayoutManagerFactory.BLOG){
101 			return buildLayoutManager(blogPageConfiguration, MockMerlinKeys.BLOG_PAGE_ID);
102 			
103 		}else if (type == ILayoutManagerFactory.DOCUMENT){
104 			return buildLayoutManager(documentPageConfiguration, MockMerlinKeys.DOCUMENT_PAGE_ID);
105 		}else if (type == ILayoutManagerFactory.FORUM) {
106 			return buildLayoutManager(documentPageConfiguration, MockMerlinKeys.DOCUMENT_PAGE_ID);
107 		}else{
108 			return null;
109 		}
110 	}
111 	
112 	/* (non-Javadoc)
113 	 * @see com.germinus.merlin.mock.IMockMerlinFactory#getLayoutManagerFactory()
114 	 */
115 	public ILayoutManagerFactory getLayoutManagerFactory(){
116 		
117 		ILayoutManagerFactory layoutManagerFactory =  
118 				EasyMock.createNiceMock(ILayoutManagerFactory.class);
119 			
120 		EasyMock.expect(layoutManagerFactory.getLayoutManager(
121 				ILayoutManagerFactory.DOCUMENT))
122 			.andStubReturn(getLayoutManager(ILayoutManagerFactory.DOCUMENT));
123 		
124 		EasyMock.expect(layoutManagerFactory.getLayoutManager(
125 				ILayoutManagerFactory.FORUM))
126 			.andStubReturn(getLayoutManager(ILayoutManagerFactory.FORUM));
127 		
128 		EasyMock.expect(layoutManagerFactory.getLayoutManager(
129 				ILayoutManagerFactory.BLOG))
130 			.andStubReturn(getLayoutManager(ILayoutManagerFactory.BLOG));
131 		
132 		//mock implemementation
133 		EasyMock.replay(layoutManagerFactory);
134 		
135 		return layoutManagerFactory;
136 	}
137 	
138 	/* (non-Javadoc)
139 	 * @see com.germinus.merlin.mock.IMockMerlinFactory#getMerlinUtil()
140 	 */
141 	public IMerlinUtil getMerlinUtil() throws PortalException, SystemException
142 	{
143 		IMerlinUtil merlinUtil = EasyMock.createNiceMock(IMerlinUtil.class);
144 		MockServletContext mockServletContext = new MockServletContext();
145 		
146 		EasyMock.expect(merlinUtil.getServletContext((RenderRequest) EasyMock.anyObject(), (RenderResponse) EasyMock.anyObject()))
147 					.andStubReturn(mockServletContext);
148 		
149 		EasyMock.expect(merlinUtil.getServletContext((RenderRequest) EasyMock.anyObject(), (RenderResponse) EasyMock.anyObject(), 
150 					(String)EasyMock.anyObject())).andStubReturn(mockServletContext);
151 		
152 		
153 	
154 		EasyMock.expect(merlinUtil.getHttpServletRequest((RenderRequest) EasyMock.anyObject(), 
155 										(RenderResponse) EasyMock.anyObject()))
156 				.andStubReturn(new MockHttpServletRequest());
157 	
158 		EasyMock.expect(merlinUtil.getHttpServletResponse((RenderRequest) EasyMock.anyObject(), 
159 										(RenderResponse) EasyMock.anyObject()))
160 					.andStubReturn(new MockHttpServletResponse());
161 	
162 	
163 		EasyMock.expect(merlinUtil.getUserType((RenderRequest) EasyMock.anyObject(), 
164 										(RenderResponse) EasyMock.anyObject(), EasyMock.anyLong()))
165 					.andStubReturn(MockMerlinKeys.TEACHER);
166 		
167 		//mock implemementation
168 		EasyMock.replay(merlinUtil);
169 
170 		return merlinUtil;
171 	}
172 
173 	/* (non-Javadoc)
174 	 * @see com.germinus.merlin.mock.IMockMerlinFactory#setBlogPageConfiguration(com.germinus.merlin.page.PagePortletConfiguration)
175 	 */
176 	public void setBlogPageConfiguration(
177 			PagePortletConfiguration blogPageConfiguration) {
178 		this.blogPageConfiguration = blogPageConfiguration;
179 	}
180 	
181 	/* (non-Javadoc)
182 	 * @see com.germinus.merlin.mock.IMockMerlinFactory#setDocumentPageConfiguration(com.germinus.merlin.page.PagePortletConfiguration)
183 	 */
184 	public void setDocumentPageConfiguration(
185 			PagePortletConfiguration documentPageConfiguration) {
186 		this.documentPageConfiguration = documentPageConfiguration;
187 	}
188 	
189 	/* (non-Javadoc)
190 	 * @see com.germinus.merlin.mock.IMockMerlinFactory#setForumPageConfiguration(com.germinus.merlin.page.PagePortletConfiguration)
191 	 */
192 	public void setForumPageConfiguration(
193 			PagePortletConfiguration forumPageConfiguration) {
194 		this.forumPageConfiguration = forumPageConfiguration;
195 	}
196 
197 	/*public void setMerlinUtil(IMerlinUtil merlinUtil) {
198 		this.merlinUtil = merlinUtil;
199 	}*/
200 
201 }