View Javadoc

1   package org.cateproject.view.json;
2   
3   import java.io.Serializable;
4   import java.lang.reflect.Method;
5   import java.util.ArrayList;
6   import java.util.HashMap;
7   import java.util.List;
8   import java.util.Map;
9   import java.util.UUID;
10  
11  import junit.framework.Assert;
12  import net.sf.cglib.proxy.Enhancer;
13  import net.sf.cglib.proxy.MethodInterceptor;
14  import net.sf.cglib.proxy.MethodProxy;
15  import net.sf.json.JSONObject;
16  import net.sf.json.JSONSerializer;
17  import net.sf.json.JsonConfig;
18  import net.sf.json.util.CycleDetectionStrategy;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.cateproject.view.json.processor.AnnotationBeanProcessor;
23  import org.cateproject.view.json.processor.CGLibEnhancedBeanProcessorMatcher;
24  import org.cateproject.view.json.processor.DateTimeJSONValueProcessor;
25  import org.cateproject.view.json.processor.DocSumTypeJSONBeanProcessor;
26  import org.cateproject.view.json.processor.InitializedHibernatePropertyFilter;
27  import org.cateproject.view.json.processor.LSIDJSONValueProcessor;
28  import org.cateproject.view.json.processor.MediaJSONBeanProcessor;
29  import org.cateproject.view.json.processor.TermBaseBeanProcessor;
30  import org.cateproject.view.json.processor.UUIDJSONValueProcessor;
31  import org.cateproject.view.json.processor.UserJSONBeanProcessor;
32  import org.hibernate.HibernateException;
33  import org.hibernate.LazyInitializationException;
34  import org.hibernate.collection.PersistentSet;
35  import org.hibernate.engine.SessionImplementor;
36  import org.hibernate.proxy.LazyInitializer;
37  import org.joda.time.DateTime;
38  import org.junit.Before;
39  import org.junit.Test;
40  import org.springframework.mock.web.MockHttpServletRequest;
41  import org.springframework.mock.web.MockHttpServletResponse;
42  import org.unitils.UnitilsJUnit4;
43  
44  import eu.etaxonomy.cdm.api.service.pager.Pager;
45  import eu.etaxonomy.cdm.api.service.pager.impl.DefaultPagerImpl;
46  import eu.etaxonomy.cdm.model.common.Annotation;
47  import eu.etaxonomy.cdm.model.common.LSID;
48  import eu.etaxonomy.cdm.model.common.User;
49  import eu.etaxonomy.cdm.model.location.NamedArea;
50  import eu.etaxonomy.cdm.model.media.Media;
51  import eu.etaxonomy.cdm.model.taxon.Taxon;
52  import gov.nih.nlm.ncbi.www.soap.eutils.esummary.DocSumType;
53  
54  public class JSONViewTest extends UnitilsJUnit4 {
55  	public class MockLazyInitializer implements LazyInitializer {
56  
57  		
58  		public String getEntityName() {
59  			// TODO Auto-generated method stub
60  			return null;
61  		}
62  
63  		
64  		public Serializable getIdentifier() {
65  			// TODO Auto-generated method stub
66  			return null;
67  		}
68  
69  		
70  		public Object getImplementation() {
71  			// TODO Auto-generated method stub
72  			return null;
73  		}
74  
75  		
76  		public Object getImplementation(SessionImplementor arg0)
77  				throws HibernateException {
78  			// TODO Auto-generated method stub
79  			return null;
80  		}
81  
82  	
83  		public Class getPersistentClass() {
84  			// TODO Auto-generated method stub
85  			return null;
86  		}
87  
88  		
89  		public SessionImplementor getSession() {
90  			// TODO Auto-generated method stub
91  			return null;
92  		}
93  
94  		
95  		public void initialize() throws HibernateException {
96  			// TODO Auto-generated method stub
97  			
98  		}
99  
100 	
101 		public boolean isUninitialized() {
102 			return true;
103 		}
104 
105 		
106 		public boolean isUnwrap() {
107 			// TODO Auto-generated method stub
108 			return false;
109 		}
110 
111 	
112 		public void setIdentifier(Serializable arg0) {
113 			// TODO Auto-generated method stub
114 			
115 		}
116 
117 	
118 		public void setImplementation(Object arg0) {
119 			// TODO Auto-generated method stub
120 			
121 		}
122 
123 	
124 		public void setSession(SessionImplementor arg0)
125 				throws HibernateException {
126 			// TODO Auto-generated method stub
127 			
128 		}
129 
130 		
131 		public void setUnwrap(boolean arg0) {
132 			// TODO Auto-generated method stub
133 			
134 		}
135 
136 
137 		public boolean isReadOnly() {
138 			// TODO Auto-generated method stub
139 			return false;
140 		}
141 
142 
143 		public boolean isReadOnlySettingAvailable() {
144 			// TODO Auto-generated method stub
145 			return false;
146 		}
147 
148 
149 		public void setReadOnly(boolean arg0) {
150 			// TODO Auto-generated method stub
151 			
152 		}
153 
154 
155 		public void unsetSession() {
156 			// TODO Auto-generated method stub
157 			
158 		}
159 
160 	}
161 
162 	public class ThrowsLazyInitializationExceptionMethodIterceptor implements MethodInterceptor {
163 
164 		public Object intercept(Object obj, Method method, Object[] args,
165 				MethodProxy proxy) throws Throwable {
166 			if(method.getName().equals("getHibernateLazyInitializer")) {
167 				log.warn("Checking if this object has been initialized");
168 				return new MockLazyInitializer();
169 			}
170 			log.warn(obj.getClass() + " " + method.getName() + " throwing lazy initialization exception");
171 			throw new LazyInitializationException(null);
172 		}
173 
174 	}
175 
176 	private static Log log = LogFactory.getLog(JSONViewTest.class);
177 
178 	public class DefaultMethodInterceptor implements MethodInterceptor {
179 
180 		public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
181 			log.warn(obj.getClass() + " " + method.getName());
182 			return proxy.invokeSuper(obj, args);
183 		}
184 
185 	}
186 
187 	private JSONView jsonView;
188 	
189 	private MockHttpServletRequest request;
190 	private MockHttpServletResponse response;
191 	private Exception exception;
192 	private Pager<Annotation> annotations;
193 	private Annotation annotation;	
194 	
195 	@Before
196 	public void setUp() {
197 		jsonView = new JSONView();
198 		
199 		JsonConfig jsonConfig = new JsonConfig();
200 		jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
201 		jsonConfig.registerJsonBeanProcessor(User.class, new UserJSONBeanProcessor());
202 		jsonConfig.registerJsonBeanProcessor(NamedArea.class, new TermBaseBeanProcessor());
203 		jsonConfig.registerJsonBeanProcessor(DocSumType.class, new DocSumTypeJSONBeanProcessor());
204 		jsonConfig.registerJsonBeanProcessor(Annotation.class,new AnnotationBeanProcessor());
205 		jsonConfig.registerJsonBeanProcessor(Media.class, new MediaJSONBeanProcessor());
206 		jsonConfig.setJsonPropertyFilter(new InitializedHibernatePropertyFilter());
207 		jsonConfig.setJsonBeanProcessorMatcher(new CGLibEnhancedBeanProcessorMatcher());
208 		jsonConfig.registerJsonValueProcessor(LSID.class,new LSIDJSONValueProcessor());
209 		jsonConfig.registerJsonValueProcessor(UUID.class, new UUIDJSONValueProcessor());
210 		jsonConfig.registerJsonValueProcessor(DateTime.class, new DateTimeJSONValueProcessor());
211 		
212 		jsonView.setJsonConfig(jsonConfig);
213 		
214 		exception = new Exception("This is an exception"); 
215 		
216 		request = new MockHttpServletRequest();
217 		response = new MockHttpServletResponse();
218 		
219 		List<Annotation> annotationList = new ArrayList<Annotation>();
220 		
221 		Annotation annotation1 = Annotation.NewDefaultLanguageInstance(null);
222 		annotation1.setId(1l);
223 //		annotation1.setNumberOfAnnotations(0);
224 //		annotation1.setTitle("Sed a diam. Integer vitae mauris. Sed ac ligula.");
225 		Taxon taxon = (Taxon)Enhancer.create(Taxon.class, new DefaultMethodInterceptor());
226 		
227 		taxon.setId(1l);
228 		annotation1.setAnnotatedObj(taxon);
229 		annotation1.setCreated(new DateTime(2007,5,29,0,0,0,0));
230 		User ben = (User)Enhancer.create(User.class, new DefaultMethodInterceptor());
231 		ben.setName("ben");
232 		ben.setAccountNonExpired(true);
233 		ben.setEnabled(true);
234 		ben.setAccountNonLocked(true);
235 		ben.setCredentialsNonExpired(true);
236 		ben.setPassword("Secret Password");
237 //		ben.setRoles((List<Role>)new PersistentList());
238 		annotation1.setCreatedBy(ben);
239 		annotationList.add(annotation1);
240 		
241 		Annotation annotation2 = Annotation.NewDefaultLanguageInstance(null);
242 		annotation2.setId(2l);
243 //		annotation2.setNumberOfAnnotations(2);
244 //		annotation2.setTitle("Etiam neque nibh, tristique in, rutrum eget, rutrum pretium");
245 		annotation2.setCreated(new DateTime(2007,5,29,0,0,0,0));
246 		annotation2.setCreatedBy(ben);
247 		annotation2.setAnnotatedObj(taxon);
248 		annotationList.add(annotation2);
249 		
250 		Annotation annotation3 = Annotation.NewDefaultLanguageInstance(null);
251 		annotation3.setId(3l);
252 //		annotation3.setNumberOfAnnotations(0);
253 //		annotation3.setTitle("Lorem ipsum dolor sit amet, consectetuer adipiscing elit.");
254 		annotation3.setCreated(new DateTime(2007,5,29,0,0,0,0));
255 		annotation3.setCreatedBy(ben);
256 		annotation3.setAnnotatedObj(taxon);
257 		annotationList.add(annotation3);
258 		
259 		annotations = new DefaultPagerImpl<Annotation>(0,3,30,annotationList);
260 		
261 		annotation = Annotation.NewDefaultLanguageInstance(null);
262 		annotation.setId(3l);
263 //		annotation.setNumberOfAnnotations(0);
264 //		annotation.setTitle("Lorem ipsum dolor sit amet, consectetuer adipiscing elit.");
265 		annotation.setCreated(new DateTime(2007,5,29,0,0,0,0));
266 		annotation.setCreatedBy(ben);
267 		annotation.setAnnotatedObj(taxon);
268 		
269 		PersistentSet annotations = new PersistentSet();
270 //		annotation.setAnnotations(annotations);
271 	}
272 		
273 	@Test
274 	public void testException() throws Exception {
275 		Map map = new HashMap();
276 		map.put("exception",exception);
277 		
278 		jsonView.render(map, request, response);
279 		
280         JSONObject jsonObject = (JSONObject)JSONSerializer.toJSON(response.getContentAsString());
281         Assert.assertTrue(jsonObject.has("exception"));
282 	}
283 	
284 	@Test
285 	public void testAnnotation() throws Exception {
286 		Map map = new HashMap();
287 		map.put("result",annotations);
288 		
289 		jsonView.render(map, request, response);
290 		System.out.println(response.getContentAsString());
291 	}
292 	
293 	@Test
294 	public void testUnInitializedPersistentCollection()throws Exception {
295 		Map map = new HashMap();
296 		map.put("result",annotation);
297 		
298 		jsonView.render(map, request, response);
299 		System.out.println(response.getContentAsString());
300 	}
301 }