Search in sources :

Example 11 with UserAgentAnalyzer

use of nl.basjes.parse.useragent.UserAgentAnalyzer in project yauaa by nielsbasjes.

the class TestUserAgentAnalyzerJavaSerialization method deserialize.

UserAgentAnalyzer deserialize(byte[] bytes) throws IOException, ClassNotFoundException {
    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
    try (ObjectInput in = new ObjectInputStream(bis)) {
        Object o = in.readObject();
        assertTrue(o instanceof UserAgentAnalyzer);
        return (UserAgentAnalyzer) o;
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) UserAgentAnalyzer(nl.basjes.parse.useragent.UserAgentAnalyzer) ObjectInput(java.io.ObjectInput) ObjectInputStream(java.io.ObjectInputStream)

Example 12 with UserAgentAnalyzer

use of nl.basjes.parse.useragent.UserAgentAnalyzer in project yauaa by nielsbasjes.

the class TestBuilder method testLimitedFields.

@Test
void testLimitedFields() {
    UserAgentAnalyzer userAgentAnalyzer = UserAgentAnalyzer.newBuilder().preheat(100).preheat().withCache(42).withoutCache().hideMatcherLoadStats().showMatcherLoadStats().withAllFields().withField("DeviceClass").withField("AgentNameVersionMajor").withUserAgentMaxLength(1234).build();
    assertEquals(1234, userAgentAnalyzer.getUserAgentMaxLength());
    runTestCase(userAgentAnalyzer);
}
Also used : UserAgentAnalyzer(nl.basjes.parse.useragent.UserAgentAnalyzer) Test(org.junit.jupiter.api.Test)

Example 13 with UserAgentAnalyzer

use of nl.basjes.parse.useragent.UserAgentAnalyzer in project yauaa by nielsbasjes.

the class TestBuilder method testLoadAdditionalRules.

@Test
void testLoadAdditionalRules() {
    UserAgentAnalyzer userAgentAnalyzer = UserAgentAnalyzer.newBuilder().withField("DeviceClass").withoutCache().hideMatcherLoadStats().addResources("ExtraLoadedRule1.yaml").withField("ExtraValue2").withField("ExtraValue1").addResources("ExtraLoadedRule2.yaml").build();
    UserAgent parsedAgent = userAgentAnalyzer.parse("Mozilla/5.0 (Linux; Android 7.0; Nexus 6 Build/NBD90Z) " + "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.124 Mobile Safari/537.36");
    // The requested fields
    assertEquals("Phone", parsedAgent.getValue("DeviceClass"));
    assertEquals("One", parsedAgent.getValue("ExtraValue1"));
    assertEquals("Two", parsedAgent.getValue("ExtraValue2"));
}
Also used : UserAgentAnalyzer(nl.basjes.parse.useragent.UserAgentAnalyzer) UserAgent(nl.basjes.parse.useragent.UserAgent) Test(org.junit.jupiter.api.Test)

Example 14 with UserAgentAnalyzer

use of nl.basjes.parse.useragent.UserAgentAnalyzer in project yauaa by nielsbasjes.

the class TestBuilder method testPreheatNoTests.

@Test
void testPreheatNoTests() {
    UserAgentAnalyzer userAgentAnalyzer = UserAgentAnalyzer.newBuilder().keepTests().hideMatcherLoadStats().withField("AgentName").build();
    assertTrue(userAgentAnalyzer.getNumberOfTestCases() > 100);
    assertEquals(0, userAgentAnalyzer.preHeat(0));
    assertEquals(0, userAgentAnalyzer.preHeat(-1));
    assertEquals(0, userAgentAnalyzer.preHeat(1000000000L));
    userAgentAnalyzer.dropTests();
    // The full testCases from the yaml are NO LONGER used for the preHeat !
    // Preheat has a separate list with all the test cases (smaller since we do not have the answers there).
    assertEquals(0, userAgentAnalyzer.getNumberOfTestCases());
    assertEquals(PreHeatCases.USERAGENTS.size(), userAgentAnalyzer.preHeat());
}
Also used : UserAgentAnalyzer(nl.basjes.parse.useragent.UserAgentAnalyzer) Test(org.junit.jupiter.api.Test)

Example 15 with UserAgentAnalyzer

use of nl.basjes.parse.useragent.UserAgentAnalyzer in project yauaa by nielsbasjes.

the class TestBuilder method testWantedFieldNamesOne.

@Test
void testWantedFieldNamesOne() {
    UserAgentAnalyzer uaa = createWithWantedFieldNames(OPERATING_SYSTEM_NAME_VERSION);
    Set<String> expectedWantedFields = fields(DEVICE_CLASS, OPERATING_SYSTEM_NAME, OPERATING_SYSTEM_VERSION, OPERATING_SYSTEM_NAME_VERSION, SET_ALL_FIELDS);
    assertEquals(expectedWantedFields, uaa.getWantedFieldNames());
    Set<String> expectedPossibleFields = fields(SYNTAX_ERROR, DEVICE_CLASS, OPERATING_SYSTEM_NAME, OPERATING_SYSTEM_VERSION, OPERATING_SYSTEM_NAME_VERSION);
    assertEquals(expectedPossibleFields, uaa.getAllPossibleFieldNames());
    UserAgent userAgent = uaa.parse(TEST_UA);
    assertEquals(expectedPossibleFields, new TreeSet<>(userAgent.getAvailableFieldNamesSorted()));
}
Also used : UserAgentAnalyzer(nl.basjes.parse.useragent.UserAgentAnalyzer) UserAgent(nl.basjes.parse.useragent.UserAgent) Test(org.junit.jupiter.api.Test)

Aggregations

UserAgentAnalyzer (nl.basjes.parse.useragent.UserAgentAnalyzer)38 Test (org.junit.jupiter.api.Test)24 UserAgent (nl.basjes.parse.useragent.UserAgent)11 ArrayList (java.util.ArrayList)4 Disabled (org.junit.jupiter.api.Disabled)4 Map (java.util.Map)3 UserAgentAnalyzerBuilder (nl.basjes.parse.useragent.UserAgentAnalyzer.UserAgentAnalyzerBuilder)3 MissingUserAgentException (nl.basjes.parse.useragent.servlet.exceptions.MissingUserAgentException)3 Kryo (com.esotericsoftware.kryo.Kryo)2 ByteBufferInput (com.esotericsoftware.kryo.io.ByteBufferInput)2 Operation (io.swagger.v3.oas.annotations.Operation)2 ApiResponse (io.swagger.v3.oas.annotations.responses.ApiResponse)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ObjectInput (java.io.ObjectInput)2 ObjectInputStream (java.io.ObjectInputStream)2 Field (java.lang.reflect.Field)2 HashMap (java.util.HashMap)2 TestCase (nl.basjes.parse.useragent.config.TestCase)2 IngestDocument (org.elasticsearch.ingest.IngestDocument)2 GetMapping (org.springframework.web.bind.annotation.GetMapping)2