Skip to content

Commit

Permalink
Merge pull request #164 from adobe/162-managing-dates
Browse files Browse the repository at this point in the history
162 managing dates
  • Loading branch information
baubakg authored Sep 11, 2024
2 parents 26daf24 + d885c90 commit 4627434
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 18 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ from any language or framework you are in.
* [Complex Types](#complex-types)
* [Files](#files)
* [Results](#results)
* [Formatting Dates](#formatting-dates)
* [Deserialization Plugins](#deserialization-plugins)
* [Managing Timeouts](#managing-timeouts)
* [Setting Timeout Globally](#setting-timeout-globally)
Expand Down Expand Up @@ -504,6 +505,9 @@ Results are returned as a JSON Object. Serializable return objects are deseriali

In the case of complex classes where the scraping is not sufficient, you can define a deserialization plugin for that class. This allow you to be specific regarding how the object can be returned. For mor information on this you can refer to the chapter [Deserialization Plugins](#deserialization-plugins).

### Formatting Dates
We now allow the extraction and the formatting of Dates. This is done by setting the environment variable `IBS.DESERIALIZATION.DATE.FORMAT`. If not set the date is not changed and remains a long. The format we cover is [SimpleDateFormat](https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html). A wrong Date format will result in using the default format (long).

### Deserialization Plugins
As of version 2.11.17, we introduced the notion of plugins. For now you can customize how an object is deserialized. This can be usefull when the default object serialization is incomplete or not to your liking.

Expand All @@ -513,7 +517,6 @@ To create your plugin you need to:

There is an example of the plugin in the tests under `integroBridgeService/src/test/java/com/adobe/campaign/tests/bridge/plugins/deserializer/MimeExtractionPluginDeserializer.java`.


## Managing Timeouts

As of version 2.11.6 we now introduce the notion of timeouts. This means that after a declared time a call will be
Expand Down
7 changes: 6 additions & 1 deletion ReleaseNotes.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
# Bridge Service - RELEASE NOTES
## 2.11.17 In-Progress
* **New Feature** [#160 Introduce Extraction Plugins](https://github.com/adobe/bridgeService/issues/160). We have now introduced a new plugin mechanism so you can define how an object you are expecting should be deserialized. Please refer to the chapter on ["Deserialization Plugins"](README.md#deserialization-plugins) in the README doc.
* **New Feature** [#162 Introduce Extraction Plugins](https://github.com/adobe/bridgeService/issues/162). We not only allow the deserialization of Date objects. You can decide the formatting of the value. For more information please refer to [Formatting Dates](README.md#formatting-dates) in the README doc.
* [#159 Errors when deserializing Milti-Part Mime Object](https://github.com/adobe/bridgeService/issues/159): We have included a couple of resilience features to better handle the deserialization of complex Objects. This includes:
* Nested scraping. We allow a nested scraping of objects.
* Ignoring calls that throw errors. We now log the error and continue with the next call.
* Ignoring methods returning objects that we do not have the rights to execute/access.
* Ignoring methods returning objects that we do not have the rights to execute/access.
* **New Environment Variables**
* IBS.DESERIALIZATION.DEPTH.LIMIT : This value sets the maximum depth of the deserialization.
* IBS.PLUGINS.PATH : The package path in which IBS should search for the plugins.
* IBS.DESERIALIZATION.DATE.FORMAT : The format in which the date should be deserialized.

## 2.11.16
* **New Feature** [#3 Include an Assertion Feature](https://github.com/adobe/bridgeService/issues/3). We have now included the possibility for users to define assertions. This allows you to clarify accepted results for the call you make with the IBS.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public static MimeMessage createMessage(String in_suffix) throws MessagingExcept
message.addRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
message.setSubject("a subject by me " + in_suffix);
message.setText("a content by yours truly " + in_suffix);
message.setSentDate(new java.util.Date());
return message;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ public void activate(String in_value) {
DESERIALIZATION_DEPTH_LIMIT("IBS.DESERIALIZATION.DEPTH.LIMIT", "1", false,
"This value sets the maximum depth of the deserialization."),
PLUGIN_DESERIALIZATION_PATH(
"IBS.PLUGINS.PATH", null, false, "The package path in which IBS should search for the plugins.");
"IBS.PLUGINS.PATH", null, false, "The package path in which IBS should search for the plugins."),
DESERIALIZATION_DATE_FORMAT(
"IBS.DESERIALIZATION.DATE.FORMAT", "NONE", false, "The date format to be used for deserialization.");

public final String systemName;
public final String defaultValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

public class MetaUtils {
public static final List<Class<?>> ManagedClasses = Arrays.asList(String.class, int.class, long.class,
boolean.class, Integer.class, Long.class, Boolean.class, Object.class);
boolean.class, Integer.class, Long.class, Boolean.class, Date.class, Object.class);
public static final int RECURSION_DEPTH_LIMIT = Integer.parseInt(
ConfigValueHandlerIBS.DESERIALIZATION_DEPTH_LIMIT.fetchValue());
private static final Logger log = LogManager.getLogger();
Expand Down Expand Up @@ -140,7 +141,7 @@ public static Object extractValuesFromObject(Object in_object, int recursionLeve
//TODO Add option with null values (extract null)
if (lt_returnValue != null) {
lr_value.put(Optional.ofNullable(extractFieldName(lt_m.getName())).orElse("this"),
(lt_returnValue instanceof Serializable) ? lt_returnValue : extractValuesFromObject(
(lt_returnValue instanceof Serializable) ? formatObject(lt_returnValue) : extractValuesFromObject(
lt_returnValue, recursionLevel + 1));
log.debug("Extracting method value {}={}", lt_m.getName(), lt_returnValue);
}
Expand Down Expand Up @@ -173,4 +174,42 @@ public static Map extractValuesFromMap(Map in_object) {
}
return lr_returnObject;
}

/**
* Performs a default formatting
*
* @param in_object The object to format
* @return The formatted object
*/
public static Object formatObject(Object in_object) {
if (in_object instanceof Date) {
return formatObject((Date) in_object);
}
return in_object;
}

/**
* Performs a formatting of the date
*
* @param in_object The object to format
* @return The formatted date object
*/
public static Object formatObject(Date in_object) {
Object lr_dateString = in_object;

if (!ConfigValueHandlerIBS.DESERIALIZATION_DATE_FORMAT.is("NONE")) {
try {
SimpleDateFormat l_format = new SimpleDateFormat(
ConfigValueHandlerIBS.DESERIALIZATION_DATE_FORMAT.fetchValue());

lr_dateString = l_format.format(in_object);
} catch (IllegalArgumentException e) {
log.error("The given format '{}' is not compatible with SimpleDateFormat",
ConfigValueHandlerIBS.DESERIALIZATION_DATE_FORMAT.fetchValue());
}
}

return lr_dateString;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package com.adobe.campaign.tests.bridge.plugins.deserializer;

import com.adobe.campaign.tests.bridge.plugins.IBSDeserializerPlugin;
import com.adobe.campaign.tests.bridge.service.MetaUtils;

import java.io.IOException;
import java.util.*;
Expand Down Expand Up @@ -59,7 +60,7 @@ public Map<String, Object> apply(Object in_object) {
l_returnMap.put("contentType", l_message.getContentType());
l_returnMap.put("description", l_message.getDescription());
l_returnMap.put("receivedDate", l_message.getReceivedDate());
l_returnMap.put("sentDate", l_message.getSentDate());
l_returnMap.put("sentDate", MetaUtils.formatObject(l_message.getSentDate()));
l_returnMap.put("size", l_message.getSize());
l_returnMap.put("flags", l_message.getFlags());
l_returnMap.put("messageNumber", l_message.getMessageNumber());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@
import java.net.ServerSocket;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.text.SimpleDateFormat;
import java.util.*;

import static io.restassured.RestAssured.given;
import static org.hamcrest.MatcherAssert.assertThat;
Expand Down Expand Up @@ -581,6 +579,12 @@ public void test_issue35_callToClassWithNoModifiers() {

@Test(groups = "E2E")
public void test_issue159_callToAlternativeMultipartMimeMessage() {
String pattern = "yyyy-MM-dd";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String myDate = simpleDateFormat.format(new Date());

ConfigValueHandlerIBS.DESERIALIZATION_DATE_FORMAT.activate(pattern);

JavaCalls jc = new JavaCalls();
CallContent l_cc = new CallContent();
l_cc.setClassName("com.adobe.campaign.tests.bridge.testdata.one.MimeMessageMethods");
Expand All @@ -589,8 +593,8 @@ public void test_issue159_callToAlternativeMultipartMimeMessage() {
jc.getCallContent().put("one", l_cc);

var response = given().body(jc).post(EndPointURL + "call");
System.out.println(response.thenReturn().getBody().asPrettyString());
response.then().assertThat().statusCode(200);
//System.out.println(response.thenReturn().getBody().asPrettyString());
response.then().assertThat().statusCode(200).body("returnValues.one.sentDate", Matchers.equalTo(myDate));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.*;

import static org.hamcrest.MatcherAssert.assertThat;
Expand Down Expand Up @@ -71,6 +72,8 @@ public void testExtractable() throws MessagingException, NoSuchMethodException {
Method l_failingMethod = l_failingClass.getClass().getMethod("hasMoreElements", null);
assertThat("This method is not extractable", !MetaUtils.isExtractable(l_failingMethod));

//issue #162 adding date as extractable
assertThat("Date should be extractable", MetaUtils.isExtractable(Date.class));
}

@Test
Expand Down Expand Up @@ -248,14 +251,48 @@ public void testDeserializer()
Map<String, Object> l_result = (Map<String, Object>) MetaUtils.extractValuesFromObject(l_message);

assertThat(l_result.keySet(),
Matchers.containsInAnyOrder("isExpunged","hashCode","contentType", "size", "content", "subject", "lineCount", "messageNumber"));
Matchers.containsInAnyOrder("isExpunged","hashCode","contentType", "size", "content", "subject", "lineCount", "messageNumber", "sentDate"));


assertThat(l_result.get("contentType"), Matchers.equalTo("text/plain"));
assertThat(l_result.get("size"), Matchers.equalTo(-1));
assertThat(l_result.get("subject"), Matchers.equalTo("a subject by me " + l_suffix));
assertThat(l_result.get("content"), Matchers.equalTo("a content by yours truly " + l_suffix));
assertThat(l_result.get("lineCount"), Matchers.equalTo(-1));
Date end = new Date();
assertThat("We should have a sent date", ((Date) l_result.get("sentDate")).getTime(), Matchers.lessThanOrEqualTo(
end.getTime()));
}

@Test
public void testDeserializerDateFormatted()
throws MessagingException {

//Prepare formatting
String pattern = "yyyy-MM-dd";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String myDate = simpleDateFormat.format(new Date());

ConfigValueHandlerIBS.DESERIALIZATION_DATE_FORMAT.activate(pattern);


String l_suffix = "one";
Message l_message = MimeMessageMethods.createMessage(l_suffix);

assertThat("This class should not be serializable", !(l_message instanceof Serializable));

Map<String, Object> l_result = (Map<String, Object>) MetaUtils.extractValuesFromObject(l_message);

assertThat(l_result.keySet(),
Matchers.containsInAnyOrder("isExpunged","hashCode","contentType", "size", "content", "subject", "lineCount", "messageNumber", "sentDate"));


assertThat(l_result.get("contentType"), Matchers.equalTo("text/plain"));
assertThat(l_result.get("size"), Matchers.equalTo(-1));
assertThat(l_result.get("subject"), Matchers.equalTo("a subject by me " + l_suffix));
assertThat(l_result.get("content"), Matchers.equalTo("a content by yours truly " + l_suffix));
assertThat(l_result.get("lineCount"), Matchers.equalTo(-1));
assertThat("We should have a sent date", l_result.get("sentDate"), Matchers.equalTo(myDate));
}

@Test
Expand All @@ -272,7 +309,7 @@ public void testDeserializer_collection()

assertThat(l_result.keySet(),
Matchers.containsInAnyOrder("contentType", "size", "content", "subject", "lineCount", "messageNumber",
"hashCode", "isExpunged"));
"hashCode", "isExpunged","sentDate"));

assertThat(l_result.get("contentType"), Matchers.equalTo("text/plain"));
assertThat(l_result.get("size"), Matchers.equalTo(-1));
Expand All @@ -297,12 +334,8 @@ public void testDeserializer_MimeMessageMultiPart() throws MessagingException {
Matchers.containsInAnyOrder("contentType", "size", "content", "subject", "lineCount", "messageNumber",
"hashCode", "isExpunged"));

//assertThat(l_result.get("contentType").toString(), Matchers.startsWith("multipart"));


assertThat(l_result.get("size"), Matchers.equalTo(-1));
assertThat(l_result.get("subject"), Matchers.equalTo("a subject by me " + l_suffix));
//assertThat(l_result.get("content"), Matchers.equalTo("a content by yours truely " + l_suffix));
assertThat(l_result.get("lineCount"), Matchers.equalTo(-1));
}

Expand Down Expand Up @@ -396,4 +429,26 @@ public void testMutliPartMimePlugin() throws MessagingException, JsonProcessingE
assertThat(l_result.get("lineCount"), Matchers.equalTo(-1));
}

@Test
public void testFormatter() {

assertThat("String formatted", MetaUtils.formatObject("test"), Matchers.equalTo("test"));

assertThat("int formatted", MetaUtils.formatObject(1), Matchers.equalTo(1));

Date l_date = new Date();
assertThat("Date formatted should be a long", MetaUtils.formatObject(l_date), Matchers.equalTo(l_date));

String pattern = "yyyy-MM-dd";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String myDate = simpleDateFormat.format(l_date);

ConfigValueHandlerIBS.DESERIALIZATION_DATE_FORMAT.activate(pattern);
assertThat("Date formatted should be a String", MetaUtils.formatObject(l_date), Matchers.equalTo(myDate));

ConfigValueHandlerIBS.DESERIALIZATION_DATE_FORMAT.activate("an arbitrary string");
assertThat("Date formatted should be a String", MetaUtils.formatObject(l_date), Matchers.equalTo(l_date));

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import javax.mail.Message;
import javax.mail.MessagingException;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -79,6 +81,7 @@ public void testThePluginManagerUsageLevel1() throws MessagingException {
assertThat(contentList.get(0), Matchers.instanceOf(Map.class));
assertThat(contentList.get(0).get("contentType"), Matchers.equalTo("text/plain"));
assertThat(l_result.get("lineCount"), Matchers.equalTo(-1));

}

@Test
Expand Down Expand Up @@ -137,6 +140,12 @@ public void testUsageOfTheExtractionPlugInConfigBased() throws MessagingExceptio
"com.adobe.campaign.tests.bridge.plugins.deserializer");
IBSPluginManager.loadPlugins();

//Activate the dates
String pattern = "yyyy-MM-dd";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String myDate = simpleDateFormat.format(new Date());
ConfigValueHandlerIBS.DESERIALIZATION_DATE_FORMAT.activate(pattern);

l_result = (Map<String, Object>) MetaUtils.extractValuesFromObject(l_message);

assertThat(l_result.get("contentType"), Matchers.equalTo("text/plain"));
Expand All @@ -149,6 +158,7 @@ public void testUsageOfTheExtractionPlugInConfigBased() throws MessagingExceptio
assertThat(contentList.get(0), Matchers.instanceOf(Map.class));
assertThat(contentList.get(0).get("contentType"), Matchers.equalTo("text/plain"));
assertThat(l_result.get("lineCount"), Matchers.equalTo(-1));
assertThat(l_result.get("sentDate"), Matchers.equalTo(myDate));
}

//Negative tests
Expand Down

0 comments on commit 4627434

Please sign in to comment.