diff --git a/app/build.gradle b/app/build.gradle index 4751a83..c69d9f1 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -24,19 +24,26 @@ apply plugin: 'com.android.application' +dependencies { + compile fileTree(dir: 'libs', include: ['*.jar']) + testCompile 'junit:junit:4.12' + compile 'com.android.support:appcompat-v7:25.0.1' + compile 'com.android.support:design:25.0.1' + compile project(':batching') + compile project(':batching-core') + compile project(':batching-gson') +} + android { - compileSdkVersion 24 - buildToolsVersion '24.0.0' + compileSdkVersion 25 + buildToolsVersion '25.0.0' defaultConfig { applicationId "com.flipkart.batchdemo" minSdkVersion 14 - targetSdkVersion 24 + targetSdkVersion 25 versionCode 1 versionName "1.0" - jackOptions { - enabled true - } } lintOptions { @@ -49,17 +56,4 @@ android { proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } - - compileOptions { - sourceCompatibility JavaVersion.VERSION_1_8 - targetCompatibility JavaVersion.VERSION_1_8 - } -} - -dependencies { - compile fileTree(dir: 'libs', include: ['*.jar']) - testCompile 'junit:junit:4.12' - compile 'com.android.support:appcompat-v7:24.0.0' - compile 'com.android.support:design:24.0.0' - compile project(':batching') -} +} \ No newline at end of file diff --git a/app/src/main/java/com/flipkart/batchdemo/CustomTagData.java b/app/src/main/java/com/flipkart/batchdemo/CustomTagData.java index 67aeae8..421b77e 100644 --- a/app/src/main/java/com/flipkart/batchdemo/CustomTagData.java +++ b/app/src/main/java/com/flipkart/batchdemo/CustomTagData.java @@ -24,8 +24,8 @@ package com.flipkart.batchdemo; -import com.flipkart.batching.data.Tag; -import com.flipkart.batching.data.TagData; +import com.flipkart.batching.core.data.Tag; +import com.flipkart.batching.core.data.TagData; import com.google.gson.annotations.SerializedName; import org.json.JSONObject; diff --git a/app/src/main/java/com/flipkart/batchdemo/EventTag.java b/app/src/main/java/com/flipkart/batchdemo/EventTag.java index c74bfb3..4ff5ae4 100644 --- a/app/src/main/java/com/flipkart/batchdemo/EventTag.java +++ b/app/src/main/java/com/flipkart/batchdemo/EventTag.java @@ -24,7 +24,7 @@ package com.flipkart.batchdemo; -import com.flipkart.batching.data.Tag; +import com.flipkart.batching.core.data.Tag; import com.google.gson.annotations.SerializedName; public class EventTag extends Tag { diff --git a/app/src/main/java/com/flipkart/batchdemo/MainActivity.java b/app/src/main/java/com/flipkart/batchdemo/MainActivity.java index 7d19ffa..02be24d 100644 --- a/app/src/main/java/com/flipkart/batchdemo/MainActivity.java +++ b/app/src/main/java/com/flipkart/batchdemo/MainActivity.java @@ -41,18 +41,18 @@ import android.view.View; import android.webkit.ValueCallback; -import com.flipkart.batching.Batch; import com.flipkart.batching.TagBatchManager; -import com.flipkart.batching.data.Tag; -import com.flipkart.batching.data.TagData; import com.flipkart.batching.listener.NetworkPersistedBatchReadyListener; import com.flipkart.batching.listener.PersistedBatchCallback; import com.flipkart.batching.listener.TrimmedBatchCallback; -import com.flipkart.batching.persistence.GsonSerializationStrategy; -import com.flipkart.batching.persistence.SerializationStrategy; import com.flipkart.batching.persistence.TapePersistenceStrategy; import com.flipkart.batching.strategy.SizeBatchingStrategy; -import com.flipkart.batching.strategy.TagBatchingStrategy; +import com.flipkart.batching.core.Batch; +import com.flipkart.batching.core.SerializationStrategy; +import com.flipkart.batching.core.batch.TagBatch; +import com.flipkart.batching.core.data.Tag; +import com.flipkart.batching.core.data.TagData; +import com.flipkart.batching.gson.GsonSerializationStrategy; import java.util.ArrayList; import java.util.Collections; @@ -133,14 +133,14 @@ public void onTrimmed(int oldSize, int newSize) { } }); - debugListener.setListener(new PersistedBatchCallback>() { + debugListener.setListener(new PersistedBatchCallback>() { @Override - public void onPersistFailure(TagBatchingStrategy.TagBatch batch, Exception e) { + public void onPersistFailure(TagBatch batch, Exception e) { } @Override - public void onPersistSuccess(TagBatchingStrategy.TagBatch batch) { + public void onPersistSuccess(TagBatch batch) { // SystemClock.sleep(2000); debugListener.finish(batch); Log.e("Debug", "Finish Called"); @@ -165,14 +165,14 @@ public void onTrimmed(int oldSize, int newSize) { } }); - dgListener.setListener(new PersistedBatchCallback>() { + dgListener.setListener(new PersistedBatchCallback>() { @Override - public void onPersistFailure(TagBatchingStrategy.TagBatch batch, Exception e) { + public void onPersistFailure(TagBatch batch, Exception e) { } @Override - public void onPersistSuccess(TagBatchingStrategy.TagBatch batch) { + public void onPersistSuccess(TagBatch batch) { //SystemClock.sleep(2000); dgListener.finish(batch); Log.e("Dg", "Finish Called"); diff --git a/batching-core/.gitignore b/batching-core/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/batching-core/.gitignore @@ -0,0 +1 @@ +/build diff --git a/batching-core/build.gradle b/batching-core/build.gradle new file mode 100644 index 0000000..c45da78 --- /dev/null +++ b/batching-core/build.gradle @@ -0,0 +1,28 @@ +apply plugin: 'com.android.library' + +dependencies { + compile fileTree(dir: 'libs', include: ['*.jar']) + compile 'com.android.support:appcompat-v7:25.0.1' + testCompile 'junit:junit:4.12' +} + +android { + compileSdkVersion 24 + buildToolsVersion '24' + + defaultConfig { + minSdkVersion 14 + targetSdkVersion 24 + versionCode 1 + versionName "1.0" + + testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} \ No newline at end of file diff --git a/batching-core/proguard-rules.pro b/batching-core/proguard-rules.pro new file mode 100644 index 0000000..3356a4c --- /dev/null +++ b/batching-core/proguard-rules.pro @@ -0,0 +1,17 @@ +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in /Users/anirudh.r/Library/Android/sdk/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the proguardFiles +# directive in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} diff --git a/batching-core/src/androidTest/java/com/flipkart/batching_core/ExampleInstrumentedTest.java b/batching-core/src/androidTest/java/com/flipkart/batching_core/ExampleInstrumentedTest.java new file mode 100644 index 0000000..7c5cc64 --- /dev/null +++ b/batching-core/src/androidTest/java/com/flipkart/batching_core/ExampleInstrumentedTest.java @@ -0,0 +1,26 @@ +package com.flipkart.batching.core; + +import android.content.Context; +import android.support.test.InstrumentationRegistry; +import android.support.test.runner.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.*; + +/** + * Instrumentation test, which will execute on an Android device. + * + * @see Testing documentation + */ +@RunWith(AndroidJUnit4.class) +public class ExampleInstrumentedTest { + @Test + public void useAppContext() throws Exception { + // Context of the app under test. + Context appContext = InstrumentationRegistry.getTargetContext(); + + assertEquals("com.flipkart.batching.core.test", appContext.getPackageName()); + } +} diff --git a/batching-core/src/main/AndroidManifest.xml b/batching-core/src/main/AndroidManifest.xml new file mode 100644 index 0000000..d5ea95e --- /dev/null +++ b/batching-core/src/main/AndroidManifest.xml @@ -0,0 +1,9 @@ + + + + + + + diff --git a/batching/src/main/java/com/flipkart/batching/Batch.java b/batching-core/src/main/java/com/flipkart/batching/core/Batch.java similarity index 97% rename from batching/src/main/java/com/flipkart/batching/Batch.java rename to batching-core/src/main/java/com/flipkart/batching/core/Batch.java index 9aaa0f5..00d46f0 100644 --- a/batching/src/main/java/com/flipkart/batching/Batch.java +++ b/batching-core/src/main/java/com/flipkart/batching/core/Batch.java @@ -22,10 +22,10 @@ * THE SOFTWARE. */ -package com.flipkart.batching; +package com.flipkart.batching.core; import java.util.Collection; public interface Batch { Collection getDataCollection(); -} +} \ No newline at end of file diff --git a/batching/src/main/java/com/flipkart/batching/BatchImpl.java b/batching-core/src/main/java/com/flipkart/batching/core/BatchImpl.java similarity index 94% rename from batching/src/main/java/com/flipkart/batching/BatchImpl.java rename to batching-core/src/main/java/com/flipkart/batching/core/BatchImpl.java index 9e9cf87..5820a7f 100644 --- a/batching/src/main/java/com/flipkart/batching/BatchImpl.java +++ b/batching-core/src/main/java/com/flipkart/batching/core/BatchImpl.java @@ -22,9 +22,7 @@ * THE SOFTWARE. */ -package com.flipkart.batching; - -import com.google.gson.annotations.SerializedName; +package com.flipkart.batching.core; import java.util.Collection; @@ -34,7 +32,6 @@ public class BatchImpl implements Batch { - @SerializedName("dataCollection") private DataCollection dataCollection; public BatchImpl(Collection dataCollection) { diff --git a/batching/src/main/java/com/flipkart/batching/Data.java b/batching-core/src/main/java/com/flipkart/batching/core/Data.java similarity index 93% rename from batching/src/main/java/com/flipkart/batching/Data.java rename to batching-core/src/main/java/com/flipkart/batching/core/Data.java index ed288aa..5a77280 100644 --- a/batching/src/main/java/com/flipkart/batching/Data.java +++ b/batching-core/src/main/java/com/flipkart/batching/core/Data.java @@ -22,10 +22,10 @@ * THE SOFTWARE. */ -package com.flipkart.batching; +package com.flipkart.batching.core; -import com.flipkart.batching.data.Tag; -import com.google.gson.annotations.SerializedName; + +import com.flipkart.batching.core.data.Tag; import java.io.Serializable; @@ -40,7 +40,6 @@ */ public abstract class Data implements Serializable { - @SerializedName("eventId") private long eventId; /** diff --git a/batching-core/src/main/java/com/flipkart/batching/core/DataCollection.java b/batching-core/src/main/java/com/flipkart/batching/core/DataCollection.java new file mode 100644 index 0000000..b6f170f --- /dev/null +++ b/batching-core/src/main/java/com/flipkart/batching/core/DataCollection.java @@ -0,0 +1,52 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016 Flipkart Internet Pvt. Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.flipkart.batching.core; + +import java.util.Collection; + +/** + * DataCollection class + */ + +public class DataCollection { + public Collection dataCollection; + + public DataCollection(Collection dataCollection) { + this.dataCollection = dataCollection; + } + + @Override + public boolean equals(Object o) { + if (o instanceof DataCollection) { + return dataCollection.equals(((DataCollection) o).dataCollection); + } + return super.equals(o); + } + + @Override + public int hashCode() { + return dataCollection == null ? 0 : dataCollection.hashCode(); + } +} diff --git a/batching/src/main/java/com/flipkart/batching/persistence/SerializationStrategy.java b/batching-core/src/main/java/com/flipkart/batching/core/SerializationStrategy.java similarity index 90% rename from batching/src/main/java/com/flipkart/batching/persistence/SerializationStrategy.java rename to batching-core/src/main/java/com/flipkart/batching/core/SerializationStrategy.java index cf3ec26..f99d706 100644 --- a/batching/src/main/java/com/flipkart/batching/persistence/SerializationStrategy.java +++ b/batching-core/src/main/java/com/flipkart/batching/core/SerializationStrategy.java @@ -22,20 +22,16 @@ * THE SOFTWARE. */ -package com.flipkart.batching.persistence; +package com.flipkart.batching.core; -import com.flipkart.batching.Batch; -import com.flipkart.batching.Data; -import com.flipkart.batching.exception.DeserializeException; -import com.flipkart.batching.exception.SerializeException; +import com.flipkart.batching.core.exception.DeserializeException; +import com.flipkart.batching.core.exception.SerializeException; import java.util.Collection; /** * This is an interface for serialization strategy. An implementation of this class * must override all it's methods. - * - * @see GsonSerializationStrategy */ public interface SerializationStrategy { @@ -79,4 +75,4 @@ public interface SerializationStrategy { Collection deserializeCollection(byte[] data) throws DeserializeException; T deserializeBatch(byte[] data) throws DeserializeException; -} +} \ No newline at end of file diff --git a/batching-core/src/main/java/com/flipkart/batching/core/batch/SizeBatch.java b/batching-core/src/main/java/com/flipkart/batching/core/batch/SizeBatch.java new file mode 100644 index 0000000..f7bcb3c --- /dev/null +++ b/batching-core/src/main/java/com/flipkart/batching/core/batch/SizeBatch.java @@ -0,0 +1,33 @@ +package com.flipkart.batching.core.batch; + + +import com.flipkart.batching.core.BatchImpl; +import com.flipkart.batching.core.Data; + +import java.util.Collection; + +public class SizeBatch extends BatchImpl { + private int maxBatchSize; + + public SizeBatch(Collection dataCollection, int maxBatchSize) { + super(dataCollection); + this.maxBatchSize = maxBatchSize; + } + + public int getMaxBatchSize() { + return maxBatchSize; + } + + @Override + public boolean equals(Object o) { + if (o instanceof SizeBatch) { + return ((SizeBatch) o).getMaxBatchSize() == maxBatchSize && super.equals(o); + } + return super.equals(o); + } + + @Override + public int hashCode() { + return 31 * super.hashCode() + maxBatchSize; + } +} \ No newline at end of file diff --git a/batching-core/src/main/java/com/flipkart/batching/core/batch/SizeTimeBatch.java b/batching-core/src/main/java/com/flipkart/batching/core/batch/SizeTimeBatch.java new file mode 100644 index 0000000..0edc69c --- /dev/null +++ b/batching-core/src/main/java/com/flipkart/batching/core/batch/SizeTimeBatch.java @@ -0,0 +1,44 @@ +package com.flipkart.batching.core.batch; + +import com.flipkart.batching.core.BatchImpl; +import com.flipkart.batching.core.Data; + +import java.util.Collection; + +public class SizeTimeBatch extends BatchImpl { + private int maxBatchSize; + private long timeOut; + + public SizeTimeBatch(Collection dataCollection, int maxBatchSize, long timeOut) { + super(dataCollection); + this.maxBatchSize = maxBatchSize; + this.timeOut = timeOut; + } + + public int getMaxBatchSize() { + return maxBatchSize; + } + + public long getTimeOut() { + return timeOut; + } + + @Override + public boolean equals(Object o) { + if (o instanceof SizeTimeBatch) { + return (((SizeTimeBatch) o).getMaxBatchSize() == maxBatchSize + && ((SizeTimeBatch) o).getTimeOut() == timeOut + && super.equals(o)); + + } + return super.equals(o); + } + + @Override + public int hashCode() { + int result = super.hashCode(); + result = 31 * result + maxBatchSize; + result = 31 * result + Long.valueOf(timeOut).hashCode(); + return result; + } +} \ No newline at end of file diff --git a/batching-core/src/main/java/com/flipkart/batching/core/batch/TagBatch.java b/batching-core/src/main/java/com/flipkart/batching/core/batch/TagBatch.java new file mode 100644 index 0000000..94bc10d --- /dev/null +++ b/batching-core/src/main/java/com/flipkart/batching/core/batch/TagBatch.java @@ -0,0 +1,32 @@ +package com.flipkart.batching.core.batch; + +import com.flipkart.batching.core.Batch; +import com.flipkart.batching.core.BatchImpl; +import com.flipkart.batching.core.data.Tag; +import com.flipkart.batching.core.data.TagData; + +public class TagBatch extends BatchImpl { + private Tag tag; + + public TagBatch(Tag tag, Batch batch) { + super(batch.getDataCollection()); + this.tag = tag; + } + + public Tag getTag() { + return tag; + } + + @Override + public boolean equals(Object o) { + if (o instanceof TagBatch) { + return ((TagBatch) o).getTag().equals(tag) && super.equals(o); + } + return super.equals(o); + } + + @Override + public int hashCode() { + return 31 * super.hashCode() + (getTag() == null ? 0 : getTag().hashCode()); + } +} \ No newline at end of file diff --git a/batching-core/src/main/java/com/flipkart/batching/core/batch/TimeBatch.java b/batching-core/src/main/java/com/flipkart/batching/core/batch/TimeBatch.java new file mode 100644 index 0000000..62facf5 --- /dev/null +++ b/batching-core/src/main/java/com/flipkart/batching/core/batch/TimeBatch.java @@ -0,0 +1,32 @@ +package com.flipkart.batching.core.batch; + +import com.flipkart.batching.core.BatchImpl; +import com.flipkart.batching.core.Data; + +import java.util.Collection; + +public class TimeBatch extends BatchImpl { + private long timeOut; + + public TimeBatch(Collection dataCollection, long timeOut) { + super(dataCollection); + this.timeOut = timeOut; + } + + public long getTimeOut() { + return timeOut; + } + + @Override + public boolean equals(Object o) { + if (o instanceof TimeBatch) { + return ((TimeBatch) o).getTimeOut() == timeOut && super.equals(o); + } + return super.equals(o); + } + + @Override + public int hashCode() { + return 31 * super.hashCode() + Long.valueOf(timeOut).hashCode(); + } +} \ No newline at end of file diff --git a/batching/src/main/java/com/flipkart/batching/data/EventData.java b/batching-core/src/main/java/com/flipkart/batching/core/data/EventData.java similarity index 94% rename from batching/src/main/java/com/flipkart/batching/data/EventData.java rename to batching-core/src/main/java/com/flipkart/batching/core/data/EventData.java index e1cbc6c..ab4f8b2 100644 --- a/batching/src/main/java/com/flipkart/batching/data/EventData.java +++ b/batching-core/src/main/java/com/flipkart/batching/core/data/EventData.java @@ -22,9 +22,9 @@ * THE SOFTWARE. */ -package com.flipkart.batching.data; +package com.flipkart.batching.core.data; -import com.flipkart.batching.Data; +import com.flipkart.batching.core.Data; /** * EventData class that extends {@link Data}. diff --git a/batching/src/main/java/com/flipkart/batching/data/Tag.java b/batching-core/src/main/java/com/flipkart/batching/core/data/Tag.java similarity index 89% rename from batching/src/main/java/com/flipkart/batching/data/Tag.java rename to batching-core/src/main/java/com/flipkart/batching/core/data/Tag.java index 5e307ab..723de17 100644 --- a/batching/src/main/java/com/flipkart/batching/data/Tag.java +++ b/batching-core/src/main/java/com/flipkart/batching/core/data/Tag.java @@ -22,19 +22,18 @@ * THE SOFTWARE. */ -package com.flipkart.batching.data; +package com.flipkart.batching.core.data; -import com.google.gson.annotations.SerializedName; +import com.flipkart.batching.core.Data; import java.io.Serializable; /** - * Tag represents a group of {@link com.flipkart.batching.Data} objects to batch together. + * Tag represents a group of {@link Data} objects to batch together. * It takes a {@link String} type ID as parameter in constructor. */ public class Tag implements Serializable { - @SerializedName("id") private String id; public Tag(String id) { @@ -62,4 +61,4 @@ public boolean equals(Object o) { public int hashCode() { return id.hashCode(); } -} +} \ No newline at end of file diff --git a/batching/src/main/java/com/flipkart/batching/data/TagData.java b/batching-core/src/main/java/com/flipkart/batching/core/data/TagData.java similarity index 92% rename from batching/src/main/java/com/flipkart/batching/data/TagData.java rename to batching-core/src/main/java/com/flipkart/batching/core/data/TagData.java index 4f31890..fad687c 100644 --- a/batching/src/main/java/com/flipkart/batching/data/TagData.java +++ b/batching-core/src/main/java/com/flipkart/batching/core/data/TagData.java @@ -22,10 +22,9 @@ * THE SOFTWARE. */ -package com.flipkart.batching.data; +package com.flipkart.batching.core.data; -import com.flipkart.batching.Data; -import com.google.gson.annotations.SerializedName; +import com.flipkart.batching.core.Data; /** * TagData Class that extends {@link Data}. @@ -33,7 +32,6 @@ */ public class TagData extends Data { - @SerializedName("tag") private final Tag tag; public TagData(Tag tag) { diff --git a/batching/src/main/java/com/flipkart/batching/exception/DeserializeException.java b/batching-core/src/main/java/com/flipkart/batching/core/exception/DeserializeException.java similarity index 97% rename from batching/src/main/java/com/flipkart/batching/exception/DeserializeException.java rename to batching-core/src/main/java/com/flipkart/batching/core/exception/DeserializeException.java index 97b5628..97d4e1f 100755 --- a/batching/src/main/java/com/flipkart/batching/exception/DeserializeException.java +++ b/batching-core/src/main/java/com/flipkart/batching/core/exception/DeserializeException.java @@ -22,7 +22,7 @@ * THE SOFTWARE. */ -package com.flipkart.batching.exception; +package com.flipkart.batching.core.exception; import java.io.IOException; diff --git a/batching/src/main/java/com/flipkart/batching/exception/SerializeException.java b/batching-core/src/main/java/com/flipkart/batching/core/exception/SerializeException.java similarity index 97% rename from batching/src/main/java/com/flipkart/batching/exception/SerializeException.java rename to batching-core/src/main/java/com/flipkart/batching/core/exception/SerializeException.java index 3af2487..1c73132 100755 --- a/batching/src/main/java/com/flipkart/batching/exception/SerializeException.java +++ b/batching-core/src/main/java/com/flipkart/batching/core/exception/SerializeException.java @@ -22,7 +22,7 @@ * THE SOFTWARE. */ -package com.flipkart.batching.exception; +package com.flipkart.batching.core.exception; import java.io.IOException; diff --git a/batching-core/src/main/res/values/strings.xml b/batching-core/src/main/res/values/strings.xml new file mode 100644 index 0000000..0072300 --- /dev/null +++ b/batching-core/src/main/res/values/strings.xml @@ -0,0 +1,3 @@ + + Batching Core + diff --git a/batching-core/src/test/java/com/flipkart/batching_core/Utils.java b/batching-core/src/test/java/com/flipkart/batching_core/Utils.java new file mode 100644 index 0000000..235a450 --- /dev/null +++ b/batching-core/src/test/java/com/flipkart/batching_core/Utils.java @@ -0,0 +1,107 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016 Flipkart Internet Pvt. Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.flipkart.batching.core; + +import com.flipkart.batching.core.Data; +import com.flipkart.batching.core.data.EventData; +import com.flipkart.batching.core.data.Tag; +import com.flipkart.batching.core.data.TagData; + +import java.util.ArrayList; + +/** + * Utils class for Test + */ +public class Utils { + + /** + * Method to create fake array list of Data. + * + * @param size + * @return dataList + */ + static Data eventData; + + public static ArrayList fakeCollection(int size) { + ArrayList dataList = new ArrayList<>(); + for (int i = 0; i < size; i++) { + eventData = new EventData(); + eventData.setEventId(System.currentTimeMillis() + System.nanoTime() + i); + dataList.add(eventData); + } + return dataList; + } + + public static ArrayList fakeAdsCollection(int size) { + ArrayList dataList = new ArrayList<>(); + for (int i = 0; i < size; i++) { + eventData = new EventData(); + eventData.setEventId(System.currentTimeMillis() + System.nanoTime() + i); + dataList.add(eventData); + } + return dataList; + } + + public static ArrayList fakeDebugCollection(int size) { + ArrayList dataList = new ArrayList<>(); + for (int i = 0; i < size; i++) { + eventData = new EventData(); + eventData.setEventId(System.currentTimeMillis() + System.nanoTime() + i); + dataList.add(eventData); + } + return dataList; + } + + public static ArrayList fakeBuisnessCollection(int size) { + ArrayList dataList = new ArrayList<>(); + for (int i = 0; i < size; i++) { + eventData = new EventData(); + eventData.setEventId(System.currentTimeMillis() + System.nanoTime() + i); + dataList.add(eventData); + } + return dataList; + } + + public static ArrayList fakeTagAdsCollection(int size) { + ArrayList dataList = new ArrayList<>(); + for (int i = 0; i < size; i++) { + TagData eventData = new TagData(new Tag("ADS")); + eventData.setEventId(System.currentTimeMillis() + System.nanoTime() + i); + dataList.add(eventData); + } + return dataList; + } + + public static ArrayList fakeTagDebugCollection(int size) { + ArrayList dataList = new ArrayList<>(); + for (int i = 0; i < size; i++) { + TagData eventData = new TagData(new Tag("DEBUG")); + eventData.setEventId(System.currentTimeMillis() + System.nanoTime() + i); + dataList.add(eventData); + } + return dataList; + } + +} diff --git a/batching-core/src/test/java/com/flipkart/batching_core/batch/SizeBatchTest.java b/batching-core/src/test/java/com/flipkart/batching_core/batch/SizeBatchTest.java new file mode 100644 index 0000000..eda3a74 --- /dev/null +++ b/batching-core/src/test/java/com/flipkart/batching_core/batch/SizeBatchTest.java @@ -0,0 +1,41 @@ +package com.flipkart.batching.core.batch; + +import junit.framework.Assert; + +import org.junit.Test; + +import java.util.Collections; + +/** + * Created by anirudh.r on 11/08/16. + * Test for {@link SizeBatch} + */ +public class SizeBatchTest { + + /** + * Test to verify the max batch size getters + * + * @throws Exception + */ + @Test + public void testMaxSize() throws Exception { + SizeBatch sizeBatch = new SizeBatch(Collections.EMPTY_LIST, 10); + int maxBatchSize = sizeBatch.getMaxBatchSize(); + Assert.assertTrue(maxBatchSize == 10); + } + + @Test + public void testSizeBatchEqualsMethod() throws Exception { + SizeBatch sizeBatch = new SizeBatch(Collections.EMPTY_LIST, 10); + SizeBatch sizeBatch1 = new SizeBatch(Collections.EMPTY_LIST, 10); + + //assert that both are equal since the sizes are same + Assert.assertTrue(sizeBatch.equals(sizeBatch1)); + + sizeBatch = new SizeBatch(Collections.EMPTY_LIST, 10); + sizeBatch1 = new SizeBatch(Collections.EMPTY_LIST, 11); + + //assert that both are not equal since the sizes are not same + Assert.assertTrue(!sizeBatch.equals(sizeBatch1)); + } +} diff --git a/batching-core/src/test/java/com/flipkart/batching_core/batch/SizeTimeBatchTest.java b/batching-core/src/test/java/com/flipkart/batching_core/batch/SizeTimeBatchTest.java new file mode 100644 index 0000000..5e2ce15 --- /dev/null +++ b/batching-core/src/test/java/com/flipkart/batching_core/batch/SizeTimeBatchTest.java @@ -0,0 +1,62 @@ +package com.flipkart.batching.core.batch; + +import junit.framework.Assert; + +import org.junit.Test; + +import java.util.Collections; + +/** + * Created by anirudh.r on 11/08/16. + * Test for {@link SizeTimeBatch} + */ +public class SizeTimeBatchTest { + + /** + * Test for max size batch + * + * @throws Exception + */ + @Test + public void testMaxSize() throws Exception { + SizeTimeBatch sizeTimeBatch = new SizeTimeBatch(Collections.EMPTY_LIST, 10, 1000); + int maxBatchSize = sizeTimeBatch.getMaxBatchSize(); + + //assert that the max batch size is 10 + Assert.assertTrue(maxBatchSize == 10); + } + + /** + * Test for timeout + * + * @throws Exception + */ + @Test + public void testTimeout() throws Exception { + SizeTimeBatch sizeTimeBatch = new SizeTimeBatch(Collections.EMPTY_LIST, 10, 1000); + long timeOut = sizeTimeBatch.getTimeOut(); + + //assert that the timeout is 1000 + Assert.assertTrue(timeOut == 1000); + } + + /** + * Test for equals method + * + * @throws Exception + */ + @Test + public void testSizeTimeBatchEqualsMethod() throws Exception { + SizeTimeBatch sizeTimeBatch = new SizeTimeBatch(Collections.EMPTY_LIST, 10, 100); + SizeTimeBatch sizeTimeBatch1 = new SizeTimeBatch(Collections.EMPTY_LIST, 10, 100); + + //assert that both are equal since the sizes and timeout are same + Assert.assertTrue(sizeTimeBatch.equals(sizeTimeBatch1)); + + sizeTimeBatch = new SizeTimeBatch(Collections.EMPTY_LIST, 10, 100); + sizeTimeBatch1 = new SizeTimeBatch(Collections.EMPTY_LIST, 10, 200); + + //assert that both are not equal since the time out are not same + Assert.assertTrue(!sizeTimeBatch.equals(sizeTimeBatch1)); + } +} diff --git a/batching-core/src/test/java/com/flipkart/batching_core/batch/TagBatchTest.java b/batching-core/src/test/java/com/flipkart/batching_core/batch/TagBatchTest.java new file mode 100644 index 0000000..84305c8 --- /dev/null +++ b/batching-core/src/test/java/com/flipkart/batching_core/batch/TagBatchTest.java @@ -0,0 +1,51 @@ +package com.flipkart.batching.core.batch; + +import com.flipkart.batching.core.data.Tag; + +import junit.framework.Assert; + +import org.junit.Test; + +import java.util.Collections; + +/** + * Created by anirudh.r on 11/08/16. + * Test for {@link TagBatch} + */ +public class TagBatchTest { + + /** + * Test for get tag + * + * @throws Exception + */ + @Test + public void testGetTag() throws Exception { + TagBatch tagBatch = new TagBatch(new Tag("test"), new SizeBatch(Collections.EMPTY_LIST, 10)); + Tag tag = tagBatch.getTag(); + String id = tag.getId(); + + //assert that the id are same + Assert.assertTrue(id.equals("test")); + } + + /** + * Test for equals method + * + * @throws Exception + */ + @Test + public void testEqualsMethod() throws Exception { + TagBatch tagBatch = new TagBatch(new Tag("test"), new SizeBatch(Collections.EMPTY_LIST, 10)); + TagBatch tagBatch1 = new TagBatch(new Tag("test"), new SizeBatch(Collections.EMPTY_LIST, 10)); + + //assert that both tag batch are same + Assert.assertTrue(tagBatch.equals(tagBatch1)); + + tagBatch = new TagBatch(new Tag("test"), new SizeBatch(Collections.EMPTY_LIST, 10)); + tagBatch1 = new TagBatch(new Tag("test1"), new SizeBatch(Collections.EMPTY_LIST, 10)); + + //assert that both tag batch are not same since they have diff tag + Assert.assertTrue(!tagBatch.equals(tagBatch1)); + } +} diff --git a/batching-core/src/test/java/com/flipkart/batching_core/batch/TimeBatchTest.java b/batching-core/src/test/java/com/flipkart/batching_core/batch/TimeBatchTest.java new file mode 100644 index 0000000..d0b4edb --- /dev/null +++ b/batching-core/src/test/java/com/flipkart/batching_core/batch/TimeBatchTest.java @@ -0,0 +1,48 @@ +package com.flipkart.batching.core.batch; + +import junit.framework.Assert; + +import org.junit.Test; + +import java.util.Collections; + +/** + * Created by anirudh.r on 11/08/16. + * Test for {@link TimeBatch} + */ +public class TimeBatchTest { + + /** + * Test for time out getters + * + * @throws Exception + */ + @Test + public void testTimeOut() throws Exception { + TimeBatch timeBatch = new TimeBatch(Collections.EMPTY_LIST, 1000); + long timeOut = timeBatch.getTimeOut(); + + //assert that time out is equal to 1000 + Assert.assertTrue(timeOut == 1000); + } + + /** + * Test for equals method in TimeBatch + * + * @throws Exception + */ + @Test + public void testEqualsMethod() throws Exception { + TimeBatch timeBatch = new TimeBatch(Collections.EMPTY_LIST, 1000); + TimeBatch timeBatch1 = new TimeBatch(Collections.EMPTY_LIST, 1000); + + //assert that both the batches are same + Assert.assertTrue(timeBatch.equals(timeBatch1)); + + timeBatch = new TimeBatch(Collections.EMPTY_LIST, 2000); + timeBatch1 = new TimeBatch(Collections.EMPTY_LIST, 1000); + + //assert that both the batches are not same + Assert.assertTrue(!timeBatch.equals(timeBatch1)); + } +} diff --git a/batching/src/test/java/com/flipkart/batching/data/BatchTest.java b/batching-core/src/test/java/com/flipkart/batching_core/data/BatchTest.java similarity index 90% rename from batching/src/test/java/com/flipkart/batching/data/BatchTest.java rename to batching-core/src/test/java/com/flipkart/batching_core/data/BatchTest.java index a68e27f..72a105d 100644 --- a/batching/src/test/java/com/flipkart/batching/data/BatchTest.java +++ b/batching-core/src/test/java/com/flipkart/batching_core/data/BatchTest.java @@ -22,12 +22,13 @@ * THE SOFTWARE. */ -package com.flipkart.batching.data; +package com.flipkart.batching.core.data; -import com.flipkart.Utils; -import com.flipkart.batching.Batch; -import com.flipkart.batching.BatchImpl; -import com.flipkart.batching.Data; + +import com.flipkart.batching.core.Batch; +import com.flipkart.batching.core.BatchImpl; +import com.flipkart.batching.core.Data; +import com.flipkart.batching.core.Utils; import junit.framework.Assert; diff --git a/batching/src/test/java/com/flipkart/batching/data/DataTest.java b/batching-core/src/test/java/com/flipkart/batching_core/data/DataTest.java similarity index 96% rename from batching/src/test/java/com/flipkart/batching/data/DataTest.java rename to batching-core/src/test/java/com/flipkart/batching_core/data/DataTest.java index 9e1c0d1..e205f5b 100644 --- a/batching/src/test/java/com/flipkart/batching/data/DataTest.java +++ b/batching-core/src/test/java/com/flipkart/batching_core/data/DataTest.java @@ -22,9 +22,9 @@ * THE SOFTWARE. */ -package com.flipkart.batching.data; +package com.flipkart.batching.core.data; -import com.flipkart.batching.Data; +import com.flipkart.batching.core.Data; import junit.framework.Assert; diff --git a/batching/src/test/java/com/flipkart/batching/data/TagTest.java b/batching-core/src/test/java/com/flipkart/batching_core/data/TagTest.java similarity index 98% rename from batching/src/test/java/com/flipkart/batching/data/TagTest.java rename to batching-core/src/test/java/com/flipkart/batching_core/data/TagTest.java index adb61c3..9778a4d 100644 --- a/batching/src/test/java/com/flipkart/batching/data/TagTest.java +++ b/batching-core/src/test/java/com/flipkart/batching_core/data/TagTest.java @@ -22,7 +22,7 @@ * THE SOFTWARE. */ -package com.flipkart.batching.data; +package com.flipkart.batching.core.data; import junit.framework.Assert; diff --git a/batching/src/test/java/com/flipkart/batching/exception/DeserializeExceptionTest.java b/batching-core/src/test/java/com/flipkart/batching_core/exception/DeserializeExceptionTest.java similarity index 97% rename from batching/src/test/java/com/flipkart/batching/exception/DeserializeExceptionTest.java rename to batching-core/src/test/java/com/flipkart/batching_core/exception/DeserializeExceptionTest.java index 424d761..27d4e42 100644 --- a/batching/src/test/java/com/flipkart/batching/exception/DeserializeExceptionTest.java +++ b/batching-core/src/test/java/com/flipkart/batching_core/exception/DeserializeExceptionTest.java @@ -22,7 +22,7 @@ * THE SOFTWARE. */ -package com.flipkart.batching.exception; +package com.flipkart.batching.core.exception; import junit.framework.Assert; diff --git a/batching/src/test/java/com/flipkart/batching/exception/SerializeExceptionTest.java b/batching-core/src/test/java/com/flipkart/batching_core/exception/SerializeExceptionTest.java similarity index 97% rename from batching/src/test/java/com/flipkart/batching/exception/SerializeExceptionTest.java rename to batching-core/src/test/java/com/flipkart/batching_core/exception/SerializeExceptionTest.java index a5c903b..70eaa29 100644 --- a/batching/src/test/java/com/flipkart/batching/exception/SerializeExceptionTest.java +++ b/batching-core/src/test/java/com/flipkart/batching_core/exception/SerializeExceptionTest.java @@ -22,7 +22,7 @@ * THE SOFTWARE. */ -package com.flipkart.batching.exception; +package com.flipkart.batching.core.exception; import junit.framework.Assert; diff --git a/batching-gson/.gitignore b/batching-gson/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/batching-gson/.gitignore @@ -0,0 +1 @@ +/build diff --git a/batching-gson/build.gradle b/batching-gson/build.gradle new file mode 100644 index 0000000..1834c4c --- /dev/null +++ b/batching-gson/build.gradle @@ -0,0 +1,43 @@ +apply plugin: 'com.android.library' +apply plugin: 'com.github.dcendents.android-maven' + +buildscript { + repositories { + maven { url 'http://repo1.maven.org/maven2' } + maven { url "https://plugins.gradle.org/m2/" } + } + dependencies { + classpath 'com.android.tools.build:gradle:2.2.2' + classpath 'org.jfrog.buildinfo:build-info-extractor-gradle:2.2.3' + classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5' + } +} + +dependencies { + compile fileTree(include: ['*.jar'], dir: 'libs') + compile 'com.google.code.gson:gson:2.4' + compile 'com.android.support:appcompat-v7:25.0.1' + compile project(':batching-core') + testCompile 'junit:junit:4.12' +} + +android { + compileSdkVersion 24 + buildToolsVersion '24' + + defaultConfig { + minSdkVersion 14 + targetSdkVersion 24 + versionCode 1 + versionName "1.0" + + testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} diff --git a/batching-gson/proguard-rules.pro b/batching-gson/proguard-rules.pro new file mode 100644 index 0000000..3356a4c --- /dev/null +++ b/batching-gson/proguard-rules.pro @@ -0,0 +1,17 @@ +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in /Users/anirudh.r/Library/Android/sdk/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the proguardFiles +# directive in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} diff --git a/batching-gson/src/androidTest/java/com/flipkart/batching_gson/ExampleInstrumentedTest.java b/batching-gson/src/androidTest/java/com/flipkart/batching_gson/ExampleInstrumentedTest.java new file mode 100644 index 0000000..f8e558f --- /dev/null +++ b/batching-gson/src/androidTest/java/com/flipkart/batching_gson/ExampleInstrumentedTest.java @@ -0,0 +1,26 @@ +package com.flipkart.batching.gson; + +import android.content.Context; +import android.support.test.InstrumentationRegistry; +import android.support.test.runner.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.*; + +/** + * Instrumentation test, which will execute on an Android device. + * + * @see Testing documentation + */ +@RunWith(AndroidJUnit4.class) +public class ExampleInstrumentedTest { + @Test + public void useAppContext() throws Exception { + // Context of the app under test. + Context appContext = InstrumentationRegistry.getTargetContext(); + + assertEquals("com.flipkart.batching.gson.test", appContext.getPackageName()); + } +} diff --git a/batching-gson/src/main/AndroidManifest.xml b/batching-gson/src/main/AndroidManifest.xml new file mode 100644 index 0000000..412b47d --- /dev/null +++ b/batching-gson/src/main/AndroidManifest.xml @@ -0,0 +1,9 @@ + + + + + + + diff --git a/batching/src/main/java/com/flipkart/batching/persistence/GsonSerializationStrategy.java b/batching-gson/src/main/java/com/flipkart/batching/gson/GsonSerializationStrategy.java similarity index 96% rename from batching/src/main/java/com/flipkart/batching/persistence/GsonSerializationStrategy.java rename to batching-gson/src/main/java/com/flipkart/batching/gson/GsonSerializationStrategy.java index 8c5452f..c5e280b 100644 --- a/batching/src/main/java/com/flipkart/batching/persistence/GsonSerializationStrategy.java +++ b/batching-gson/src/main/java/com/flipkart/batching/gson/GsonSerializationStrategy.java @@ -22,14 +22,16 @@ * THE SOFTWARE. */ -package com.flipkart.batching.persistence; - -import com.flipkart.batching.Batch; -import com.flipkart.batching.Data; -import com.flipkart.batching.DataCollection; -import com.flipkart.batching.exception.DeserializeException; -import com.flipkart.batching.exception.SerializeException; -import com.flipkart.batching.toolbox.RuntimeTypeAdapterFactory; +package com.flipkart.batching.gson; + +import com.flipkart.batching.gson.utils.Deserializer; +import com.flipkart.batching.gson.utils.Serializer; +import com.flipkart.batching.core.Batch; +import com.flipkart.batching.core.Data; +import com.flipkart.batching.core.DataCollection; +import com.flipkart.batching.core.SerializationStrategy; +import com.flipkart.batching.core.exception.DeserializeException; +import com.flipkart.batching.core.exception.SerializeException; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonArray; @@ -250,13 +252,12 @@ public void build() { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapterFactory(dataAdapter); gsonBuilder.registerTypeAdapterFactory(batchInfoAdapter); - gsonBuilder.registerTypeAdapter(DataCollection.class, new DataCollection.Serializer()); - gsonBuilder.registerTypeAdapter(DataCollection.class, new DataCollection.DeSerializer()); + gsonBuilder.registerTypeAdapter(DataCollection.class, new Serializer()); + gsonBuilder.registerTypeAdapter(DataCollection.class, new Deserializer()); gsonBuilder.registerTypeAdapter(JSONObject.class, new JSONObjectDeSerializer()); gsonBuilder.registerTypeAdapter(JSONObject.class, new JSONObjectSerializer()); gsonBuilder.registerTypeAdapter(JSONArray.class, new JSONArrayDeSerializer()); gsonBuilder.registerTypeAdapter(JSONArray.class, new JSONArraySerializer()); - //gsonBuilder.registerTypeAdapterFactory(collectionAdapter); gson = gsonBuilder.create(); } diff --git a/batching/src/main/java/com/flipkart/batching/toolbox/RuntimeTypeAdapterFactory.java b/batching-gson/src/main/java/com/flipkart/batching/gson/RuntimeTypeAdapterFactory.java similarity index 99% rename from batching/src/main/java/com/flipkart/batching/toolbox/RuntimeTypeAdapterFactory.java rename to batching-gson/src/main/java/com/flipkart/batching/gson/RuntimeTypeAdapterFactory.java index 3c90022..1815859 100644 --- a/batching/src/main/java/com/flipkart/batching/toolbox/RuntimeTypeAdapterFactory.java +++ b/batching-gson/src/main/java/com/flipkart/batching/gson/RuntimeTypeAdapterFactory.java @@ -22,7 +22,7 @@ * THE SOFTWARE. */ -package com.flipkart.batching.toolbox; +package com.flipkart.batching.gson; import com.google.gson.Gson; import com.google.gson.JsonElement; diff --git a/batching-gson/src/main/java/com/flipkart/batching/gson/utils/Deserializer.java b/batching-gson/src/main/java/com/flipkart/batching/gson/utils/Deserializer.java new file mode 100644 index 0000000..fe036cf --- /dev/null +++ b/batching-gson/src/main/java/com/flipkart/batching/gson/utils/Deserializer.java @@ -0,0 +1,28 @@ +package com.flipkart.batching.gson.utils; + +import com.flipkart.batching.core.Data; +import com.flipkart.batching.core.DataCollection; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonParseException; +import com.google.gson.reflect.TypeToken; + +import java.lang.reflect.Type; +import java.util.Collection; + +public class Deserializer implements JsonDeserializer { + @Override + public DataCollection deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + if (null != json && json.isJsonObject()) { + JsonElement dataCollectionJson = json.getAsJsonObject().get("dataCollection"); + if (null != dataCollectionJson) { + Type type = new TypeToken>() { + }.getType(); + Collection dataCollection = context.deserialize(dataCollectionJson, type); + return new DataCollection(dataCollection); + } + } + return null; + } +} \ No newline at end of file diff --git a/batching-gson/src/main/java/com/flipkart/batching/gson/utils/Serializer.java b/batching-gson/src/main/java/com/flipkart/batching/gson/utils/Serializer.java new file mode 100644 index 0000000..04e9ce6 --- /dev/null +++ b/batching-gson/src/main/java/com/flipkart/batching/gson/utils/Serializer.java @@ -0,0 +1,27 @@ +package com.flipkart.batching.gson.utils; + +import com.flipkart.batching.core.Data; +import com.flipkart.batching.core.DataCollection; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; +import com.google.gson.reflect.TypeToken; + +import java.lang.reflect.Type; +import java.util.Collection; + +public class Serializer implements JsonSerializer { + @Override + public JsonElement serialize(DataCollection src, Type typeOfSrc, JsonSerializationContext context) { + JsonObject result = new JsonObject(); + JsonElement dataCollectionArray = null; + if (null != src.dataCollection) { + Type type = new TypeToken>() { + }.getType(); + dataCollectionArray = context.serialize(src.dataCollection, type); + } + result.add("dataCollection", dataCollectionArray); + return result; + } +} \ No newline at end of file diff --git a/batching-gson/src/main/res/values/strings.xml b/batching-gson/src/main/res/values/strings.xml new file mode 100644 index 0000000..3e0630c --- /dev/null +++ b/batching-gson/src/main/res/values/strings.xml @@ -0,0 +1,3 @@ + + Batching Gson + diff --git a/batching/src/test/java/com/flipkart/batching/persistence/GsonSerializationTest.java b/batching-gson/src/test/java/com/flipkart/batching_gson/GsonSerializationTest.java similarity index 81% rename from batching/src/test/java/com/flipkart/batching/persistence/GsonSerializationTest.java rename to batching-gson/src/test/java/com/flipkart/batching_gson/GsonSerializationTest.java index a0e3f3b..357433f 100644 --- a/batching/src/test/java/com/flipkart/batching/persistence/GsonSerializationTest.java +++ b/batching-gson/src/test/java/com/flipkart/batching_gson/GsonSerializationTest.java @@ -22,16 +22,21 @@ * THE SOFTWARE. */ -package com.flipkart.batching.persistence; - -import com.flipkart.Utils; -import com.flipkart.batching.Batch; -import com.flipkart.batching.BatchImpl; -import com.flipkart.batching.BatchManager; -import com.flipkart.batching.Data; -import com.flipkart.batching.data.EventData; -import com.flipkart.batching.exception.DeserializeException; -import com.flipkart.batching.exception.SerializeException; +package com.flipkart.batching.gson; + +import com.flipkart.batching.gson.GsonSerializationStrategy; +import com.flipkart.batching.core.Batch; +import com.flipkart.batching.core.BatchImpl; +import com.flipkart.batching.core.Data; +import com.flipkart.batching.core.SerializationStrategy; +import com.flipkart.batching.core.batch.SizeBatch; +import com.flipkart.batching.core.batch.SizeTimeBatch; +import com.flipkart.batching.core.batch.TagBatch; +import com.flipkart.batching.core.batch.TimeBatch; +import com.flipkart.batching.core.data.EventData; +import com.flipkart.batching.core.data.TagData; +import com.flipkart.batching.core.exception.DeserializeException; +import com.flipkart.batching.core.exception.SerializeException; import com.google.gson.JsonSyntaxException; import junit.framework.Assert; @@ -48,7 +53,6 @@ */ public class GsonSerializationTest { - /** * Test the working of {@link GsonSerializationStrategy#serializeBatch(Batch)} * and {@link GsonSerializationStrategy#deserializeBatch(byte[])} @@ -57,7 +61,7 @@ public class GsonSerializationTest { public void testGSONSerialization() { GsonSerializationStrategy> serializationStrategy; serializationStrategy = new GsonSerializationStrategy<>(); - BatchManager.registerBuiltInTypes(serializationStrategy); + registerBuiltInTypes(serializationStrategy); serializationStrategy.build(); ArrayList dataCollection = Utils.fakeCollection(4); @@ -85,7 +89,7 @@ public void testIfExceptionThrownWhenCorrupt() throws SerializeException, Deseri GsonSerializationStrategy> serializationStrategy; ArrayList dataCollection = Utils.fakeCollection(4); serializationStrategy = new GsonSerializationStrategy<>(); - BatchManager.registerBuiltInTypes(serializationStrategy); + registerBuiltInTypes(serializationStrategy); serializationStrategy.build(); Batch batch = new BatchImpl<>(dataCollection); @@ -109,7 +113,7 @@ public void testCollectionSerialization() { GsonSerializationStrategy> serializationStrategy; ArrayList dataCollection = Utils.fakeCollection(4); serializationStrategy = new GsonSerializationStrategy<>(); - BatchManager.registerBuiltInTypes(serializationStrategy); + registerBuiltInTypes(serializationStrategy); serializationStrategy.build(); Collection fakeCollection = Utils.fakeCollection(4); @@ -133,7 +137,7 @@ public void testIfBuildNotCalled() { GsonSerializationStrategy> serializationStrategy; ArrayList dataCollection = Utils.fakeCollection(4); serializationStrategy = new GsonSerializationStrategy<>(); - BatchManager.registerBuiltInTypes(serializationStrategy); + registerBuiltInTypes(serializationStrategy); serializationStrategy.build(); Batch batch = new BatchImpl<>(dataCollection); @@ -157,7 +161,7 @@ public void testGSONSerializationForData() throws SerializeException, Deserializ //test to serialize hashmap GsonSerializationStrategy> serializationStrategy = new GsonSerializationStrategy<>(); serializationStrategy.registerDataType(Data.class); - BatchManager.registerBuiltInTypes(serializationStrategy); + registerBuiltInTypes(serializationStrategy); serializationStrategy.build(); HashMap hashMap = new HashMap<>(); @@ -176,4 +180,14 @@ public void testGSONSerializationForData() throws SerializeException, Deserializ data = serializationStrategy.deserializeData(serializedData); Assert.assertEquals(eventData, data); } + + private void registerBuiltInTypes(SerializationStrategy serializationStrategy) { + serializationStrategy.registerDataType(TagData.class); + serializationStrategy.registerBatch(BatchImpl.class); + serializationStrategy.registerDataType(EventData.class); + serializationStrategy.registerBatch(SizeBatch.class); + serializationStrategy.registerBatch(TimeBatch.class); + serializationStrategy.registerBatch(TagBatch.class); + serializationStrategy.registerBatch(SizeTimeBatch.class); + } } diff --git a/batching-gson/src/test/java/com/flipkart/batching_gson/Utils.java b/batching-gson/src/test/java/com/flipkart/batching_gson/Utils.java new file mode 100644 index 0000000..aaa1a9a --- /dev/null +++ b/batching-gson/src/test/java/com/flipkart/batching_gson/Utils.java @@ -0,0 +1,107 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016 Flipkart Internet Pvt. Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.flipkart.batching.gson; + +import com.flipkart.batching.core.Data; +import com.flipkart.batching.core.data.EventData; +import com.flipkart.batching.core.data.Tag; +import com.flipkart.batching.core.data.TagData; + +import java.util.ArrayList; + +/** + * Utils class for Test + */ +public class Utils { + + /** + * Method to create fake array list of Data. + * + * @param size + * @return dataList + */ + static Data eventData; + + public static ArrayList fakeCollection(int size) { + ArrayList dataList = new ArrayList<>(); + for (int i = 0; i < size; i++) { + eventData = new EventData(); + eventData.setEventId(System.currentTimeMillis() + System.nanoTime() + i); + dataList.add(eventData); + } + return dataList; + } + + public static ArrayList fakeAdsCollection(int size) { + ArrayList dataList = new ArrayList<>(); + for (int i = 0; i < size; i++) { + eventData = new EventData(); + eventData.setEventId(System.currentTimeMillis() + System.nanoTime() + i); + dataList.add(eventData); + } + return dataList; + } + + public static ArrayList fakeDebugCollection(int size) { + ArrayList dataList = new ArrayList<>(); + for (int i = 0; i < size; i++) { + eventData = new EventData(); + eventData.setEventId(System.currentTimeMillis() + System.nanoTime() + i); + dataList.add(eventData); + } + return dataList; + } + + public static ArrayList fakeBuisnessCollection(int size) { + ArrayList dataList = new ArrayList<>(); + for (int i = 0; i < size; i++) { + eventData = new EventData(); + eventData.setEventId(System.currentTimeMillis() + System.nanoTime() + i); + dataList.add(eventData); + } + return dataList; + } + + public static ArrayList fakeTagAdsCollection(int size) { + ArrayList dataList = new ArrayList<>(); + for (int i = 0; i < size; i++) { + TagData eventData = new TagData(new Tag("ADS")); + eventData.setEventId(System.currentTimeMillis() + System.nanoTime() + i); + dataList.add(eventData); + } + return dataList; + } + + public static ArrayList fakeTagDebugCollection(int size) { + ArrayList dataList = new ArrayList<>(); + for (int i = 0; i < size; i++) { + TagData eventData = new TagData(new Tag("DEBUG")); + eventData.setEventId(System.currentTimeMillis() + System.nanoTime() + i); + dataList.add(eventData); + } + return dataList; + } + +} diff --git a/batching/build.gradle b/batching/build.gradle index f1d213c..6dc6a95 100644 --- a/batching/build.gradle +++ b/batching/build.gradle @@ -28,7 +28,7 @@ apply plugin: "net.ltgt.errorprone" android { compileSdkVersion 24 - buildToolsVersion '24.0.0' + buildToolsVersion '24' lintOptions { abortOnError false @@ -51,19 +51,18 @@ android { testOptions { unitTests.returnDefaultValues = true } - - compileOptions { - sourceCompatibility JavaVersion.VERSION_1_8 - targetCompatibility JavaVersion.VERSION_1_8 - } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' testCompile 'org.mockito:mockito-core:1.10.19' - testCompile 'org.robolectric:robolectric:3.1' - compile 'com.google.code.gson:gson:2.4' + testCompile 'org.robolectric:robolectric:3.1.1' + testCompile project(':batching-gson') + //temp fix, will need to look into this dependency since batching-gson already has this dependency + testCompile 'com.google.code.gson:gson:2.4' + + compile project(':batching-core') compile 'org.jetbrains:annotations-java5:15.0' compile 'org.slf4j:slf4j-api:1.7.6' errorprone 'com.google.errorprone:error_prone_core:2.0.9' @@ -75,9 +74,9 @@ buildscript { maven { url "https://plugins.gradle.org/m2/" } } dependencies { - classpath 'com.android.tools.build:gradle:2.1.2' + classpath 'com.android.tools.build:gradle:2.2.2' classpath 'org.jfrog.buildinfo:build-info-extractor-gradle:2.2.3' - classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3' + classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5' classpath "net.ltgt.gradle:gradle-errorprone-plugin:0.0.8" } } \ No newline at end of file diff --git a/batching/src/main/java/com/flipkart/batching/BatchController.java b/batching/src/main/java/com/flipkart/batching/BatchController.java index 2c5a360..6da83a7 100644 --- a/batching/src/main/java/com/flipkart/batching/BatchController.java +++ b/batching/src/main/java/com/flipkart/batching/BatchController.java @@ -26,7 +26,9 @@ import android.os.Handler; -import com.flipkart.batching.persistence.SerializationStrategy; +import com.flipkart.batching.core.Batch; +import com.flipkart.batching.core.Data; +import com.flipkart.batching.core.SerializationStrategy; import java.util.Collection; diff --git a/batching/src/main/java/com/flipkart/batching/BatchManager.java b/batching/src/main/java/com/flipkart/batching/BatchManager.java index 28ef876..8b8ec7e 100644 --- a/batching/src/main/java/com/flipkart/batching/BatchManager.java +++ b/batching/src/main/java/com/flipkart/batching/BatchManager.java @@ -28,13 +28,16 @@ import android.os.Handler; import android.os.HandlerThread; -import com.flipkart.batching.data.EventData; -import com.flipkart.batching.data.TagData; -import com.flipkart.batching.persistence.SerializationStrategy; -import com.flipkart.batching.strategy.SizeBatchingStrategy; -import com.flipkart.batching.strategy.SizeTimeBatchingStrategy; -import com.flipkart.batching.strategy.TagBatchingStrategy; -import com.flipkart.batching.strategy.TimeBatchingStrategy; +import com.flipkart.batching.core.Batch; +import com.flipkart.batching.core.BatchImpl; +import com.flipkart.batching.core.Data; +import com.flipkart.batching.core.SerializationStrategy; +import com.flipkart.batching.core.batch.SizeBatch; +import com.flipkart.batching.core.batch.SizeTimeBatch; +import com.flipkart.batching.core.batch.TagBatch; +import com.flipkart.batching.core.batch.TimeBatch; +import com.flipkart.batching.core.data.EventData; +import com.flipkart.batching.core.data.TagData; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -93,10 +96,10 @@ public static void registerBuiltInTypes(SerializationStrategy serializationStrat serializationStrategy.registerDataType(TagData.class); serializationStrategy.registerBatch(BatchImpl.class); serializationStrategy.registerDataType(EventData.class); - serializationStrategy.registerBatch(SizeBatchingStrategy.SizeBatch.class); - serializationStrategy.registerBatch(TimeBatchingStrategy.TimeBatch.class); - serializationStrategy.registerBatch(TagBatchingStrategy.TagBatch.class); - serializationStrategy.registerBatch(SizeTimeBatchingStrategy.SizeTimeBatch.class); + serializationStrategy.registerBatch(SizeBatch.class); + serializationStrategy.registerBatch(TimeBatch.class); + serializationStrategy.registerBatch(TagBatch.class); + serializationStrategy.registerBatch(SizeTimeBatch.class); } private void registerSuppliedTypes(Builder builder, SerializationStrategy serializationStrategy) { diff --git a/batching/src/main/java/com/flipkart/batching/BatchingStrategy.java b/batching/src/main/java/com/flipkart/batching/BatchingStrategy.java index 7647dc6..7483d6d 100644 --- a/batching/src/main/java/com/flipkart/batching/BatchingStrategy.java +++ b/batching/src/main/java/com/flipkart/batching/BatchingStrategy.java @@ -27,6 +27,9 @@ import android.content.Context; import android.os.Handler; +import com.flipkart.batching.core.Batch; +import com.flipkart.batching.core.Data; + import java.util.Collection; /** diff --git a/batching/src/main/java/com/flipkart/batching/DataCollection.java b/batching/src/main/java/com/flipkart/batching/DataCollection.java deleted file mode 100644 index 9850f9d..0000000 --- a/batching/src/main/java/com/flipkart/batching/DataCollection.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016 Flipkart Internet Pvt. Ltd. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -package com.flipkart.batching; - -import com.google.gson.JsonDeserializationContext; -import com.google.gson.JsonDeserializer; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; -import com.google.gson.JsonSerializationContext; -import com.google.gson.JsonSerializer; -import com.google.gson.annotations.SerializedName; -import com.google.gson.reflect.TypeToken; - -import java.lang.reflect.Type; -import java.util.Collection; - -/** - * DataCollection class - */ - -public class DataCollection { - @SerializedName("dataCollection") - Collection dataCollection; - - DataCollection(Collection dataCollection) { - this.dataCollection = dataCollection; - } - - @Override - public boolean equals(Object o) { - if (o instanceof DataCollection) { - return dataCollection.equals(((DataCollection) o).dataCollection); - } - return super.equals(o); - } - - @Override - public int hashCode() { - return dataCollection == null ? 0 : dataCollection.hashCode(); - } - - public static class Serializer implements JsonSerializer { - @Override - public JsonElement serialize(DataCollection src, Type typeOfSrc, JsonSerializationContext context) { - JsonObject result = new JsonObject(); - JsonElement dataCollectionArray = null; - if (null != src.dataCollection) { - Type type = new TypeToken>() { - }.getType(); - dataCollectionArray = context.serialize(src.dataCollection, type); - } - result.add("dataCollection", dataCollectionArray); - return result; - } - } - - public static class DeSerializer implements JsonDeserializer { - @Override - public DataCollection deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { - if (null != json && json.isJsonObject()) { - JsonElement dataCollectionJson = json.getAsJsonObject().get("dataCollection"); - if (null != dataCollectionJson) { - Type type = new TypeToken>() { - }.getType(); - Collection dataCollection = context.deserialize(dataCollectionJson, type); - return new DataCollection(dataCollection); - } - } - return null; - } - } -} diff --git a/batching/src/main/java/com/flipkart/batching/OnBatchReadyListener.java b/batching/src/main/java/com/flipkart/batching/OnBatchReadyListener.java index 8860e87..07c51bd 100644 --- a/batching/src/main/java/com/flipkart/batching/OnBatchReadyListener.java +++ b/batching/src/main/java/com/flipkart/batching/OnBatchReadyListener.java @@ -25,6 +25,9 @@ package com.flipkart.batching; +import com.flipkart.batching.core.Batch; +import com.flipkart.batching.core.Data; + public interface OnBatchReadyListener> { void onReady(BatchingStrategy causingStrategy, T batch); } diff --git a/batching/src/main/java/com/flipkart/batching/TagBatchManager.java b/batching/src/main/java/com/flipkart/batching/TagBatchManager.java index bb504fd..c0d7d34 100644 --- a/batching/src/main/java/com/flipkart/batching/TagBatchManager.java +++ b/batching/src/main/java/com/flipkart/batching/TagBatchManager.java @@ -28,16 +28,20 @@ import android.os.Handler; import android.os.HandlerThread; -import com.flipkart.batching.data.EventData; -import com.flipkart.batching.data.Tag; -import com.flipkart.batching.data.TagData; import com.flipkart.batching.listener.NetworkPersistedBatchReadyListener; import com.flipkart.batching.listener.TagBatchReadyListener; -import com.flipkart.batching.persistence.SerializationStrategy; -import com.flipkart.batching.strategy.SizeBatchingStrategy; -import com.flipkart.batching.strategy.SizeTimeBatchingStrategy; import com.flipkart.batching.strategy.TagBatchingStrategy; -import com.flipkart.batching.strategy.TimeBatchingStrategy; +import com.flipkart.batching.core.Batch; +import com.flipkart.batching.core.BatchImpl; +import com.flipkart.batching.core.Data; +import com.flipkart.batching.core.SerializationStrategy; +import com.flipkart.batching.core.batch.SizeBatch; +import com.flipkart.batching.core.batch.SizeTimeBatch; +import com.flipkart.batching.core.batch.TagBatch; +import com.flipkart.batching.core.batch.TimeBatch; +import com.flipkart.batching.core.data.EventData; +import com.flipkart.batching.core.data.Tag; +import com.flipkart.batching.core.data.TagData; import java.util.ArrayList; import java.util.Collection; @@ -93,10 +97,10 @@ public static void registerBuiltInTypes(SerializationStrategy serializationStrat serializationStrategy.registerDataType(TagData.class); serializationStrategy.registerBatch(BatchImpl.class); serializationStrategy.registerDataType(EventData.class); - serializationStrategy.registerBatch(SizeBatchingStrategy.SizeBatch.class); - serializationStrategy.registerBatch(TimeBatchingStrategy.TimeBatch.class); - serializationStrategy.registerBatch(TagBatchingStrategy.TagBatch.class); - serializationStrategy.registerBatch(SizeTimeBatchingStrategy.SizeTimeBatch.class); + serializationStrategy.registerBatch(SizeBatch.class); + serializationStrategy.registerBatch(TimeBatch.class); + serializationStrategy.registerBatch(TagBatch.class); + serializationStrategy.registerBatch(SizeTimeBatch.class); } void initialize(TagBatchManager tagBatchManager, Context context, OnBatchReadyListener onBatchReadyListener, Handler handler) { @@ -136,7 +140,6 @@ void assignEventIds(Collection dataCollection) { } } - @Override public Handler getHandler() { return this.handler; diff --git a/batching/src/main/java/com/flipkart/batching/listener/NetworkPersistedBatchReadyListener.java b/batching/src/main/java/com/flipkart/batching/listener/NetworkPersistedBatchReadyListener.java index e915e82..84919e1 100644 --- a/batching/src/main/java/com/flipkart/batching/listener/NetworkPersistedBatchReadyListener.java +++ b/batching/src/main/java/com/flipkart/batching/listener/NetworkPersistedBatchReadyListener.java @@ -33,10 +33,10 @@ import android.os.Handler; import android.webkit.ValueCallback; -import com.flipkart.batching.Batch; import com.flipkart.batching.BatchingStrategy; -import com.flipkart.batching.Data; -import com.flipkart.batching.persistence.SerializationStrategy; +import com.flipkart.batching.core.Batch; +import com.flipkart.batching.core.Data; +import com.flipkart.batching.core.SerializationStrategy; import org.slf4j.LoggerFactory; diff --git a/batching/src/main/java/com/flipkart/batching/listener/PersistedBatchCallback.java b/batching/src/main/java/com/flipkart/batching/listener/PersistedBatchCallback.java index 380b4d6..3b13bb8 100644 --- a/batching/src/main/java/com/flipkart/batching/listener/PersistedBatchCallback.java +++ b/batching/src/main/java/com/flipkart/batching/listener/PersistedBatchCallback.java @@ -24,7 +24,7 @@ package com.flipkart.batching.listener; -import com.flipkart.batching.Batch; +import com.flipkart.batching.core.Batch; /** * Persisted Batch Callback @@ -35,4 +35,4 @@ public interface PersistedBatchCallback { void onPersistSuccess(T batch); void onFinish(); -} +} \ No newline at end of file diff --git a/batching/src/main/java/com/flipkart/batching/listener/PersistedBatchReadyListener.java b/batching/src/main/java/com/flipkart/batching/listener/PersistedBatchReadyListener.java index 7ce96e5..191db15 100644 --- a/batching/src/main/java/com/flipkart/batching/listener/PersistedBatchReadyListener.java +++ b/batching/src/main/java/com/flipkart/batching/listener/PersistedBatchReadyListener.java @@ -26,16 +26,16 @@ import android.os.Handler; -import com.flipkart.batching.Batch; import com.flipkart.batching.BatchingStrategy; -import com.flipkart.batching.Data; import com.flipkart.batching.OnBatchReadyListener; import com.flipkart.batching.persistence.BatchObjectConverter; -import com.flipkart.batching.persistence.SerializationStrategy; import com.flipkart.batching.tape.InMemoryObjectQueue; import com.flipkart.batching.tape.ObjectQueue; import com.flipkart.batching.toolbox.LenientFileObjectQueue; import com.flipkart.batching.toolbox.LenientQueueFile; +import com.flipkart.batching.core.Batch; +import com.flipkart.batching.core.Data; +import com.flipkart.batching.core.SerializationStrategy; import org.jetbrains.annotations.Nullable; import org.slf4j.LoggerFactory; @@ -259,4 +259,4 @@ private void callQueueEnd() { listener.onFinish(); } } -} +} \ No newline at end of file diff --git a/batching/src/main/java/com/flipkart/batching/listener/TagBatchReadyListener.java b/batching/src/main/java/com/flipkart/batching/listener/TagBatchReadyListener.java index 6d2abac..1f6c002 100644 --- a/batching/src/main/java/com/flipkart/batching/listener/TagBatchReadyListener.java +++ b/batching/src/main/java/com/flipkart/batching/listener/TagBatchReadyListener.java @@ -26,9 +26,9 @@ import com.flipkart.batching.BatchingStrategy; import com.flipkart.batching.OnBatchReadyListener; -import com.flipkart.batching.data.Tag; -import com.flipkart.batching.data.TagData; -import com.flipkart.batching.strategy.TagBatchingStrategy; +import com.flipkart.batching.core.batch.TagBatch; +import com.flipkart.batching.core.data.Tag; +import com.flipkart.batching.core.data.TagData; import java.util.HashMap; import java.util.Map; @@ -36,27 +36,27 @@ /** * TagBatchReadyListener that implements {@link OnBatchReadyListener}. */ -public class TagBatchReadyListener implements OnBatchReadyListener> { - private Map>> tagOnBatchReadyListenerMap; +public class TagBatchReadyListener implements OnBatchReadyListener> { + private Map>> tagOnBatchReadyListenerMap; public TagBatchReadyListener() { tagOnBatchReadyListenerMap = new HashMap<>(); } - public Map>> getTagOnBatchReadyListenerMap() { + public Map>> getTagOnBatchReadyListenerMap() { return tagOnBatchReadyListenerMap; } - public void addListenerForTag(Tag tag, OnBatchReadyListener> listener) { + public void addListenerForTag(Tag tag, OnBatchReadyListener> listener) { tagOnBatchReadyListenerMap.put(tag, listener); } - private OnBatchReadyListener> getListenerByTag(Tag tag) { + private OnBatchReadyListener> getListenerByTag(Tag tag) { return tagOnBatchReadyListenerMap.get(tag); } @Override - public void onReady(BatchingStrategy> causingStrategy, TagBatchingStrategy.TagBatch batch) { + public void onReady(BatchingStrategy> causingStrategy, TagBatch batch) { getListenerByTag(batch.getTag()).onReady(causingStrategy, batch); } -} +} \ No newline at end of file diff --git a/batching/src/main/java/com/flipkart/batching/listener/TrimPersistedBatchReadyListener.java b/batching/src/main/java/com/flipkart/batching/listener/TrimPersistedBatchReadyListener.java index 469de22..bae230a 100644 --- a/batching/src/main/java/com/flipkart/batching/listener/TrimPersistedBatchReadyListener.java +++ b/batching/src/main/java/com/flipkart/batching/listener/TrimPersistedBatchReadyListener.java @@ -26,10 +26,10 @@ import android.os.Handler; -import com.flipkart.batching.Batch; import com.flipkart.batching.BatchingStrategy; -import com.flipkart.batching.Data; -import com.flipkart.batching.persistence.SerializationStrategy; +import com.flipkart.batching.core.Batch; +import com.flipkart.batching.core.Data; +import com.flipkart.batching.core.SerializationStrategy; import org.slf4j.LoggerFactory; diff --git a/batching/src/main/java/com/flipkart/batching/listener/TrimmedBatchCallback.java b/batching/src/main/java/com/flipkart/batching/listener/TrimmedBatchCallback.java index 4b3fc18..d18ee1b 100644 --- a/batching/src/main/java/com/flipkart/batching/listener/TrimmedBatchCallback.java +++ b/batching/src/main/java/com/flipkart/batching/listener/TrimmedBatchCallback.java @@ -29,4 +29,4 @@ */ public interface TrimmedBatchCallback { void onTrimmed(int oldSize, int newSize); -} +} \ No newline at end of file diff --git a/batching/src/main/java/com/flipkart/batching/persistence/BatchObjectConverter.java b/batching/src/main/java/com/flipkart/batching/persistence/BatchObjectConverter.java index acbf7b9..23126a8 100644 --- a/batching/src/main/java/com/flipkart/batching/persistence/BatchObjectConverter.java +++ b/batching/src/main/java/com/flipkart/batching/persistence/BatchObjectConverter.java @@ -24,9 +24,10 @@ package com.flipkart.batching.persistence; -import com.flipkart.batching.Batch; -import com.flipkart.batching.Data; import com.flipkart.batching.tape.FileObjectQueue; +import com.flipkart.batching.core.Batch; +import com.flipkart.batching.core.Data; +import com.flipkart.batching.core.SerializationStrategy; import java.io.IOException; import java.io.OutputStream; diff --git a/batching/src/main/java/com/flipkart/batching/persistence/DataObjectConverter.java b/batching/src/main/java/com/flipkart/batching/persistence/DataObjectConverter.java index a50ac10..20348f8 100644 --- a/batching/src/main/java/com/flipkart/batching/persistence/DataObjectConverter.java +++ b/batching/src/main/java/com/flipkart/batching/persistence/DataObjectConverter.java @@ -24,9 +24,10 @@ package com.flipkart.batching.persistence; -import com.flipkart.batching.Batch; -import com.flipkart.batching.Data; import com.flipkart.batching.tape.FileObjectQueue; +import com.flipkart.batching.core.Batch; +import com.flipkart.batching.core.Data; +import com.flipkart.batching.core.SerializationStrategy; import java.io.IOException; import java.io.OutputStream; diff --git a/batching/src/main/java/com/flipkart/batching/persistence/DatabaseHelper.java b/batching/src/main/java/com/flipkart/batching/persistence/DatabaseHelper.java index efe7021..f3c0575 100644 --- a/batching/src/main/java/com/flipkart/batching/persistence/DatabaseHelper.java +++ b/batching/src/main/java/com/flipkart/batching/persistence/DatabaseHelper.java @@ -31,10 +31,11 @@ import android.database.sqlite.SQLiteOpenHelper; import android.text.TextUtils; -import com.flipkart.batching.Batch; -import com.flipkart.batching.Data; -import com.flipkart.batching.exception.DeserializeException; -import com.flipkart.batching.exception.SerializeException; +import com.flipkart.batching.core.Batch; +import com.flipkart.batching.core.Data; +import com.flipkart.batching.core.SerializationStrategy; +import com.flipkart.batching.core.exception.DeserializeException; +import com.flipkart.batching.core.exception.SerializeException; import java.util.ArrayList; import java.util.Collection; diff --git a/batching/src/main/java/com/flipkart/batching/persistence/InMemoryPersistenceStrategy.java b/batching/src/main/java/com/flipkart/batching/persistence/InMemoryPersistenceStrategy.java index 54978f1..2095616 100644 --- a/batching/src/main/java/com/flipkart/batching/persistence/InMemoryPersistenceStrategy.java +++ b/batching/src/main/java/com/flipkart/batching/persistence/InMemoryPersistenceStrategy.java @@ -24,7 +24,7 @@ package com.flipkart.batching.persistence; -import com.flipkart.batching.Data; +import com.flipkart.batching.core.Data; import java.util.ArrayList; import java.util.Collection; diff --git a/batching/src/main/java/com/flipkart/batching/persistence/PersistenceStrategy.java b/batching/src/main/java/com/flipkart/batching/persistence/PersistenceStrategy.java index d0d8d50..695130d 100644 --- a/batching/src/main/java/com/flipkart/batching/persistence/PersistenceStrategy.java +++ b/batching/src/main/java/com/flipkart/batching/persistence/PersistenceStrategy.java @@ -24,7 +24,7 @@ package com.flipkart.batching.persistence; -import com.flipkart.batching.Data; +import com.flipkart.batching.core.Data; import java.util.Collection; diff --git a/batching/src/main/java/com/flipkart/batching/persistence/SQLPersistenceStrategy.java b/batching/src/main/java/com/flipkart/batching/persistence/SQLPersistenceStrategy.java index b288acb..2c3b6ff 100644 --- a/batching/src/main/java/com/flipkart/batching/persistence/SQLPersistenceStrategy.java +++ b/batching/src/main/java/com/flipkart/batching/persistence/SQLPersistenceStrategy.java @@ -26,10 +26,11 @@ import android.content.Context; -import com.flipkart.batching.Batch; -import com.flipkart.batching.Data; -import com.flipkart.batching.exception.DeserializeException; -import com.flipkart.batching.exception.SerializeException; +import com.flipkart.batching.core.Batch; +import com.flipkart.batching.core.Data; +import com.flipkart.batching.core.SerializationStrategy; +import com.flipkart.batching.core.exception.DeserializeException; +import com.flipkart.batching.core.exception.SerializeException; import org.slf4j.LoggerFactory; diff --git a/batching/src/main/java/com/flipkart/batching/persistence/TagBasedPersistenceStrategy.java b/batching/src/main/java/com/flipkart/batching/persistence/TagBasedPersistenceStrategy.java index 6010a06..6f49d6b 100644 --- a/batching/src/main/java/com/flipkart/batching/persistence/TagBasedPersistenceStrategy.java +++ b/batching/src/main/java/com/flipkart/batching/persistence/TagBasedPersistenceStrategy.java @@ -24,9 +24,9 @@ package com.flipkart.batching.persistence; -import com.flipkart.batching.Data; -import com.flipkart.batching.data.Tag; -import com.flipkart.batching.data.TagData; +import com.flipkart.batching.core.Data; +import com.flipkart.batching.core.data.Tag; +import com.flipkart.batching.core.data.TagData; import java.util.Collection; import java.util.Iterator; diff --git a/batching/src/main/java/com/flipkart/batching/persistence/TapePersistenceStrategy.java b/batching/src/main/java/com/flipkart/batching/persistence/TapePersistenceStrategy.java index 66f93a3..7989867 100644 --- a/batching/src/main/java/com/flipkart/batching/persistence/TapePersistenceStrategy.java +++ b/batching/src/main/java/com/flipkart/batching/persistence/TapePersistenceStrategy.java @@ -24,13 +24,14 @@ package com.flipkart.batching.persistence; -import com.flipkart.batching.Batch; -import com.flipkart.batching.Data; import com.flipkart.batching.tape.FileObjectQueue; import com.flipkart.batching.tape.InMemoryObjectQueue; import com.flipkart.batching.tape.ObjectQueue; import com.flipkart.batching.toolbox.LenientFileObjectQueue; import com.flipkart.batching.toolbox.LenientQueueFile; +import com.flipkart.batching.core.Batch; +import com.flipkart.batching.core.Data; +import com.flipkart.batching.core.SerializationStrategy; import org.slf4j.LoggerFactory; diff --git a/batching/src/main/java/com/flipkart/batching/strategy/BaseBatchingStrategy.java b/batching/src/main/java/com/flipkart/batching/strategy/BaseBatchingStrategy.java index 5a6898c..9a05c0e 100644 --- a/batching/src/main/java/com/flipkart/batching/strategy/BaseBatchingStrategy.java +++ b/batching/src/main/java/com/flipkart/batching/strategy/BaseBatchingStrategy.java @@ -27,12 +27,12 @@ import android.content.Context; import android.os.Handler; -import com.flipkart.batching.Batch; import com.flipkart.batching.BatchController; import com.flipkart.batching.BatchingStrategy; -import com.flipkart.batching.Data; import com.flipkart.batching.OnBatchReadyListener; import com.flipkart.batching.persistence.PersistenceStrategy; +import com.flipkart.batching.core.Batch; +import com.flipkart.batching.core.Data; import java.util.Collection; diff --git a/batching/src/main/java/com/flipkart/batching/strategy/SizeBatchingStrategy.java b/batching/src/main/java/com/flipkart/batching/strategy/SizeBatchingStrategy.java index 8311f3b..713545a 100644 --- a/batching/src/main/java/com/flipkart/batching/strategy/SizeBatchingStrategy.java +++ b/batching/src/main/java/com/flipkart/batching/strategy/SizeBatchingStrategy.java @@ -27,12 +27,11 @@ import android.content.Context; import android.os.Handler; -import com.flipkart.batching.BatchImpl; import com.flipkart.batching.BatchingStrategy; -import com.flipkart.batching.Data; import com.flipkart.batching.OnBatchReadyListener; import com.flipkart.batching.persistence.PersistenceStrategy; -import com.google.gson.annotations.SerializedName; +import com.flipkart.batching.core.Data; +import com.flipkart.batching.core.batch.SizeBatch; import java.util.Collection; @@ -43,7 +42,7 @@ * {@link PersistenceStrategy} and calls {@link #onReadyListener} when the batch reaches the * maxBatchSize limit. */ -public class SizeBatchingStrategy extends BaseBatchingStrategy> { +public class SizeBatchingStrategy extends BaseBatchingStrategy> { private int currentBatchSize; private int maxBatchSize; @@ -75,8 +74,7 @@ public void flush(boolean forced) { } @Override - public void onInitialized(Context context, - OnBatchReadyListener> onBatchReadyListener, Handler handler) { + public void onInitialized(Context context, OnBatchReadyListener> onBatchReadyListener, Handler handler) { super.onInitialized(context, onBatchReadyListener, handler); } @@ -88,31 +86,4 @@ public void onInitialized(Context context, protected boolean isBatchReady() { return currentBatchSize >= maxBatchSize; } - - public static class SizeBatch extends BatchImpl { - @SerializedName("maxBatchSize") - private int maxBatchSize; - - public SizeBatch(Collection dataCollection, int maxBatchSize) { - super(dataCollection); - this.maxBatchSize = maxBatchSize; - } - - public int getMaxBatchSize() { - return maxBatchSize; - } - - @Override - public boolean equals(Object o) { - if (o instanceof SizeBatch) { - return ((SizeBatch) o).getMaxBatchSize() == maxBatchSize && super.equals(o); - } - return super.equals(o); - } - - @Override - public int hashCode() { - return 31 * super.hashCode() + maxBatchSize; - } - } } diff --git a/batching/src/main/java/com/flipkart/batching/strategy/SizeTimeBatchingStrategy.java b/batching/src/main/java/com/flipkart/batching/strategy/SizeTimeBatchingStrategy.java index 3137e23..2885300 100644 --- a/batching/src/main/java/com/flipkart/batching/strategy/SizeTimeBatchingStrategy.java +++ b/batching/src/main/java/com/flipkart/batching/strategy/SizeTimeBatchingStrategy.java @@ -27,12 +27,10 @@ import android.content.Context; import android.os.Handler; -import com.flipkart.batching.Batch; -import com.flipkart.batching.BatchImpl; -import com.flipkart.batching.Data; import com.flipkart.batching.OnBatchReadyListener; import com.flipkart.batching.persistence.PersistenceStrategy; -import com.google.gson.annotations.SerializedName; +import com.flipkart.batching.core.Data; +import com.flipkart.batching.core.batch.SizeTimeBatch; import java.util.Collection; @@ -40,7 +38,7 @@ * Created by kushal.sharma on 18/04/16. * Time Size Batching Strategy */ -public class SizeTimeBatchingStrategy extends BaseBatchingStrategy> { +public class SizeTimeBatchingStrategy extends BaseBatchingStrategy> { private int currentBatchSize; private int maxBatchSize; private long timeOut; @@ -118,44 +116,4 @@ private void startTimer() { private void stopTimer() { handler.removeCallbacks(runnable); } - - public static class SizeTimeBatch extends BatchImpl { - @SerializedName("maxBatchSize") - private int maxBatchSize; - @SerializedName("timeOut") - private long timeOut; - - public SizeTimeBatch(Collection dataCollection, int maxBatchSize, long timeOut) { - super(dataCollection); - this.maxBatchSize = maxBatchSize; - this.timeOut = timeOut; - } - - public int getMaxBatchSize() { - return maxBatchSize; - } - - public long getTimeOut() { - return timeOut; - } - - @Override - public boolean equals(Object o) { - if (o instanceof SizeTimeBatch) { - return (((SizeTimeBatch) o).getMaxBatchSize() == maxBatchSize - && ((SizeTimeBatch) o).getTimeOut() == timeOut - && super.equals(o)); - - } - return super.equals(o); - } - - @Override - public int hashCode() { - int result = super.hashCode(); - result = 31 * result + maxBatchSize; - result = 31 * result + Long.valueOf(timeOut).hashCode(); - return result; - } - } } diff --git a/batching/src/main/java/com/flipkart/batching/strategy/TagBatchingStrategy.java b/batching/src/main/java/com/flipkart/batching/strategy/TagBatchingStrategy.java index 25e45a3..c90c6a1 100644 --- a/batching/src/main/java/com/flipkart/batching/strategy/TagBatchingStrategy.java +++ b/batching/src/main/java/com/flipkart/batching/strategy/TagBatchingStrategy.java @@ -27,14 +27,13 @@ import android.content.Context; import android.os.Handler; -import com.flipkart.batching.Batch; -import com.flipkart.batching.BatchImpl; import com.flipkart.batching.BatchingStrategy; -import com.flipkart.batching.Data; import com.flipkart.batching.OnBatchReadyListener; -import com.flipkart.batching.data.Tag; -import com.flipkart.batching.data.TagData; -import com.google.gson.annotations.SerializedName; +import com.flipkart.batching.core.Batch; +import com.flipkart.batching.core.Data; +import com.flipkart.batching.core.batch.TagBatch; +import com.flipkart.batching.core.data.Tag; +import com.flipkart.batching.core.data.TagData; import java.util.Collection; import java.util.Collections; @@ -53,7 +52,7 @@ * @see TimeBatchingStrategy * @see TagBatchingStrategy */ -public class TagBatchingStrategy implements BatchingStrategy> { +public class TagBatchingStrategy implements BatchingStrategy> { private Map>> batchingStrategyMap = new HashMap<>(); private boolean initialized = false; @@ -119,32 +118,4 @@ public void flush(boolean forced) { public void addTagStrategy(Tag tag, BatchingStrategy> strategy) { batchingStrategyMap.put(tag, strategy); } - - - public static class TagBatch extends BatchImpl { - @SerializedName("tag") - private Tag tag; - - public TagBatch(Tag tag, Batch batch) { - super(batch.getDataCollection()); - this.tag = tag; - } - - public Tag getTag() { - return tag; - } - - @Override - public boolean equals(Object o) { - if (o instanceof TagBatch) { - return ((TagBatch) o).getTag().equals(tag) && super.equals(o); - } - return super.equals(o); - } - - @Override - public int hashCode() { - return 31 * super.hashCode() + (getTag() == null ? 0 : getTag().hashCode()); - } - } } diff --git a/batching/src/main/java/com/flipkart/batching/strategy/TimeBatchingStrategy.java b/batching/src/main/java/com/flipkart/batching/strategy/TimeBatchingStrategy.java index 89f08ab..ea60cea 100644 --- a/batching/src/main/java/com/flipkart/batching/strategy/TimeBatchingStrategy.java +++ b/batching/src/main/java/com/flipkart/batching/strategy/TimeBatchingStrategy.java @@ -27,13 +27,11 @@ import android.content.Context; import android.os.Handler; -import com.flipkart.batching.Batch; -import com.flipkart.batching.BatchImpl; import com.flipkart.batching.BatchingStrategy; -import com.flipkart.batching.Data; import com.flipkart.batching.OnBatchReadyListener; import com.flipkart.batching.persistence.PersistenceStrategy; -import com.google.gson.annotations.SerializedName; +import com.flipkart.batching.core.Data; +import com.flipkart.batching.core.batch.TimeBatch; import java.util.Collection; @@ -44,7 +42,7 @@ * {@link PersistenceStrategy}, starts/reset the timer whenever {@link Data} objects are pushed * and calls {@link #onReadyListener} when timeOut happens. */ -public class TimeBatchingStrategy extends BaseBatchingStrategy> { +public class TimeBatchingStrategy extends BaseBatchingStrategy> { private long timeOut; private Handler handler; private Runnable runnable = new Runnable() { @@ -112,32 +110,5 @@ private void startTimer() { private void stopTimer() { handler.removeCallbacks(runnable); } - - public static class TimeBatch extends BatchImpl { - @SerializedName("timeOut") - private long timeOut; - - public TimeBatch(Collection dataCollection, long timeOut) { - super(dataCollection); - this.timeOut = timeOut; - } - - public long getTimeOut() { - return timeOut; - } - - @Override - public boolean equals(Object o) { - if (o instanceof TimeBatch) { - return ((TimeBatch) o).getTimeOut() == timeOut && super.equals(o); - } - return super.equals(o); - } - - @Override - public int hashCode() { - return 31 * super.hashCode() + Long.valueOf(timeOut).hashCode(); - } - } } diff --git a/batching/src/main/java/com/flipkart/batching/toolbox/SizeTimeStrategyFactory.java b/batching/src/main/java/com/flipkart/batching/toolbox/SizeTimeStrategyFactory.java index fa8a932..54a285a 100644 --- a/batching/src/main/java/com/flipkart/batching/toolbox/SizeTimeStrategyFactory.java +++ b/batching/src/main/java/com/flipkart/batching/toolbox/SizeTimeStrategyFactory.java @@ -26,14 +26,14 @@ import android.content.Context; -import com.flipkart.batching.Data; -import com.flipkart.batching.data.Tag; import com.flipkart.batching.persistence.InMemoryPersistenceStrategy; import com.flipkart.batching.persistence.SQLPersistenceStrategy; -import com.flipkart.batching.persistence.SerializationStrategy; import com.flipkart.batching.persistence.TagBasedPersistenceStrategy; import com.flipkart.batching.persistence.TapePersistenceStrategy; import com.flipkart.batching.strategy.SizeTimeBatchingStrategy; +import com.flipkart.batching.core.Data; +import com.flipkart.batching.core.SerializationStrategy; +import com.flipkart.batching.core.data.Tag; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; diff --git a/batching/src/test/java/com/flipkart/Utils.java b/batching/src/test/java/com/flipkart/Utils.java index 70c5893..fe5c02a 100644 --- a/batching/src/test/java/com/flipkart/Utils.java +++ b/batching/src/test/java/com/flipkart/Utils.java @@ -24,10 +24,10 @@ package com.flipkart; -import com.flipkart.batching.Data; -import com.flipkart.batching.data.EventData; -import com.flipkart.batching.data.Tag; -import com.flipkart.batching.data.TagData; +import com.flipkart.batching.core.Data; +import com.flipkart.batching.core.data.EventData; +import com.flipkart.batching.core.data.Tag; +import com.flipkart.batching.core.data.TagData; import java.util.ArrayList; diff --git a/batching/src/test/java/com/flipkart/batching/BaseTestClass.java b/batching/src/test/java/com/flipkart/batching/BaseTestClass.java index 41be4d2..a638389 100644 --- a/batching/src/test/java/com/flipkart/batching/BaseTestClass.java +++ b/batching/src/test/java/com/flipkart/batching/BaseTestClass.java @@ -24,6 +24,8 @@ package com.flipkart.batching; +import org.junit.After; + import java.io.File; /** @@ -40,7 +42,7 @@ public class BaseTestClass { */ public String createRandomString() { double random = Math.random(); - return "Random" + random; + return "Random" + random + TEST_FILE_SUFFIX; } /** @@ -77,5 +79,21 @@ public void deleteRandomFiles() { } } folder.delete(); + + folder = new File(""); + fList = folder.listFiles(); + if (fList != null) { + for (File testFile : fList) { + if (testFile.getName().endsWith(TEST_FILE_SUFFIX)) { + testFile.delete(); + } + } + } } + + @After + public void tearDownBase() { + deleteRandomFiles(); + } + } diff --git a/batching/src/test/java/com/flipkart/batching/BatchManagerTest.java b/batching/src/test/java/com/flipkart/batching/BatchManagerTest.java index 49c9206..142e796 100644 --- a/batching/src/test/java/com/flipkart/batching/BatchManagerTest.java +++ b/batching/src/test/java/com/flipkart/batching/BatchManagerTest.java @@ -34,14 +34,17 @@ import com.flipkart.batching.listener.NetworkPersistedBatchReadyListener; import com.flipkart.batching.listener.NetworkPersistedBatchReadyListener.NetworkBatchListener; import com.flipkart.batching.listener.TrimPersistedBatchReadyListener; -import com.flipkart.batching.persistence.GsonSerializationStrategy; import com.flipkart.batching.persistence.PersistenceStrategy; -import com.flipkart.batching.persistence.SerializationStrategy; import com.flipkart.batching.persistence.TapePersistenceStrategy; import com.flipkart.batching.strategy.BaseBatchingStrategy; import com.flipkart.batching.strategy.SizeBatchingStrategy; import com.flipkart.batching.strategy.SizeTimeBatchingStrategy; import com.flipkart.batching.tape.ObjectQueue; +import com.flipkart.batching.gson.GsonSerializationStrategy; +import com.flipkart.batching.core.Batch; +import com.flipkart.batching.core.BatchImpl; +import com.flipkart.batching.core.Data; +import com.flipkart.batching.core.SerializationStrategy; import junit.framework.Assert; @@ -286,8 +289,8 @@ public void testReInitialized() { ShadowLooper shadowLooper = Shadows.shadowOf(Looper.getMainLooper()); Handler handler = new Handler(); - String filePath = createRandomString(); - String filePath1 = createRandomString(); + String filePath = createRandomFile().getPath(); + String filePath1 = createRandomFile().getPath(); SerializationStrategy> serializationStrategy = new GsonSerializationStrategy<>(); TapePersistenceStrategy persistenceStrategy = new TapePersistenceStrategy<>(filePath1, serializationStrategy); Context context = RuntimeEnvironment.application; @@ -360,4 +363,6 @@ public boolean isNetworkConnected(Context context) { Assert.assertTrue(outputData.size() == 6); } + + } \ No newline at end of file diff --git a/batching/src/test/java/com/flipkart/batching/listener/NetworkPersistedBatchReadyTest.java b/batching/src/test/java/com/flipkart/batching/listener/NetworkPersistedBatchReadyTest.java index 284a321..bdbad26 100644 --- a/batching/src/test/java/com/flipkart/batching/listener/NetworkPersistedBatchReadyTest.java +++ b/batching/src/test/java/com/flipkart/batching/listener/NetworkPersistedBatchReadyTest.java @@ -34,13 +34,14 @@ import com.flipkart.Utils; import com.flipkart.batching.BaseTestClass; -import com.flipkart.batching.Batch; import com.flipkart.batching.BatchManager; import com.flipkart.batching.BuildConfig; -import com.flipkart.batching.Data; -import com.flipkart.batching.exception.SerializeException; -import com.flipkart.batching.persistence.GsonSerializationStrategy; -import com.flipkart.batching.persistence.SerializationStrategy; +import com.flipkart.batching.gson.GsonSerializationStrategy; +import com.flipkart.batching.core.Batch; +import com.flipkart.batching.core.Data; +import com.flipkart.batching.core.SerializationStrategy; +import com.flipkart.batching.core.batch.SizeBatch; +import com.flipkart.batching.core.exception.SerializeException; import com.flipkart.batching.strategy.SizeBatchingStrategy; import com.flipkart.batching.tape.ObjectQueue; @@ -98,14 +99,14 @@ public void test5XXRetryPolicy() throws IOException { Handler handler = new Handler(looper); ShadowLooper shadowLooper = Shadows.shadowOf(looper); SizeBatchingStrategy strategy = mock(SizeBatchingStrategy.class); - SizeBatchingStrategy.SizeBatch firstBatch = new SizeBatchingStrategy.SizeBatch<>(Utils.fakeCollection(5), 5); + SizeBatch firstBatch = new SizeBatch<>(Utils.fakeCollection(5), 5); SerializationStrategy serializationStrategy = new GsonSerializationStrategy(); BatchManager.registerBuiltInTypes(serializationStrategy); serializationStrategy.build(); TrimmedBatchCallback trimmedBatchCallback = mock(TrimmedBatchCallback.class); MockNetworkPersistedBatchReadyListener networkBatchListener = spy(new MockNetworkPersistedBatchReadyListener(new NetworkPersistedBatchReadyListener.NetworkRequestResponse(true, ERROR_CODE_5XX), handler, callbackIdle, context)); - NetworkPersistedBatchReadyListener networkPersistedBatchReadyListener = new NetworkPersistedBatchReadyListener(context, createRandomString(), serializationStrategy, handler, networkBatchListener, maxRetryCount, 50, 10, TrimPersistedBatchReadyListener.MODE_TRIM_AT_START, trimmedBatchCallback); + NetworkPersistedBatchReadyListener networkPersistedBatchReadyListener = new NetworkPersistedBatchReadyListener(context, createRandomFile().getPath(), serializationStrategy, handler, networkBatchListener, maxRetryCount, 50, 10, TrimPersistedBatchReadyListener.MODE_TRIM_AT_START, trimmedBatchCallback); networkPersistedBatchReadyListener.onReady(strategy, firstBatch); shadowLooper.runToEndOfTasks(); @@ -168,14 +169,14 @@ public void test4XXRetryPolicy() throws IOException { Handler handler = new Handler(looper); ShadowLooper shadowLooper = Shadows.shadowOf(looper); SizeBatchingStrategy strategy = mock(SizeBatchingStrategy.class); - SizeBatchingStrategy.SizeBatch firstBatch = new SizeBatchingStrategy.SizeBatch<>(Utils.fakeCollection(5), 5); + SizeBatch firstBatch = new SizeBatch<>(Utils.fakeCollection(5), 5); SerializationStrategy serializationStrategy = new GsonSerializationStrategy(); BatchManager.registerBuiltInTypes(serializationStrategy); serializationStrategy.build(); TrimmedBatchCallback trimmedBatchCallback = mock(TrimmedBatchCallback.class); MockNetworkPersistedBatchReadyListener networkBatchListener = spy(new MockNetworkPersistedBatchReadyListener(new NetworkPersistedBatchReadyListener.NetworkRequestResponse(true, ERROR_CODE_4XX), handler, callbackIdle, context)); - NetworkPersistedBatchReadyListener networkPersistedBatchReadyListener = new NetworkPersistedBatchReadyListener(context, createRandomString(), serializationStrategy, handler, networkBatchListener, maxRetryCount, 50, 10, TrimPersistedBatchReadyListener.MODE_TRIM_AT_START, trimmedBatchCallback); + NetworkPersistedBatchReadyListener networkPersistedBatchReadyListener = new NetworkPersistedBatchReadyListener(context, createRandomFile().getPath(), serializationStrategy, handler, networkBatchListener, maxRetryCount, 50, 10, TrimPersistedBatchReadyListener.MODE_TRIM_AT_START, trimmedBatchCallback); networkPersistedBatchReadyListener.onReady(strategy, firstBatch); shadowLooper.runToEndOfTasks(); //verify that it gets called once @@ -204,14 +205,14 @@ public void test2XXRetryPolicy() throws IOException { Handler handler = new Handler(looper); ShadowLooper shadowLooper = Shadows.shadowOf(looper); SizeBatchingStrategy strategy = mock(SizeBatchingStrategy.class); - SizeBatchingStrategy.SizeBatch firstBatch = new SizeBatchingStrategy.SizeBatch<>(Utils.fakeCollection(5), 5); + SizeBatch firstBatch = new SizeBatch<>(Utils.fakeCollection(5), 5); SerializationStrategy serializationStrategy = new GsonSerializationStrategy(); BatchManager.registerBuiltInTypes(serializationStrategy); serializationStrategy.build(); TrimmedBatchCallback trimmedBatchCallback = mock(TrimmedBatchCallback.class); MockNetworkPersistedBatchReadyListener networkBatchListener = spy(new MockNetworkPersistedBatchReadyListener(new NetworkPersistedBatchReadyListener.NetworkRequestResponse(true, ERROR_CODE_2XX), handler, callbackIdle, context)); - NetworkPersistedBatchReadyListener networkPersistedBatchReadyListener = new NetworkPersistedBatchReadyListener(context, createRandomString(), serializationStrategy, handler, networkBatchListener, 5, 50, 10, TrimPersistedBatchReadyListener.MODE_TRIM_AT_START, trimmedBatchCallback); + NetworkPersistedBatchReadyListener networkPersistedBatchReadyListener = new NetworkPersistedBatchReadyListener(context, createRandomFile().getPath(), serializationStrategy, handler, networkBatchListener, 5, 50, 10, TrimPersistedBatchReadyListener.MODE_TRIM_AT_START, trimmedBatchCallback); networkPersistedBatchReadyListener.onReady(strategy, firstBatch); shadowLooper.runToEndOfTasks(); //verify that it gets called once @@ -237,14 +238,14 @@ public void testNetworkBroadcast() throws IOException { Handler handler = new Handler(looper); ShadowLooper shadowLooper = Shadows.shadowOf(looper); SizeBatchingStrategy strategy = mock(SizeBatchingStrategy.class); - SizeBatchingStrategy.SizeBatch firstBatch = new SizeBatchingStrategy.SizeBatch<>(Utils.fakeCollection(5), 3); + SizeBatch firstBatch = new SizeBatch<>(Utils.fakeCollection(5), 3); SerializationStrategy serializationStrategy = new GsonSerializationStrategy(); BatchManager.registerBuiltInTypes(serializationStrategy); serializationStrategy.build(); TrimmedBatchCallback trimmedBatchCallback = mock(TrimmedBatchCallback.class); MockNetworkPersistedBatchReadyListener networkBatchListener = spy(new MockNetworkPersistedBatchReadyListener(new NetworkPersistedBatchReadyListener.NetworkRequestResponse(true, ERROR_CODE_2XX), handler, callbackIdle, context)); - NetworkPersistedBatchReadyListener networkPersistedBatchReadyListener = new NetworkPersistedBatchReadyListener(context, createRandomString(), serializationStrategy, handler, networkBatchListener, 5, 50, 10, TrimPersistedBatchReadyListener.MODE_TRIM_AT_START, trimmedBatchCallback); + NetworkPersistedBatchReadyListener networkPersistedBatchReadyListener = new NetworkPersistedBatchReadyListener(context, createRandomFile().getPath(), serializationStrategy, handler, networkBatchListener, 5, 50, 10, TrimPersistedBatchReadyListener.MODE_TRIM_AT_START, trimmedBatchCallback); networkPersistedBatchReadyListener.onReady(strategy, firstBatch); shadowLooper.runToEndOfTasks(); @@ -258,7 +259,7 @@ public void testNetworkBroadcast() throws IOException { //PURPOSE : if network broadcast is received, perform any pending requests which were paused due to no network networkBatchListener.setMockedNetworkConnected(false); //simulating network not connected - SizeBatchingStrategy.SizeBatch secondBatch = new SizeBatchingStrategy.SizeBatch<>(Utils.fakeCollection(5), 4); + SizeBatch secondBatch = new SizeBatch<>(Utils.fakeCollection(5), 4); networkPersistedBatchReadyListener.onReady(strategy, secondBatch); shadowLooper.idle(100); // no new request is sent since no network exists @@ -296,22 +297,22 @@ public void testReinitialize() { NetworkPersistedBatchReadyListener.NetworkRequestResponse networkRequestResponse = new NetworkPersistedBatchReadyListener.NetworkRequestResponse(false, 500); MockNetworkPersistedBatchReadyListener networkBatchListener = spy(new MockNetworkPersistedBatchReadyListener(networkRequestResponse, handler, callbackIdle, context)); - NetworkPersistedBatchReadyListener networkPersistedBatchReadyListener = new NetworkPersistedBatchReadyListener(context, createRandomString(), serializationStrategy, handler, networkBatchListener, 1, 50, 10, TrimPersistedBatchReadyListener.MODE_TRIM_AT_START, trimmedBatchCallback); - SizeBatchingStrategy.SizeBatch firstBatch = new SizeBatchingStrategy.SizeBatch<>(Utils.fakeCollection(5), 3); + NetworkPersistedBatchReadyListener networkPersistedBatchReadyListener = new NetworkPersistedBatchReadyListener(context, createRandomFile().getPath(), serializationStrategy, handler, networkBatchListener, 1, 50, 10, TrimPersistedBatchReadyListener.MODE_TRIM_AT_START, trimmedBatchCallback); + SizeBatch firstBatch = new SizeBatch<>(Utils.fakeCollection(5), 3); networkPersistedBatchReadyListener.onReady(strategy, firstBatch); shadowLooper.runToEndOfTasks(); - SizeBatchingStrategy.SizeBatch secondBatch = new SizeBatchingStrategy.SizeBatch<>(Utils.fakeCollection(5), 3); + SizeBatch secondBatch = new SizeBatch<>(Utils.fakeCollection(5), 3); networkPersistedBatchReadyListener.onReady(strategy, secondBatch); shadowLooper.runToEndOfTasks(); ObjectQueue> oldQueueFile = networkPersistedBatchReadyListener.getQueueFile(); //reinitialize - networkPersistedBatchReadyListener = new NetworkPersistedBatchReadyListener(context, createRandomString(), serializationStrategy, handler, networkBatchListener, 5, 50, 10, TrimPersistedBatchReadyListener.MODE_TRIM_AT_START, trimmedBatchCallback); + networkPersistedBatchReadyListener = new NetworkPersistedBatchReadyListener(context, createRandomFile().getPath(), serializationStrategy, handler, networkBatchListener, 5, 50, 10, TrimPersistedBatchReadyListener.MODE_TRIM_AT_START, trimmedBatchCallback); networkPersistedBatchReadyListener.setQueueFile(oldQueueFile); //since 2 queuefiles cannot point to same disk file, we had to do this networkRequestResponse.complete = true; networkRequestResponse.httpErrorCode = 200; - SizeBatchingStrategy.SizeBatch thirdBatch = new SizeBatchingStrategy.SizeBatch<>(Utils.fakeCollection(5), 3); + SizeBatch thirdBatch = new SizeBatch<>(Utils.fakeCollection(5), 3); networkPersistedBatchReadyListener.onReady(strategy, thirdBatch); shadowLooper.runToEndOfTasks(); //all retries finished @@ -332,7 +333,7 @@ public void testRetryPolicy() { ShadowLooper shadowLooper = Shadows.shadowOf(Looper.getMainLooper()); Handler handler = new Handler(); SizeBatchingStrategy strategy = mock(SizeBatchingStrategy.class); - SizeBatchingStrategy.SizeBatch firstBatch = new SizeBatchingStrategy.SizeBatch<>(Utils.fakeCollection(5), 5); + SizeBatch firstBatch = new SizeBatch<>(Utils.fakeCollection(5), 5); SerializationStrategy serializationStrategy = new GsonSerializationStrategy(); BatchManager.registerBuiltInTypes(serializationStrategy); serializationStrategy.build(); @@ -340,7 +341,7 @@ public void testRetryPolicy() { TrimmedBatchCallback trimmedBatchCallback = mock(TrimmedBatchCallback.class); MockNetworkPersistedBatchReadyListener networkBatchListener = spy(new MockNetworkPersistedBatchReadyListener(requestResponse, handler, callbackIdle, context)); - NetworkPersistedBatchReadyListener networkPersistedBatchReadyListener = new NetworkPersistedBatchReadyListener(context, createRandomString(), serializationStrategy, handler, networkBatchListener, retryCount, 50, 10, TrimPersistedBatchReadyListener.MODE_TRIM_AT_START, trimmedBatchCallback); + NetworkPersistedBatchReadyListener networkPersistedBatchReadyListener = new NetworkPersistedBatchReadyListener(context, createRandomFile().getPath(), serializationStrategy, handler, networkBatchListener, retryCount, 50, 10, TrimPersistedBatchReadyListener.MODE_TRIM_AT_START, trimmedBatchCallback); networkPersistedBatchReadyListener.onReady(strategy, firstBatch); shadowLooper.runToEndOfTasks(); @@ -362,7 +363,7 @@ public void testRetryPolicy() { verify(networkBatchListener, times(4)).performNetworkRequest(eq(firstBatch), any(ValueCallback.class)); sendFakeNetworkBroadcast(context); - SizeBatchingStrategy.SizeBatch secondBatch = new SizeBatchingStrategy.SizeBatch<>(Utils.fakeCollection(5), 5); + SizeBatch secondBatch = new SizeBatch<>(Utils.fakeCollection(5), 5); networkPersistedBatchReadyListener.onReady(strategy, secondBatch); shadowLooper.idle(); @@ -395,7 +396,7 @@ public void testFinishCalledIfRemoveAfterMaxRetryTrue() { ShadowLooper shadowLooper = Shadows.shadowOf(Looper.getMainLooper()); Handler handler = new Handler(); SizeBatchingStrategy strategy = mock(SizeBatchingStrategy.class); - SizeBatchingStrategy.SizeBatch firstBatch = new SizeBatchingStrategy.SizeBatch<>(Utils.fakeCollection(5), 5); + SizeBatch firstBatch = new SizeBatch<>(Utils.fakeCollection(5), 5); SerializationStrategy serializationStrategy = new GsonSerializationStrategy(); BatchManager.registerBuiltInTypes(serializationStrategy); serializationStrategy.build(); @@ -403,7 +404,7 @@ public void testFinishCalledIfRemoveAfterMaxRetryTrue() { TrimmedBatchCallback trimmedBatchCallback = mock(TrimmedBatchCallback.class); MockNetworkPersistedBatchReadyListener networkBatchListener = spy(new MockNetworkPersistedBatchReadyListener(requestResponse, handler, callbackIdle, context)); - NetworkPersistedBatchReadyListener networkPersistedBatchReadyListener = new NetworkPersistedBatchReadyListener(context, createRandomString(), serializationStrategy, handler, networkBatchListener, retryCount, 50, 10, TrimPersistedBatchReadyListener.MODE_TRIM_AT_START, trimmedBatchCallback); + NetworkPersistedBatchReadyListener networkPersistedBatchReadyListener = new NetworkPersistedBatchReadyListener(context, createRandomFile().getPath(), serializationStrategy, handler, networkBatchListener, retryCount, 50, 10, TrimPersistedBatchReadyListener.MODE_TRIM_AT_START, trimmedBatchCallback); networkPersistedBatchReadyListener.onReady(strategy, firstBatch); networkPersistedBatchReadyListener.setCallFinishAfterMaxRetry(true); shadowLooper.runToEndOfTasks(); @@ -447,7 +448,7 @@ public void testRetryTimeOut() { ShadowLooper shadowLooper = Shadows.shadowOf(Looper.getMainLooper()); Handler handler = new Handler(); SizeBatchingStrategy strategy = mock(SizeBatchingStrategy.class); - SizeBatchingStrategy.SizeBatch firstBatch = new SizeBatchingStrategy.SizeBatch<>(Utils.fakeCollection(5), 5); + SizeBatch firstBatch = new SizeBatch<>(Utils.fakeCollection(5), 5); SerializationStrategy serializationStrategy = new GsonSerializationStrategy(); BatchManager.registerBuiltInTypes(serializationStrategy); serializationStrategy.build(); @@ -455,7 +456,7 @@ public void testRetryTimeOut() { TrimmedBatchCallback trimmedBatchCallback = mock(TrimmedBatchCallback.class); MockNetworkPersistedBatchReadyListener networkBatchListener = spy(new MockNetworkPersistedBatchReadyListener(requestResponse, handler, callbackIdle, context)); - NetworkPersistedBatchReadyListener networkPersistedBatchReadyListener = new NetworkPersistedBatchReadyListener(context, createRandomString(), serializationStrategy, handler, networkBatchListener, retryCount, 50, 10, TrimPersistedBatchReadyListener.MODE_TRIM_AT_START, trimmedBatchCallback); + NetworkPersistedBatchReadyListener networkPersistedBatchReadyListener = new NetworkPersistedBatchReadyListener(context, createRandomFile().getPath(), serializationStrategy, handler, networkBatchListener, retryCount, 50, 10, TrimPersistedBatchReadyListener.MODE_TRIM_AT_START, trimmedBatchCallback); networkPersistedBatchReadyListener.setDefaultTimeoutMs(timeOut); networkPersistedBatchReadyListener.setDefaultBackoffMultiplier(backOffMultiplier); @@ -526,18 +527,18 @@ public void testDoNotTrimmingIfWaitingForFinish() throws Exception { TrimmedBatchCallback trimmedBatchCallback = mock(TrimmedBatchCallback.class); SizeBatchingStrategy sizeBatchingStrategy = mock(SizeBatchingStrategy.class); final ArrayList dataList1 = Utils.fakeAdsCollection(10); - SizeBatchingStrategy.SizeBatch sizeBatch1 = new SizeBatchingStrategy.SizeBatch<>(dataList1, 3); + SizeBatch sizeBatch1 = new SizeBatch<>(dataList1, 3); MockNetworkPersistedBatchReadyListener networkBatchListener = spy(new MockNetworkPersistedBatchReadyListener(new NetworkPersistedBatchReadyListener.NetworkRequestResponse(true, ERROR_CODE_2XX), handler, callbackIdle, context)); - NetworkPersistedBatchReadyListener networkPersistedBatchReadyListener = new NetworkPersistedBatchReadyListener(context, createRandomString(), serializationStrategy, handler, networkBatchListener, 1, 2, 1, TrimPersistedBatchReadyListener.MODE_TRIM_AT_START | TrimPersistedBatchReadyListener.MODE_TRIM_ON_READY, trimmedBatchCallback); + NetworkPersistedBatchReadyListener networkPersistedBatchReadyListener = new NetworkPersistedBatchReadyListener(context, createRandomFile().getPath(), serializationStrategy, handler, networkBatchListener, 1, 2, 1, TrimPersistedBatchReadyListener.MODE_TRIM_AT_START | TrimPersistedBatchReadyListener.MODE_TRIM_ON_READY, trimmedBatchCallback); networkPersistedBatchReadyListener.onReady(sizeBatchingStrategy, sizeBatch1); shadowLooper.idle(100); //this will start waiting for finish callback final ArrayList dataList2 = Utils.fakeAdsCollection(10); - SizeBatchingStrategy.SizeBatch sizeBatch2 = new SizeBatchingStrategy.SizeBatch<>(dataList2, 3); + SizeBatch sizeBatch2 = new SizeBatch<>(dataList2, 3); networkPersistedBatchReadyListener.onReady(sizeBatchingStrategy,sizeBatch2); shadowLooper.idle(100); // this will initiate a trim since size is 2 final ArrayList dataList3 = Utils.fakeAdsCollection(10); - SizeBatchingStrategy.SizeBatch sizeBatch3 = new SizeBatchingStrategy.SizeBatch<>(dataList2, 3); + SizeBatch sizeBatch3 = new SizeBatch<>(dataList2, 3); networkPersistedBatchReadyListener.onReady(sizeBatchingStrategy,sizeBatch2); shadowLooper.idle(100); // this will fire onReady once more diff --git a/batching/src/test/java/com/flipkart/batching/listener/PersistedBatchReadyTest.java b/batching/src/test/java/com/flipkart/batching/listener/PersistedBatchReadyTest.java index dc66aec..03a6e28 100644 --- a/batching/src/test/java/com/flipkart/batching/listener/PersistedBatchReadyTest.java +++ b/batching/src/test/java/com/flipkart/batching/listener/PersistedBatchReadyTest.java @@ -30,15 +30,16 @@ import com.flipkart.Utils; import com.flipkart.batching.BaseTestClass; -import com.flipkart.batching.Batch; import com.flipkart.batching.BatchManager; import com.flipkart.batching.BuildConfig; -import com.flipkart.batching.Data; -import com.flipkart.batching.exception.SerializeException; -import com.flipkart.batching.persistence.GsonSerializationStrategy; -import com.flipkart.batching.persistence.SerializationStrategy; import com.flipkart.batching.strategy.SizeBatchingStrategy; import com.flipkart.batching.tape.QueueFile; +import com.flipkart.batching.gson.GsonSerializationStrategy; +import com.flipkart.batching.core.Batch; +import com.flipkart.batching.core.Data; +import com.flipkart.batching.core.SerializationStrategy; +import com.flipkart.batching.core.batch.SizeBatch; +import com.flipkart.batching.core.exception.SerializeException; import junit.framework.Assert; @@ -80,13 +81,13 @@ public void testIfInitializedCalled() { Handler handler = new Handler(looper); SizeBatchingStrategy strategy = mock(SizeBatchingStrategy.class); - SizeBatchingStrategy.SizeBatch sizeBatchInfo = new SizeBatchingStrategy.SizeBatch<>(Utils.fakeCollection(5), 5); + SizeBatch sizeBatchInfo = new SizeBatch<>(Utils.fakeCollection(5), 5); PersistedBatchCallback persistedBatchCallback = mock(PersistedBatchCallback.class); SerializationStrategy serializationStrategy = new GsonSerializationStrategy(); BatchManager.registerBuiltInTypes(serializationStrategy); serializationStrategy.build(); - PersistedBatchReadyListener> persistedBatchReadyListener = new PersistedBatchReadyListener<>(createRandomString(), serializationStrategy, handler, persistedBatchCallback); + PersistedBatchReadyListener> persistedBatchReadyListener = new PersistedBatchReadyListener<>(createRandomFile().getPath(), serializationStrategy, handler, persistedBatchCallback); persistedBatchReadyListener.onReady(strategy, sizeBatchInfo); shadowLooper.runToEndOfTasks(); //should be initialized first time @@ -106,13 +107,13 @@ public void testIfPersistSuccessCalled() { ShadowLooper shadowLooper = Shadows.shadowOf(looper); Handler handler = new Handler(looper); SizeBatchingStrategy strategy = mock(SizeBatchingStrategy.class); - SizeBatchingStrategy.SizeBatch sizeBatchInfo = new SizeBatchingStrategy.SizeBatch<>(Utils.fakeCollection(5), 5); + SizeBatch sizeBatchInfo = new SizeBatch<>(Utils.fakeCollection(5), 5); SerializationStrategy serializationStrategy = new GsonSerializationStrategy(); BatchManager.registerBuiltInTypes(serializationStrategy); serializationStrategy.build(); PersistedBatchCallback persistedBatchCallback = mock(PersistedBatchCallback.class); - PersistedBatchReadyListener persistedBatchReadyListener = new PersistedBatchReadyListener(createRandomString(), serializationStrategy, handler, persistedBatchCallback); + PersistedBatchReadyListener persistedBatchReadyListener = new PersistedBatchReadyListener(createRandomFile().getPath(), serializationStrategy, handler, persistedBatchCallback); persistedBatchReadyListener.onReady(strategy, sizeBatchInfo); shadowLooper.runToEndOfTasks(); @@ -136,13 +137,13 @@ public void testFinishCalled() throws IOException { ShadowLooper shadowLooper = Shadows.shadowOf(looper); Handler handler = new Handler(looper); SizeBatchingStrategy strategy = mock(SizeBatchingStrategy.class); - SizeBatchingStrategy.SizeBatch sizeBatchInfo = new SizeBatchingStrategy.SizeBatch<>(Utils.fakeCollection(5), 5); + SizeBatch sizeBatchInfo = new SizeBatch<>(Utils.fakeCollection(5), 5); SerializationStrategy serializationStrategy = new GsonSerializationStrategy(); BatchManager.registerBuiltInTypes(serializationStrategy); serializationStrategy.build(); PersistedBatchCallback persistedBatchCallback = mock(PersistedBatchCallback.class); - PersistedBatchReadyListener persistedBatchReadyListener = new PersistedBatchReadyListener(createRandomString(), serializationStrategy, handler, persistedBatchCallback); + PersistedBatchReadyListener persistedBatchReadyListener = new PersistedBatchReadyListener(createRandomFile().getPath(), serializationStrategy, handler, persistedBatchCallback); ArrayList arrayList = Utils.fakeCollection(4); when(queueFile.peek()).thenReturn(serializationStrategy.serializeCollection(arrayList)); @@ -180,15 +181,15 @@ public void testPersistSuccessNotCalledMoreThanOnce() throws IOException { BatchManager.registerBuiltInTypes(serializationStrategy); serializationStrategy.build(); PersistedBatchCallback persistedBatchCallback = mock(PersistedBatchCallback.class); - PersistedBatchReadyListener persistedBatchReadyListener = new PersistedBatchReadyListener(createRandomString(), serializationStrategy, handler, persistedBatchCallback); + PersistedBatchReadyListener persistedBatchReadyListener = new PersistedBatchReadyListener(createRandomFile().getPath(), serializationStrategy, handler, persistedBatchCallback); - SizeBatchingStrategy.SizeBatch firstBatch = new SizeBatchingStrategy.SizeBatch<>(Utils.fakeCollection(5), 5); + SizeBatch firstBatch = new SizeBatch<>(Utils.fakeCollection(5), 5); persistedBatchReadyListener.onReady(strategy, firstBatch); - SizeBatchingStrategy.SizeBatch secondBatch = new SizeBatchingStrategy.SizeBatch<>(Utils.fakeCollection(5), 5); + SizeBatch secondBatch = new SizeBatch<>(Utils.fakeCollection(5), 5); persistedBatchReadyListener.onReady(strategy, secondBatch); - SizeBatchingStrategy.SizeBatch thirdBatch = new SizeBatchingStrategy.SizeBatch<>(Utils.fakeCollection(5), 5); + SizeBatch thirdBatch = new SizeBatch<>(Utils.fakeCollection(5), 5); persistedBatchReadyListener.onReady(strategy, thirdBatch); - SizeBatchingStrategy.SizeBatch fourthBatch = new SizeBatchingStrategy.SizeBatch<>(Utils.fakeCollection(5), 5); + SizeBatch fourthBatch = new SizeBatch<>(Utils.fakeCollection(5), 5); persistedBatchReadyListener.onReady(strategy, fourthBatch); shadowLooper.runToEndOfTasks(); @@ -210,7 +211,7 @@ public void testPersistSuccessNotCalledMoreThanOnce() throws IOException { /** * Test to verify that finish throws an {@link IllegalStateException} when it gets called with a different batch. * Ignoring this test because we are catching this exception and logging it now. - * + *

* UPDATE : not throwing this exception anymore, thus ignore. * * @throws SerializeException @@ -227,20 +228,20 @@ public void testFinishException() throws IOException { ShadowLooper shadowLooper = Shadows.shadowOf(looper); Handler handler = new Handler(looper); SizeBatchingStrategy strategy = mock(SizeBatchingStrategy.class); - SizeBatchingStrategy.SizeBatch sizeBatchInfo = new SizeBatchingStrategy.SizeBatch<>(Utils.fakeCollection(5), 5); + SizeBatch sizeBatchInfo = new SizeBatch<>(Utils.fakeCollection(5), 5); SerializationStrategy serializationStrategy = new GsonSerializationStrategy(); BatchManager.registerBuiltInTypes(serializationStrategy); serializationStrategy.build(); PersistedBatchCallback persistedBatchCallback = mock(PersistedBatchCallback.class); - PersistedBatchReadyListener persistedBatchReadyListener = new PersistedBatchReadyListener(createRandomString(), serializationStrategy, handler, persistedBatchCallback); + PersistedBatchReadyListener persistedBatchReadyListener = new PersistedBatchReadyListener(createRandomFile().getPath(), serializationStrategy, handler, persistedBatchCallback); byte[] peeked = serializationStrategy.serializeBatch(sizeBatchInfo); when(queueFile.peek()).thenReturn(peeked); persistedBatchReadyListener.onReady(strategy, sizeBatchInfo); shadowLooper.runToEndOfTasks(); - SizeBatchingStrategy.SizeBatch sizeBatchInfo1 = new SizeBatchingStrategy.SizeBatch<>(Utils.fakeAdsCollection(4), 5); + SizeBatch sizeBatchInfo1 = new SizeBatch<>(Utils.fakeAdsCollection(4), 5); try { persistedBatchReadyListener.finish(sizeBatchInfo1); } catch (Exception e) { @@ -265,12 +266,12 @@ public void testIfPersistFailureCalled() throws IOException { ShadowLooper shadowLooper = Shadows.shadowOf(looper); Handler handler = new Handler(looper); SizeBatchingStrategy strategy = mock(SizeBatchingStrategy.class); - SizeBatchingStrategy.SizeBatch sizeBatchInfo = new SizeBatchingStrategy.SizeBatch<>(Utils.fakeCollection(5), 5); + SizeBatch sizeBatchInfo = new SizeBatch<>(Utils.fakeCollection(5), 5); SerializationStrategy serializationStrategy = new GsonSerializationStrategy(); BatchManager.registerBuiltInTypes(serializationStrategy); serializationStrategy.build(); PersistedBatchCallback persistedBatchCallback = mock(PersistedBatchCallback.class); - PersistedBatchReadyListener persistedBatchReadyListener = new PersistedBatchReadyListener(createRandomString(), serializationStrategy, handler, persistedBatchCallback); + PersistedBatchReadyListener persistedBatchReadyListener = new PersistedBatchReadyListener(createRandomFile().getPath(), serializationStrategy, handler, persistedBatchCallback); byte[] data = serializationStrategy.serializeCollection(Utils.fakeAdsCollection(4)); doThrow(new IOException()).when(queueFile).add(data); @@ -290,21 +291,19 @@ public void testListenerNotNull() { ShadowLooper shadowLooper = Shadows.shadowOf(looper); Handler handler = new Handler(looper); SizeBatchingStrategy strategy = mock(SizeBatchingStrategy.class); - SizeBatchingStrategy.SizeBatch sizeBatchInfo = new SizeBatchingStrategy.SizeBatch<>(Utils.fakeCollection(5), 5); + SizeBatch sizeBatchInfo = new SizeBatch<>(Utils.fakeCollection(5), 5); SerializationStrategy serializationStrategy = new GsonSerializationStrategy(); BatchManager.registerBuiltInTypes(serializationStrategy); serializationStrategy.build(); PersistedBatchCallback persistedBatchCallback = mock(PersistedBatchCallback.class); - PersistedBatchReadyListener persistedBatchReadyListener = new PersistedBatchReadyListener(createRandomString(), serializationStrategy, handler, persistedBatchCallback); + PersistedBatchReadyListener persistedBatchReadyListener = new PersistedBatchReadyListener(createRandomFile().getPath(), serializationStrategy, handler, persistedBatchCallback); persistedBatchReadyListener.onReady(strategy, sizeBatchInfo); Assert.assertTrue(persistedBatchReadyListener.getListener() != null); - persistedBatchReadyListener = new PersistedBatchReadyListener(createRandomString(), serializationStrategy, handler, null); + persistedBatchReadyListener = new PersistedBatchReadyListener(createRandomFile().getPath(), serializationStrategy, handler, null); persistedBatchReadyListener.setListener(persistedBatchCallback); Assert.assertTrue(persistedBatchReadyListener.getListener() != null); - - } diff --git a/batching/src/test/java/com/flipkart/batching/listener/TagBatchReadyTest.java b/batching/src/test/java/com/flipkart/batching/listener/TagBatchReadyTest.java index b0af10a..e4e2e3f 100644 --- a/batching/src/test/java/com/flipkart/batching/listener/TagBatchReadyTest.java +++ b/batching/src/test/java/com/flipkart/batching/listener/TagBatchReadyTest.java @@ -28,8 +28,10 @@ import com.flipkart.batching.BatchingStrategy; import com.flipkart.batching.BuildConfig; import com.flipkart.batching.OnBatchReadyListener; -import com.flipkart.batching.data.Tag; -import com.flipkart.batching.data.TagData; +import com.flipkart.batching.core.batch.SizeBatch; +import com.flipkart.batching.core.batch.TagBatch; +import com.flipkart.batching.core.data.Tag; +import com.flipkart.batching.core.data.TagData; import com.flipkart.batching.strategy.SizeBatchingStrategy; import com.flipkart.batching.strategy.TagBatchingStrategy; @@ -63,7 +65,7 @@ public void testAddTagListener() { Tag BUSINESS = new Tag("BUSINESS"); Tag DEBUG = new Tag("DEBUG"); - OnBatchReadyListener> onBatchReadyListener = mock(OnBatchReadyListener.class); + OnBatchReadyListener> onBatchReadyListener = mock(OnBatchReadyListener.class); tagBatchReadyListener = new TagBatchReadyListener<>(); tagBatchReadyListener.addListenerForTag(AD, onBatchReadyListener); tagBatchReadyListener.addListenerForTag(DEBUG, onBatchReadyListener); @@ -75,7 +77,7 @@ public void testAddTagListener() { /** - * Test to verify {@link TagBatchReadyListener#onReady(BatchingStrategy, TagBatchingStrategy.TagBatch)} + * Test to verify {@link TagBatchReadyListener#onReady(BatchingStrategy, TagBatch)} */ @Test public void testOnReady() { @@ -84,14 +86,14 @@ public void testOnReady() { Tag BUSINESS = new Tag("BUSINESS"); Tag DEBUG = new Tag("DEBUG"); - OnBatchReadyListener> onBatchReadyListener = mock(OnBatchReadyListener.class); + OnBatchReadyListener> onBatchReadyListener = mock(OnBatchReadyListener.class); tagBatchReadyListener = new TagBatchReadyListener<>(); tagBatchReadyListener.addListenerForTag(AD, onBatchReadyListener); tagBatchReadyListener.addListenerForTag(DEBUG, onBatchReadyListener); tagBatchReadyListener.addListenerForTag(BUSINESS, onBatchReadyListener); - tagBatchReadyListener.onReady(new TagBatchingStrategy<>(), new TagBatchingStrategy.TagBatch<>(new Tag("ADS"), new SizeBatchingStrategy.SizeBatch(Utils.fakeCollection(2), 4))); + tagBatchReadyListener.onReady(new TagBatchingStrategy<>(), new TagBatch<>(new Tag("ADS"), new SizeBatch(Utils.fakeCollection(2), 4))); //verify that it gets called once , when tagBatchReadyListener's onReady gets called - verify(onBatchReadyListener, times(1)).onReady(any(BatchingStrategy.class), any(TagBatchingStrategy.TagBatch.class)); + verify(onBatchReadyListener, times(1)).onReady(any(BatchingStrategy.class), any(TagBatch.class)); } } diff --git a/batching/src/test/java/com/flipkart/batching/listener/TrimPersistedBatchReadyTest.java b/batching/src/test/java/com/flipkart/batching/listener/TrimPersistedBatchReadyTest.java index 39b1aa7..9664e6d 100644 --- a/batching/src/test/java/com/flipkart/batching/listener/TrimPersistedBatchReadyTest.java +++ b/batching/src/test/java/com/flipkart/batching/listener/TrimPersistedBatchReadyTest.java @@ -30,13 +30,14 @@ import com.flipkart.Utils; import com.flipkart.batching.BaseTestClass; -import com.flipkart.batching.Batch; import com.flipkart.batching.BatchManager; import com.flipkart.batching.BuildConfig; -import com.flipkart.batching.Data; -import com.flipkart.batching.exception.SerializeException; -import com.flipkart.batching.persistence.GsonSerializationStrategy; import com.flipkart.batching.strategy.SizeBatchingStrategy; +import com.flipkart.batching.gson.GsonSerializationStrategy; +import com.flipkart.batching.core.Batch; +import com.flipkart.batching.core.Data; +import com.flipkart.batching.core.batch.SizeBatch; +import com.flipkart.batching.core.exception.SerializeException; import org.junit.After; import org.junit.Test; @@ -88,9 +89,9 @@ public void testOnTrimmedCalled() throws IOException { TrimmedBatchCallback trimmedBatchCallback = mock(TrimmedBatchCallback.class); SizeBatchingStrategy sizeBatchingStrategy = mock(SizeBatchingStrategy.class); - SizeBatchingStrategy.SizeBatch sizeBatch = new SizeBatchingStrategy.SizeBatch<>(dataList, 3); + SizeBatch sizeBatch = new SizeBatch<>(dataList, 3); - TrimPersistedBatchReadyListener> trimPersistedBatchReadyListener = new TrimPersistedBatchReadyListener>(createRandomString(), serializationStrategy, + TrimPersistedBatchReadyListener> trimPersistedBatchReadyListener = new TrimPersistedBatchReadyListener>(createRandomFile().getPath(), serializationStrategy, handler, MAX_QUEUE_SIZE, TRIM_TO_SIZE, TrimPersistedBatchReadyListener.MODE_TRIM_AT_START | TrimPersistedBatchReadyListener.MODE_TRIM_ON_READY, null, trimmedBatchCallback); trimPersistedBatchReadyListener.onReady(sizeBatchingStrategy, sizeBatch); @@ -126,9 +127,9 @@ public void testTrimmedNotCalled() throws IOException { TrimmedBatchCallback trimmedBatchCallback = mock(TrimmedBatchCallback.class); SizeBatchingStrategy sizeBatchingStrategy = mock(SizeBatchingStrategy.class); - SizeBatchingStrategy.SizeBatch sizeBatch = new SizeBatchingStrategy.SizeBatch<>(dataList, 3); + SizeBatch sizeBatch = new SizeBatch<>(dataList, 3); - TrimPersistedBatchReadyListener> trimPersistedBatchReadyListener = new TrimPersistedBatchReadyListener>(createRandomString(), serializationStrategy, + TrimPersistedBatchReadyListener> trimPersistedBatchReadyListener = new TrimPersistedBatchReadyListener>(createRandomFile().getPath(), serializationStrategy, handler, MAX_QUEUE_SIZE, TRIM_TO_SIZE, TrimPersistedBatchReadyListener.MODE_TRIM_AT_START | TrimPersistedBatchReadyListener.MODE_TRIM_ON_READY, null, trimmedBatchCallback); trimPersistedBatchReadyListener.onReady(sizeBatchingStrategy, sizeBatch); @@ -160,11 +161,11 @@ public void testTrimMode() throws IOException { Looper looper = handlerThread.getLooper(); ShadowLooper shadowLooper = Shadows.shadowOf(looper); Handler handler = new Handler(looper); - String filePath = createRandomString(); + String filePath = createRandomFile().getPath(); TrimmedBatchCallback trimmedBatchCallback = mock(TrimmedBatchCallback.class); SizeBatchingStrategy sizeBatchingStrategy = mock(SizeBatchingStrategy.class); - SizeBatchingStrategy.SizeBatch sizeBatch = new SizeBatchingStrategy.SizeBatch<>(dataList, 3); + SizeBatch sizeBatch = new SizeBatch<>(dataList, 3); TrimPersistedBatchReadyListener> trimPersistedBatchReadyListener = new TrimPersistedBatchReadyListener>(filePath, serializationStrategy, handler, MAX_QUEUE_SIZE, TRIM_TO_SIZE, TrimPersistedBatchReadyListener.MODE_TRIM_AT_START, null, trimmedBatchCallback); @@ -178,7 +179,7 @@ public void testTrimMode() throws IOException { trimPersistedBatchReadyListener = new TrimPersistedBatchReadyListener>(filePath, serializationStrategy, handler, MAX_QUEUE_SIZE, TRIM_TO_SIZE, TrimPersistedBatchReadyListener.MODE_TRIM_AT_START, null, trimmedBatchCallback); - SizeBatchingStrategy.SizeBatch sizeBatch2 = new SizeBatchingStrategy.SizeBatch<>(dataList, 1); + SizeBatch sizeBatch2 = new SizeBatch<>(dataList, 1); trimPersistedBatchReadyListener.onReady(sizeBatchingStrategy, sizeBatch2); shadowLooper.runToEndOfTasks(); //verify that onTrimmed gets called @@ -187,7 +188,7 @@ public void testTrimMode() throws IOException { /** * Test to verify that exception is thrown - * when {@link TrimPersistedBatchReadyListener#trimSize} > {@link TrimPersistedBatchReadyListener#queueSize} + * when {@link TrimPersistedBatchReadyListener#trimSize} > {@link TrimPersistedBatchReadyListener#maxQueueSize} */ @Test(expected = IllegalArgumentException.class) public void testThrowException() { @@ -208,7 +209,7 @@ public void testThrowException() { TrimmedBatchCallback trimmedBatchCallback = mock(TrimmedBatchCallback.class); //throw exception as TrimToSize is greater than MaxQueueSize - TrimPersistedBatchReadyListener> trimPersistedBatchReadyListener = new TrimPersistedBatchReadyListener>(createRandomString(), serializationStrategy, + TrimPersistedBatchReadyListener> trimPersistedBatchReadyListener = new TrimPersistedBatchReadyListener>(createRandomFile().getPath(), serializationStrategy, handler, MAX_QUEUE_SIZE, 5, TrimPersistedBatchReadyListener.MODE_TRIM_AT_START, null, trimmedBatchCallback); } diff --git a/batching/src/test/java/com/flipkart/batching/persistence/DatabaseHelperTest.java b/batching/src/test/java/com/flipkart/batching/persistence/DatabaseHelperTest.java index d3464ea..46f1c11 100644 --- a/batching/src/test/java/com/flipkart/batching/persistence/DatabaseHelperTest.java +++ b/batching/src/test/java/com/flipkart/batching/persistence/DatabaseHelperTest.java @@ -28,13 +28,14 @@ import android.database.sqlite.SQLiteDatabase; import com.flipkart.Utils; -import com.flipkart.batching.Batch; import com.flipkart.batching.BatchManager; import com.flipkart.batching.BuildConfig; -import com.flipkart.batching.Data; -import com.flipkart.batching.data.EventData; -import com.flipkart.batching.exception.DeserializeException; -import com.flipkart.batching.exception.SerializeException; +import com.flipkart.batching.gson.GsonSerializationStrategy; +import com.flipkart.batching.core.Batch; +import com.flipkart.batching.core.Data; +import com.flipkart.batching.core.data.EventData; +import com.flipkart.batching.core.exception.DeserializeException; +import com.flipkart.batching.core.exception.SerializeException; import junit.framework.Assert; diff --git a/batching/src/test/java/com/flipkart/batching/persistence/InMemoryPersistenceTest.java b/batching/src/test/java/com/flipkart/batching/persistence/InMemoryPersistenceTest.java index 9928cd9..a25e6ce 100644 --- a/batching/src/test/java/com/flipkart/batching/persistence/InMemoryPersistenceTest.java +++ b/batching/src/test/java/com/flipkart/batching/persistence/InMemoryPersistenceTest.java @@ -25,7 +25,7 @@ package com.flipkart.batching.persistence; import com.flipkart.Utils; -import com.flipkart.batching.Data; +import com.flipkart.batching.core.Data; import junit.framework.Assert; diff --git a/batching/src/test/java/com/flipkart/batching/persistence/SQLPersistenceTest.java b/batching/src/test/java/com/flipkart/batching/persistence/SQLPersistenceTest.java index 95dcf9f..46fa865 100644 --- a/batching/src/test/java/com/flipkart/batching/persistence/SQLPersistenceTest.java +++ b/batching/src/test/java/com/flipkart/batching/persistence/SQLPersistenceTest.java @@ -30,7 +30,9 @@ import com.flipkart.batching.BaseTestClass; import com.flipkart.batching.BatchManager; import com.flipkart.batching.BuildConfig; -import com.flipkart.batching.Data; +import com.flipkart.batching.gson.GsonSerializationStrategy; +import com.flipkart.batching.core.Data; +import com.flipkart.batching.core.SerializationStrategy; import junit.framework.Assert; @@ -108,7 +110,7 @@ public void testSerializeException() { PersistenceStrategy persistenceStrategy; Context context; context = RuntimeEnvironment.application; - persistenceStrategy = new SQLPersistenceStrategy<>(new GsonSerializationStrategy<>(), createRandomString(), context); + persistenceStrategy = new SQLPersistenceStrategy<>(new GsonSerializationStrategy<>(), createRandomFile().getPath(), context); persistenceStrategy.onInitialized(); persistenceStrategy.add(Utils.fakeCollection(4)); } diff --git a/batching/src/test/java/com/flipkart/batching/persistence/TagBasedPersistenceTest.java b/batching/src/test/java/com/flipkart/batching/persistence/TagBasedPersistenceTest.java index afa51fa..6a5cdfc 100644 --- a/batching/src/test/java/com/flipkart/batching/persistence/TagBasedPersistenceTest.java +++ b/batching/src/test/java/com/flipkart/batching/persistence/TagBasedPersistenceTest.java @@ -27,11 +27,13 @@ import android.content.Context; import com.flipkart.Utils; -import com.flipkart.batching.Batch; import com.flipkart.batching.BatchManager; import com.flipkart.batching.BuildConfig; -import com.flipkart.batching.data.Tag; -import com.flipkart.batching.data.TagData; +import com.flipkart.batching.gson.GsonSerializationStrategy; +import com.flipkart.batching.core.Batch; +import com.flipkart.batching.core.SerializationStrategy; +import com.flipkart.batching.core.data.Tag; +import com.flipkart.batching.core.data.TagData; import org.junit.Assert; import org.junit.Test; diff --git a/batching/src/test/java/com/flipkart/batching/persistence/TapePersistenceTest.java b/batching/src/test/java/com/flipkart/batching/persistence/TapePersistenceTest.java index bd6d3f5..62cd47d 100644 --- a/batching/src/test/java/com/flipkart/batching/persistence/TapePersistenceTest.java +++ b/batching/src/test/java/com/flipkart/batching/persistence/TapePersistenceTest.java @@ -28,7 +28,9 @@ import com.flipkart.batching.BaseTestClass; import com.flipkart.batching.BatchManager; import com.flipkart.batching.BuildConfig; -import com.flipkart.batching.Data; +import com.flipkart.batching.gson.GsonSerializationStrategy; +import com.flipkart.batching.core.Data; +import com.flipkart.batching.core.SerializationStrategy; import junit.framework.Assert; @@ -106,7 +108,7 @@ private PersistenceStrategy initializeTapePersistence() { SerializationStrategy serializationStrategy = new GsonSerializationStrategy(); BatchManager.registerBuiltInTypes(serializationStrategy); serializationStrategy.build(); - TapePersistenceStrategy persistenceStrategy = new TapePersistenceStrategy<>(createRandomString(), serializationStrategy); + TapePersistenceStrategy persistenceStrategy = new TapePersistenceStrategy<>(createRandomFile().getPath(), serializationStrategy); persistenceStrategy.onInitialized(); return persistenceStrategy; diff --git a/batching/src/test/java/com/flipkart/batching/strategy/BaseBatchingStrategyTest.java b/batching/src/test/java/com/flipkart/batching/strategy/BaseBatchingStrategyTest.java index 11a1fc7..d6a47b2 100644 --- a/batching/src/test/java/com/flipkart/batching/strategy/BaseBatchingStrategyTest.java +++ b/batching/src/test/java/com/flipkart/batching/strategy/BaseBatchingStrategyTest.java @@ -28,13 +28,13 @@ import android.os.Handler; import com.flipkart.Utils; -import com.flipkart.batching.Batch; import com.flipkart.batching.BatchController; import com.flipkart.batching.BatchingStrategy; import com.flipkart.batching.BuildConfig; -import com.flipkart.batching.Data; import com.flipkart.batching.OnBatchReadyListener; import com.flipkart.batching.persistence.PersistenceStrategy; +import com.flipkart.batching.core.Batch; +import com.flipkart.batching.core.Data; import junit.framework.Assert; diff --git a/batching/src/test/java/com/flipkart/batching/strategy/SizeBatchingTest.java b/batching/src/test/java/com/flipkart/batching/strategy/SizeBatchingTest.java index 9dce370..8bbf25b 100644 --- a/batching/src/test/java/com/flipkart/batching/strategy/SizeBatchingTest.java +++ b/batching/src/test/java/com/flipkart/batching/strategy/SizeBatchingTest.java @@ -28,11 +28,12 @@ import android.os.Handler; import com.flipkart.Utils; -import com.flipkart.batching.Batch; import com.flipkart.batching.BatchingStrategy; -import com.flipkart.batching.Data; import com.flipkart.batching.listener.PersistedBatchReadyListener; import com.flipkart.batching.persistence.PersistenceStrategy; +import com.flipkart.batching.core.Batch; +import com.flipkart.batching.core.Data; +import com.flipkart.batching.core.batch.SizeBatch; import junit.framework.Assert; @@ -295,8 +296,8 @@ public void testIfPersistenceNull() { public void testSizeBatchInfo() { ArrayList list1 = Utils.fakeCollection(5); ArrayList list2 = new ArrayList<>(list1); - SizeBatchingStrategy.SizeBatch sizeBatchInfo = new SizeBatchingStrategy.SizeBatch<>(list1, 5); - SizeBatchingStrategy.SizeBatch sizeBatchInfo1 = new SizeBatchingStrategy.SizeBatch<>(list2, 5); + SizeBatch sizeBatchInfo = new SizeBatch<>(list1, 5); + SizeBatch sizeBatchInfo1 = new SizeBatch<>(list2, 5); Assert.assertTrue(sizeBatchInfo.equals(sizeBatchInfo1)); Assert.assertTrue(!sizeBatchInfo.equals("event")); diff --git a/batching/src/test/java/com/flipkart/batching/strategy/SizeTimeBatchingTest.java b/batching/src/test/java/com/flipkart/batching/strategy/SizeTimeBatchingTest.java index 7968e3f..f4ca99c 100644 --- a/batching/src/test/java/com/flipkart/batching/strategy/SizeTimeBatchingTest.java +++ b/batching/src/test/java/com/flipkart/batching/strategy/SizeTimeBatchingTest.java @@ -30,13 +30,14 @@ import android.os.Looper; import com.flipkart.Utils; -import com.flipkart.batching.Batch; import com.flipkart.batching.BatchController; import com.flipkart.batching.BatchingStrategy; import com.flipkart.batching.BuildConfig; -import com.flipkart.batching.Data; import com.flipkart.batching.OnBatchReadyListener; import com.flipkart.batching.persistence.PersistenceStrategy; +import com.flipkart.batching.core.Batch; +import com.flipkart.batching.core.Data; +import com.flipkart.batching.core.batch.SizeTimeBatch; import junit.framework.Assert; @@ -241,8 +242,8 @@ public void testOnInitialized() { public void testComboBatchInfo() { ArrayList list1 = Utils.fakeCollection(5); ArrayList list2 = new ArrayList<>(list1); - SizeTimeBatchingStrategy.SizeTimeBatch sizeTimeBatch = new SizeTimeBatchingStrategy.SizeTimeBatch(list1, 5, 5000); - SizeTimeBatchingStrategy.SizeTimeBatch sizeTimeBatch1 = new SizeTimeBatchingStrategy.SizeTimeBatch(list2, 5, 5000); + SizeTimeBatch sizeTimeBatch = new SizeTimeBatch(list1, 5, 5000); + SizeTimeBatch sizeTimeBatch1 = new SizeTimeBatch(list2, 5, 5000); Assert.assertTrue(sizeTimeBatch.equals(sizeTimeBatch1)); Assert.assertTrue(!sizeTimeBatch.equals("event")); diff --git a/batching/src/test/java/com/flipkart/batching/strategy/TagBatchingTest.java b/batching/src/test/java/com/flipkart/batching/strategy/TagBatchingTest.java index 48a48cc..73c5dc9 100644 --- a/batching/src/test/java/com/flipkart/batching/strategy/TagBatchingTest.java +++ b/batching/src/test/java/com/flipkart/batching/strategy/TagBatchingTest.java @@ -30,13 +30,15 @@ import android.os.Looper; import com.flipkart.Utils; -import com.flipkart.batching.Batch; import com.flipkart.batching.BatchingStrategy; import com.flipkart.batching.BuildConfig; -import com.flipkart.batching.Data; import com.flipkart.batching.OnBatchReadyListener; -import com.flipkart.batching.data.Tag; -import com.flipkart.batching.data.TagData; +import com.flipkart.batching.core.Batch; +import com.flipkart.batching.core.Data; +import com.flipkart.batching.core.batch.SizeBatch; +import com.flipkart.batching.core.batch.TagBatch; +import com.flipkart.batching.core.data.Tag; +import com.flipkart.batching.core.data.TagData; import com.flipkart.batching.persistence.InMemoryPersistenceStrategy; import com.flipkart.batching.persistence.SQLPersistenceStrategy; import com.flipkart.batching.persistence.TagBasedPersistenceStrategy; @@ -193,7 +195,7 @@ public void testOnReadyCallbackFlushTrue() { tagBatchingStrategy.flush(true); //verify that onReady is called, as flush force is true - verify(onBatchReadyListener, times(2)).onReady(eq(tagBatchingStrategy), any(TagBatchingStrategy.TagBatch.class)); + verify(onBatchReadyListener, times(2)).onReady(eq(tagBatchingStrategy), any(TagBatch.class)); } /** @@ -227,7 +229,7 @@ public void testOnReadyCallbackDataForSize() { when(inMemoryPersistenceStrategy.getData()).thenReturn(adsTagDataList); when(inMemoryPersistenceStrategy.getDataSize()).thenReturn(adsTagDataList.size()); tagBatchingStrategy.flush(false); - verify(onBatchReadyListener, times(1)).onReady(eq(tagBatchingStrategy), any(TagBatchingStrategy.TagBatch.class)); + verify(onBatchReadyListener, times(1)).onReady(eq(tagBatchingStrategy), any(TagBatch.class)); } /** @@ -263,7 +265,7 @@ public void testOnReadyCallbackDataForTime() { tagBatchingStrategy.flush(false); shadowLooper.idle(5000); //verify it gets called after 5000ms - verify(onBatchReadyListener, times(1)).onReady(eq(tagBatchingStrategy), any(TagBatchingStrategy.TagBatch.class)); + verify(onBatchReadyListener, times(1)).onReady(eq(tagBatchingStrategy), any(TagBatch.class)); } /** @@ -351,8 +353,8 @@ public void testTagBatchEquals() { ArrayList list1 = Utils.fakeCollection(5); ArrayList list2 = new ArrayList<>(list1); - TagBatchingStrategy.TagBatch tagBatchInfo = new TagBatchingStrategy.TagBatch(AD_TAG, new SizeBatchingStrategy.SizeBatch<>(list1, 5)); - TagBatchingStrategy.TagBatch tagBatchInfo1 = new TagBatchingStrategy.TagBatch(AD_TAG, new SizeBatchingStrategy.SizeBatch<>(list2, 5)); + TagBatch tagBatchInfo = new TagBatch(AD_TAG, new SizeBatch<>(list1, 5)); + TagBatch tagBatchInfo1 = new TagBatch(AD_TAG, new SizeBatch<>(list2, 5)); Assert.assertNotNull(tagBatchInfo.getTag()); Assert.assertTrue(tagBatchInfo.equals(tagBatchInfo1)); @@ -366,7 +368,7 @@ public void testTagBatchEquals() { public void testTagBatchCollection() { Tag AD_TAG = new Tag("ADS"); ArrayList tagDatas = Utils.fakeTagAdsCollection(4); - TagBatchingStrategy.TagBatch tagBatch = new TagBatchingStrategy.TagBatch(AD_TAG, new SizeBatchingStrategy.SizeBatch(tagDatas, 4)); + TagBatch tagBatch = new TagBatch(AD_TAG, new SizeBatch(tagDatas, 4)); Assert.assertTrue(tagDatas == tagBatch.getDataCollection()); } } diff --git a/batching/src/test/java/com/flipkart/batching/strategy/TimeBatchingTest.java b/batching/src/test/java/com/flipkart/batching/strategy/TimeBatchingTest.java index 44a981f..84621a8 100644 --- a/batching/src/test/java/com/flipkart/batching/strategy/TimeBatchingTest.java +++ b/batching/src/test/java/com/flipkart/batching/strategy/TimeBatchingTest.java @@ -30,12 +30,13 @@ import android.os.Looper; import com.flipkart.Utils; -import com.flipkart.batching.Batch; import com.flipkart.batching.BatchingStrategy; import com.flipkart.batching.BuildConfig; -import com.flipkart.batching.Data; import com.flipkart.batching.listener.PersistedBatchReadyListener; import com.flipkart.batching.persistence.PersistenceStrategy; +import com.flipkart.batching.core.Batch; +import com.flipkart.batching.core.Data; +import com.flipkart.batching.core.batch.TimeBatch; import junit.framework.Assert; @@ -139,7 +140,7 @@ public void testOnReadyCallbackFlushTrue() { when(persistenceStrategy.getDataSize()).thenReturn(data.size()); timeBatchingStrategy.flush(true); //verify that it gets called once - verify(onBatchReadyListener, times(1)).onReady(eq(timeBatchingStrategy), any(TimeBatchingStrategy.TimeBatch.class)); + verify(onBatchReadyListener, times(1)).onReady(eq(timeBatchingStrategy), any(TimeBatch.class)); data.clear(); data = Utils.fakeCollection(5); @@ -149,7 +150,7 @@ public void testOnReadyCallbackFlushTrue() { when(persistenceStrategy.getDataSize()).thenReturn(data.size()); timeBatchingStrategy.flush(true); //verify that it gets called once - verify(onBatchReadyListener, times(1)).onReady(eq(timeBatchingStrategy), any(TimeBatchingStrategy.TimeBatch.class)); + verify(onBatchReadyListener, times(1)).onReady(eq(timeBatchingStrategy), any(TimeBatch.class)); } /** @@ -175,7 +176,7 @@ public void testOnReadyCallbackData() { timeBatchingStrategy.flush(false); shadowLooper.idle(TIME_OUT); //verify that it gets called once - verify(onBatchReadyListener, times(1)).onReady(eq(timeBatchingStrategy), any(TimeBatchingStrategy.TimeBatch.class)); + verify(onBatchReadyListener, times(1)).onReady(eq(timeBatchingStrategy), any(TimeBatch.class)); reset(onBatchReadyListener); data.clear(); @@ -187,7 +188,7 @@ public void testOnReadyCallbackData() { timeBatchingStrategy.flush(false); shadowLooper.idle(TIME_OUT); //verify that it gets called once - verify(onBatchReadyListener, times(1)).onReady(eq(timeBatchingStrategy), any(TimeBatchingStrategy.TimeBatch.class)); + verify(onBatchReadyListener, times(1)).onReady(eq(timeBatchingStrategy), any(TimeBatch.class)); } /** @@ -212,7 +213,7 @@ public void testOnReadyForEmptyData() { when(persistenceStrategy.getDataSize()).thenReturn(data.size()); timeBatchingStrategy.flush(false); //verify that onReady is NOT called since the data list is empty. - verify(onBatchReadyListener, times(0)).onReady(eq(timeBatchingStrategy), any(TimeBatchingStrategy.TimeBatch.class)); + verify(onBatchReadyListener, times(0)).onReady(eq(timeBatchingStrategy), any(TimeBatch.class)); } /** @@ -222,8 +223,8 @@ public void testOnReadyForEmptyData() { public void testTimeBatch() { ArrayList list1 = Utils.fakeCollection(2); ArrayList list2 = new ArrayList<>(list1); - TimeBatchingStrategy.TimeBatch timeBatchInfo = new TimeBatchingStrategy.TimeBatch<>(list1, 5000); - TimeBatchingStrategy.TimeBatch timeBatchInfo1 = new TimeBatchingStrategy.TimeBatch<>(list2, 5000); + TimeBatch timeBatchInfo = new TimeBatch<>(list1, 5000); + TimeBatch timeBatchInfo1 = new TimeBatch<>(list2, 5000); Assert.assertTrue(timeBatchInfo.equals(timeBatchInfo1)); Assert.assertTrue(!timeBatchInfo.equals("a")); diff --git a/build.gradle b/build.gradle index b38460e..958a73e 100644 --- a/build.gradle +++ b/build.gradle @@ -29,7 +29,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:2.1.2' + classpath 'com.android.tools.build:gradle:2.2.2' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files @@ -39,6 +39,7 @@ buildscript { allprojects { repositories { jcenter() + maven { url "https://jitpack.io" } } } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 94c985f..23384c2 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Mon Mar 14 17:53:58 GMT+05:30 2016 +#Sat Nov 26 16:20:47 IST 2016 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip diff --git a/settings.gradle b/settings.gradle index ab38e5b..d308db5 100644 --- a/settings.gradle +++ b/settings.gradle @@ -22,4 +22,4 @@ * THE SOFTWARE. */ -include ':app', ':batching' +include ':app', ':batching', ':batching-core', ':batching-gson'