Search in sources :

Example 16 with UserAgent

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

the class TestCase method verify.

public boolean verify(Analyzer analyzer, boolean verbose) {
    UserAgent result = analyzer.parse(userAgent);
    TreeSet<String> combinedKeys = new TreeSet<>();
    combinedKeys.addAll(expected.keySet());
    combinedKeys.addAll(result.toMap().keySet());
    // Remove the input "field" from the result set.
    combinedKeys.remove(USERAGENT_FIELDNAME);
    combinedKeys.remove(SYNTAX_ERROR);
    boolean passed = true;
    StringBuilder sb = new StringBuilder();
    int maxFieldLength = 20;
    int maxExpectLength = 20;
    int maxActualLength = 20;
    if (verbose) {
        maxFieldLength = combinedKeys.stream().map(String::length).max(Integer::compareTo).orElse(0);
        maxExpectLength = expected.values().stream().map(String::length).max(Integer::compareTo).orElse(0);
        maxActualLength = expected.values().stream().map(String::length).max(Integer::compareTo).orElse(0);
        sb.append(logSeparator(maxFieldLength, maxExpectLength, maxActualLength)).append('\n');
        sb.append(logLine("Field", maxFieldLength, "Expected", maxExpectLength, "Actual", maxActualLength)).append('\n');
        sb.append(logSeparator(maxFieldLength, maxExpectLength, maxActualLength)).append('\n');
    }
    for (String key : combinedKeys) {
        String expectedValue = expected.get(key);
        String actualValue = result.getValue(key);
        if (verbose) {
            sb.append(logLine(key, maxFieldLength, expectedValue, maxExpectLength, actualValue, maxActualLength));
        }
        if (expectedValue == null) {
            // If we do not expect anything it is ok to get a Default value.
            if (!result.get(key).isDefaultValue()) {
                passed = false;
                sb.append(" --> UNEXPECTED");
            }
        } else {
            if (!expectedValue.equals(actualValue)) {
                passed = false;
                sb.append(" --> !!! FAIL !!!");
            }
        }
        sb.append('\n');
    }
    if (verbose) {
        sb.append(logSeparator(maxFieldLength, maxExpectLength, maxActualLength)).append('\n');
        LOG.info("\n{}", sb);
    }
    return passed;
}
Also used : TreeSet(java.util.TreeSet) UserAgent(nl.basjes.parse.useragent.UserAgent)

Example 17 with UserAgent

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

the class DebugUserAgent method toMatchTrace.

String toMatchTrace(List<String> highlightNames) {
    StringBuilder sb = new StringBuilder(4096);
    sb.append('\n');
    sb.append("+=========================================+\n");
    sb.append("| Matcher results that have been combined |\n");
    sb.append("+=========================================+\n");
    sb.append('\n');
    appliedMatcherResults.sort((o1, o2) -> {
        Matcher m1 = o1.getValue();
        Matcher m2 = o2.getValue();
        return m1.getMatcherSourceLocation().compareTo(m2.getMatcherSourceLocation());
    });
    for (Pair<UserAgent, Matcher> pair : appliedMatcherResults) {
        sb.append('\n');
        sb.append("+================\n");
        sb.append("+ Applied matcher\n");
        sb.append("+----------------\n");
        UserAgent result = pair.getLeft();
        Matcher matcher = pair.getRight();
        sb.append(matcher.toString());
        sb.append("+----------------\n");
        sb.append("+ Results\n");
        sb.append("+----------------\n");
        for (String fieldName : result.getAvailableFieldNamesSorted()) {
            AgentField field = result.get(fieldName);
            if (field.getConfidence() >= 0) {
                String marker = "";
                if (highlightNames.contains(fieldName)) {
                    marker = " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<";
                }
                sb.append("| ").append(fieldName).append('(').append(field.getConfidence());
                if (field.isDefaultValue()) {
                    sb.append(" => isDefaultValue");
                }
                sb.append(") = ").append(field.getValue()).append(marker).append('\n');
            }
        }
        sb.append("+================\n");
    }
    return sb.toString();
}
Also used : Matcher(nl.basjes.parse.useragent.analyze.Matcher) AgentField(nl.basjes.parse.useragent.AgentField) UserAgent(nl.basjes.parse.useragent.UserAgent) MutableUserAgent(nl.basjes.parse.useragent.UserAgent.MutableUserAgent)

Example 18 with UserAgent

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

the class TestBuilder method testWantedFieldNamesTwo.

@Test
void testWantedFieldNamesTwo() {
    UserAgentAnalyzer uaa = createWithWantedFieldNames(OPERATING_SYSTEM_NAME, AGENT_VERSION);
    Set<String> expectedWantedFields = fields(DEVICE_CLASS, OPERATING_SYSTEM_NAME, AGENT_VERSION, SET_ALL_FIELDS);
    assertEquals(expectedWantedFields, uaa.getWantedFieldNames());
    Set<String> expectedPossibleFields = fields(SYNTAX_ERROR, DEVICE_CLASS, OPERATING_SYSTEM_NAME, AGENT_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)

Example 19 with UserAgent

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

the class TestBuilder method runTestCase.

private void runTestCase(AbstractUserAgentAnalyzerDirect userAgentAnalyzer) {
    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");
    LogManager.getLogger(TestBuilder.class).info("{}", parsedAgent.toYamlTestCase(true));
    // The requested fields
    // Phone
    assertEquals("Phone", parsedAgent.getValue("DeviceClass"));
    // Chrome 53
    assertEquals("Chrome 53", parsedAgent.getValue("AgentNameVersionMajor"));
    // The fields that are internally needed to build the requested fields
    // Needed for AgentNameVersionMajor
    // Chrome
    assertEquals("Chrome", parsedAgent.getValue("AgentName"));
    // 53
    assertEquals("53", parsedAgent.getValue("AgentVersionMajor"));
    // Needed for AgentVersionMajor
    // 53.0.2785.124
    assertEquals("53.0.2785.124", parsedAgent.getValue("AgentVersion"));
    // Needed for AgentName (The brand is used as fallback in edge cases that occurs with spiders)
    // Google
    assertEquals("Google", parsedAgent.getValue("DeviceBrand"));
    // The rest must be the default value (i.e. no rules fired)
    // Nexus 6
    assertTrue(parsedAgent.get("DeviceName").isDefaultValue());
    // Mobile
    assertTrue(parsedAgent.get("OperatingSystemClass").isDefaultValue());
    // Android
    assertTrue(parsedAgent.get("OperatingSystemName").isDefaultValue());
    // 7.0
    assertTrue(parsedAgent.get("OperatingSystemVersion").isDefaultValue());
    // Android 7.0
    assertTrue(parsedAgent.get("OperatingSystemNameVersion").isDefaultValue());
    // NBD90Z
    assertTrue(parsedAgent.get("OperatingSystemVersionBuild").isDefaultValue());
    // Browser
    assertTrue(parsedAgent.get("LayoutEngineClass").isDefaultValue());
    // Blink
    assertTrue(parsedAgent.get("LayoutEngineName").isDefaultValue());
    // 53.0
    assertTrue(parsedAgent.get("LayoutEngineVersion").isDefaultValue());
    // 53
    assertTrue(parsedAgent.get("LayoutEngineVersionMajor").isDefaultValue());
    // Blink 53.0
    assertTrue(parsedAgent.get("LayoutEngineNameVersion").isDefaultValue());
    // Blink 53
    assertTrue(parsedAgent.get("LayoutEngineNameVersionMajor").isDefaultValue());
    // Browser
    assertTrue(parsedAgent.get("AgentClass").isDefaultValue());
    // Chrome 53.0.2785.124
    assertTrue(parsedAgent.get("AgentNameVersion").isDefaultValue());
}
Also used : UserAgent(nl.basjes.parse.useragent.UserAgent)

Example 20 with UserAgent

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

the class TestBuilder method testLoadOnlyCustomRules.

@Test
void testLoadOnlyCustomRules() {
    UserAgentAnalyzer userAgentAnalyzer = UserAgentAnalyzer.newBuilder().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("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)

Aggregations

UserAgent (nl.basjes.parse.useragent.UserAgent)30 UserAgentAnalyzer (nl.basjes.parse.useragent.UserAgentAnalyzer)11 Test (org.junit.jupiter.api.Test)9 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)4 MissingUserAgentException (nl.basjes.parse.useragent.servlet.exceptions.MissingUserAgentException)4 MutableUserAgent (nl.basjes.parse.useragent.UserAgent.MutableUserAgent)3 AgentField (nl.basjes.parse.useragent.AgentField)2 Matcher (nl.basjes.parse.useragent.analyze.Matcher)2 Event (co.elastic.logstash.api.Event)1 BlockBuilder (io.trino.spi.block.BlockBuilder)1 Description (io.trino.spi.function.Description)1 ScalarFunction (io.trino.spi.function.ScalarFunction)1 SqlType (io.trino.spi.function.SqlType)1 MapType (io.trino.spi.type.MapType)1 TypeOperators (io.trino.spi.type.TypeOperators)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Map (java.util.Map)1 TreeSet (java.util.TreeSet)1 ParsedField (nl.basjes.parse.core.ParsedField)1