1835db5976499476bc173209c56b9755bd87b636
[controller.git] / opendaylight / samples / clustersession / src / test / java / org / opendaylight / controller / clustersession / ClusterSessionServiceImplTest.java
1 package org.opendaylight.controller.clustersession;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertNull;
6 import static org.mockito.Mockito.mock;
7 import static org.mockito.Mockito.when;
8
9 import java.util.HashMap;
10 import java.util.Map;
11 import java.util.concurrent.ConcurrentHashMap;
12 import java.util.concurrent.ConcurrentMap;
13
14 import org.apache.catalina.Context;
15 import org.apache.catalina.Session;
16 import org.apache.catalina.util.SessionIdGenerator;
17 import org.junit.AfterClass;
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.opendaylight.controller.clustering.services.IClusterGlobalServices;
22 import org.opendaylight.controller.clustersession.impl.ClusterSessionServiceImpl;
23 import org.osgi.framework.Bundle;
24 import org.osgi.framework.BundleContext;
25 import org.osgi.framework.FrameworkUtil;
26 import org.osgi.framework.ServiceReference;
27 import org.powermock.api.mockito.PowerMockito;
28 import org.powermock.core.classloader.annotations.PrepareForTest;
29 import org.powermock.modules.junit4.PowerMockRunner;
30
31 @RunWith(PowerMockRunner.class)
32 @PrepareForTest({FrameworkUtil.class})
33 public class ClusterSessionServiceImplTest {
34   static ClusterSessionManager manager = null;
35   static ClusterSessionServiceImpl sessionService = null;
36   private static final String SESSION_CACHE = "customSessionManager.sessionData";
37   static ConcurrentMap<String, ClusterSessionData> sessions = new ConcurrentHashMap<String, ClusterSessionData>();
38   private String sessionId = "1234567";
39   final String AUTH_TYPE = "FORM";
40   final String ATTRIBUTE_NAME = "AuthType";
41
42   @SuppressWarnings("unchecked")
43   @BeforeClass
44   public static void init(){
45     Bundle bundle = mock(Bundle.class);
46     BundleContext context = mock(BundleContext.class);
47     IClusterGlobalServices clusterGlobalService = mock(IClusterGlobalServices.class);
48     ServiceReference<IClusterGlobalServices> serviceReference = mock(ServiceReference.class);
49     PowerMockito.mockStatic(FrameworkUtil.class);
50     when(FrameworkUtil.getBundle(ClusterSessionManager.class)).thenReturn(bundle);
51     when(bundle.getBundleContext()).thenReturn(context);
52     when(context.getService(serviceReference)).thenReturn(clusterGlobalService);
53     when((ConcurrentMap<String, ClusterSessionData>)clusterGlobalService.getCache(SESSION_CACHE)).thenReturn(sessions);
54     Context containerContext = mock(Context.class);
55     manager = new ClusterSessionManager();
56     sessionService = (ClusterSessionServiceImpl) manager.getSessionService();
57     manager.setContainer(containerContext);
58     sessionService.startInternal(new SessionIdGenerator());
59     sessionService.addingService(serviceReference);
60   }
61
62   @Test
63   public void testCreateEmptySession(){
64     Session session = sessionService.createEmptySession();
65     assertEquals("session manager does not match", manager, session.getManager());
66   }
67
68   @Test
69   public void testCreateSessionwithRandomId(){
70     Session session = sessionService.createSession(null);
71     assertEquals("Session should be valid", true, session.isValid());
72     sessionService.removeSession(session.getId());
73   }
74
75   @Test
76   public void testCreateSession(){
77     Session session = sessionService.createSession(sessionId);
78     assertEquals("Session should be valid", true, session.isValid());
79     assertEquals("Session id does not match", sessionId, session.getId());
80     sessionService.removeSession(sessionId);
81   }
82
83   @Test
84   public void testNullfindSession() {
85     Session session = sessionService.findSession(null);
86     assertNull("Session should be null", session);
87   }
88
89   @Test
90   public void testSessionCRUD(){
91     Session foundSession = sessionService.findSession(sessionId);
92     assertNull("Session should not exist here", foundSession);
93     Session session = sessionService.createSession(sessionId);
94     foundSession = sessionService.findSession(sessionId);
95     assertEquals("Session was not added", sessionId, foundSession.getId());
96     session.setAuthType(AUTH_TYPE);
97     sessionService.updateSession((ClusterSession)session);
98     foundSession = sessionService.findSession(sessionId);
99     assertEquals("Session was not found, id does not match", sessionId, foundSession.getId());
100     assertEquals("Session was not found, auth type does match", AUTH_TYPE, foundSession.getAuthType());
101     sessionService.removeSession(sessionId);
102     foundSession = sessionService.findSession(sessionId);
103     assertEquals("Session was not removed", null, foundSession);
104   }
105
106   @Test
107   public void testExpireSession(){
108     Session session = sessionService.createSession(sessionId);
109     session.setAuthType(AUTH_TYPE);
110     sessionService.addSession((ClusterSession)session);
111     Session foundSession = sessionService.findSession(sessionId);
112     assertEquals("Session was not found", sessionId, foundSession.getId());
113     sessionService.expireSession(sessionId);
114     foundSession = sessionService.findSession(sessionId);
115     assertEquals("Session was not expired", null, foundSession);
116   }
117
118   @Test
119   public void testFindSessions(){
120     Session session = sessionService.createSession(sessionId);
121     session.setAuthType(AUTH_TYPE);
122     sessionService.addSession((ClusterSession)session);
123     Session[] sessions = sessionService.findSessions();
124     assertEquals("Session array size does not match", 1, sessions.length);
125     assertEquals("Session array size does not match", sessionId, sessions[0].getId());
126     sessionService.removeSession(sessionId);
127   }
128
129   @Test
130   public void testGetSession(){
131     ClusterSession session = (ClusterSession) sessionService.createSession(sessionId);
132     session.setAttribute(ATTRIBUTE_NAME, AUTH_TYPE);
133     HashMap<String, String> sessionAttributes = sessionService.getSession(sessionId);
134     assertNotNull("Session attribute should not be null", sessionAttributes);
135     assertEquals("Session attribute size does not match", 1, sessionAttributes.size());
136     assertEquals("Session attribute size does not match", AUTH_TYPE, sessionAttributes.get(ATTRIBUTE_NAME));
137     sessionService.removeSession(sessionId);
138   }
139
140   @Test
141   public void testNullSessionCache(){
142     ClusterSessionManager clustermanager = new ClusterSessionManager();
143     ClusterSessionServiceImpl service = new ClusterSessionServiceImpl(clustermanager);
144     Session session = service.findSession(sessionId);
145     assertNull("Session should be null, as cache is null", session);
146     Session[] sessions = service.findSessions();
147     assertEquals("Session array should be empty", 0, sessions.length);
148     service.removeSession(sessionId);
149     service.expireSession(sessionId);
150     session = service.createSession(sessionId);
151     assertNull("Session should be null, as cache is null", session);
152     service.addSession(null);
153     Map<String,String> attributes = service.getSession(sessionId);
154     assertNull("Attributes should be null, as cache is null", attributes);
155     service.updateSession(null);
156   }
157
158   @AfterClass
159   public static void cleanup(){
160       sessionService.stopInternal();
161   }
162 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.