Search in sources :

Example 11 with MutableUserAgent

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

the class TestConcatenation method testFieldConcatenationNonExistent.

@Test
void testFieldConcatenationNonExistent() {
    FieldCalculator fc;
    MutableUserAgent userAgent = createUserAgent();
    fc = new ConcatNONDuplicatedCalculator("Combined2", "One", "NonExistent");
    fc.calculate(userAgent);
    assertEquals("One Unknown", userAgent.getValue("Combined2"));
    fc = new ConcatNONDuplicatedCalculator("Combined3", "NonExistent", "Two");
    fc.calculate(userAgent);
    assertEquals("Unknown Two", userAgent.getValue("Combined3"));
    fc = new ConcatNONDuplicatedCalculator("Combined4", "NonExistent1", "NonExistent2");
    fc.calculate(userAgent);
    assertEquals("Unknown", userAgent.getValue("Combined4"));
}
Also used : MutableUserAgent(nl.basjes.parse.useragent.UserAgent.MutableUserAgent) FieldCalculator(nl.basjes.parse.useragent.calculate.FieldCalculator) ConcatNONDuplicatedCalculator(nl.basjes.parse.useragent.calculate.ConcatNONDuplicatedCalculator) Test(org.junit.jupiter.api.Test)

Example 12 with MutableUserAgent

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

the class TestErrorHandling method methodInputValidation.

@Test
void methodInputValidation() {
    UserAgentAnalyzer uaa = UserAgentAnalyzer.newBuilder().withField("AgentClass").build();
    UserAgent agent = uaa.parse((String) null);
    assertNotNull(agent);
    assertNull(agent.getUserAgentString());
    agent = uaa.parse((MutableUserAgent) null);
    assertNotNull(agent);
    assertNull(EvilManualUseragentStringHacks.fixIt(null));
}
Also used : MutableUserAgent(nl.basjes.parse.useragent.UserAgent.MutableUserAgent) MutableUserAgent(nl.basjes.parse.useragent.UserAgent.MutableUserAgent) Test(org.junit.jupiter.api.Test)

Example 13 with MutableUserAgent

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

the class TestUseragent method testDefaults.

@Test
void testDefaults() {
    MutableUserAgent agent = new MutableUserAgent();
    // Defaults for known fields
    assertEquals(UNKNOWN_VALUE, agent.getValue("DeviceClass"));
    assertEquals(UNKNOWN_VERSION, agent.getValue("AgentVersion"));
    // The field is not present
    assertEquals(UNKNOWN_VALUE, agent.getValue("SomethingNew"));
    // The field is present
    agent.set("SomethingNew", "A value", 10);
    assertEquals("A value", agent.getValue("SomethingNew"));
    agent.set("SomethingNew", NULL_VALUE, 20);
    assertEquals(UNKNOWN_VALUE, agent.getValue("SomethingNew"));
    agent.reset();
    assertEquals(UNKNOWN_VALUE, agent.getValue("SomethingNew"));
}
Also used : MutableUserAgent(nl.basjes.parse.useragent.UserAgent.MutableUserAgent) Test(org.junit.jupiter.api.Test)

Example 14 with MutableUserAgent

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

the class TestUseragent method testUseragentValuesDebug.

private void testUseragentValuesDebug(boolean debug) {
    String name = "Attribute";
    String uaString = "Foo Bar";
    MutableUserAgent agent = new MutableUserAgent(uaString);
    agent.setDebug(debug);
    // Setting unknown new attributes
    assertNotNull(agent.get("UnknownOne"));
    assertEquals("Unknown", agent.getValue("UnknownOne"));
    assertEquals(-1, agent.getConfidence("UnknownOne").longValue());
    agent.set("UnknownOne", "One", 111);
    check(agent, "UnknownOne", "One", 111);
    ((MutableAgentField) agent.get("UnknownOne")).reset();
    check(agent, "UnknownOne", "Unknown", -1);
    // Setting unknown new attributes FORCED
    assertNotNull(agent.get("UnknownTwo"));
    assertEquals("Unknown", agent.getValue("UnknownTwo"));
    assertEquals(-1, agent.getConfidence("UnknownTwo").longValue());
    agent.setForced("UnknownTwo", "Two", 222);
    check(agent, "UnknownTwo", "Two", 222);
    ((MutableAgentField) agent.get("UnknownTwo")).reset();
    check(agent, "UnknownTwo", "Unknown", -1);
    // Setting known attributes
    check(agent, "AgentClass", "Unknown", -1);
    agent.set("AgentClass", "One", 111);
    check(agent, "AgentClass", "One", 111);
    ((MutableAgentField) agent.get("AgentClass")).reset();
    check(agent, "AgentClass", "Unknown", -1);
    // Setting known attributes FORCED
    check(agent, "AgentVersion", "??", -1);
    agent.setForced("AgentVersion", "Two", 222);
    check(agent, "AgentVersion", "Two", 222);
    ((MutableAgentField) agent.get("AgentVersion")).reset();
    check(agent, "AgentVersion", "??", -1);
    agent.set(name, "One", 111);
    check(agent, name, "One", 111);
    // Should be ignored
    agent.set(name, "Two", 22);
    check(agent, name, "One", 111);
    // Should be used
    agent.set(name, "Three", 333);
    check(agent, name, "Three", 333);
    // Should be used
    agent.setForced(name, "Four", 4);
    check(agent, name, "Four", 4);
    // Should be ignored
    agent.set(name, "<<<null>>>", 2);
    check(agent, name, "Four", 4);
    // Should be used
    agent.set(name, "<<<null>>>", 5);
    check(agent, name, "Unknown", 5);
    // Should be IGNORED
    agent.set(name, "Four", 4);
    check(agent, name, "Unknown", 5);
    // Set a 'normal' value again.
    // Should be used
    agent.set(name, "Three", 333);
    check(agent, name, "Three", 333);
    MutableAgentField field = (MutableAgentField) agent.get(name);
    // Should be used
    field.setValueForced("Five", 5);
    check(agent, name, "Five", 5);
    // Should be used
    field.setValueForced("<<<null>>>", 4);
    check(agent, name, "Unknown", 4);
}
Also used : MutableUserAgent(nl.basjes.parse.useragent.UserAgent.MutableUserAgent) MutableAgentField(nl.basjes.parse.useragent.AgentField.MutableAgentField)

Example 15 with MutableUserAgent

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

the class TestUseragent method limitedToString.

@Test
void limitedToString() {
    List<String> wanted = Arrays.asList("Unsure", "DeviceClass", "AgentVersion");
    // When only asking for a limited set of fields then the internal datastructures are
    // initialized with only the known attributes for which we have 'non standard' default values.
    MutableUserAgent userAgent = new MutableUserAgent("Some Agent", wanted);
    assertEquals("  - user_agent_string  : 'Some Agent'\n" + "    DeviceClass        : 'Unknown'\n" + "    AgentVersion       : '??'\n" + "    Unsure             : 'Unknown'\n", userAgent.toString());
    // We set the wanted field
    // --> isDefault !
    ((MutableAgentField) (userAgent.get("Unsure"))).setValueForced("--> Unsure", -1);
    userAgent.set("DeviceClass", "--> DeviceClass", 1);
    userAgent.set("AgentVersion", "--> AgentVersion", 2);
    // We also set an unwanted field
    userAgent.set("SomethingElse", "--> SomethingElse", 3);
    // We should be able to retrieve all of them
    assertEquals("--> Unsure", userAgent.getValue("Unsure"));
    assertEquals("--> DeviceClass", userAgent.getValue("DeviceClass"));
    assertEquals("--> AgentVersion", userAgent.getValue("AgentVersion"));
    assertEquals("--> SomethingElse", userAgent.getValue("SomethingElse"));
    // Default value?
    assertTrue(userAgent.get("Unsure").isDefaultValue());
    assertFalse(userAgent.get("DeviceClass").isDefaultValue());
    assertFalse(userAgent.get("AgentVersion").isDefaultValue());
    assertFalse(userAgent.get("SomethingElse").isDefaultValue());
    // The output map if requested should only contain the wanted fields.
    assertEquals("  - user_agent_string  : 'Some Agent'\n" + "    DeviceClass        : '--> DeviceClass'\n" + "    AgentVersion       : '--> AgentVersion'\n" + "    Unsure             : '--> Unsure'\n", userAgent.toString());
    userAgent.destroy();
}
Also used : MutableUserAgent(nl.basjes.parse.useragent.UserAgent.MutableUserAgent) MutableAgentField(nl.basjes.parse.useragent.AgentField.MutableAgentField) Test(org.junit.jupiter.api.Test)

Aggregations

MutableUserAgent (nl.basjes.parse.useragent.UserAgent.MutableUserAgent)18 Test (org.junit.jupiter.api.Test)13 ConcatNONDuplicatedCalculator (nl.basjes.parse.useragent.calculate.ConcatNONDuplicatedCalculator)6 FieldCalculator (nl.basjes.parse.useragent.calculate.FieldCalculator)5 MutableAgentField (nl.basjes.parse.useragent.AgentField.MutableAgentField)4 ImmutableUserAgent (nl.basjes.parse.useragent.UserAgent.ImmutableUserAgent)4 CalculateDeviceBrand (nl.basjes.parse.useragent.calculate.CalculateDeviceBrand)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 Match (nl.basjes.parse.useragent.analyze.MatchesList.Match)1 CalculateAgentName (nl.basjes.parse.useragent.calculate.CalculateAgentName)1 MajorVersionCalculator (nl.basjes.parse.useragent.calculate.MajorVersionCalculator)1 UserAgentAnalyzerTester (nl.basjes.parse.useragent.debug.UserAgentAnalyzerTester)1 MethodSource (org.junit.jupiter.params.provider.MethodSource)1