|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2017 Intuit |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + *******************************************************************************/ |
| 16 | +package com.intuit.ipp.serialization; |
| 17 | + |
| 18 | +import com.fasterxml.jackson.core.JsonFactory; |
| 19 | +import com.fasterxml.jackson.core.JsonGenerator; |
| 20 | +import com.google.gson.Gson; |
| 21 | +import com.google.gson.JsonObject; |
| 22 | +import com.intuit.ipp.data.BatchItemRequest; |
| 23 | +import com.intuit.ipp.data.CDCQuery; |
| 24 | +import com.intuit.ipp.data.Customer; |
| 25 | +import com.intuit.ipp.data.Invoice; |
| 26 | +import com.intuit.ipp.data.Line; |
| 27 | +import com.intuit.ipp.data.ObjectFactory; |
| 28 | +import com.intuit.ipp.data.OperationEnum; |
| 29 | +import com.intuit.ipp.data.ReferenceType; |
| 30 | +import com.intuit.ipp.data.Vendor; |
| 31 | +import org.testng.Assert; |
| 32 | +import org.testng.annotations.BeforeClass; |
| 33 | +import org.testng.annotations.Test; |
| 34 | + |
| 35 | +import javax.xml.bind.JAXBElement; |
| 36 | +import java.io.ByteArrayOutputStream; |
| 37 | +import java.io.IOException; |
| 38 | +import java.io.OutputStream; |
| 39 | +import java.lang.reflect.Method; |
| 40 | +import java.math.BigDecimal; |
| 41 | +import java.util.Calendar; |
| 42 | +import java.util.Collections; |
| 43 | +import java.util.Date; |
| 44 | +import java.util.TimeZone; |
| 45 | + |
| 46 | +public class BatchItemRequestSerializerTest { |
| 47 | + |
| 48 | + private BatchItemRequestSerializer serializer; |
| 49 | + private JsonFactory factory; |
| 50 | + private Gson gson; |
| 51 | + |
| 52 | + @BeforeClass |
| 53 | + public void setUp() { |
| 54 | + serializer = new BatchItemRequestSerializer(); |
| 55 | + factory = new JsonFactory(); |
| 56 | + gson = new Gson(); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testBatchSerialize_CustomerCreate() throws IOException { |
| 61 | + BatchItemRequest request = new BatchItemRequest(); |
| 62 | + OutputStream outputStream = new ByteArrayOutputStream(); |
| 63 | + JsonGenerator generator = factory.createGenerator(outputStream); |
| 64 | + |
| 65 | + Customer customer = getCustomerObject(); |
| 66 | + |
| 67 | + request.setBId("bID1"); |
| 68 | + JAXBElement element = getJaxBElementObject(customer); |
| 69 | + request.setIntuitObject(element); |
| 70 | + request.setOperation(OperationEnum.CREATE); |
| 71 | + |
| 72 | + serializer.serialize(request, generator, null); |
| 73 | + |
| 74 | + generator.flush(); |
| 75 | + String output = outputStream.toString(); |
| 76 | + Assert.assertEquals(output, "{\"bId\":\"bID1\",\"Customer\":{\"Title\":\"Mr.\",\"GivenName\":\"John\",\"MiddleName\":\"David\",\"FamilyName\":\"Thomson\"},\"operation\":\"create\"}"); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void testBatchSerialize_VendorCreate() throws IOException { |
| 81 | + BatchItemRequest request = new BatchItemRequest(); |
| 82 | + OutputStream outputStream = new ByteArrayOutputStream(); |
| 83 | + JsonGenerator generator = factory.createGenerator(outputStream); |
| 84 | + |
| 85 | + Vendor vendor = getVendorObject(); |
| 86 | + |
| 87 | + JAXBElement element = getJaxBElementObject(vendor); |
| 88 | + request.setIntuitObject(element); |
| 89 | + request.setOperation(OperationEnum.CREATE); |
| 90 | + |
| 91 | + serializer.serialize(request, generator, null); |
| 92 | + |
| 93 | + generator.flush(); |
| 94 | + String output = outputStream.toString(); |
| 95 | + Assert.assertEquals(output, "{\"Vendor\":{\"Title\":\"Ms.\",\"GivenName\":\"Penny\",\"MiddleName\":\"Kaley\",\"FamilyName\":\"Cuoco\"},\"operation\":\"create\"}"); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testBatchSerialize_InvoiceUpdate() throws IOException { |
| 100 | + BatchItemRequest request = new BatchItemRequest(); |
| 101 | + OutputStream outputStream = new ByteArrayOutputStream(); |
| 102 | + JsonGenerator generator = factory.createGenerator(outputStream); |
| 103 | + |
| 104 | + Invoice invoice = getInvoiceObject(); |
| 105 | + |
| 106 | + request.setBId("bID3"); |
| 107 | + JAXBElement element = getJaxBElementObject(invoice); |
| 108 | + request.setIntuitObject(element); |
| 109 | + request.setOperation(OperationEnum.UPDATE); |
| 110 | + |
| 111 | + serializer.serialize(request, generator, null); |
| 112 | + |
| 113 | + generator.flush(); |
| 114 | + String output = outputStream.toString(); |
| 115 | + Assert.assertEquals(output, "{\"bId\":\"bID3\",\"Invoice\":{\"DocNumber\":\"1001\",\"TxnDate\":\"2019-01-01\",\"Line\":[{\"Description\":\"Buttons\",\"Amount\":1000}],\"CustomerRef\":{\"value\":\"33\",\"name\":\"Account Receivable\",\"type\":\"Account\"}},\"operation\":\"update\"}"); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void testBatchSerialize_QueryInvoice() throws IOException { |
| 120 | + BatchItemRequest request = new BatchItemRequest(); |
| 121 | + OutputStream outputStream = new ByteArrayOutputStream(); |
| 122 | + JsonGenerator generator = factory.createGenerator(outputStream); |
| 123 | + |
| 124 | + request.setBId("bID4"); |
| 125 | + request.setQuery("select * from Invoice"); |
| 126 | + serializer.serialize(request, generator, null); |
| 127 | + |
| 128 | + generator.flush(); |
| 129 | + String output = outputStream.toString(); |
| 130 | + Assert.assertEquals(output, "{\"bId\":\"bID4\",\"Query\":\"select * from Invoice\"}"); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + public void testBatchSerialize_CustomerDelete() throws IOException { |
| 135 | + BatchItemRequest request = new BatchItemRequest(); |
| 136 | + OutputStream outputStream = new ByteArrayOutputStream(); |
| 137 | + JsonGenerator generator = factory.createGenerator(outputStream); |
| 138 | + |
| 139 | + Customer customer = getCustomerObject(); |
| 140 | + customer.setId("34"); |
| 141 | + |
| 142 | + request.setBId("bID5"); |
| 143 | + JAXBElement element = getJaxBElementObject(customer); |
| 144 | + request.setIntuitObject(element); |
| 145 | + request.setOperation(OperationEnum.DELETE); |
| 146 | + |
| 147 | + serializer.serialize(request, generator, null); |
| 148 | + |
| 149 | + generator.flush(); |
| 150 | + String output = outputStream.toString(); |
| 151 | + Assert.assertEquals(output, "{\"bId\":\"bID5\",\"Customer\":{\"Id\":\"34\",\"Title\":\"Mr.\",\"GivenName\":\"John\",\"MiddleName\":\"David\",\"FamilyName\":\"Thomson\"},\"operation\":\"delete\"}"); |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + public void testBatchSerialize_CDCQuery() throws IOException { |
| 156 | + BatchItemRequest request = new BatchItemRequest(); |
| 157 | + OutputStream outputStream = new ByteArrayOutputStream(); |
| 158 | + JsonGenerator generator = factory.createGenerator(outputStream); |
| 159 | + |
| 160 | + CDCQuery cdcQuery = new CDCQuery(); |
| 161 | + cdcQuery.setEntities("Customer"); |
| 162 | + cdcQuery.setChangedSince(getDate()); |
| 163 | + request.setBId("bID6"); |
| 164 | + request.setCDCQuery(cdcQuery); |
| 165 | + |
| 166 | + serializer.serialize(request, generator, null); |
| 167 | + |
| 168 | + generator.flush(); |
| 169 | + String output = outputStream.toString(); |
| 170 | + JsonObject jsonObject = gson.fromJson(output, JsonObject.class); |
| 171 | + Assert.assertNotNull(output); |
| 172 | + Assert.assertEquals(jsonObject.get("bId").getAsString(), "bID6"); |
| 173 | + JsonObject cdcQueryJson = jsonObject.get("CDCQuery").getAsJsonObject(); |
| 174 | + Assert.assertNotNull(cdcQueryJson); |
| 175 | + Assert.assertEquals(cdcQueryJson.get("Entities").getAsString(), "Customer"); |
| 176 | + Assert.assertEquals(cdcQueryJson.get("ChangedSince").getAsString().split("T")[0], "2019-01-01"); |
| 177 | + } |
| 178 | + |
| 179 | + @Test |
| 180 | + public void testBatchSerialize_ReportQuery() throws IOException { |
| 181 | + BatchItemRequest request = new BatchItemRequest(); |
| 182 | + OutputStream outputStream = new ByteArrayOutputStream(); |
| 183 | + JsonGenerator generator = factory.createGenerator(outputStream); |
| 184 | + |
| 185 | + request.setBId("bID6"); |
| 186 | + request.setReportQuery("report queryData"); |
| 187 | + request.setOptionsData("testOptionsData"); |
| 188 | + |
| 189 | + serializer.serialize(request, generator, null); |
| 190 | + |
| 191 | + generator.flush(); |
| 192 | + String output = outputStream.toString(); |
| 193 | + Assert.assertEquals(output, "{\"bId\":\"bID6\",\"ReportQuery\":\"report queryData\",\"optionsData\":\"testOptionsData\"}"); |
| 194 | + } |
| 195 | + |
| 196 | + |
| 197 | + private Customer getCustomerObject() { |
| 198 | + Customer customer = new Customer(); |
| 199 | + customer.setGivenName("John"); |
| 200 | + customer.setTitle("Mr."); |
| 201 | + customer.setFamilyName("Thomson"); |
| 202 | + customer.setMiddleName("David"); |
| 203 | + |
| 204 | + return customer; |
| 205 | + } |
| 206 | + |
| 207 | + private Vendor getVendorObject() { |
| 208 | + Vendor vendor = new Vendor(); |
| 209 | + vendor.setGivenName("Penny"); |
| 210 | + vendor.setTitle("Ms."); |
| 211 | + vendor.setMiddleName("Kaley"); |
| 212 | + vendor.setFamilyName("Cuoco"); |
| 213 | + |
| 214 | + return vendor; |
| 215 | + } |
| 216 | + |
| 217 | + private Invoice getInvoiceObject() { |
| 218 | + Invoice invoice = new Invoice(); |
| 219 | + invoice.setDocNumber("1001"); |
| 220 | + invoice.setTxnDate(getDate()); |
| 221 | + ReferenceType customerRef = new ReferenceType(); |
| 222 | + customerRef.setType("Customer"); |
| 223 | + customerRef.setName("Mary"); |
| 224 | + customerRef.setValue("1"); |
| 225 | + invoice.setCustomerRef(customerRef); |
| 226 | + |
| 227 | + ReferenceType accountRef = new ReferenceType(); |
| 228 | + accountRef.setName("Account Receivable"); |
| 229 | + accountRef.setType("Account"); |
| 230 | + accountRef.setValue("33"); |
| 231 | + invoice.setCustomerRef(accountRef); |
| 232 | + |
| 233 | + Line invLine = new Line(); |
| 234 | + invLine.setAmount(new BigDecimal(1000)); |
| 235 | + invLine.setDescription("Buttons"); |
| 236 | + invoice.setLine(Collections.singletonList(invLine)); |
| 237 | + |
| 238 | + return invoice; |
| 239 | + } |
| 240 | + |
| 241 | + private Date getDate() { |
| 242 | + Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")); |
| 243 | + calendar.setTimeInMillis(1546300800666L); |
| 244 | + return calendar.getTime(); |
| 245 | + } |
| 246 | + |
| 247 | + @SuppressWarnings("unchecked") |
| 248 | + private <T> JAXBElement<? extends Object> getJaxBElementObject(T object) { |
| 249 | + Class<?> objectClass = object.getClass(); |
| 250 | + String methodName = "create".concat(objectClass.getSimpleName()); |
| 251 | + ObjectFactory objectEntity = new ObjectFactory(); |
| 252 | + Class<?> objectEntityClass = objectEntity.getClass(); |
| 253 | + |
| 254 | + Method method; |
| 255 | + JAXBElement<? extends Object> jaxbElement = null; |
| 256 | + try { |
| 257 | + method = objectEntityClass.getMethod(methodName, Class.forName(objectClass.getName())); |
| 258 | + jaxbElement = (JAXBElement<? extends Object>) method.invoke(objectEntity, object); |
| 259 | + } catch (Exception e) { |
| 260 | + e.printStackTrace(); |
| 261 | + } |
| 262 | + return jaxbElement; |
| 263 | + } |
| 264 | +} |
0 commit comments