Make sure invokeOperation is set once
[controller.git] / opendaylight / adsal / samples / clustersession / src / test / java / org / opendaylight / controller / clustersession / ClusterSessionManagerTest.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.io.IOException;
10 import java.util.HashMap;
11 import java.util.concurrent.ConcurrentHashMap;
12 import java.util.concurrent.ConcurrentMap;
13
14 import org.apache.catalina.Context;
15 import org.apache.catalina.LifecycleException;
16 import org.apache.catalina.Session;
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 ClusterSessionManagerTest {
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   final int SESSION_ID_LENGTH = 7;
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     manager.setContainer(containerContext);
57     try {
58       manager.startInternal();
59     } catch (LifecycleException e) {
60     }
61     sessionService = (ClusterSessionServiceImpl) manager.getSessionService();
62     sessionService.addingService(serviceReference);
63   }
64
65   @Test
66   public void checkSessionManagerCreated(){
67     assertEquals("session manager info does not match", "ClusterSessionManager/1.0", manager.getInfo());
68     assertEquals("session manager name does not match", "ClusterSessionManager", manager.getName());
69   }
70
71   @Test
72   public void testCreateEmptySession(){
73     Session session = manager.createEmptySession();
74     assertEquals("session manager does not match", manager, session.getManager());
75   }
76
77   @Test
78   public void testCreateRandomSessionId(){
79     Session session = manager.createSession(null);
80     assertEquals("Session should be valid", true, session.isValid());
81     manager.remove(session);
82   }
83
84   @Test
85   public void testCreateSession(){
86     Session session = manager.createSession(sessionId);
87     assertEquals("Session should be valid", true, session.isValid());
88     assertEquals("Session id does not match", sessionId, session.getId());
89     manager.remove(session);
90   }
91
92   @Test
93   public void testReCreateSession(){
94     Session session = manager.createSession(sessionId);
95     assertEquals("Session should be valid", true, session.isValid());
96     assertEquals("Session id does not match", sessionId, session.getId());
97     manager.createSession(sessionId);
98     manager.remove(session);
99   }
100
101   @Test
102   public void testSessionCRUD() throws IOException{
103     Session foundSession = manager.findSession(sessionId);
104     assertNull("Session should not exist here", foundSession);
105     Session session = manager.createSession(sessionId);
106     manager.add(session);
107     foundSession = manager.findSession(sessionId);
108     assertEquals("Session was not found, id does not match", sessionId, foundSession.getId());
109     manager.remove(session);
110     foundSession = manager.findSession(sessionId);
111     assertEquals("Session was not removed", null, foundSession);
112   }
113
114   @Test
115   public void testExpireSession() throws IOException{
116     Session session = manager.createSession(sessionId);
117     session.setAuthType(AUTH_TYPE);
118     manager.add(session);
119     Session foundSession = manager.findSession(sessionId);
120     assertEquals("Session was not found", sessionId, foundSession.getId());
121     manager.expireSession(sessionId);
122     foundSession = manager.findSession(sessionId);
123     assertEquals("Session was not expired", null, foundSession);
124   }
125
126   @Test
127   public void testFindSessions(){
128     Session session = manager.createSession(sessionId);
129     session.setAuthType(AUTH_TYPE);
130     manager.add(session);
131     Session[] sessions = manager.findSessions();
132     assertEquals("Session array size does not match", 1, sessions.length);
133     assertEquals("Session array size does not match", sessionId, sessions[0].getId());
134     manager.remove(session);
135   }
136
137   @Test
138   public void testGetSession(){
139     ClusterSession session = (ClusterSession) manager.createSession(sessionId);
140     session.setAttribute(ATTRIBUTE_NAME, AUTH_TYPE);
141     manager.add(session);
142     HashMap<String, String> sessionAttributes = manager.getSession(sessionId);
143     assertNotNull("Session attribute should not be null", sessionAttributes);
144     assertEquals("Session attribute size does not match", 1, sessionAttributes.size());
145     assertEquals("Session attribute size does not match", AUTH_TYPE, sessionAttributes.get(ATTRIBUTE_NAME));
146     manager.remove(session);
147   }
148
149   @AfterClass
150   public static void cleanup(){
151     try {
152       manager.stopInternal();
153     } catch (LifecycleException e) {
154     }
155   }
156
157 }