@Test public void runTest_1(){ tracedata = mock(PacketAnalyzerResult.class); session = mock(Session.class); sessionlist = new ArrayList<Session>(); sessionlist.add(session); req = mock(HttpRequestResponseInfo.class); req.setDirection(HttpDirection.REQUEST); List<HttpRequestResponseInfo> reqList = new ArrayList<HttpRequestResponseInfo>(); reqList.add(req); Mockito.when(session.getRequestResponseInfo()).thenReturn(reqList); Mockito.when(session.getDomainName()).thenReturn("mock.domain.name"); Mockito.when(tracedata.getSessionlist()).thenReturn(sessionlist); Mockito.when(req.getDirection()).thenReturn(HttpDirection.REQUEST); Mockito.when(req.getObjName()).thenReturn("mock.obj.name"); Mockito.when(req.getPacketDirection()).thenReturn(PacketDirection.DOWNLINK); Mockito.when(req.getContentLength()).thenReturn(1001024); Mockito.when(req.getContentType()).thenReturn("message/http"); Mockito.when(req.getContentEncoding()).thenReturn("identity"); FileCompressionImpl = (FileCompressionImpl)context.getBean("textFileCompression"); AbstractBestPracticeResult result = FileCompressionImpl.runTest(tracedata); result = FileCompressionImpl.runTest(tracedata); assertEquals("Sending compressed files over the network will speed delivery, and unzipping files on a device is a very low overhead operation. Ensure that all your text files are compressed while being sent over the network.",result.getAboutText()); assertEquals("FILE_COMPRESSION", result.getBestPracticeType().toString()); assertEquals("Text File Compression",result.getDetailTitle()); assertEquals("File Download: Text File Compression",result.getOverviewTitle()); assertEquals(ApplicationConfig.getInstance().getAppShortName() + " detected 100% (977KB of 977KB) of text files were sent without compression. Adding compression will speed the delivery of your content to your customers. (Note: Only files larger than 850 bytes are flagged.)",result.getResultText()); assertEquals("FAIL",result.getResultType().toString()); }
@Override public AbstractBestPracticeResult runTest(PacketAnalyzerResult tracedata) { FileCompressionResult result = new FileCompressionResult(); List<TextFileCompressionEntry> resultlist = new ArrayList<TextFileCompressionEntry>(); int uncompressedCounter = 0; int compressedCounter = 0; int totalUncompressBytes = 0; int totalDownloadedBytes = 0; for(Session session: tracedata.getSessionlist()){ HttpRequestResponseInfo lastRequestObj = null; for(HttpRequestResponseInfo req:session.getRequestResponseInfo()){ if(req.getDirection() == HttpDirection.REQUEST){ lastRequestObj = req; } if(req.getPacketDirection() == PacketDirection.DOWNLINK && req.getContentLength() > 0 && req.getContentType() != null && isTextContent(req.getContentType())){ totalDownloadedBytes += req.getContentLength(); if(req.getContentEncoding() == null || req.getContentEncoding().contains("identity")){ if(req.getContentLength() > FILE_SIZE_THRESHOLD_BYTES){ uncompressedCounter++; totalUncompressBytes += req.getContentLength(); TextFileCompressionEntry tfcEntry = new TextFileCompressionEntry(req, lastRequestObj, session.getDomainName()); tfcEntry.setSavingsTextPercentage(calculateSavingForTextBasedOnGzip(req, session)); resultlist.add(tfcEntry); }else{ compressedCounter++; } }else{ compressedCounter++; } } } } result.setNoOfCompressedFiles(compressedCounter); result.setNoOfUncompressedFiles(uncompressedCounter); result.setTotalUncompressedSize(totalUncompressBytes); result.setResults(resultlist); String text = ""; if(uncompressedCounter == 0){ result.setResultType(BPResultType.PASS); }else if(totalUncompressBytes >= UNCOMPRESSED_SIZE_FAILED_THRESHOLD){ result.setResultType(BPResultType.FAIL); }else{ result.setResultType(BPResultType.WARNING); } if(result.getResultType() == BPResultType.PASS){ text = MessageFormat.format(textResultPass, ApplicationConfig.getInstance().getAppShortName(), FILE_SIZE_THRESHOLD_BYTES, FILE_SIZE_THRESHOLD_BYTES); }else{ String percentageSaving = String .valueOf(Math.round(((double) totalUncompressBytes / totalDownloadedBytes) * 100)); text = MessageFormat.format(textResults, ApplicationConfig.getInstance().getAppShortName(), totalUncompressBytes / 1024, FILE_SIZE_THRESHOLD_BYTES, percentageSaving, (totalDownloadedBytes / 1024)); } result.setResultText(text); result.setAboutText(aboutText); result.setDetailTitle(detailTitle); result.setLearnMoreUrl(learnMoreUrl); result.setOverviewTitle(overviewTitle); result.setExportAll(exportAll); result.setExportAllKb(exportAllKb); return result; }
FileCompressionImpl implements IBestPractice { @Override public AbstractBestPracticeResult runTest(PacketAnalyzerResult tracedata) { FileCompressionResult result = new FileCompressionResult(); List<TextFileCompressionEntry> resultlist = new ArrayList<TextFileCompressionEntry>(); int uncompressedCounter = 0; int compressedCounter = 0; int totalUncompressBytes = 0; int totalDownloadedBytes = 0; for(Session session: tracedata.getSessionlist()){ HttpRequestResponseInfo lastRequestObj = null; for(HttpRequestResponseInfo req:session.getRequestResponseInfo()){ if(req.getDirection() == HttpDirection.REQUEST){ lastRequestObj = req; } if(req.getPacketDirection() == PacketDirection.DOWNLINK && req.getContentLength() > 0 && req.getContentType() != null && isTextContent(req.getContentType())){ totalDownloadedBytes += req.getContentLength(); if(req.getContentEncoding() == null || req.getContentEncoding().contains("identity")){ if(req.getContentLength() > FILE_SIZE_THRESHOLD_BYTES){ uncompressedCounter++; totalUncompressBytes += req.getContentLength(); TextFileCompressionEntry tfcEntry = new TextFileCompressionEntry(req, lastRequestObj, session.getDomainName()); tfcEntry.setSavingsTextPercentage(calculateSavingForTextBasedOnGzip(req, session)); resultlist.add(tfcEntry); }else{ compressedCounter++; } }else{ compressedCounter++; } } } } result.setNoOfCompressedFiles(compressedCounter); result.setNoOfUncompressedFiles(uncompressedCounter); result.setTotalUncompressedSize(totalUncompressBytes); result.setResults(resultlist); String text = ""; if(uncompressedCounter == 0){ result.setResultType(BPResultType.PASS); }else if(totalUncompressBytes >= UNCOMPRESSED_SIZE_FAILED_THRESHOLD){ result.setResultType(BPResultType.FAIL); }else{ result.setResultType(BPResultType.WARNING); } if(result.getResultType() == BPResultType.PASS){ text = MessageFormat.format(textResultPass, ApplicationConfig.getInstance().getAppShortName(), FILE_SIZE_THRESHOLD_BYTES, FILE_SIZE_THRESHOLD_BYTES); }else{ String percentageSaving = String .valueOf(Math.round(((double) totalUncompressBytes / totalDownloadedBytes) * 100)); text = MessageFormat.format(textResults, ApplicationConfig.getInstance().getAppShortName(), totalUncompressBytes / 1024, FILE_SIZE_THRESHOLD_BYTES, percentageSaving, (totalDownloadedBytes / 1024)); } result.setResultText(text); result.setAboutText(aboutText); result.setDetailTitle(detailTitle); result.setLearnMoreUrl(learnMoreUrl); result.setOverviewTitle(overviewTitle); result.setExportAll(exportAll); result.setExportAllKb(exportAllKb); return result; } }
FileCompressionImpl implements IBestPractice { @Override public AbstractBestPracticeResult runTest(PacketAnalyzerResult tracedata) { FileCompressionResult result = new FileCompressionResult(); List<TextFileCompressionEntry> resultlist = new ArrayList<TextFileCompressionEntry>(); int uncompressedCounter = 0; int compressedCounter = 0; int totalUncompressBytes = 0; int totalDownloadedBytes = 0; for(Session session: tracedata.getSessionlist()){ HttpRequestResponseInfo lastRequestObj = null; for(HttpRequestResponseInfo req:session.getRequestResponseInfo()){ if(req.getDirection() == HttpDirection.REQUEST){ lastRequestObj = req; } if(req.getPacketDirection() == PacketDirection.DOWNLINK && req.getContentLength() > 0 && req.getContentType() != null && isTextContent(req.getContentType())){ totalDownloadedBytes += req.getContentLength(); if(req.getContentEncoding() == null || req.getContentEncoding().contains("identity")){ if(req.getContentLength() > FILE_SIZE_THRESHOLD_BYTES){ uncompressedCounter++; totalUncompressBytes += req.getContentLength(); TextFileCompressionEntry tfcEntry = new TextFileCompressionEntry(req, lastRequestObj, session.getDomainName()); tfcEntry.setSavingsTextPercentage(calculateSavingForTextBasedOnGzip(req, session)); resultlist.add(tfcEntry); }else{ compressedCounter++; } }else{ compressedCounter++; } } } } result.setNoOfCompressedFiles(compressedCounter); result.setNoOfUncompressedFiles(uncompressedCounter); result.setTotalUncompressedSize(totalUncompressBytes); result.setResults(resultlist); String text = ""; if(uncompressedCounter == 0){ result.setResultType(BPResultType.PASS); }else if(totalUncompressBytes >= UNCOMPRESSED_SIZE_FAILED_THRESHOLD){ result.setResultType(BPResultType.FAIL); }else{ result.setResultType(BPResultType.WARNING); } if(result.getResultType() == BPResultType.PASS){ text = MessageFormat.format(textResultPass, ApplicationConfig.getInstance().getAppShortName(), FILE_SIZE_THRESHOLD_BYTES, FILE_SIZE_THRESHOLD_BYTES); }else{ String percentageSaving = String .valueOf(Math.round(((double) totalUncompressBytes / totalDownloadedBytes) * 100)); text = MessageFormat.format(textResults, ApplicationConfig.getInstance().getAppShortName(), totalUncompressBytes / 1024, FILE_SIZE_THRESHOLD_BYTES, percentageSaving, (totalDownloadedBytes / 1024)); } result.setResultText(text); result.setAboutText(aboutText); result.setDetailTitle(detailTitle); result.setLearnMoreUrl(learnMoreUrl); result.setOverviewTitle(overviewTitle); result.setExportAll(exportAll); result.setExportAllKb(exportAllKb); return result; }  }
FileCompressionImpl implements IBestPractice { @Override public AbstractBestPracticeResult runTest(PacketAnalyzerResult tracedata) { FileCompressionResult result = new FileCompressionResult(); List<TextFileCompressionEntry> resultlist = new ArrayList<TextFileCompressionEntry>(); int uncompressedCounter = 0; int compressedCounter = 0; int totalUncompressBytes = 0; int totalDownloadedBytes = 0; for(Session session: tracedata.getSessionlist()){ HttpRequestResponseInfo lastRequestObj = null; for(HttpRequestResponseInfo req:session.getRequestResponseInfo()){ if(req.getDirection() == HttpDirection.REQUEST){ lastRequestObj = req; } if(req.getPacketDirection() == PacketDirection.DOWNLINK && req.getContentLength() > 0 && req.getContentType() != null && isTextContent(req.getContentType())){ totalDownloadedBytes += req.getContentLength(); if(req.getContentEncoding() == null || req.getContentEncoding().contains("identity")){ if(req.getContentLength() > FILE_SIZE_THRESHOLD_BYTES){ uncompressedCounter++; totalUncompressBytes += req.getContentLength(); TextFileCompressionEntry tfcEntry = new TextFileCompressionEntry(req, lastRequestObj, session.getDomainName()); tfcEntry.setSavingsTextPercentage(calculateSavingForTextBasedOnGzip(req, session)); resultlist.add(tfcEntry); }else{ compressedCounter++; } }else{ compressedCounter++; } } } } result.setNoOfCompressedFiles(compressedCounter); result.setNoOfUncompressedFiles(uncompressedCounter); result.setTotalUncompressedSize(totalUncompressBytes); result.setResults(resultlist); String text = ""; if(uncompressedCounter == 0){ result.setResultType(BPResultType.PASS); }else if(totalUncompressBytes >= UNCOMPRESSED_SIZE_FAILED_THRESHOLD){ result.setResultType(BPResultType.FAIL); }else{ result.setResultType(BPResultType.WARNING); } if(result.getResultType() == BPResultType.PASS){ text = MessageFormat.format(textResultPass, ApplicationConfig.getInstance().getAppShortName(), FILE_SIZE_THRESHOLD_BYTES, FILE_SIZE_THRESHOLD_BYTES); }else{ String percentageSaving = String .valueOf(Math.round(((double) totalUncompressBytes / totalDownloadedBytes) * 100)); text = MessageFormat.format(textResults, ApplicationConfig.getInstance().getAppShortName(), totalUncompressBytes / 1024, FILE_SIZE_THRESHOLD_BYTES, percentageSaving, (totalDownloadedBytes / 1024)); } result.setResultText(text); result.setAboutText(aboutText); result.setDetailTitle(detailTitle); result.setLearnMoreUrl(learnMoreUrl); result.setOverviewTitle(overviewTitle); result.setExportAll(exportAll); result.setExportAllKb(exportAllKb); return result; }  @Autowired void setHttpRequestResponseHelper(IHttpRequestResponseHelper reqhelper); @Override AbstractBestPracticeResult runTest(PacketAnalyzerResult tracedata); boolean isTextContent(String contentType); int calculateSavingForTextBasedOnGzip(HttpRequestResponseInfo req, Session session); }
FileCompressionImpl implements IBestPractice { @Override public AbstractBestPracticeResult runTest(PacketAnalyzerResult tracedata) { FileCompressionResult result = new FileCompressionResult(); List<TextFileCompressionEntry> resultlist = new ArrayList<TextFileCompressionEntry>(); int uncompressedCounter = 0; int compressedCounter = 0; int totalUncompressBytes = 0; int totalDownloadedBytes = 0; for(Session session: tracedata.getSessionlist()){ HttpRequestResponseInfo lastRequestObj = null; for(HttpRequestResponseInfo req:session.getRequestResponseInfo()){ if(req.getDirection() == HttpDirection.REQUEST){ lastRequestObj = req; } if(req.getPacketDirection() == PacketDirection.DOWNLINK && req.getContentLength() > 0 && req.getContentType() != null && isTextContent(req.getContentType())){ totalDownloadedBytes += req.getContentLength(); if(req.getContentEncoding() == null || req.getContentEncoding().contains("identity")){ if(req.getContentLength() > FILE_SIZE_THRESHOLD_BYTES){ uncompressedCounter++; totalUncompressBytes += req.getContentLength(); TextFileCompressionEntry tfcEntry = new TextFileCompressionEntry(req, lastRequestObj, session.getDomainName()); tfcEntry.setSavingsTextPercentage(calculateSavingForTextBasedOnGzip(req, session)); resultlist.add(tfcEntry); }else{ compressedCounter++; } }else{ compressedCounter++; } } } } result.setNoOfCompressedFiles(compressedCounter); result.setNoOfUncompressedFiles(uncompressedCounter); result.setTotalUncompressedSize(totalUncompressBytes); result.setResults(resultlist); String text = ""; if(uncompressedCounter == 0){ result.setResultType(BPResultType.PASS); }else if(totalUncompressBytes >= UNCOMPRESSED_SIZE_FAILED_THRESHOLD){ result.setResultType(BPResultType.FAIL); }else{ result.setResultType(BPResultType.WARNING); } if(result.getResultType() == BPResultType.PASS){ text = MessageFormat.format(textResultPass, ApplicationConfig.getInstance().getAppShortName(), FILE_SIZE_THRESHOLD_BYTES, FILE_SIZE_THRESHOLD_BYTES); }else{ String percentageSaving = String .valueOf(Math.round(((double) totalUncompressBytes / totalDownloadedBytes) * 100)); text = MessageFormat.format(textResults, ApplicationConfig.getInstance().getAppShortName(), totalUncompressBytes / 1024, FILE_SIZE_THRESHOLD_BYTES, percentageSaving, (totalDownloadedBytes / 1024)); } result.setResultText(text); result.setAboutText(aboutText); result.setDetailTitle(detailTitle); result.setLearnMoreUrl(learnMoreUrl); result.setOverviewTitle(overviewTitle); result.setExportAll(exportAll); result.setExportAllKb(exportAllKb); return result; }  @Autowired void setHttpRequestResponseHelper(IHttpRequestResponseHelper reqhelper); @Override AbstractBestPracticeResult runTest(PacketAnalyzerResult tracedata); boolean isTextContent(String contentType); int calculateSavingForTextBasedOnGzip(HttpRequestResponseInfo req, Session session); static final int FILE_SIZE_THRESHOLD_BYTES; static final int UNCOMPRESSED_SIZE_FAILED_THRESHOLD; }