|
| 1 | +/* |
| 2 | + * Copyright 2016, Optimizely |
| 3 | + * <p/> |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * <p/> |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * <p/> |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.optimizely.ab.android.sdk; |
| 17 | + |
| 18 | +import android.content.Context; |
| 19 | +import android.os.Build; |
| 20 | +import android.support.annotation.RequiresApi; |
| 21 | +import android.support.test.InstrumentationRegistry; |
| 22 | +import android.support.test.espresso.core.deps.guava.util.concurrent.ListeningExecutorService; |
| 23 | +import android.support.test.espresso.core.deps.guava.util.concurrent.MoreExecutors; |
| 24 | +import android.support.test.runner.AndroidJUnit4; |
| 25 | + |
| 26 | +import com.optimizely.ab.android.shared.Cache; |
| 27 | +import com.optimizely.ab.android.shared.Client; |
| 28 | + |
| 29 | +import org.json.JSONException; |
| 30 | +import org.json.JSONObject; |
| 31 | +import org.junit.After; |
| 32 | +import org.junit.Before; |
| 33 | +import org.junit.Test; |
| 34 | +import org.junit.runner.RunWith; |
| 35 | +import org.slf4j.Logger; |
| 36 | + |
| 37 | +import java.io.IOException; |
| 38 | +import java.net.MalformedURLException; |
| 39 | +import java.util.concurrent.TimeUnit; |
| 40 | + |
| 41 | +import static junit.framework.Assert.assertEquals; |
| 42 | +import static junit.framework.Assert.assertNotNull; |
| 43 | +import static junit.framework.Assert.assertNull; |
| 44 | +import static junit.framework.Assert.fail; |
| 45 | +import static org.mockito.Matchers.any; |
| 46 | +import static org.mockito.Matchers.anyInt; |
| 47 | +import static org.mockito.Mockito.atMost; |
| 48 | +import static org.mockito.Mockito.mock; |
| 49 | +import static org.mockito.Mockito.verify; |
| 50 | +import static org.mockito.Mockito.when; |
| 51 | + |
| 52 | +/** |
| 53 | + * Tests for {@link DataFileLoader} |
| 54 | + */ |
| 55 | +@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB) |
| 56 | +@RunWith(AndroidJUnit4.class) |
| 57 | +public class DataFileLoaderTest { |
| 58 | + |
| 59 | + private DataFileService datafileService; |
| 60 | + private DataFileCache dataFileCache; |
| 61 | + private DataFileClient dataFileClient; |
| 62 | + private Client client; |
| 63 | + private Logger logger; |
| 64 | + private DataFileLoadedListener dataFileLoadedListener; |
| 65 | + |
| 66 | + @Before |
| 67 | + public void setup() { |
| 68 | + datafileService = mock(DataFileService.class); |
| 69 | + logger = mock(Logger.class); |
| 70 | + final Context targetContext = InstrumentationRegistry.getTargetContext(); |
| 71 | + dataFileCache = new DataFileCache("1", new Cache(targetContext, logger), logger); |
| 72 | + client = mock(Client.class); |
| 73 | + dataFileClient = new DataFileClient(client, logger); |
| 74 | + dataFileLoadedListener = mock(DataFileLoadedListener.class); |
| 75 | + when(datafileService.getApplicationContext()).thenReturn(targetContext); |
| 76 | + when(datafileService.isBound()).thenReturn(true); |
| 77 | + } |
| 78 | + |
| 79 | + @After |
| 80 | + public void tearDown() { |
| 81 | + dataFileCache.delete(); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void loadFromCDNWhenNoCachedFile() throws MalformedURLException, JSONException { |
| 86 | + final ListeningExecutorService executor = MoreExecutors.newDirectExecutorService(); |
| 87 | + DataFileLoader dataFileLoader = |
| 88 | + new DataFileLoader(datafileService, dataFileClient, dataFileCache, executor, logger); |
| 89 | + |
| 90 | + when(client.execute(any(Client.Request.class), anyInt(), anyInt())).thenReturn("{}"); |
| 91 | + |
| 92 | + dataFileLoader.getDataFile("1", dataFileLoadedListener); |
| 93 | + try { |
| 94 | + executor.awaitTermination(5, TimeUnit.SECONDS); |
| 95 | + } catch (InterruptedException e) { |
| 96 | + fail(); |
| 97 | + } |
| 98 | + |
| 99 | + final JSONObject cachedDataFile = dataFileCache.load(); |
| 100 | + assertNotNull(cachedDataFile); |
| 101 | + assertEquals("{}", cachedDataFile.toString()); |
| 102 | + verify(dataFileLoadedListener, atMost(1)).onDataFileLoaded("{}"); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void loadWhenCacheFileExistsAndCDNNotModified() { |
| 107 | + final ListeningExecutorService executor = MoreExecutors.newDirectExecutorService(); |
| 108 | + DataFileLoader dataFileLoader = |
| 109 | + new DataFileLoader(datafileService, dataFileClient, dataFileCache, executor, logger); |
| 110 | + dataFileCache.save("{}"); |
| 111 | + |
| 112 | + when(client.execute(any(Client.Request.class), anyInt(), anyInt())).thenReturn(""); |
| 113 | + |
| 114 | + dataFileLoader.getDataFile("1", dataFileLoadedListener); |
| 115 | + try { |
| 116 | + executor.awaitTermination(5, TimeUnit.SECONDS); |
| 117 | + } catch (InterruptedException e) { |
| 118 | + fail(); |
| 119 | + } |
| 120 | + |
| 121 | + final JSONObject cachedDataFile = dataFileCache.load(); |
| 122 | + assertNotNull(cachedDataFile); |
| 123 | + assertEquals("{}", cachedDataFile.toString()); |
| 124 | + verify(dataFileLoadedListener, atMost(1)).onDataFileLoaded("{}"); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + public void noCacheAndLoadFromCDNFails() { |
| 129 | + final ListeningExecutorService executor = MoreExecutors.newDirectExecutorService(); |
| 130 | + DataFileLoader dataFileLoader = |
| 131 | + new DataFileLoader(datafileService, dataFileClient, dataFileCache, executor, logger); |
| 132 | + |
| 133 | + when(client.execute(any(Client.Request.class), anyInt(), anyInt())).thenReturn(null); |
| 134 | + |
| 135 | + dataFileLoader.getDataFile("1", dataFileLoadedListener); |
| 136 | + try { |
| 137 | + executor.awaitTermination(5, TimeUnit.SECONDS); |
| 138 | + } catch (InterruptedException e) { |
| 139 | + fail(); |
| 140 | + } |
| 141 | + |
| 142 | + final JSONObject cachedDataFile = dataFileCache.load(); |
| 143 | + assertNull(cachedDataFile); |
| 144 | + verify(dataFileLoadedListener, atMost(1)).onDataFileLoaded(null); |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + public void warningsAreLogged() throws IOException { |
| 149 | + final ListeningExecutorService executor = MoreExecutors.newDirectExecutorService(); |
| 150 | + Cache cache = mock(Cache.class); |
| 151 | + dataFileCache = new DataFileCache("1", cache, logger); |
| 152 | + DataFileLoader dataFileLoader = |
| 153 | + new DataFileLoader(datafileService, dataFileClient, dataFileCache, executor, logger); |
| 154 | + |
| 155 | + when(client.execute(any(Client.Request.class), anyInt(), anyInt())).thenReturn("{}"); |
| 156 | + when(cache.exists(dataFileCache.getFileName())).thenReturn(true); |
| 157 | + when(cache.delete(dataFileCache.getFileName())).thenReturn(false); |
| 158 | + when(cache.save(dataFileCache.getFileName(), "{}")).thenReturn(false); |
| 159 | + |
| 160 | + dataFileLoader.getDataFile("1", dataFileLoadedListener); |
| 161 | + try { |
| 162 | + executor.awaitTermination(5, TimeUnit.SECONDS); |
| 163 | + } catch (InterruptedException e) { |
| 164 | + fail(); |
| 165 | + } |
| 166 | + |
| 167 | + verify(logger).warn("Unable to delete old datafile"); |
| 168 | + verify(logger).warn("Unable to save new datafile"); |
| 169 | + verify(dataFileLoadedListener, atMost(1)).onDataFileLoaded("{}"); |
| 170 | + } |
| 171 | + |
| 172 | +} |
0 commit comments