Skip to content

Feature/proguard and code coverage #136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions android-sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ android {
versionName version_name
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
buildConfigField "String", "CLIENT_VERSION", "\"$version_name\""
consumerProguardFiles '../proguard-rules.txt'
}
testOptions {
unitTests.returnDefaultValues = true
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/****************************************************************************
* Copyright 2017, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
***************************************************************************/

package com.optimizely.ab.android.sdk;

import android.app.UiModeManager;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.test.InstrumentationRegistry;
import android.support.test.espresso.core.deps.guava.util.concurrent.ListeningExecutorService;
import android.support.test.runner.AndroidJUnit4;

import com.optimizely.ab.Optimizely;
import com.optimizely.ab.android.datafile_handler.DefaultDatafileHandler;
import com.optimizely.ab.android.event_handler.DefaultEventHandler;
import com.optimizely.ab.android.user_profile.DefaultUserProfileService;
import com.optimizely.ab.config.Experiment;
import com.optimizely.ab.config.ProjectConfig;
import com.optimizely.ab.config.Variation;
import com.optimizely.ab.event.internal.payload.Event;
import com.optimizely.ab.notification.NotificationListener;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.exceptions.verification.junit.ArgumentsAreDifferent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;

@RunWith(AndroidJUnit4.class)
public class OptimizelyManagerBuilderTest {
private Logger logger = mock(Logger.class);

private String testProjectId = "7595190003";

private String minDatafile = "{\"groups\": [], \"projectId\": \"8504447126\", \"variables\": [{\"defaultValue\": \"true\", \"type\": \"boolean\", \"id\": \"8516291943\", \"key\": \"test_variable\"}], \"version\": \"3\", \"experiments\": [{\"status\": \"Running\", \"key\": \"android_experiment_key\", \"layerId\": \"8499056327\", \"trafficAllocation\": [{\"entityId\": \"8509854340\", \"endOfRange\": 5000}, {\"entityId\": \"8505434669\", \"endOfRange\": 10000}], \"audienceIds\": [], \"variations\": [{\"variables\": [], \"id\": \"8509854340\", \"key\": \"var_1\"}, {\"variables\": [], \"id\": \"8505434669\", \"key\": \"var_2\"}], \"forcedVariations\": {}, \"id\": \"8509139139\"}], \"audiences\": [], \"anonymizeIP\": true, \"attributes\": [], \"revision\": \"7\", \"events\": [{\"experimentIds\": [\"8509139139\"], \"id\": \"8505434668\", \"key\": \"test_event\"}], \"accountId\": \"8362480420\"}";

@Before
public void setUp() throws Exception {
}

@Test
public void testBuilderWith() {
OptimizelyManager manager = OptimizelyManager.builder(testProjectId).withUserProfileService(DefaultUserProfileService.newInstance(testProjectId, InstrumentationRegistry.getTargetContext()))
.withDatafileDownloadInterval(30L)
.withEventDispatchInterval(30L)
.withDatafileHandler(new DefaultDatafileHandler())
.withEventHandler(DefaultEventHandler.getInstance(InstrumentationRegistry.getTargetContext()))
.withLogger(logger).build(InstrumentationRegistry.getTargetContext());

assertNotNull(manager);
assertNotNull(manager.getDatafileHandler());
assertNotNull(manager.getUserProfileService());
assertNotNull(manager.getEventHandler(InstrumentationRegistry.getTargetContext()));
}

@Test
public void testBuilderWithOut() {
OptimizelyManager manager = OptimizelyManager.builder(testProjectId).build(InstrumentationRegistry.getTargetContext());

assertNotNull(manager);
assertNotNull(manager.getDatafileHandler());
assertNotNull(manager.getUserProfileService());
assertNotNull(manager.getEventHandler(InstrumentationRegistry.getTargetContext()));
}

}
1 change: 1 addition & 0 deletions datafile-handler/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ android {
versionCode 1
versionName version_name
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
consumerProguardFiles '../proguard-rules.txt'
}
testOptions {
unitTests.returnDefaultValues = true
Expand Down
1 change: 1 addition & 0 deletions event-handler/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ android {
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
consumerProguardFiles '../proguard-rules.txt'
}
testOptions {
unitTests.returnDefaultValues = true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/****************************************************************************
* Copyright 2017, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
***************************************************************************/

package com.optimizely.ab.android.event_handler;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.util.Pair;

import com.optimizely.ab.event.LogEvent;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

/**
* Tests {@link DefaultEventHandler}
*/
@RunWith(AndroidJUnit4.class)
public class DefaultEventHandlerTest {
private Context context;
private Logger logger;

private DefaultEventHandler eventHandler;
private String url = "http://www.foo.com";
private String requestBody = "key1=val1&key2=val2&key3=val3";

@Before
public void setupEventHandler() {
context = InstrumentationRegistry.getTargetContext();
logger = mock(Logger.class);
eventHandler = DefaultEventHandler.getInstance(context);
eventHandler.logger = logger;

eventHandler.setDispatchInterval(60L);
}

@Test
public void dispatchEventSuccess() throws MalformedURLException {
eventHandler.dispatchEvent(new LogEvent(LogEvent.RequestMethod.POST, url, new HashMap<String, String>(), requestBody));
//verify(context).startService(any(Intent.class));
verify(logger).info("Sent url {} to the event handler service", "http://www.foo.com");
}

@Test
public void dispatchEmptyUrlString() {
eventHandler.dispatchEvent(new LogEvent(LogEvent.RequestMethod.POST, "", new HashMap<String, String>(), requestBody));
verify(logger).error("Event dispatcher received an empty url");
}

@Test
public void dispatchEmptyParams() {
eventHandler.dispatchEvent(new LogEvent(LogEvent.RequestMethod.POST, url, new HashMap<String, String>(), requestBody));
//verify(context).startService(any(Intent.class));
verify(logger).info("Sent url {} to the event handler service", "http://www.foo.com");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/****************************************************************************
* Copyright 2017, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
***************************************************************************/

package com.optimizely.ab.android.event_handler;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.util.Pair;

import com.optimizely.ab.android.shared.Client;
import com.optimizely.ab.event.LogEvent;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.slf4j.Logger;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
* Tests {@link DefaultEventHandler}
*/
@RunWith(AndroidJUnit4.class)
public class EventClientTest {

@Mock
Logger logger = mock(Logger.class);
@Mock
Client client = mock(Client.class);
private HttpURLConnection urlConnection;
private EventClient eventClient;
private Event event;

@Before
public void setupEventClient() throws IOException {
urlConnection = mock(HttpURLConnection.class);
when(urlConnection.getOutputStream()).thenReturn(mock(OutputStream.class));
when(urlConnection.getInputStream()).thenReturn(mock(InputStream.class));
this.eventClient = new EventClient(client, logger);
URL url = new URL("http://www.foo.com");
event = new Event(url, "");
}

@Test
public void testEventClient() {
eventClient.sendEvent(event);

verify(logger).info("Successfully dispatched event: {}",
event);
}
}
42 changes: 42 additions & 0 deletions proguard-rules.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Users/jdeffibaugh/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 *;
#}

## Below are the suggested rules from the developer documentation:
## https://developers.optimizely.com/x/solutions/sdks/reference/index.html?language=android&platform=mobile#installation

# Optimizely
-keep class com.optimizely.ab.** { *; }

# Gson
-keepnames class com.google.gson.Gson

# Safely ignore warnings about other libraries since we are using Gson
-dontwarn com.fasterxml.jackson.**
-dontwarn org.json.**

# Annotations
-dontwarn javax.annotation.**

# Findbugs
-dontwarn edu.umd.cs.findbugs.annotations.SuppressFBWarnings

# slf4j
-dontwarn org.slf4j.**

# Android Logger
-keep class com.noveogroup.android.log.** { *; }
3 changes: 2 additions & 1 deletion shared/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ android {
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
consumerProguardFiles '../proguard-rules.txt'
}
testOptions {
unitTests.returnDefaultValues = true
Expand Down Expand Up @@ -123,4 +124,4 @@ android.libraryVariants.all { variant ->
from variant.javaCompile.source
}
project.artifacts.add("archives", tasks["${variant.name}SourcesJar"]);
}
}
Loading