use of com.adobe.target.edge.client.model.ondevice.OnDeviceDecisioningRuleSet in project target-java-sdk by adobe.
the class OnDeviceDecisioningEvaluatorTest method testUnknownView.
@Test
public void testUnknownView() throws JsonProcessingException, NoSuchFieldException {
OnDeviceDecisioningRuleSet ruleSet = new OnDeviceDecisioningRuleSet();
List<String> views = new ArrayList<>();
views.add("test");
FieldSetter.setField(ruleSet, ruleSet.getClass().getDeclaredField("localViews"), views);
FieldSetter.setField(ruleSet, ruleSet.getClass().getDeclaredField("remoteViews"), new ArrayList<>());
String serializedRuleSet = objectMapper.writeValueAsString(ruleSet);
RuleLoader testRuleLoader = TargetTestDeliveryRequestUtils.getTestRuleLoader(serializedRuleSet);
evaluator = new OnDeviceDecisioningEvaluator(testRuleLoader);
TargetDeliveryRequest request = TargetDeliveryRequest.builder().prefetch(new PrefetchRequest().addViewsItem(new ViewRequest().name("test")).addViewsItem(new ViewRequest().name("test2"))).build();
OnDeviceDecisioningEvaluation evaluation = evaluator.evaluateLocalExecution(request);
assertFalse(evaluation.isAllLocal());
List<String> remoteViews = new ArrayList<>();
remoteViews.add("test2");
assertEquals(remoteViews, evaluation.getRemoteViews());
assertNull(evaluation.getRemoteMBoxes());
}
use of com.adobe.target.edge.client.model.ondevice.OnDeviceDecisioningRuleSet in project target-java-sdk by adobe.
the class DefaultRuleLoaderTest method testRuleLoaderArtifactPayload.
@Test
void testRuleLoaderArtifactPayload() {
DefaultRuleLoader defaultRuleLoader = mock(DefaultRuleLoader.class, CALLS_REAL_METHODS);
String etag = "5b1cf3c050e1a0d16934922bf19ba6ea";
Mockito.doReturn(null).when(defaultRuleLoader).generateRequest(any(ClientConfig.class));
Mockito.doReturn(getTestResponse(TEST_RULE_SET, etag, HttpStatus.SC_OK)).when(defaultRuleLoader).executeRequest(any());
ClientConfig payloadClientConfig = ClientConfig.builder().organizationId(TEST_ORG_ID).onDeviceEnvironment("production").defaultDecisioningMethod(DecisioningMethod.ON_DEVICE).exceptionHandler(exceptionHandler).onDeviceDecisioningHandler(executionHandler).onDeviceArtifactPayload(TEST_RULE_SET.getBytes(StandardCharsets.UTF_8)).build();
defaultRuleLoader.start(payloadClientConfig, telemetryService);
verify(defaultRuleLoader, timeout(1000)).setLatestRules(any(OnDeviceDecisioningRuleSet.class));
verify(executionHandler, timeout(1000)).onDeviceDecisioningReady();
verify(executionHandler, never()).artifactDownloadSucceeded(any());
verify(executionHandler, never()).artifactDownloadFailed(any());
OnDeviceDecisioningRuleSet rules = defaultRuleLoader.getLatestRules();
assertNotNull(rules);
defaultRuleLoader.refresh();
verify(exceptionHandler, never()).handleException(any(TargetClientException.class));
defaultRuleLoader.stop();
}
use of com.adobe.target.edge.client.model.ondevice.OnDeviceDecisioningRuleSet in project target-java-sdk by adobe.
the class OnDeviceDecisioningEvaluatorTest method testAllLocalNoRemoteView.
@Test
public void testAllLocalNoRemoteView() throws JsonProcessingException, NoSuchFieldException {
OnDeviceDecisioningRuleSet ruleSet = new OnDeviceDecisioningRuleSet();
List<String> localViews = new ArrayList<>();
localViews.add("test");
localViews.add("test2");
FieldSetter.setField(ruleSet, ruleSet.getClass().getDeclaredField("localViews"), localViews);
FieldSetter.setField(ruleSet, ruleSet.getClass().getDeclaredField("remoteViews"), new ArrayList<String>());
String serializedRuleSet = objectMapper.writeValueAsString(ruleSet);
RuleLoader testRuleLoader = TargetTestDeliveryRequestUtils.getTestRuleLoader(serializedRuleSet);
evaluator = new OnDeviceDecisioningEvaluator(testRuleLoader);
TargetDeliveryRequest request = TargetDeliveryRequest.builder().prefetch(new PrefetchRequest().addViewsItem(new ViewRequest().name("test")).addViewsItem(new ViewRequest().name("test2"))).build();
OnDeviceDecisioningEvaluation evaluation = evaluator.evaluateLocalExecution(request);
assertTrue(evaluation.isAllLocal());
assertNull(evaluation.getRemoteMBoxes());
assertNull(evaluation.getRemoteViews());
}
use of com.adobe.target.edge.client.model.ondevice.OnDeviceDecisioningRuleSet in project target-java-sdk by adobe.
the class DefaultRuleLoaderTest method getTestResponse.
static HttpResponse<OnDeviceDecisioningRuleSet> getTestResponse(final String ruleSet, final String etag, final int status) {
return new HttpResponse<OnDeviceDecisioningRuleSet>() {
@Override
public int getStatus() {
return status;
}
@Override
public String getStatusText() {
return null;
}
@Override
public Headers getHeaders() {
Headers headers = new Headers();
if (etag != null) {
headers.add("ETag", etag);
}
return headers;
}
@Override
public OnDeviceDecisioningRuleSet getBody() {
if (ruleSet == null) {
return null;
}
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.configure(JsonGenerator.Feature.IGNORE_UNKNOWN, true);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(DeserializationFeature.READ_ENUMS_USING_TO_STRING, true);
try {
return mapper.readValue(ruleSet, new TypeReference<OnDeviceDecisioningRuleSet>() {
});
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public Optional<UnirestParsingException> getParsingError() {
return Optional.empty();
}
@Override
public <V> V mapBody(Function<OnDeviceDecisioningRuleSet, V> func) {
return null;
}
@Override
public <V> HttpResponse<V> map(Function<OnDeviceDecisioningRuleSet, V> func) {
return null;
}
@Override
public HttpResponse<OnDeviceDecisioningRuleSet> ifSuccess(Consumer<HttpResponse<OnDeviceDecisioningRuleSet>> consumer) {
return null;
}
@Override
public HttpResponse<OnDeviceDecisioningRuleSet> ifFailure(Consumer<HttpResponse<OnDeviceDecisioningRuleSet>> consumer) {
return null;
}
@Override
public <E> HttpResponse<OnDeviceDecisioningRuleSet> ifFailure(Class<? extends E> errorClass, Consumer<HttpResponse<E>> consumer) {
return null;
}
@Override
public boolean isSuccess() {
return false;
}
@Override
public <E> E mapError(Class<? extends E> errorClass) {
return null;
}
@Override
public Cookies getCookies() {
return null;
}
};
}
use of com.adobe.target.edge.client.model.ondevice.OnDeviceDecisioningRuleSet in project target-java-sdk by adobe.
the class DefaultRuleLoaderTest method testDefaultRuleLoader.
@Test
void testDefaultRuleLoader() {
DefaultRuleLoader defaultRuleLoader = mock(DefaultRuleLoader.class, CALLS_REAL_METHODS);
String etag = "5b1cf3c050e1a0d16934922bf19ba6ea";
Mockito.doReturn(null).when(defaultRuleLoader).generateRequest(any(ClientConfig.class));
Mockito.doReturn(getTestResponse(TEST_RULE_SET, etag, HttpStatus.SC_OK)).when(defaultRuleLoader).executeRequest(any());
defaultRuleLoader.start(clientConfig, telemetryService);
verify(defaultRuleLoader, timeout(1000)).setLatestRules(any(OnDeviceDecisioningRuleSet.class));
verify(defaultRuleLoader, timeout(1000)).setLatestETag(eq(etag));
verify(executionHandler, timeout(1000)).onDeviceDecisioningReady();
verify(executionHandler, timeout(1000)).artifactDownloadSucceeded(any());
verify(executionHandler, never()).artifactDownloadFailed(any());
OnDeviceDecisioningRuleSet rules = defaultRuleLoader.getLatestRules();
assertNotNull(rules);
defaultRuleLoader.stop();
// do it again, make sure starting works again after a stop
reset(executionHandler);
defaultRuleLoader.start(clientConfig, telemetryService);
verify(defaultRuleLoader, timeout(1000)).setLatestRules(any(OnDeviceDecisioningRuleSet.class));
verify(defaultRuleLoader, timeout(1000)).setLatestETag(eq(etag));
verify(executionHandler, timeout(1000)).onDeviceDecisioningReady();
verify(executionHandler, timeout(1000)).artifactDownloadSucceeded(any());
verify(executionHandler, never()).artifactDownloadFailed(any());
rules = defaultRuleLoader.getLatestRules();
assertNotNull(rules);
Mockito.doReturn(getTestResponse(TEST_RULE_SET, "5b1cf3c050e1a0d16934922bf19ba6ea", HttpStatus.SC_NOT_MODIFIED)).when(defaultRuleLoader).executeRequest(any());
defaultRuleLoader.refresh();
verify(exceptionHandler, never()).handleException(any(TargetClientException.class));
defaultRuleLoader.stop();
}
Aggregations