Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ private boolean isImageType(String mime) {
*/
private String getMime(String name, String delimiter) {
if (StringUtils.hasText(name)) {
return name.substring(name.lastIndexOf(delimiter), name.length());
return name.substring(name.lastIndexOf(delimiter) + 1, name.length());
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@
import com.intuit.ipp.data.Attachable;
import com.intuit.ipp.exception.FMSException;
import com.intuit.ipp.util.Config;

import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import javax.imageio.ImageIO;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.namespace.QName;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -24,16 +33,28 @@

public class SerializeInterceptorTest {

private SerializeInterceptor serializeInterceptor = new SerializeInterceptor();
private SerializeInterceptor serializeInterceptor;
private IntuitMessage message;
Map<String, String> requestParams;
JAXBElement jaxbElement;

@BeforeTest
public void setUp() {
TestJson json = new TestJson();
json.setFoo("bar");
jaxbElement = new JAXBElement(new QName(TestJson.class.getSimpleName()), TestJson.class, json);
serializeInterceptor = new SerializeInterceptor();
}

@BeforeMethod
public void beforeEach() {
message = new IntuitMessage();
requestParams = new HashMap<>();
}

@Test(description = "Given a POST request with object for serialization, " +
"the serialized data should be present in the serializedData object")
public void execute_positiveCase1() throws FMSException {
IntuitMessage message = new IntuitMessage();
TestJson json = new TestJson();
json.setFoo("bar");
JAXBElement<TestJson> jaxbElement = new JAXBElement(new QName(TestJson.class.getSimpleName()), TestJson.class, json);
Map<String, String> requestParams = new HashMap<>();
requestParams.put(REQ_PARAM_METHOD_TYPE, "POST");
message.getRequestElements().setRequestParameters(requestParams);
message.getRequestElements().setObjectToSerialize(jaxbElement);
Expand All @@ -44,8 +65,6 @@ public void execute_positiveCase1() throws FMSException {
@Test(description = "Given a POST request with post body for serialization, " +
"the serialized data should be present in the serializedData object")
public void execute_positiveCase2() throws FMSException {
IntuitMessage message = new IntuitMessage();
Map<String, String> requestParams = new HashMap<>();
requestParams.put(REQ_PARAM_METHOD_TYPE, "POST");
message.getRequestElements().setRequestParameters(requestParams);
String jsonInput = "{\"foo\":\"bar\"}";
Expand All @@ -56,8 +75,6 @@ public void execute_positiveCase2() throws FMSException {

@Test(description = "Given a GET request for serialization, the serialized data should be null")
public void execute_positiveCase3() throws FMSException {
IntuitMessage message = new IntuitMessage();
Map<String, String> requestParams = new HashMap<>();
requestParams.put(REQ_PARAM_METHOD_TYPE, "GET");
message.getRequestElements().setRequestParameters(requestParams);
serializeInterceptor.execute(message);
Expand All @@ -69,17 +86,14 @@ public void execute_positiveCase3() throws FMSException {
"a file to be uploaded. The serialized data should be present in the " +
"serializedData object along with the file that would be uploaded.")
public void execute_positiveCase4() throws FMSException {
IntuitMessage message = new IntuitMessage();
TestJson json = new TestJson();
json.setFoo("bar");
JAXBElement<TestJson> jaxbElement = new JAXBElement(new QName(TestJson.class.getSimpleName()), TestJson.class, json);
Map<String, String> requestParams = new HashMap<>();
requestParams.put(REQ_PARAM_METHOD_TYPE, "POST");
message.getRequestElements().setRequestParameters(requestParams);
message.getRequestElements().setObjectToSerialize(jaxbElement);

message.getRequestElements().setAction(UPLOAD.toString());
message.getRequestElements().getUploadRequestElements().setBoundaryForEntity("Entity");
message.getRequestElements().getUploadRequestElements().setBoundaryForContent("Content");

Attachable attachable = new Attachable();
attachable.setFileName("test.txt");
message.getRequestElements().setEntity(attachable);
Expand Down Expand Up @@ -110,11 +124,37 @@ public void execute_positiveCase5() throws FMSException {
assertEquals(message.getRequestElements().getSerializedData(), "{\"foo\":\"bar\"}");
}

@Test(description = "Given a POST request with an image file upload, " +
"the serializeData object should return the boundaries with the serialized data")
public void execute_uploadFileImageContent() throws FMSException, IOException {
requestParams.put(REQ_PARAM_METHOD_TYPE, "POST");
message.getRequestElements().setRequestParameters(requestParams);

// Generate an image byte array
BufferedImage image = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", baos);
InputStream mockedStream = new ByteArrayInputStream(baos.toByteArray());

// set message request elements
message.getRequestElements().setAction("upload");
message.getRequestElements().getUploadRequestElements().setDocContent(mockedStream);
message.getRequestElements().getUploadRequestElements().setBoundaryForContent("ContentBoundary");
message.getRequestElements().getUploadRequestElements().setBoundaryForEntity("EntityBoundary");

Attachable attachable = new Attachable();
attachable.setContentType("something/jpeg");
message.getRequestElements().setEntity(attachable);
message.getRequestElements().setObjectToSerialize(jaxbElement);

serializeInterceptor.execute(message);
Assert.assertEquals(message.getRequestElements().getSerializedData(), "EntityBoundary{\"foo\":\"bar\"}ContentBoundary");
}

@Test(description = "Serialization request format returned should be of " +
"the form: message.request.serialization")
public void getSerializationRequestFormat() {
SerializeInterceptor interceptor = new SerializeInterceptor();
assertTrue(interceptor
assertTrue(serializeInterceptor
.getSerializationRequestFormat()
.equalsIgnoreCase(Config.getProperty(SERIALIZATION_REQUEST_FORMAT)));
}
Expand All @@ -133,4 +173,5 @@ public void setFoo(String foo) {
this.foo = foo;
}
}

}