|
| 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.interceptors; |
| 17 | + |
| 18 | +import com.intuit.ipp.compression.CompressorFactory; |
| 19 | +import com.intuit.ipp.compression.DeflateCompressor; |
| 20 | +import com.intuit.ipp.compression.GZIPCompressor; |
| 21 | +import com.intuit.ipp.exception.FMSException; |
| 22 | +import com.intuit.ipp.util.Config; |
| 23 | +import org.testng.Assert; |
| 24 | +import org.testng.annotations.AfterTest; |
| 25 | +import org.testng.annotations.BeforeMethod; |
| 26 | +import org.testng.annotations.BeforeTest; |
| 27 | +import org.testng.annotations.Test; |
| 28 | + |
| 29 | +public class CompressionInterceptorTest { |
| 30 | + |
| 31 | + private CompressionInterceptor instance; |
| 32 | + private IntuitMessage message; |
| 33 | + private String compressorConfigPropertyPriorTest; |
| 34 | + |
| 35 | + @BeforeTest |
| 36 | + public void GetCurrentCompressionProperty() { |
| 37 | + compressorConfigPropertyPriorTest = Config.getProperty(Config.COMPRESSION_REQUEST_FORMAT); |
| 38 | + } |
| 39 | + |
| 40 | + @AfterTest |
| 41 | + public void ResetCompressionProperty(){ |
| 42 | + Config.setProperty(Config.COMPRESSION_REQUEST_FORMAT,compressorConfigPropertyPriorTest); |
| 43 | + } |
| 44 | + |
| 45 | + @BeforeMethod |
| 46 | + public void InitTest() { |
| 47 | + // Fresh message and compressor to ensure tests runs are completely independent |
| 48 | + message = new IntuitMessage(); |
| 49 | + instance = new CompressionInterceptor(); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void CompressionNoFormatTest() throws FMSException { |
| 54 | + String testMessage = "Test No Format Serialized"; |
| 55 | + |
| 56 | + // No Compression format test case |
| 57 | + Config.setProperty(Config.COMPRESSION_REQUEST_FORMAT, ""); |
| 58 | + |
| 59 | + message.getRequestElements().setSerializedData(testMessage); |
| 60 | + instance.execute(message); |
| 61 | + |
| 62 | + Assert.assertEquals(message.getRequestElements().getCompressedData(), testMessage.getBytes()); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void CompressionNoFormatAndUploadFileTest() throws FMSException { |
| 67 | + String testMessage = "Test No Format and Upload Serialized"; |
| 68 | + String testUploadFile = "Test Upload File"; |
| 69 | + byte[] allBytes = (testMessage + testUploadFile).getBytes(); |
| 70 | + |
| 71 | + // No Compression format test case |
| 72 | + Config.setProperty(Config.COMPRESSION_REQUEST_FORMAT, ""); |
| 73 | + |
| 74 | + message.getRequestElements().setSerializedData(testMessage); |
| 75 | + message.getRequestElements().setUploadFile(testUploadFile.getBytes()); |
| 76 | + instance.execute(message); |
| 77 | + |
| 78 | + Assert.assertEquals(message.getRequestElements().getCompressedData(), allBytes); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void CompressionGZipFormatCompressionTest() throws FMSException { |
| 83 | + String testMessage = "Test Gzip Serialized"; |
| 84 | + GZIPCompressor compressor = new GZIPCompressor(); |
| 85 | + byte[] compressed = compressor.compress(testMessage, null); |
| 86 | + |
| 87 | + Config.setProperty(Config.COMPRESSION_REQUEST_FORMAT, CompressorFactory.GZIP_COMPRESS_FORMAT); |
| 88 | + |
| 89 | + message.getRequestElements().setSerializedData(testMessage); |
| 90 | + instance.execute(message); |
| 91 | + |
| 92 | + Assert.assertEquals(message.getRequestElements().getCompressedData(), compressed); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void CompressionDeflateFormatCompressionTest() throws FMSException { |
| 97 | + String testMessage = "Test Deflate Serialized"; |
| 98 | + DeflateCompressor compressor = new DeflateCompressor(); |
| 99 | + byte[] compressed = compressor.compress(testMessage, null); |
| 100 | + |
| 101 | + Config.setProperty(Config.COMPRESSION_REQUEST_FORMAT, CompressorFactory.DEFLATE_COMPRESS_FORMAT); |
| 102 | + |
| 103 | + message.getRequestElements().setSerializedData(testMessage); |
| 104 | + instance.execute(message); |
| 105 | + |
| 106 | + Assert.assertEquals(message.getRequestElements().getCompressedData(), compressed); |
| 107 | + } |
| 108 | +} |
0 commit comments