Skip to content

Commit

Permalink
Fix bug when key not present
Browse files Browse the repository at this point in the history
Add null check when key "thank_you_link_url_settings" not present in
eligibility response.

Changes

- Add null check
- Migrate to AndroidX
- Add tests

Issue: #81
  • Loading branch information
diegoserranoa committed Feb 28, 2020
1 parent 5782055 commit 094ee4b
Show file tree
Hide file tree
Showing 20 changed files with 137 additions and 45 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 2.14.2 (2020-02-28)

- Fix bug when thank_you_link_url_settings is not present
- Migrate to AndroidX

## 2.14.1 (2019-12-18)

- Fix bug while writing to parcel
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ If you use Maven, you can include this library as a dependency:
<dependency>
<groupId>com.wootric</groupId>
<artifactId>wootric-sdk-android</artifactId>
<version>2.14.1</version>
<version>2.14.2</version>
</dependency>
```

### Using Gradle

```xml
compile 'com.wootric:wootric-sdk-android:2.14.1'
compile 'com.wootric:wootric-sdk-android:2.14.2'
```

## Initializing Wootric
Expand Down
13 changes: 7 additions & 6 deletions androidsdk/build.gradle
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 26
compileSdkVersion 28
buildToolsVersion '28.0.3'

defaultConfig {
minSdkVersion 16
targetSdkVersion 26
targetSdkVersion 28
versionName project.VERSION_NAME
versionCode Integer.parseInt(project.VERSION_CODE)
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
Expand Down Expand Up @@ -44,14 +44,15 @@ def isReleaseBuild() {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'commons-codec:commons-codec:1.8'
implementation 'commons-codec:commons-codec:1.10'

testImplementation 'junit:junit:4.12'
testImplementation 'org.assertj:assertj-core:1.7.0'
testImplementation 'org.mockito:mockito-core:1.9.5'
testImplementation 'org.robolectric:robolectric:4.1'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:support-fragment:26.0.0'
testImplementation 'org.json:json:20190722'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.fragment:fragment:1.2.2'
}

apply from: 'maven_push.gradle'
5 changes: 3 additions & 2 deletions androidsdk/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
VERSION_NAME=2.14.1
VERSION_CODE=2141
VERSION_NAME=2.14.2
VERSION_CODE=2142
GROUP=com.wootric

POM_DESCRIPTION=WootricSDK Android
Expand All @@ -16,3 +16,4 @@ POM_DEVELOPER_NAME=Diego Serrano
POM_NAME=WootricSDK Android
POM_ARTIFACT_ID=wootric-sdk-android
POM_PACKAGING=aar

Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,13 @@
package com.wootric.androidsdk;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;

import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import android.util.Log;
import android.view.View;

import com.wootric.androidsdk.network.WootricApiCallback;
import com.wootric.androidsdk.network.WootricRemoteClient;
Expand All @@ -42,8 +39,6 @@
import com.wootric.androidsdk.utils.PreferencesUtils;
import com.wootric.androidsdk.views.support.SurveyFragment;

import java.util.HashMap;

/**
* Created by maciejwitowski on 9/3/15.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import android.app.Activity;
import android.content.Context;
import android.support.v4.app.FragmentActivity;
import androidx.fragment.app.FragmentActivity;

import com.wootric.androidsdk.network.WootricRemoteClient;
import com.wootric.androidsdk.objects.EndUser;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,9 +419,12 @@ public static WootricCustomThankYou fromJson(JSONObject customThankYouJson) thro

wootricCustomThankYou.setLinkText(thankYouLinksJson.optString("thank_you_link_text", null));
wootricCustomThankYou.setLinkUri(Uri.parse(thankYouLinksJson.optString("thank_you_link_url")));
wootricCustomThankYou.setEmailInUrl(thankYouLinkUrlSettings.getBoolean("add_email_param_to_url"));
wootricCustomThankYou.setScoreInUrl(thankYouLinkUrlSettings.getBoolean("add_score_param_to_url"));
wootricCustomThankYou.setCommentInUrl(thankYouLinkUrlSettings.getBoolean("add_comment_param_to_url"));

if (thankYouLinkUrlSettings != null) {
wootricCustomThankYou.setEmailInUrl(thankYouLinkUrlSettings.getBoolean("add_email_param_to_url"));
wootricCustomThankYou.setScoreInUrl(thankYouLinkUrlSettings.getBoolean("add_score_param_to_url"));
wootricCustomThankYou.setCommentInUrl(thankYouLinkUrlSettings.getBoolean("add_comment_param_to_url"));
}

if (thankYouLinkTextList != null) {
wootricCustomThankYou.uniqueByScore = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import android.graphics.drawable.Drawable;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.constraint.ConstraintLayout;
import androidx.constraintlayout.widget.ConstraintLayout;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import androidx.fragment.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
Expand Down
4 changes: 2 additions & 2 deletions androidsdk/src/main/res/layout/wootric_survey_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<android.support.constraint.ConstraintLayout
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/wootric_survey_layout_body"
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand Down Expand Up @@ -177,7 +177,7 @@
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toLeftOf="@+id/wootric_btn_submit"
app:layout_constraintTop_toBottomOf="@+id/wootric_et_feedback" />
</android.support.constraint.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
</ScrollView>
</merge>
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package com.wootric.androidsdk;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.support.v4.app.FragmentActivity;
import androidx.fragment.app.FragmentActivity;

import com.wootric.androidsdk.network.WootricRemoteClient;
import com.wootric.androidsdk.objects.EndUser;
Expand All @@ -19,7 +15,6 @@

import java.util.HashMap;

import static com.wootric.androidsdk.TestHelper.ORIGIN_URL;
import static com.wootric.androidsdk.TestHelper.TEST_FRAGMENT_ACTIVITY;
import static com.wootric.androidsdk.TestHelper.testEndUser;
import static com.wootric.androidsdk.TestHelper.testUser;
Expand Down
24 changes: 23 additions & 1 deletion androidsdk/src/test/java/com/wootric/androidsdk/TestHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.support.v4.app.FragmentActivity;
import androidx.fragment.app.FragmentActivity;

import com.wootric.androidsdk.objects.EndUser;
import com.wootric.androidsdk.objects.User;
import com.wootric.androidsdk.utils.PreferencesUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;

public class TestHelper {
Expand Down Expand Up @@ -50,4 +54,22 @@ public Context getApplicationContext() {
private static final ApplicationInfo TEST_APPLICATION_INFO = new ApplicationInfo() {
public static final String packageName = "";
};

public String loadJSONFromAsset(String fileName) throws IOException {
String json = null;
try {
String path = "src/test/resources/" + fileName;
File file = new File(path);
InputStream is = new FileInputStream(file);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.wootric.androidsdk;

import android.support.v4.app.FragmentActivity;
import androidx.fragment.app.FragmentActivity;

import com.wootric.androidsdk.network.WootricRemoteClient;
import com.wootric.androidsdk.objects.User;
Expand All @@ -12,7 +12,6 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.internal.matchers.Null;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.HashMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

import android.os.Parcel;

import com.wootric.androidsdk.TestHelper;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;

import java.io.IOException;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -243,4 +249,29 @@ public void testWootricCustomThankYouParcelable(){
WootricCustomThankYou createdFromParcel = WootricCustomThankYou.CREATOR.createFromParcel(parcel);
assertThat(createdFromParcel.getTextForScore(10, "NPS", 0)).isEqualTo("testing parcel");
}

@Test
public void testWootricCustomThankYouFromJson() throws IOException, JSONException {
TestHelper helper = new TestHelper();
JSONObject customThankYouJson = new JSONObject(helper.loadJSONFromAsset("custom_thank_you.json"));

WootricCustomThankYou customThankYou = WootricCustomThankYou.fromJson(customThankYouJson);

assertThat(customThankYou).isNotNull();
assertThat(customThankYou.getEmailInUri(0, "NPS", 0)).isTrue();
assertThat(customThankYou.getScoreInUri(0, "NPS", 0)).isFalse();
}

@Test
public void testWootricCustomThankYouFromJsonWhenThankYouLinkUrlSettingsIsNull() throws IOException, JSONException {
TestHelper helper = new TestHelper();
JSONObject customThankYouJson = new JSONObject(helper.loadJSONFromAsset("custom_thank_you.json"));

customThankYouJson.getJSONObject("thank_you_links").remove("thank_you_link_url_settings");

WootricCustomThankYou customThankYou = WootricCustomThankYou.fromJson(customThankYouJson);

assertThat(customThankYou).isNotNull();
assertThat(customThankYouJson.optJSONObject("thank_you_links").optString("thank_you_link_url_settings")).isNullOrEmpty();
}
}
38 changes: 38 additions & 0 deletions androidsdk/src/test/resources/custom_thank_you.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"thank_you_links": {
"thank_you_link_text": "All",
"thank_you_link_url": "https://all.com",
"thank_you_link_url_settings": {
"add_email_param_to_url": "true",
"add_score_param_to_url": "true",
"add_comment_param_to_url": "true"
},
"thank_you_link_text_list": {
"detractor_thank_you_link_text": "Detractor thank you link text",
"passive_thank_you_link_text": "Passive thank you link text",
"promoter_thank_you_link_text": "Promoter thank you link text"
},
"thank_you_link_url_list": {
"detractor_thank_you_link_url": "http://detractor.com/",
"passive_thank_you_link_url": "http://passive.com/",
"promoter_thank_you_link_url": "http://promoter.com/"
},
"thank_you_link_url_settings_list": {
"detractor_thank_you_link_url_settings": {
"add_email_param_to_url": "true",
"add_score_param_to_url": "false",
"add_comment_param_to_url": "true"
},
"passive_thank_you_link_url_settings": {
"add_email_param_to_url": "true",
"add_score_param_to_url": "true",
"add_comment_param_to_url": "true"
},
"promoter_thank_you_link_url_settings": {
"add_email_param_to_url": "true",
"add_score_param_to_url": "true",
"add_comment_param_to_url": "true"
}
}
}
}
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 26
compileSdkVersion 28
buildToolsVersion '28.0.3'

defaultConfig {
applicationId "com.wootric.androidsdk_demo"
minSdkVersion 16
targetSdkVersion 26
targetSdkVersion 28
versionCode 1
versionName "1.0"
}
Expand All @@ -23,5 +23,5 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(':androidsdk')

implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'androidx.appcompat:appcompat:1.0.0'
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.wootric.androidsdk_demo;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import androidx.fragment.app.FragmentActivity;
import android.view.View;

import com.wootric.androidsdk.Wootric;

public class MainActivity extends FragmentActivity {

private static final String CLIENT_ID = "CLIENT ID";
private static final String ACCOUNT_TOKEN = "ACCOUNT TOKEN";
private static final String CLIENT_ID = "CLIENT_ID";
private static final String ACCOUNT_TOKEN = "ACCOUNT_TOKEN";

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
classpath 'com.android.tools.build:gradle:3.6.0'
classpath 'com.github.dcendents:android-maven-plugin:1.2'

// NOTE: Do not place your application dependencies here; they belong
Expand Down
4 changes: 3 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
# org.gradle.parallel=true
android.enableJetifier=true
android.useAndroidX=true
Loading

0 comments on commit 094ee4b

Please sign in to comment.