Skip to content

Commit 50bb81f

Browse files
Feature/proguard and code coverage (#136)
* add code coverage up to 75% or so. add consumerProguardFiles * copyright date needs to be 2017
1 parent ac8db66 commit 50bb81f

File tree

12 files changed

+795
-9
lines changed

12 files changed

+795
-9
lines changed

android-sdk/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ android {
3232
versionName version_name
3333
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
3434
buildConfigField "String", "CLIENT_VERSION", "\"$version_name\""
35+
consumerProguardFiles '../proguard-rules.txt'
3536
}
3637
testOptions {
3738
unitTests.returnDefaultValues = true

android-sdk/src/androidTest/java/com/optimizely/ab/android/sdk/OptimizelyClientTest.java

+373
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/****************************************************************************
2+
* Copyright 2017, Optimizely, Inc. and contributors *
3+
* *
4+
* Licensed under the Apache License, Version 2.0 (the "License"); *
5+
* you may not use this file except in compliance with the License. *
6+
* You may obtain a copy of the License at *
7+
* *
8+
* http://www.apache.org/licenses/LICENSE-2.0 *
9+
* *
10+
* Unless required by applicable law or agreed to in writing, software *
11+
* distributed under the License is distributed on an "AS IS" BASIS, *
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13+
* See the License for the specific language governing permissions and *
14+
* limitations under the License. *
15+
***************************************************************************/
16+
17+
package com.optimizely.ab.android.sdk;
18+
19+
import android.app.UiModeManager;
20+
import android.content.Context;
21+
import android.content.res.Configuration;
22+
import android.os.Build;
23+
import android.support.annotation.RequiresApi;
24+
import android.support.test.InstrumentationRegistry;
25+
import android.support.test.espresso.core.deps.guava.util.concurrent.ListeningExecutorService;
26+
import android.support.test.runner.AndroidJUnit4;
27+
28+
import com.optimizely.ab.Optimizely;
29+
import com.optimizely.ab.android.datafile_handler.DefaultDatafileHandler;
30+
import com.optimizely.ab.android.event_handler.DefaultEventHandler;
31+
import com.optimizely.ab.android.user_profile.DefaultUserProfileService;
32+
import com.optimizely.ab.config.Experiment;
33+
import com.optimizely.ab.config.ProjectConfig;
34+
import com.optimizely.ab.config.Variation;
35+
import com.optimizely.ab.event.internal.payload.Event;
36+
import com.optimizely.ab.notification.NotificationListener;
37+
38+
import org.junit.Assert;
39+
import org.junit.Before;
40+
import org.junit.Test;
41+
import org.junit.runner.RunWith;
42+
import org.mockito.Mock;
43+
import org.mockito.exceptions.verification.junit.ArgumentsAreDifferent;
44+
import org.slf4j.Logger;
45+
import org.slf4j.LoggerFactory;
46+
47+
import java.util.Collections;
48+
import java.util.HashMap;
49+
import java.util.Map;
50+
51+
import static junit.framework.Assert.assertEquals;
52+
import static junit.framework.Assert.assertFalse;
53+
import static junit.framework.Assert.assertNotNull;
54+
import static junit.framework.Assert.assertNull;
55+
import static junit.framework.Assert.assertTrue;
56+
import static org.mockito.Mockito.mock;
57+
import static org.mockito.Mockito.verify;
58+
import static org.mockito.Mockito.verifyZeroInteractions;
59+
import static org.mockito.Mockito.when;
60+
61+
@RunWith(AndroidJUnit4.class)
62+
public class OptimizelyManagerBuilderTest {
63+
private Logger logger = mock(Logger.class);
64+
65+
private String testProjectId = "7595190003";
66+
67+
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\"}";
68+
69+
@Before
70+
public void setUp() throws Exception {
71+
}
72+
73+
@Test
74+
public void testBuilderWith() {
75+
OptimizelyManager manager = OptimizelyManager.builder(testProjectId).withUserProfileService(DefaultUserProfileService.newInstance(testProjectId, InstrumentationRegistry.getTargetContext()))
76+
.withDatafileDownloadInterval(30L)
77+
.withEventDispatchInterval(30L)
78+
.withDatafileHandler(new DefaultDatafileHandler())
79+
.withEventHandler(DefaultEventHandler.getInstance(InstrumentationRegistry.getTargetContext()))
80+
.withLogger(logger).build(InstrumentationRegistry.getTargetContext());
81+
82+
assertNotNull(manager);
83+
assertNotNull(manager.getDatafileHandler());
84+
assertNotNull(manager.getUserProfileService());
85+
assertNotNull(manager.getEventHandler(InstrumentationRegistry.getTargetContext()));
86+
}
87+
88+
@Test
89+
public void testBuilderWithOut() {
90+
OptimizelyManager manager = OptimizelyManager.builder(testProjectId).build(InstrumentationRegistry.getTargetContext());
91+
92+
assertNotNull(manager);
93+
assertNotNull(manager.getDatafileHandler());
94+
assertNotNull(manager.getUserProfileService());
95+
assertNotNull(manager.getEventHandler(InstrumentationRegistry.getTargetContext()));
96+
}
97+
98+
}

datafile-handler/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ android {
3131
versionCode 1
3232
versionName version_name
3333
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
34+
consumerProguardFiles '../proguard-rules.txt'
3435
}
3536
testOptions {
3637
unitTests.returnDefaultValues = true

event-handler/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ android {
3131
versionCode 1
3232
versionName "1.0"
3333
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
34+
consumerProguardFiles '../proguard-rules.txt'
3435
}
3536
testOptions {
3637
unitTests.returnDefaultValues = true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/****************************************************************************
2+
* Copyright 2017, Optimizely, Inc. and contributors *
3+
* *
4+
* Licensed under the Apache License, Version 2.0 (the "License"); *
5+
* you may not use this file except in compliance with the License. *
6+
* You may obtain a copy of the License at *
7+
* *
8+
* http://www.apache.org/licenses/LICENSE-2.0 *
9+
* *
10+
* Unless required by applicable law or agreed to in writing, software *
11+
* distributed under the License is distributed on an "AS IS" BASIS, *
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13+
* See the License for the specific language governing permissions and *
14+
* limitations under the License. *
15+
***************************************************************************/
16+
17+
package com.optimizely.ab.android.event_handler;
18+
19+
import android.content.Context;
20+
import android.content.Intent;
21+
import android.os.Build;
22+
import android.support.annotation.RequiresApi;
23+
import android.support.test.InstrumentationRegistry;
24+
import android.support.test.runner.AndroidJUnit4;
25+
import android.util.Pair;
26+
27+
import com.optimizely.ab.event.LogEvent;
28+
29+
import org.junit.After;
30+
import org.junit.Before;
31+
import org.junit.Test;
32+
import org.junit.runner.RunWith;
33+
import org.slf4j.Logger;
34+
35+
import java.net.MalformedURLException;
36+
import java.net.URL;
37+
import java.util.HashMap;
38+
import java.util.List;
39+
40+
import static junit.framework.Assert.assertEquals;
41+
import static junit.framework.Assert.assertFalse;
42+
import static junit.framework.Assert.assertTrue;
43+
import static org.mockito.Matchers.any;
44+
import static org.mockito.Mockito.mock;
45+
import static org.mockito.Mockito.verify;
46+
47+
/**
48+
* Tests {@link DefaultEventHandler}
49+
*/
50+
@RunWith(AndroidJUnit4.class)
51+
public class DefaultEventHandlerTest {
52+
private Context context;
53+
private Logger logger;
54+
55+
private DefaultEventHandler eventHandler;
56+
private String url = "http://www.foo.com";
57+
private String requestBody = "key1=val1&key2=val2&key3=val3";
58+
59+
@Before
60+
public void setupEventHandler() {
61+
context = InstrumentationRegistry.getTargetContext();
62+
logger = mock(Logger.class);
63+
eventHandler = DefaultEventHandler.getInstance(context);
64+
eventHandler.logger = logger;
65+
66+
eventHandler.setDispatchInterval(60L);
67+
}
68+
69+
@Test
70+
public void dispatchEventSuccess() throws MalformedURLException {
71+
eventHandler.dispatchEvent(new LogEvent(LogEvent.RequestMethod.POST, url, new HashMap<String, String>(), requestBody));
72+
//verify(context).startService(any(Intent.class));
73+
verify(logger).info("Sent url {} to the event handler service", "http://www.foo.com");
74+
}
75+
76+
@Test
77+
public void dispatchEmptyUrlString() {
78+
eventHandler.dispatchEvent(new LogEvent(LogEvent.RequestMethod.POST, "", new HashMap<String, String>(), requestBody));
79+
verify(logger).error("Event dispatcher received an empty url");
80+
}
81+
82+
@Test
83+
public void dispatchEmptyParams() {
84+
eventHandler.dispatchEvent(new LogEvent(LogEvent.RequestMethod.POST, url, new HashMap<String, String>(), requestBody));
85+
//verify(context).startService(any(Intent.class));
86+
verify(logger).info("Sent url {} to the event handler service", "http://www.foo.com");
87+
}
88+
89+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/****************************************************************************
2+
* Copyright 2017, Optimizely, Inc. and contributors *
3+
* *
4+
* Licensed under the Apache License, Version 2.0 (the "License"); *
5+
* you may not use this file except in compliance with the License. *
6+
* You may obtain a copy of the License at *
7+
* *
8+
* http://www.apache.org/licenses/LICENSE-2.0 *
9+
* *
10+
* Unless required by applicable law or agreed to in writing, software *
11+
* distributed under the License is distributed on an "AS IS" BASIS, *
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13+
* See the License for the specific language governing permissions and *
14+
* limitations under the License. *
15+
***************************************************************************/
16+
17+
package com.optimizely.ab.android.event_handler;
18+
19+
import android.content.Context;
20+
import android.content.Intent;
21+
import android.os.Build;
22+
import android.support.annotation.RequiresApi;
23+
import android.support.test.InstrumentationRegistry;
24+
import android.support.test.runner.AndroidJUnit4;
25+
import android.util.Pair;
26+
27+
import com.optimizely.ab.android.shared.Client;
28+
import com.optimizely.ab.event.LogEvent;
29+
30+
import org.junit.After;
31+
import org.junit.Before;
32+
import org.junit.Test;
33+
import org.junit.runner.RunWith;
34+
import org.mockito.Mock;
35+
import org.slf4j.Logger;
36+
37+
import java.io.IOException;
38+
import java.io.InputStream;
39+
import java.io.OutputStream;
40+
import java.net.HttpURLConnection;
41+
import java.net.MalformedURLException;
42+
import java.net.URL;
43+
import java.util.HashMap;
44+
import java.util.List;
45+
46+
import static junit.framework.Assert.assertEquals;
47+
import static junit.framework.Assert.assertFalse;
48+
import static junit.framework.Assert.assertTrue;
49+
import static org.mockito.Matchers.any;
50+
import static org.mockito.Mockito.mock;
51+
import static org.mockito.Mockito.verify;
52+
import static org.mockito.Mockito.when;
53+
54+
/**
55+
* Tests {@link DefaultEventHandler}
56+
*/
57+
@RunWith(AndroidJUnit4.class)
58+
public class EventClientTest {
59+
60+
@Mock
61+
Logger logger = mock(Logger.class);
62+
@Mock
63+
Client client = mock(Client.class);
64+
private HttpURLConnection urlConnection;
65+
private EventClient eventClient;
66+
private Event event;
67+
68+
@Before
69+
public void setupEventClient() throws IOException {
70+
urlConnection = mock(HttpURLConnection.class);
71+
when(urlConnection.getOutputStream()).thenReturn(mock(OutputStream.class));
72+
when(urlConnection.getInputStream()).thenReturn(mock(InputStream.class));
73+
this.eventClient = new EventClient(client, logger);
74+
URL url = new URL("http://www.foo.com");
75+
event = new Event(url, "");
76+
}
77+
78+
@Test
79+
public void testEventClient() {
80+
eventClient.sendEvent(event);
81+
82+
verify(logger).info("Successfully dispatched event: {}",
83+
event);
84+
}
85+
}

proguard-rules.txt

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in /Users/jdeffibaugh/Library/Android/sdk/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
18+
19+
## Below are the suggested rules from the developer documentation:
20+
## https://developers.optimizely.com/x/solutions/sdks/reference/index.html?language=android&platform=mobile#installation
21+
22+
# Optimizely
23+
-keep class com.optimizely.ab.** { *; }
24+
25+
# Gson
26+
-keepnames class com.google.gson.Gson
27+
28+
# Safely ignore warnings about other libraries since we are using Gson
29+
-dontwarn com.fasterxml.jackson.**
30+
-dontwarn org.json.**
31+
32+
# Annotations
33+
-dontwarn javax.annotation.**
34+
35+
# Findbugs
36+
-dontwarn edu.umd.cs.findbugs.annotations.SuppressFBWarnings
37+
38+
# slf4j
39+
-dontwarn org.slf4j.**
40+
41+
# Android Logger
42+
-keep class com.noveogroup.android.log.** { *; }

shared/build.gradle

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ android {
3131
versionCode 1
3232
versionName "1.0"
3333
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
34+
consumerProguardFiles '../proguard-rules.txt'
3435
}
3536
testOptions {
3637
unitTests.returnDefaultValues = true
@@ -123,4 +124,4 @@ android.libraryVariants.all { variant ->
123124
from variant.javaCompile.source
124125
}
125126
project.artifacts.add("archives", tasks["${variant.name}SourcesJar"]);
126-
}
127+
}

0 commit comments

Comments
 (0)