// Path: shared/src/main/java/com/optimizely/ab/android/shared/Cache.java
// public class Cache {
// 
//     @NonNull private final Context context;
//     @NonNull private final Logger logger;
// 
//     /**
//      * Create new instance of {@link Cache}.
//      *
//      * @param context any {@link Context instance}
//      * @param logger  a {@link Logger} instances
//      */
//     public Cache(@NonNull Context context, @NonNull Logger logger) {
//         this.context = context;
//         this.logger = logger;
//     }
// 
//     /**
//      * Delete the cache file.
//      *
//      * @param filename the path to the file
//      * @return true if the file was deleted or false otherwise
//      */
//     public boolean delete(String filename) {
//         return context.deleteFile(filename);
//     }
// 
//     /**
//      * Check if the cache file exists.
//      *
//      * @param filename the path to the file
//      * @return true if the file exists or false otherwise
//      */
//     public boolean exists(String filename) {
//         String[] files = context.fileList();
//         return files != null && Arrays.asList(files).contains(filename);
//     }
// 
//     /**
//      * Load data from the cache file.
//      *
//      * @param filename the path to the file
//      * @return the loaded cache file as String or null if the file cannot be loaded
//      */
//     @Nullable
//     public String load(String filename) {
//         FileInputStream fileInputStream = null;
//         try {
//             fileInputStream = context.openFileInput(filename);
//             InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
//             BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
//             StringBuilder sb = new StringBuilder();
//             String line;
//             while ((line = bufferedReader.readLine()) != null) {
//                 sb.append(line);
//             }
//             return sb.toString();
//         } catch (Exception e) {
//             logger.warn("Unable to load file {}.", filename);
//             return null;
//         } finally {
//             try {
//                 if (fileInputStream != null) {
//                     fileInputStream.close();
//                 }
//             } catch (Exception e) {
//                 logger.warn("Unable to close file {}.", filename, e);
//             }
//         }
//     }
// 
//     /**
//      * Save data to the cache file and overwrite any existing data.
//      *
//      * @param filename the path to the file
//      * @param data     the String data to write to the file
//      * @return true if the file was saved
//      */
//     public boolean save(String filename, String data) {
//         FileOutputStream fileOutputStream = null;
//         try {
//             fileOutputStream = context.openFileOutput(filename, Context.MODE_PRIVATE);
//             fileOutputStream.write(data.getBytes());
//             return true;
//         } catch (Exception e) {
//             logger.error("Error saving file {}.", filename);
//             return false;
//         } finally {
//             if (fileOutputStream != null) {
//                 try {
//                     fileOutputStream.close();
//                 } catch (Exception e) {
//                     logger.warn("Unable to close file {}.", filename, e);
// 
//                 }
//             }
//         }
//     }
// }


import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;
import com.optimizely.ab.android.shared.Cache;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
        decisionMap1.put("variation_id", "var_1");
        experimentBucketMap.put("exp_1", decisionMap1);
        Map<String, String> decisionMap2 = new ConcurrentHashMap<>();
        decisionMap2.put("variation_id", "var_2");
        experimentBucketMap.put("exp_2", decisionMap2);
        Map<String, Object> userProfileMap = new ConcurrentHashMap<>();
        userProfileMap.put("user_id", userId);
        userProfileMap.put("experiment_bucket_map", experimentBucketMap);
        memoryCache.put(userId, userProfileMap);
    }

    @After
    public void teardown() {
        cache.delete(diskCache.getFileName());
    }

    @Test
    public void testGetFileName() {
        assertEquals("optly-user-profile-service-123.json", diskCache.getFileName());
    }

    @Test
    public void testLoadWhenNoFile() throws JSONException {
        assertEquals(diskCache.load().toString(), new JSONObject().toString());
        verify(logger).warn("Unable to load file {}.", diskCache.getFileName());
        verify(logger).warn("Unable to load user profile cache from disk.");
    }

    @Test
    public void testLoadIOException() throws JSONException {
// Path: shared/src/main/java/com/optimizely/ab/android/shared/Cache.java
// public class Cache {
// 
//     @NonNull private final Context context;
//     @NonNull private final Logger logger;
// 
//     /**
//      * Create new instance of {@link Cache}.
//      *
//      * @param context any {@link Context instance}
//      * @param logger  a {@link Logger} instances
//      */
//     public Cache(@NonNull Context context, @NonNull Logger logger) {
//         this.context = context;
//         this.logger = logger;
//     }
// 
//     /**
//      * Delete the cache file.
//      *
//      * @param filename the path to the file
//      * @return true if the file was deleted or false otherwise
//      */
//     public boolean delete(String filename) {
//         return context.deleteFile(filename);
//     }
// 
//     /**
//      * Check if the cache file exists.
//      *
//      * @param filename the path to the file
//      * @return true if the file exists or false otherwise
//      */
//     public boolean exists(String filename) {
//         String[] files = context.fileList();
//         return files != null && Arrays.asList(files).contains(filename);
//     }
// 
//     /**
//      * Load data from the cache file.
//      *
//      * @param filename the path to the file
//      * @return the loaded cache file as String or null if the file cannot be loaded
//      */
//     @Nullable
//     public String load(String filename) {
//         FileInputStream fileInputStream = null;
//         try {
//             fileInputStream = context.openFileInput(filename);
//             InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
//             BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
//             StringBuilder sb = new StringBuilder();
//             String line;
//             while ((line = bufferedReader.readLine()) != null) {
//                 sb.append(line);
//             }
//             return sb.toString();
//         } catch (Exception e) {
//             logger.warn("Unable to load file {}.", filename);
//             return null;
//         } finally {
//             try {
//                 if (fileInputStream != null) {
//                     fileInputStream.close();
//                 }
//             } catch (Exception e) {
//                 logger.warn("Unable to close file {}.", filename, e);
//             }
//         }
//     }
// 
//     /**
//      * Save data to the cache file and overwrite any existing data.
//      *
//      * @param filename the path to the file
//      * @param data     the String data to write to the file
//      * @return true if the file was saved
//      */
//     public boolean save(String filename, String data) {
//         FileOutputStream fileOutputStream = null;
//         try {
//             fileOutputStream = context.openFileOutput(filename, Context.MODE_PRIVATE);
//             fileOutputStream.write(data.getBytes());
//             return true;
//         } catch (Exception e) {
//             logger.error("Error saving file {}.", filename);
//             return false;
//         } finally {
//             if (fileOutputStream != null) {
//                 try {
//                     fileOutputStream.close();
//                 } catch (Exception e) {
//                     logger.warn("Unable to close file {}.", filename, e);
// 
//                 }
//             }
//         }
//     }
// }


// Path: user-profile/src/androidTest/java/com/optimizely/ab/android/user_profile/DiskCacheTest.java
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;
import com.optimizely.ab.android.shared.Cache;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

        decisionMap1.put("variation_id", "var_1");
        experimentBucketMap.put("exp_1", decisionMap1);
        Map<String, String> decisionMap2 = new ConcurrentHashMap<>();
        decisionMap2.put("variation_id", "var_2");
        experimentBucketMap.put("exp_2", decisionMap2);
        Map<String, Object> userProfileMap = new ConcurrentHashMap<>();
        userProfileMap.put("user_id", userId);
        userProfileMap.put("experiment_bucket_map", experimentBucketMap);
        memoryCache.put(userId, userProfileMap);
    }

    @After
    public void teardown() {
        cache.delete(diskCache.getFileName());
    }

    @Test
    public void testGetFileName() {
        assertEquals("optly-user-profile-service-123.json", diskCache.getFileName());
    }

    @Test
    public void testLoadWhenNoFile() throws JSONException {
        assertEquals(diskCache.load().toString(), new JSONObject().toString());
        verify(logger).warn("Unable to load file {}.", diskCache.getFileName());
        verify(logger).warn("Unable to load user profile cache from disk.");
    }

    @Test
    public void testLoadIOException() throws JSONException {
        cache = mock(Cache.class);