updater_sample: Improve UpdateConfig

UpdateConfig:
- constant names changed
- added parsing streaming metadata
- added InnerFile to describe a file in zip

Android.mk
- added guava

tests fixed

Test: using junit4

Change-Id: Ibe3c8a3bde20259b0eea9a79aca4b22ed7b048f4
Signed-off-by: Zhomart Mukhamejanov <zhomart@google.com>
diff --git a/updater_sample/tests/src/com/example/android/systemupdatersample/UpdateConfigTest.java b/updater_sample/tests/src/com/example/android/systemupdatersample/UpdateConfigTest.java
index 8715371..0975e76 100644
--- a/updater_sample/tests/src/com/example/android/systemupdatersample/UpdateConfigTest.java
+++ b/updater_sample/tests/src/com/example/android/systemupdatersample/UpdateConfigTest.java
@@ -19,14 +19,23 @@
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertSame;
 
+import android.content.Context;
+import android.support.test.InstrumentationRegistry;
 import android.support.test.filters.SmallTest;
 import android.support.test.runner.AndroidJUnit4;
 
+import com.example.android.systemupdatersample.tests.R;
+import com.google.common.io.CharStreams;
+
+import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
 import org.junit.runner.RunWith;
 
+import java.io.IOException;
+import java.io.InputStreamReader;
+
 /**
  * Tests for {@link UpdateConfig}
  */
@@ -36,27 +45,48 @@
 
     private static final String JSON_NON_STREAMING =
             "{\"name\": \"vip update\", \"url\": \"file:///builds/a.zip\", "
-            + " \"type\": \"NON_STREAMING\"}";
-
-    private static final String JSON_STREAMING =
-            "{\"name\": \"vip update 2\", \"url\": \"http://foo.bar/a.zip\", "
-            + "\"type\": \"STREAMING\"}";
+            + " \"ab_install_type\": \"NON_STREAMING\"}";
 
     @Rule
     public final ExpectedException thrown = ExpectedException.none();
 
+    private Context mContext;
+    private Context mTargetContext;
+    private String mJsonStreaming001;
+
+    @Before
+    public void setUp() throws Exception {
+        mContext = InstrumentationRegistry.getContext();
+        mTargetContext = InstrumentationRegistry.getTargetContext();
+        mJsonStreaming001 = readResource(R.raw.update_config_stream_001);
+    }
+
     @Test
-    public void fromJson_parsesJsonConfigWithoutMetadata() throws Exception {
+    public void fromJson_parsesNonStreaming() throws Exception {
         UpdateConfig config = UpdateConfig.fromJson(JSON_NON_STREAMING);
         assertEquals("name is parsed", "vip update", config.getName());
         assertEquals("stores raw json", JSON_NON_STREAMING, config.getRawJson());
-        assertSame("type is parsed", UpdateConfig.TYPE_NON_STREAMING, config.getInstallType());
+        assertSame("type is parsed",
+                UpdateConfig.AB_INSTALL_TYPE_NON_STREAMING,
+                config.getInstallType());
         assertEquals("url is parsed", "file:///builds/a.zip", config.getUrl());
     }
 
     @Test
+    public void fromJson_parsesStreaming() throws Exception {
+        UpdateConfig config = UpdateConfig.fromJson(mJsonStreaming001);
+        assertEquals("streaming-001", config.getName());
+        assertEquals("http://foo.bar/update.zip", config.getUrl());
+        assertSame(UpdateConfig.AB_INSTALL_TYPE_STREAMING, config.getInstallType());
+        assertEquals("payload.bin",
+                config.getStreamingMetadata().getPropertyFiles()[0].getFilename());
+        assertEquals(195, config.getStreamingMetadata().getPropertyFiles()[0].getOffset());
+        assertEquals(8, config.getStreamingMetadata().getPropertyFiles()[0].getSize());
+    }
+
+    @Test
     public void getUpdatePackageFile_throwsErrorIfStreaming() throws Exception {
-        UpdateConfig config = UpdateConfig.fromJson(JSON_STREAMING);
+        UpdateConfig config = UpdateConfig.fromJson(mJsonStreaming001);
         thrown.expect(RuntimeException.class);
         config.getUpdatePackageFile();
     }
@@ -64,7 +94,7 @@
     @Test
     public void getUpdatePackageFile_throwsErrorIfNotAFile() throws Exception {
         String json = "{\"name\": \"upd\", \"url\": \"http://foo.bar\","
-                + " \"type\": \"NON_STREAMING\"}";
+                + " \"ab_install_type\": \"NON_STREAMING\"}";
         UpdateConfig config = UpdateConfig.fromJson(json);
         thrown.expect(RuntimeException.class);
         config.getUpdatePackageFile();
@@ -73,7 +103,11 @@
     @Test
     public void getUpdatePackageFile_works() throws Exception {
         UpdateConfig c = UpdateConfig.fromJson(JSON_NON_STREAMING);
-        assertEquals("correct path", "/builds/a.zip", c.getUpdatePackageFile().getAbsolutePath());
+        assertEquals("/builds/a.zip", c.getUpdatePackageFile().getAbsolutePath());
     }
 
+    private String readResource(int id) throws IOException {
+        return CharStreams.toString(new InputStreamReader(
+            mContext.getResources().openRawResource(id)));
+    }
 }