Skip to content

Commit

Permalink
Geo context cleanup (#71)
Browse files Browse the repository at this point in the history
* check for empty geo context

* lint

* check for NPE & inverse test case as a sanity check
  • Loading branch information
shandilya3 authored Jan 8, 2024
1 parent b4b58e2 commit 03e0d3e
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import static org.apache.http.HttpStatus.SC_OK;

import com.adobe.target.delivery.v1.model.DeliveryResponse;
import com.adobe.target.delivery.v1.model.Geo;
import com.adobe.target.delivery.v1.model.Telemetry;
import com.adobe.target.edge.client.ClientConfig;
import com.adobe.target.edge.client.http.DefaultTargetHttpClient;
Expand All @@ -26,12 +27,14 @@
import com.adobe.target.edge.client.utils.CookieUtils;
import com.adobe.target.edge.client.utils.StringUtils;
import com.adobe.target.edge.client.utils.TimingTool;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.concurrent.CompletableFuture;

import kong.unirest.HttpResponse;
import kong.unirest.UnirestParsingException;
import org.slf4j.Logger;
Expand Down Expand Up @@ -94,7 +97,7 @@ public TargetDeliveryResponse executeRequest(TargetDeliveryRequest deliveryReque
if (!telemetry.getEntries().isEmpty()) {
deliveryRequest.getDeliveryRequest().setTelemetry(telemetry);
}

cleanUpGeoContext(deliveryRequest);
ResponseWrapper<DeliveryResponse> response = callDeliveryApi(deliveryRequest);

targetDeliveryResponse = getTargetDeliveryResponse(deliveryRequest, response.getHttpResponse());
Expand All @@ -121,6 +124,7 @@ public CompletableFuture<TargetDeliveryResponse> executeRequestAsync(
if (!telemetry.getEntries().isEmpty()) {
deliveryRequest.getDeliveryRequest().setTelemetry(telemetry);
}
cleanUpGeoContext(deliveryRequest);
CompletableFuture<ResponseWrapper<DeliveryResponse>> responseCompletableFuture =
callDeliveryApiAsync(deliveryRequest);
return responseCompletableFuture.thenApply(
Expand All @@ -147,6 +151,7 @@ public ResponseStatus executeNotification(TargetDeliveryRequest deliveryRequest)
if (!telemetry.getEntries().isEmpty()) {
deliveryRequest.getDeliveryRequest().setTelemetry(telemetry);
}
cleanUpGeoContext(deliveryRequest);
ResponseWrapper response = callDeliveryApi(deliveryRequest);
targetDeliveryResponse = getTargetDeliveryResponse(deliveryRequest, response.getHttpResponse());
telemetryService.addTelemetry(
Expand All @@ -170,6 +175,7 @@ public CompletableFuture<ResponseStatus> executeNotificationAsync(
if (!telemetry.getEntries().isEmpty()) {
deliveryRequest.getDeliveryRequest().setTelemetry(telemetry);
}
cleanUpGeoContext(deliveryRequest);
CompletableFuture<ResponseWrapper<DeliveryResponse>> responseCompletableFuture =
callDeliveryApiAsync(deliveryRequest);
return responseCompletableFuture.thenApply(
Expand Down Expand Up @@ -257,4 +263,24 @@ private CompletableFuture<ResponseWrapper<DeliveryResponse>> callDeliveryApiAsyn
deliveryRequest.getDeliveryRequest(),
DeliveryResponse.class);
}

private void cleanUpGeoContext(TargetDeliveryRequest deliveryRequest) {
Geo geo = null;
if (deliveryRequest.getDeliveryRequest() != null
&& deliveryRequest.getDeliveryRequest().getContext() != null) {
geo = deliveryRequest.getDeliveryRequest().getContext().getGeo();
}

if (geo != null) {
if (geo.getStateCode() != null && geo.getStateCode().isEmpty()) {
geo.setStateCode(null);
}
if (geo.getCity() != null && geo.getCity().isEmpty()) {
geo.setCity(null);
}
if (geo.getCountryCode() != null && geo.getCountryCode().isEmpty()) {
geo.setCountryCode(null);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import static com.adobe.target.edge.client.utils.TargetTestDeliveryRequestUtils.getMboxPrefetchLocalRequest;
import static com.adobe.target.edge.client.utils.TargetTestDeliveryRequestUtils.getTestDeliveryResponse;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand All @@ -24,6 +26,7 @@
import com.adobe.target.delivery.v1.model.DeliveryRequest;
import com.adobe.target.delivery.v1.model.DeliveryResponse;
import com.adobe.target.delivery.v1.model.ExecuteRequest;
import com.adobe.target.delivery.v1.model.Geo;
import com.adobe.target.delivery.v1.model.MetricType;
import com.adobe.target.delivery.v1.model.Notification;
import com.adobe.target.delivery.v1.model.PrefetchRequest;
Expand Down Expand Up @@ -209,6 +212,65 @@ public void testExecuteNotificationAsync() {
any(Map.class), any(String.class), any(DeliveryRequest.class), any(Class.class));
}

@Test
public void testContextCleanUpBeforeCallToDeliveryApi() {
Geo geo = new Geo();
geo.setStateCode("");
geo.setCity("");
geo.setCountryCode("");

Context context = new Context();
context.setGeo(geo);

ResponseWrapper<DeliveryResponse> mockedResponseWrapper = getTestDeliveryResponse();
getMockedTelemetry();
Mockito.lenient()
.doReturn(mockedResponseWrapper)
.when(targetHttpClient)
.execute(any(Map.class), any(String.class), any(DeliveryRequest.class), any(Class.class));

TargetDeliveryRequest targetDeliveryRequestMock = getDeliveryRequest();
targetDeliveryRequestMock.getDeliveryRequest().setContext(context);
targetService.executeRequest(targetDeliveryRequestMock);

assertNull(targetDeliveryRequestMock.getDeliveryRequest().getContext().getGeo().getStateCode());
assertNull(targetDeliveryRequestMock.getDeliveryRequest().getContext().getGeo().getCity());
assertNull(targetDeliveryRequestMock.getDeliveryRequest().getContext().getGeo().getCountryCode());

verify(targetHttpClient, times(1))
.execute(any(Map.class), any(String.class), any(DeliveryRequest.class), any(Class.class));

}
@Test
public void inverseTestContextCleanUpBeforeCallToDeliveryApi() {
Geo geo = new Geo();
geo.setStateCode("CA");
geo.setCity("Foster City");
geo.setCountryCode("011");

Context context = new Context();
context.setGeo(geo);

ResponseWrapper<DeliveryResponse> mockedResponseWrapper = getTestDeliveryResponse();
getMockedTelemetry();
Mockito.lenient()
.doReturn(mockedResponseWrapper)
.when(targetHttpClient)
.execute(any(Map.class), any(String.class), any(DeliveryRequest.class), any(Class.class));

TargetDeliveryRequest targetDeliveryRequestMock = getDeliveryRequest();
targetDeliveryRequestMock.getDeliveryRequest().setContext(context);
targetService.executeRequest(targetDeliveryRequestMock);

assertEquals(targetDeliveryRequestMock.getDeliveryRequest().getContext().getGeo().getStateCode(), "CA");
assertEquals(targetDeliveryRequestMock.getDeliveryRequest().getContext().getGeo().getCity(), "Foster City");
assertEquals(targetDeliveryRequestMock.getDeliveryRequest().getContext().getGeo().getCountryCode(), "011");

verify(targetHttpClient, times(1))
.execute(any(Map.class), any(String.class), any(DeliveryRequest.class), any(Class.class));

}

private void getMockedTelemetry() {
Telemetry telemetryMock = new Telemetry();
TelemetryEntry telemetryEntryMock = new TelemetryEntry();
Expand Down

0 comments on commit 03e0d3e

Please sign in to comment.