View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.service.impl;
17  
18  
19  import org.junit.Assert;
20  import org.junit.Before;
21  import org.junit.Test;
22  import org.kuali.rice.kew.api.KewApiConstants;
23  import org.kuali.rice.kew.api.WorkflowDocument;
24  import org.kuali.rice.kew.impl.document.WorkflowDocumentImpl;
25  import org.kuali.rice.krad.UserSession;
26  import org.mockito.Mockito;
27  
28  
29  import java.util.concurrent.*;
30  
31  
32  /**
33   * Copyright 2005-2012 The Kuali Foundation
34   *
35   * Licensed under the Educational Community License, Version 2.0 (the "License");
36   * you may not use this file except in compliance with the License.
37   * You may obtain a copy of the License at
38   *
39   * http://www.opensource.org/licenses/ecl2.php
40   *
41   * Unless required by applicable law or agreed to in writing, software
42   * distributed under the License is distributed on an "AS IS" BASIS,
43   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
44   * See the License for the specific language governing permissions and
45   * limitations under the License.
46   */
47  public class SessionDocumentServiceImplTest {
48  
49      // Generate sample objects for mocks and testing
50      private SessionDocumentServiceImpl sessionDocumentServiceImpl = new SessionDocumentServiceImpl();
51      private UserSession session;
52      private WorkflowDocument doc;
53      private String docId = "1";
54      private ConcurrentHashMap<String, WorkflowDocument> map = new ConcurrentHashMap<String, WorkflowDocument>();
55  
56      @Before
57      public void setUp() {
58          session = Mockito.mock(UserSession.class);
59          doc = Mockito.mock(WorkflowDocumentImpl.class);
60          Mockito.when(doc.getDocumentId()).thenReturn(docId);
61          map.put(docId,doc);
62      }
63  
64      @Test
65      public void testAddDocumentToUserSession() {
66          try {
67              sessionDocumentServiceImpl.addDocumentToUserSession(session, doc);
68          } catch(Exception e) {
69              Assert.fail("Exception occurred adding document to user session");
70          }
71      }
72      @Test
73      public void testGetDocumentFromSessionWithEntry() {
74          try {
75              Mockito.when(session.retrieveObject(KewApiConstants.WORKFLOW_DOCUMENT_MAP_ATTR_NAME)).thenReturn(map);
76              WorkflowDocument returnedDoc = sessionDocumentServiceImpl.getDocumentFromSession(session,docId);
77              Assert.assertEquals("Document should match mock document", doc, returnedDoc);
78          } catch(Exception e) {
79              Assert.fail("Exception occurred retrieving document to user session");
80          }
81      }
82  
83      @Test
84      public void testGetDocumentFromSessionWithNoEntry() {
85          try {
86              Mockito.when(session.retrieveObject(KewApiConstants.WORKFLOW_DOCUMENT_MAP_ATTR_NAME)).thenReturn(null);
87              WorkflowDocument returnedDoc = sessionDocumentServiceImpl.getDocumentFromSession(session,docId);
88              Assert.assertNull("Document should have returned null", returnedDoc);
89          } catch(Exception e) {
90              Assert.fail("Exception occurred retrieving document to user session");
91          }
92      }
93  
94  }
95