1616package software .aws .solution .clickstream ;
1717
1818import android .content .Context ;
19- import android .database .Cursor ;
2019import androidx .test .core .app .ApplicationProvider ;
2120
22- import org .json .JSONObject ;
23- import org .junit .After ;
2421import org .junit .Assert ;
2522import org .junit .Before ;
2623import org .junit .Test ;
3128import software .aws .solution .clickstream .client .ClickstreamManager ;
3229import software .aws .solution .clickstream .client .Session ;
3330import software .aws .solution .clickstream .client .SessionClient ;
34- import software .aws .solution .clickstream .client .db .ClickstreamDBUtil ;
3531import software .aws .solution .clickstream .util .ReflectUtil ;
3632
3733import static org .mockito .Mockito .mock ;
4339public class SessionClientTest {
4440 private SessionClient client ;
4541 private AnalyticsClient analyticsClient ;
46- private ClickstreamDBUtil dbUtil ;
4742 private ClickstreamContext clickstreamContext ;
4843
4944 /**
@@ -53,7 +48,6 @@ public class SessionClientTest {
5348 public void setup () {
5449 Context context = ApplicationProvider .getApplicationContext ();
5550
56- dbUtil = new ClickstreamDBUtil (context );
5751 AWSClickstreamPluginConfiguration .Builder configurationBuilder = AWSClickstreamPluginConfiguration .builder ();
5852 configurationBuilder .withAppId ("demo-app" )
5953 .withEndpoint ("http://cs-se-serve-1qtj719j88vwn-1291141553.ap-southeast-1.elb.amazonaws.com/collect" )
@@ -74,22 +68,12 @@ public void setup() {
7468 */
7569 @ Test
7670 public void testExecuteStart () throws Exception {
77- client .initialSession ();
71+ boolean isNewSession = client .initialSession ();
7872 Session session = (Session ) ReflectUtil .getFiled (client , "session" );
7973 Assert .assertNotNull (session );
8074 Session clientSession = (Session ) ReflectUtil .getFiled (analyticsClient , "session" );
8175 Assert .assertNotNull (clientSession );
82- Assert .assertEquals (1 , dbUtil .getTotalNumber ());
83- Cursor cursor = dbUtil .queryAllEvents ();
84- cursor .moveToFirst ();
85- String eventString = cursor .getString (2 );
86- JSONObject jsonObject = new JSONObject (eventString );
87- Assert .assertEquals ("_session_start" , jsonObject .getString ("event_type" ));
88- JSONObject attributes = jsonObject .getJSONObject ("attributes" );
89- Assert .assertNotNull (attributes .getString ("_session_id" ));
90- Assert .assertNotNull (attributes .getString ("_session_start_timestamp" ));
91- Assert .assertNotNull (attributes .getString ("_session_duration" ));
92- cursor .close ();
76+ Assert .assertTrue (isNewSession );
9377 }
9478
9579 /**
@@ -106,17 +90,6 @@ public void testExecuteStartAndStore() throws Exception {
10690 client .storeSession ();
10791 Session storedSession = (Session ) ReflectUtil .getFiled (client , "session" );
10892 Assert .assertFalse (storedSession .isNewSession ());
109-
110- Assert .assertEquals (1 , dbUtil .getTotalNumber ());
111- Cursor cursor = dbUtil .queryAllEvents ();
112- cursor .moveToFirst ();
113- String eventString = cursor .getString (2 );
114- JSONObject jsonObject = new JSONObject (eventString );
115- JSONObject attributes = jsonObject .getJSONObject ("attributes" );
116- Assert .assertNotNull (attributes .getString ("_session_id" ));
117- Assert .assertNotNull (attributes .getString ("_session_start_timestamp" ));
118- Assert .assertNotNull (attributes .getString ("_session_duration" ));
119- cursor .close ();
12093 }
12194
12295
@@ -143,8 +116,6 @@ public void testExecuteStartTwiceWithoutSessionTimeout() throws Exception {
143116 Assert .assertEquals (session .getSessionID (), newSession .getSessionID ());
144117 Assert .assertEquals (session .getStartTime (), newSession .getStartTime ());
145118 Assert .assertEquals (1 , newSession .getSessionIndex ());
146-
147- Assert .assertEquals (1 , dbUtil .getTotalNumber ());
148119 }
149120
150121
@@ -172,8 +143,6 @@ public void testExecuteStartTwiceWithSessionTimeout() throws Exception {
172143 Assert .assertNotEquals (session .getSessionID (), newSession .getSessionID ());
173144 Assert .assertNotEquals (session .getStartTime (), newSession .getStartTime ());
174145 Assert .assertEquals (2 , newSession .getSessionIndex ());
175-
176- Assert .assertEquals (2 , dbUtil .getTotalNumber ());
177146 }
178147
179148
@@ -185,12 +154,4 @@ public void testInitSessionClientWithNullAnalyticsClient() {
185154 ClickstreamContext context = mock (ClickstreamContext .class );
186155 new SessionClient (context );
187156 }
188-
189- /**
190- * close db.
191- */
192- @ After
193- public void tearDown () {
194- dbUtil .closeDB ();
195- }
196157}
0 commit comments