Skip to content

Commit 1e0ea05

Browse files
authored
Merge pull request #14 from optimizely/mng/move-to-new-endpoint
Update event handler to send events to the new endpoint.
2 parents bb1528d + 9dbd364 commit 1e0ea05

File tree

18 files changed

+102
-98
lines changed

18 files changed

+102
-98
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
* The test app depends on all of the other project modules
2020
* The modules are built from source
2121
* Changes in any modules source will be applied to the test app on the next build
22-
7. Disocver more gradle tasks
22+
7. Discover more gradle tasks
2323
* `./gradlew tasks`
24-
* To see the task of an inidvidual module
24+
* To see the task of an individual module
2525
* `/./gradlew user-experiment-record:tasks`
2626

2727
### Android Studio

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ ext {
2727
min_sdk_version = 14
2828
target_sdk_version = 23
2929

30-
java_core_ver = "0.1.68"
30+
java_core_ver = "0.1.71"
3131
android_logger_ver = "1.3.1"
3232
support_annotations_ver = "23.0.1"
3333
junit_ver = "4.12"

event-handler/src/androidTest/java/com/optimizely/ab/android/event_handler/EventDAOTest.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ public void tearDownEventDAO() {
4747
@Test
4848
public void storeEvent() {
4949
Event event = mock(Event.class);
50-
when(event.toString()).thenReturn("http://www.foo.com?bar=baz");
50+
when(event.toString()).thenReturn("http://www.foo.com");
51+
when(event.getRequestBody()).thenReturn("bar1=baz1");
5152
assertTrue(eventDAO.storeEvent(event));
5253
verify(logger).info("Inserted {} into db", event);
5354
}
@@ -58,9 +59,12 @@ public void getEvents() {
5859
Event event2 = mock(Event.class);
5960
Event event3 = mock(Event.class);
6061

61-
when(event1.toString()).thenReturn("http://www.foo1.com?bar1=baz1");
62-
when(event2.toString()).thenReturn("http://www.foo2.com?bar2=baz2");
63-
when(event3.toString()).thenReturn("http://www.foo3.com?bar3=baz3");
62+
when(event1.toString()).thenReturn("http://www.foo1.com");
63+
when(event1.getRequestBody()).thenReturn("bar1=baz1");
64+
when(event2.toString()).thenReturn("http://www.foo2.com");
65+
when(event2.getRequestBody()).thenReturn("bar2=baz2");
66+
when(event3.toString()).thenReturn("http://www.foo3.com");
67+
when(event3.getRequestBody()).thenReturn("bar3=baz3");
6468

6569
assertTrue(eventDAO.storeEvent(event1));
6670
assertTrue(eventDAO.storeEvent(event2));
@@ -77,17 +81,21 @@ public void getEvents() {
7781
assertEquals(2, pair2.first.longValue());
7882
assertEquals(3, pair3.first.longValue());
7983

80-
assertEquals("http://www.foo1.com?bar1=baz1", pair1.second.toString());
81-
assertEquals("http://www.foo2.com?bar2=baz2", pair2.second.toString());
82-
assertEquals("http://www.foo3.com?bar3=baz3", pair3.second.toString());
84+
assertEquals("http://www.foo1.com", pair1.second.toString());
85+
assertEquals("bar1=baz1", pair1.second.getRequestBody());
86+
assertEquals("http://www.foo2.com", pair2.second.toString());
87+
assertEquals("bar2=baz2", pair2.second.getRequestBody());
88+
assertEquals("http://www.foo3.com", pair3.second.toString());
89+
assertEquals("bar3=baz3", pair3.second.getRequestBody());
8390

8491
verify(logger).info("Got events from SQLite");
8592
}
8693

8794
@Test
8895
public void removeEventSuccess() {
8996
Event event = mock(Event.class);
90-
when(event.toString()).thenReturn("http://www.foo.com?bar=baz");
97+
when(event.toString()).thenReturn("http://www.foo.com");
98+
when(event.getRequestBody()).thenReturn("bar=baz");
9199
assertTrue(eventDAO.storeEvent(event));
92100
assertTrue(eventDAO.removeEvent(1));
93101
verify(logger).info("Removed event with id {} from db", 1L);
@@ -96,7 +104,8 @@ public void removeEventSuccess() {
96104
@Test
97105
public void removeEventInvalid() {
98106
Event event = mock(Event.class);
99-
when(event.toString()).thenReturn("http://www.foo.com?bar=baz");
107+
when(event.toString()).thenReturn("http://www.foo.com");
108+
when(event.getRequestBody()).thenReturn("bar=baz");
100109
assertTrue(eventDAO.storeEvent(event));
101110
assertFalse(eventDAO.removeEvent(2));
102111
verify(logger).error("Tried to remove an event id {} that does not exist", 2L);

event-handler/src/androidTest/java/com/optimizely/ab/android/event_handler/EventDispatcherTest.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ public void setup() {
5757

5858
@Test
5959
public void handleIntentSchedulesWhenEventsLeftInStorage() throws MalformedURLException {
60-
Event event1 = new Event(new URL("http://www.foo1.com"));
61-
Event event2 = new Event(new URL("http://www.foo2.com"));
62-
Event event3= new Event(new URL("http://www.foo3.com"));
60+
Event event1 = new Event(new URL("http://www.foo1.com"), "");
61+
Event event2 = new Event(new URL("http://www.foo2.com"), "");
62+
Event event3= new Event(new URL("http://www.foo3.com"), "");
6363
when(eventDAO.getEvents()).thenReturn(new LinkedList<>(Arrays.asList(
6464
new Pair<>(1L, event1),
6565
new Pair<>(2L, event2),
@@ -87,7 +87,7 @@ public void handleIntentSchedulesWhenEventsLeftInStorage() throws MalformedURLEx
8787
@Test
8888
public void handleIntentSchedulesWhenNewEventFailsToSend() throws MalformedURLException {
8989
String url= "http://www.foo.com";
90-
Event event = new Event(new URL(url));
90+
Event event = new Event(new URL(url), "");
9191

9292
when(eventDAO.getEvents()).thenReturn(new LinkedList<Pair<Long, Event>>());
9393

@@ -107,7 +107,7 @@ public void handleIntentSchedulesWhenNewEventFailsToSend() throws MalformedURLEx
107107
@Test
108108
public void getIntervalFromIntent() throws MalformedURLException {
109109
String url= "http://www.foo.com";
110-
Event event = new Event(new URL(url));
110+
Event event = new Event(new URL(url), "");
111111

112112
when(eventDAO.getEvents()).thenReturn(new LinkedList<Pair<Long, Event>>());
113113

@@ -120,13 +120,16 @@ public void getIntervalFromIntent() throws MalformedURLException {
120120
@Test
121121
public void handleIntentLogsWhenUnableToSendOrStoreEvent() throws MalformedURLException {
122122
String url= "http://www.foo.com";
123-
Event event = new Event(new URL(url));
123+
String requestBody = "param1=123";
124+
Event event = new Event(new URL(url), requestBody);
124125

125126
when(eventDAO.getEvents()).thenReturn(new LinkedList<Pair<Long, Event>>());
126127

127128
Intent mockIntent = mock(Intent.class);
128129
when(mockIntent.hasExtra(EventIntentService.EXTRA_URL)).thenReturn(true);
130+
when(mockIntent.hasExtra(EventIntentService.EXTRA_REQUEST_BODY)).thenReturn(true);
129131
when(mockIntent.getStringExtra(EventIntentService.EXTRA_URL)).thenReturn(url);
132+
when(mockIntent.getStringExtra(EventIntentService.EXTRA_REQUEST_BODY)).thenReturn(requestBody);
130133
when(eventClient.sendEvent(event)).thenReturn(false);
131134
when(eventDAO.storeEvent(event)).thenReturn(false);
132135

event-handler/src/androidTest/java/com/optimizely/ab/android/event_handler/OptlyEventHandlerTest.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import android.content.Intent;
55
import android.support.test.runner.AndroidJUnit4;
66

7+
import com.optimizely.ab.event.LogEvent;
8+
79
import org.junit.Before;
810
import org.junit.Test;
911
import org.junit.runner.RunWith;
@@ -33,45 +35,40 @@ public class OptlyEventHandlerTest {
3335

3436
OptlyEventHandler optlyEventHandler;
3537
String url = "http://www.foo.com";
36-
Map<String,String> params;
38+
String requestBody = "key1=val1&key2=val2&key3=val3";
3739

3840
@Before
3941
public void setupEventHandler() {
4042
context = mock(Context.class);
4143
logger = mock(Logger.class);
4244
optlyEventHandler = OptlyEventHandler.getInstance(context);
4345
optlyEventHandler.logger = logger;
44-
45-
params = new TreeMap<>();
46-
params.put("key1", "val1");
47-
params.put("key2", "val2");
48-
params.put("key3", "val3");
4946
}
5047

5148
@Test
5249
public void dispatchEventSuccess() throws MalformedURLException {
53-
optlyEventHandler.dispatchEvent(url, params);
50+
optlyEventHandler.dispatchEvent(new LogEvent(null, url, null, requestBody));
5451
verify(context).startService(any(Intent.class));
55-
verify(logger).info("Sent url {} to the event handler service", "http://www.foo.com?key1=val1&key2=val2&key3=val3");
52+
verify(logger).info("Sent url {} to the event handler service", "http://www.foo.com");
5653
}
5754

5855
@Test public void dispatchEventNullURL() {
59-
optlyEventHandler.dispatchEvent(null, params);
56+
optlyEventHandler.dispatchEvent(new LogEvent(null, null, null, requestBody));
6057
verify(logger).error("Event dispatcher received a null url");
6158
}
6259

6360
@Test public void dispatchEventNullParams() {
64-
optlyEventHandler.dispatchEvent(url, null);
65-
verify(logger).error("Event dispatcher received a null params map");
61+
optlyEventHandler.dispatchEvent(new LogEvent(null, url, null, null));
62+
verify(logger).error("Event dispatcher received a null request body");
6663
}
6764

6865
@Test public void dispatchEmptyUrlString() {
69-
optlyEventHandler.dispatchEvent("", params);
66+
optlyEventHandler.dispatchEvent(new LogEvent(null, "", null, requestBody));
7067
verify(logger).error("Event dispatcher received an empty url");
7168
}
7269

7370
@Test public void dispatchEmptyParams() {
74-
optlyEventHandler.dispatchEvent(url, new HashMap<String, String>());
71+
optlyEventHandler.dispatchEvent(new LogEvent(null, url, null, ""));
7572
verify(context).startService(any(Intent.class));
7673
verify(logger).info("Sent url {} to the event handler service", "http://www.foo.com");
7774
}

event-handler/src/main/java/com/optimizely/ab/android/event_handler/Event.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.optimizely.ab.android.event_handler;
22

33
import java.io.IOException;
4+
import java.io.OutputStream;
5+
import java.net.HttpURLConnection;
46
import java.net.URL;
57
import java.net.URLConnection;
68

@@ -13,13 +15,29 @@
1315
*/
1416
public class Event {
1517
private URL url;
18+
private String requestBody;
1619

17-
public Event(URL url) {
20+
public Event(URL url, String requestBody) {
1821
this.url = url;
22+
this.requestBody = requestBody;
1923
}
2024

2125
public URLConnection send() throws IOException {
22-
return this.url.openConnection();
26+
HttpURLConnection urlConnection = (HttpURLConnection) this.url.openConnection();
27+
28+
urlConnection.setRequestMethod("POST");
29+
urlConnection.setRequestProperty("Content-Type", "application/json");
30+
urlConnection.setDoOutput(true);
31+
OutputStream outputStream = urlConnection.getOutputStream();
32+
outputStream.write(this.requestBody.getBytes());
33+
outputStream.flush();
34+
outputStream.close();
35+
36+
return urlConnection;
37+
}
38+
39+
public String getRequestBody() {
40+
return this.requestBody;
2341
}
2442

2543
@Override

event-handler/src/main/java/com/optimizely/ab/android/event_handler/EventClient.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ public boolean sendEvent(Event event) {
2525
logger.info("Dispatching event: {}", event);
2626
HttpURLConnection urlConnection = (HttpURLConnection) event.send();
2727
int status = urlConnection.getResponseCode();
28-
if (status >= 200 && status < 300) {
29-
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
30-
in.close();
28+
if (status >= 200 && status < 300) {
29+
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
30+
in.close();
3131
return true;
3232
} else {
3333
logger.error("Unexpected response from event endpoint, status: " + status);

event-handler/src/main/java/com/optimizely/ab/android/event_handler/EventDAO.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ static EventDAO getInstance(@NonNull Context context, @NonNull Logger logger) {
3838
public boolean storeEvent(@NonNull Event event) {
3939
ContentValues values = new ContentValues();
4040
values.put(EventTable.Column.URL, event.toString());
41+
values.put(EventTable.Column.REQUEST_BODY, event.getRequestBody());
4142

4243
// Since we are setting the "null column hack" param to null empty values will not be inserted
4344
// at all instead of inserting null.
@@ -56,7 +57,8 @@ public List<Pair<Long, Event>> getEvents() {
5657
// you will actually use after this query.
5758
String[] projection = {
5859
EventTable.Column._ID,
59-
EventTable.Column.URL
60+
EventTable.Column.URL,
61+
EventTable.Column.REQUEST_BODY,
6062
};
6163

6264
Cursor cursor = dbHelper.getReadableDatabase().query(
@@ -77,8 +79,11 @@ public List<Pair<Long, Event>> getEvents() {
7779
String url = cursor.getString(
7880
cursor.getColumnIndexOrThrow(EventTable.Column.URL)
7981
);
82+
String requestBody = cursor.getString(
83+
cursor.getColumnIndexOrThrow(EventTable.Column.REQUEST_BODY)
84+
);
8085
try {
81-
events.add(new Pair<>(itemId, new Event(new URL(url))));
86+
events.add(new Pair<>(itemId, new Event(new URL(url), requestBody)));
8287
} catch (MalformedURLException e) {
8388
logger.error("Retrieved a malformed event from storage", e);
8489

event-handler/src/main/java/com/optimizely/ab/android/event_handler/EventDispatcher.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ public void dispatch(@NonNull Intent intent) {
5050
if (intent.hasExtra(EventIntentService.EXTRA_URL)) {
5151
try {
5252
String urlExtra = intent.getStringExtra(EventIntentService.EXTRA_URL);
53-
Event event = new Event(new URL(urlExtra));
53+
String requestBody = intent.getStringExtra(EventIntentService.EXTRA_REQUEST_BODY);
54+
Event event = new Event(new URL(urlExtra), requestBody);
5455
// Send the event that triggered this run of the service for store it if sending fails
5556
dispatched = dispatch(event);
5657
} catch (MalformedURLException e) {

event-handler/src/main/java/com/optimizely/ab/android/event_handler/EventIntentService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class EventIntentService extends IntentService {
1515
Logger logger = LoggerFactory.getLogger(EventIntentService.class);
1616

1717
static final String EXTRA_URL = "com.optimizely.ab.android.EXTRA_URL";
18+
static final String EXTRA_REQUEST_BODY = "com.optimizely.ab.andrdoid.EXTRA_REQUEST_BODY";
1819
static final String EXTRA_INTERVAL = "com.optimizely.ab.android.EXTRA_INTERVAL";
1920

2021
@Nullable EventDispatcher eventDispatcher;

0 commit comments

Comments
 (0)