1 package org.bitbucket.jrsofty.parser.logging.api; 2 3 import org.bitbucket.jrsofty.parser.logging.api.IllegalClassException; 4 import org.bitbucket.jrsofty.parser.logging.api.LogEntry; 5 import org.bitbucket.jrsofty.parser.logging.api.LogParser; 6 import org.bitbucket.jrsofty.parser.logging.api.LogParserFactory; 7 import org.bitbucket.jrsofty.parser.logging.api.TokenParseException; 8 import org.bitbucket.jrsofty.parser.logging.util.LogLineFormatException; 9 import org.junit.Assert; 10 import org.junit.Before; 11 import org.junit.Test; 12 13 import test.util.Squid3TestElement; 14 15 public class Squid3LogParser { 16 private final String squid3TestParse = "192.168.178.26 [15/Jul/2017:15:08:56 +0200] GET \"http://galahtech.stinkbugonline.com/forum/index.php?action=forum\" \"HTTP/1.1\" 200 8600 \"-\" \"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:54.0) Gecko/20100101 Firefox/54.0\" TCP_MISS:HIER_DIRECT"; 17 private final String squid3Format = "%ip4 %dtm{dd/MMM/yyyy:HH:mm:ss Z} %opt{GET,POST,DELETE,PUT,HEAD,OPTIONS,CONNECT} %url %str %int %int %url %msg %str"; 18 private LogParser parser; 19 20 @Before 21 public void setup() throws LogLineFormatException, IllegalClassException { 22 this.parser = LogParserFactory.createDefaultInstance().createParserForFormat(this.squid3Format, 23 Squid3TestElement.class); 24 } 25 26 @Test 27 public void testSquid3Log01() throws TokenParseException, IllegalClassException { 28 final LogEntry entry = this.parser.parseLogString(this.squid3TestParse); 29 Assert.assertTrue(entry instanceof Squid3TestElement); 30 } 31 32 @Test 33 public void testSquid3Log02() throws TokenParseException, IllegalClassException { 34 final LogEntry entry = this.parser.parseLogString(this.squid3TestParse); 35 final Squid3TestElement actual = (Squid3TestElement) entry; 36 Assert.assertTrue(actual.getIp().equals("192.168.178.26")); 37 } 38 39 @Test 40 public void testSquid3Log03() throws TokenParseException, IllegalClassException { 41 final LogEntry entry = this.parser.parseLogString(this.squid3TestParse); 42 final Squid3TestElement actual = (Squid3TestElement) entry; 43 Assert.assertTrue(actual.getDateString().equals("15/Jul/2017:15:08:56 +0200")); 44 } 45 46 @Test 47 public void testSquid3Log04() throws TokenParseException, IllegalClassException { 48 final LogEntry entry = this.parser.parseLogString(this.squid3TestParse); 49 final Squid3TestElement actual = (Squid3TestElement) entry; 50 Assert.assertTrue(actual.getMethod().equals("GET")); 51 52 } 53 54 @Test 55 public void testSquid3Log05() throws TokenParseException, IllegalClassException { 56 final LogEntry entry = this.parser.parseLogString(this.squid3TestParse); 57 final Squid3TestElement actual = (Squid3TestElement) entry; 58 Assert.assertTrue(actual.getRequestedUrl() 59 .equals("http://galahtech.stinkbugonline.com/forum/index.php?action=forum")); 60 } 61 62 @Test 63 public void testSquid3Log06() throws TokenParseException, IllegalClassException { 64 final LogEntry entry = this.parser.parseLogString(this.squid3TestParse); 65 final Squid3TestElement actual = (Squid3TestElement) entry; 66 Assert.assertTrue(actual.getHttpType() 67 .equals("HTTP/1.1")); 68 } 69 70 @Test 71 public void testSquid3Log07() throws TokenParseException, IllegalClassException { 72 final LogEntry entry = this.parser.parseLogString(this.squid3TestParse); 73 final Squid3TestElement actual = (Squid3TestElement) entry; 74 Assert.assertTrue(actual.getStatus() 75 .equals("200")); 76 } 77 78 @Test 79 public void testSquid3Log08() throws TokenParseException, IllegalClassException { 80 final LogEntry entry = this.parser.parseLogString(this.squid3TestParse); 81 final Squid3TestElement actual = (Squid3TestElement) entry; 82 Assert.assertTrue(actual.getDataSize() 83 .equals("8600")); 84 } 85 86 @Test 87 public void testSquid3Log09() throws TokenParseException, IllegalClassException { 88 final LogEntry entry = this.parser.parseLogString(this.squid3TestParse); 89 final Squid3TestElement actual = (Squid3TestElement) entry; 90 Assert.assertTrue(actual.getReferringUrl() 91 .equals("-")); 92 } 93 94 @Test 95 public void testSquid3Log10() throws TokenParseException, IllegalClassException { 96 final LogEntry entry = this.parser.parseLogString(this.squid3TestParse); 97 final Squid3TestElement actual = (Squid3TestElement) entry; 98 Assert.assertTrue(actual.getUserAgent() 99 .equals("Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:54.0) Gecko/20100101 Firefox/54.0")); 100 } 101 102 @Test 103 public void testSquid3Log11() throws TokenParseException, IllegalClassException { 104 final LogEntry entry = this.parser.parseLogString(this.squid3TestParse); 105 final Squid3TestElement actual = (Squid3TestElement) entry; 106 Assert.assertTrue(actual.getValue1() 107 .equals("TCP_MISS:HIER_DIRECT")); 108 } 109 110 }