Skip to content
This repository has been archived by the owner on Jan 15, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2 from ardevd/1-profiledata
Browse files Browse the repository at this point in the history
Implemented the /profile API for user profile data parsing
  • Loading branch information
ardevd committed Apr 5, 2018
2 parents 76b39e2 + 1e2f839 commit 7808008
Show file tree
Hide file tree
Showing 15 changed files with 381 additions and 96 deletions.
73 changes: 45 additions & 28 deletions app/build.gradle
@@ -1,7 +1,8 @@
apply plugin: 'com.android.application'
apply plugin: 'org.sonarqube'
apply plugin: 'net.saliman.cobertura'
apply plugin: 'com.github.ksoichiro.console.reporter'
apply plugin: 'jacoco-android'


android {
signingConfigs {
Expand All @@ -13,7 +14,7 @@ android {
minSdkVersion 23
targetSdkVersion 27
versionCode 42
versionName "1.0.4"
versionName project.habitatVersionName
vectorDrawables.useSupportLibrary = true
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
Expand All @@ -28,6 +29,14 @@ android {
path "CMakeLists.txt"
}
}

testOptions {
unitTests.all {
jacoco {
includeNoLocationClasses = true
}
}
}
}

apply plugin: 'kotlin-android'
Expand All @@ -49,6 +58,8 @@ dependencies {
// Dependencies for local unit tests
testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-all:1.10.19'
testImplementation 'org.json:json:20140107'

// Google stuff
implementation 'com.android.support:appcompat-v7:27.1.0'
implementation 'com.android.support:design:27.1.0'
Expand All @@ -74,38 +85,44 @@ dependencies {
apply plugin: 'com.google.gms.google-services'
}

consoleReporter {
jacoco {
reportFile = new File("${project.buildDir}/reports/jacoco/jacocoTestDebugUnitTestReport/jacocoTestDebugUnitTestReport.xml")
}
}

jacocoAndroidUnitTestReport {

excludes += ['**/*Activity.*',
'**/*Fragment.*',
'**/*DbHelper.*',
'**/*BackupNotifications.*',
'**/*GooglePlayServicesHelper.*',
'**/*KeyStoreHelper.*',
'**/*InputStreamVolleyRequest.*',
'**/*PaletteHelper.*',
'**/*UserCredentialsManager.*',
'**/adapters/**',
'**/base/**',
'**/dialogs/**',
'**/fcm/**',
'**/jobservices/**',
'**/participate/**',
'**/preferences/**',
'**/provider/**',
'**/splash/**',
'**/zautomation/**']
}

sonarqube {
properties {
property "sonar.projectName", "Habitat"
property "sonar.projectKey", "android_habitat"
property "sonar.sources","src/main/java"
property "sonar.language","java"
property "sonar.java.coveragePlugin", "jacoco"
property "sonar.jacoco.reportPaths","${project.buildDir}/jacoco/testDebugUnitTest.exec"
}
}


cobertura {
coverageIncludes = ['.*no.aegisdynamics.habitat.automationsadd.*',
'.*no.aegisdynamics.habitat.automations.*',
'.*no.aegisdynamics.habitat.backup.*',
'.*no.aegisdynamics.habitat.controller',
'.*no.aegisdynamics.habitat.dashboard.*',
'.*no.aegisdynamics.habitat.data.*',
'.*no.aegisdynamics.habitat.devicedetail',
'.*no.aegisdynamics.habitat.devices.*',
'.*no.aegisdynamics.habitat.locationadd.*',
'.*no.aegisdynamics.habitat.locations.*',
'.*no.aegisdynamics.habitat.notifications.*',
'.*no.aegisdynamics.habitat.setup.*',
'.*no.aegisdynamics.habitat.util.*']

coverageExcludes = ['.*Activity.*',
'.*Fragment.*',
'.*DbHelper.*',
'.*BackupNotifications.*',
'.*GooglePlayServicesHelper.*',
'.*KeyStoreHelper.*',
'.*InputStreamVolleyRequest.*',
'.*PaletteHelper.*',
'.*UserCredentialsManager.*']
}
tasks.sonarqube.dependsOn jacocoTestReport
Expand Up @@ -168,7 +168,7 @@ public void showBackupsLoaded(List<Backup> backups) {
View view = getView();
if (view != null) {
RelativeLayout emptyBackupLayout = view.findViewById(R.id.backups_empty_layout);
if (backups.size() > 0) {
if (!backups.isEmpty()) {
emptyBackupLayout.setVisibility(View.GONE);
} else {
emptyBackupLayout.setVisibility(View.VISIBLE);
Expand Down
Expand Up @@ -25,6 +25,7 @@

import no.aegisdynamics.habitat.R;
import no.aegisdynamics.habitat.util.NotificationTimestampComparator;
import no.aegisdynamics.habitat.util.VolleyResponseHelper;
import no.aegisdynamics.habitat.zautomation.ZWayNetworkHelper;
import no.aegisdynamics.habitat.util.RequestQueueSingelton;

Expand Down Expand Up @@ -56,18 +57,11 @@ public void updateNotification(final Context context, final long notificationId,

@Override
public void onResponse(JSONObject response) {
try {
// Verify response code
int responseCode = response.getInt("code");
if (responseCode == 200) {
callback.onLoaded(true);
} else {
callback.onError(context.getString(R.string.error_generic));
}

} catch (JSONException e) {
callback.onError(e.getMessage());
e.printStackTrace();
// Verify response code
if (VolleyResponseHelper.hasResponseReturnedOK(response)) {
callback.onLoaded(true);
} else {
callback.onError(context.getString(R.string.error_generic));
}
}
}, new Response.ErrorListener() {
Expand Down Expand Up @@ -113,18 +107,10 @@ public void deleteNotification(final Context context, final long notificationId,

@Override
public void onResponse(JSONObject response) {
try {
// Verify response code
int responseCode = response.getInt("code");
if (responseCode == 200) {
callback.onLoaded(true);
} else {
callback.onError(context.getString(R.string.error_generic));
}

} catch (JSONException e) {
callback.onError(e.getMessage());
e.printStackTrace();
if (VolleyResponseHelper.hasResponseReturnedOK(response)) {
callback.onLoaded(true);
} else {
callback.onError(context.getString(R.string.error_generic));
}
}
}, new Response.ErrorListener() {
Expand Down Expand Up @@ -170,8 +156,7 @@ public void onResponse(JSONObject response) {
List<Notification> notifications = new ArrayList<>();
try {
// Verify response code
int responseCode = response.getInt("code");
if (responseCode == 200) {
if (VolleyResponseHelper.hasResponseReturnedOK(response)) {

JSONObject dataObjects = response.getJSONObject("data");
JSONArray notificationsArray = dataObjects.getJSONArray("notifications");
Expand Down
Expand Up @@ -5,6 +5,7 @@ package no.aegisdynamics.habitat.data.profile
*/

class Profile(val id: Int,
val userName: String?,
val name: String?,
val email: String?,
val dashboardDevices: List<String>)
@@ -0,0 +1,47 @@
package no.aegisdynamics.habitat.data.profile;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
* Helper class for parsing profile data from JSON API.
*/
public class ProfileDataHelper {

private ProfileDataHelper() {
// Empty constructor
}

/**
* Parse profile data from JSONObject.
* @param dataObject JSONObject containing profile data from Z-Way API
* @return valid user Profile object.
*/
public static Profile getProfileFromJsonData(JSONObject dataObject) {

try {
int profileId = dataObject.getInt("id");
String profileUserName = dataObject.getString("login");
String profileEmail = dataObject.getString("email");
String profileName = dataObject.getString("name");

// parse dashboard devices
JSONArray dashboardDevices = dataObject.getJSONArray("dashboard");
List<String> dashboardDevicesList = new ArrayList<>();
for (int i = 0; i < dashboardDevices.length(); i++) {
dashboardDevicesList.add(dashboardDevices.getString(i));
}

return new Profile(profileId, profileUserName, profileName, profileEmail,
dashboardDevicesList);
} catch (JSONException ex) {
return null;
}


}
}
Expand Up @@ -8,18 +8,14 @@
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import no.aegisdynamics.habitat.R;
import no.aegisdynamics.habitat.util.RequestQueueSingelton;
import no.aegisdynamics.habitat.util.VolleyResponseHelper;
import no.aegisdynamics.habitat.zautomation.ZWayNetworkHelper;

/**
Expand All @@ -38,47 +34,20 @@ public void getProfile(final Context context, final String username, final Profi
@Override
public void onResponse(JSONObject response) {
try {
// Verify response code
int responseCode = response.getInt("code");
if (responseCode == 200) {

JSONArray dataArray = response.getJSONArray("data");
Pattern pattern = Pattern.compile("(\\D+)");

for (int i = 0; i < dataArray.length(); i++) {
try {
JSONObject profileObject = dataArray.getJSONObject(i);
// Check if this is the correct profile for the given username.

Matcher matcher = pattern.matcher(profileObject.getString("qrcode"));
if (matcher.find()) {

if (matcher.group(0).equals(username)) {
// Parse user profile data
String name = profileObject.getString("name");
String email = profileObject.getString("email");
int userId = profileObject.getInt("id");
JSONArray dashboardDevices = profileObject.getJSONArray("dashboard");
List<String> dashboardDevicesList = new ArrayList();
for (int x = 0; x < dashboardDevices.length(); x++) {
dashboardDevicesList.add(dashboardDevices.getString(x));
}

callback.onLoaded(new Profile(userId, name, email, dashboardDevicesList));
return;
}
}

} catch (JSONException e) {
}
if (VolleyResponseHelper.hasResponseReturnedOK(response)) {

JSONObject dataObject = response.getJSONObject("data");
Profile userProfile = ProfileDataHelper.getProfileFromJsonData(dataObject);
if (userProfile != null) {
callback.onLoaded(userProfile);
}
}

} catch (JSONException e) {
e.printStackTrace();
}

callback.onError("No profile found");
callback.onError("No profile data found");

}
}, new Response.ErrorListener() {
Expand Down
@@ -0,0 +1,23 @@
package no.aegisdynamics.habitat.util;

import org.json.JSONException;
import org.json.JSONObject;

/**
* Helper class for Volley response handling
*/
public class VolleyResponseHelper {

private VolleyResponseHelper() {
// empty constructor
}

public static boolean hasResponseReturnedOK(JSONObject response) {
try {
int responseCode = response.getInt("code");
return responseCode == 200;
} catch (JSONException ex) {
return false;
}
}
}
Expand Up @@ -162,7 +162,7 @@ public static String getZwayAppSupportModule(Context context) {
}

public static String getZwayProfileUrl(Context context) {
return String.format("%s%s/ZAutomation/api/v1/profiles", getURLPrefix(context),
return String.format("%s%s/ZAutomation/api/v1/session", getURLPrefix(context),
getZWayServerHostname(context));
}

Expand Down
Expand Up @@ -121,7 +121,7 @@ public void loadUserProfileData() {
verify(mDashboardView).setProgressIndicator(true);
verify(mProfileRepository).getProfile(any(String.class), mGetProfileCallbackCaptor.capture());
mGetProfileCallbackCaptor.getValue().onProfileLoaded(new Profile(0, "admin",
"admin@mail.com", dashboardDevices));
"Administrator", "admin@mail.com", dashboardDevices));

verify(mDashboardView).onDashboardDevicesRetrieved(dashboardDevices);

Expand Down

0 comments on commit 7808008

Please sign in to comment.