1package com.example.food_front.utils;
3import android.content.Context;
4import android.content.SharedPreferences;
6import org.junit.Before;
8import org.junit.runner.RunWith;
9import org.mockito.Mock;
10import org.mockito.junit.MockitoJUnitRunner;
12import static org.junit.Assert.*;
13import static org.mockito.ArgumentMatchers.anyInt;
14import static org.mockito.ArgumentMatchers.anyString;
15import static org.mockito.ArgumentMatchers.eq;
16import static org.mockito.Mockito.verify;
17import static org.mockito.Mockito.when;
22@RunWith(MockitoJUnitRunner.class)
25 private static final String PREF_NAME =
"user_session";
26 private static final String KEY_TOKEN =
"token";
27 private static final String KEY_EMAIL =
"email";
28 private static final String KEY_LAST_INIT_POINT =
"last_init_point";
34 Context mockAppContext;
37 SharedPreferences mockSharedPreferences;
40 SharedPreferences.Editor mockEditor;
47 when(mockContext.getApplicationContext()).thenReturn(mockAppContext);
48 when(mockAppContext.getSharedPreferences(eq(PREF_NAME), anyInt())).thenReturn(mockSharedPreferences);
49 when(mockSharedPreferences.edit()).thenReturn(mockEditor);
50 when(mockEditor.putString(anyString(), anyString())).thenReturn(mockEditor);
59 String testToken =
"test_auth_token_12345";
62 sessionManager.saveToken(testToken);
65 verify(mockEditor).putString(KEY_TOKEN, testToken);
66 verify(mockEditor).apply();
72 String expectedToken =
"saved_auth_token_12345";
73 when(mockSharedPreferences.getString(eq(KEY_TOKEN), eq(
null))).thenReturn(expectedToken);
76 String actualToken = sessionManager.getToken();
79 assertEquals(expectedToken, actualToken);
85 String testEmail =
"test@example.com";
88 sessionManager.saveEmail(testEmail);
91 verify(mockEditor).putString(KEY_EMAIL, testEmail);
92 verify(mockEditor).apply();
98 String expectedEmail =
"user@example.com";
99 when(mockSharedPreferences.getString(eq(KEY_EMAIL), eq(
null))).thenReturn(expectedEmail);
102 String actualEmail = sessionManager.getUserEmail();
105 assertEquals(expectedEmail, actualEmail);
111 String testInitPoint =
"https://mercadopago.com/checkout/12345";
114 sessionManager.saveLastInitPoint(testInitPoint);
117 verify(mockEditor).putString(KEY_LAST_INIT_POINT, testInitPoint);
118 verify(mockEditor).apply();
124 sessionManager.clearSession();
127 verify(mockEditor).clear();
128 verify(mockEditor).apply();
134 sessionManager.logout();
137 verify(mockEditor).clear();
138 verify(mockEditor).apply();
void getToken_retrievesTokenFromSharedPreferences()
void saveLastInitPoint_storesInitPointInSharedPreferences()
void clearSession_removesAllValuesFromSharedPreferences()
void saveToken_storesTokenInSharedPreferences()
void logout_callsClearSession()
void saveEmail_storesEmailInSharedPreferences()
void getUserEmail_retrievesEmailFromSharedPreferences()