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;
}
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();
}
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()));
}
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());
}
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"));
}
Aggregations