Search in sources :

Example 6 with TestCase

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

the class TestPredefinedBrowsers method makeSureWeDoNotHaveDuplicateTests.

@Test
void makeSureWeDoNotHaveDuplicateTests() {
    UserAgentAnalyzerTester uaa = UserAgentAnalyzerTester.newBuilder().build();
    Map<String, List<String>> allTestInputs = new HashMap<>(2000);
    Set<String> duplicates = new HashSet<>();
    for (TestCase testCase : uaa.getTestCases()) {
        String input = testCase.getUserAgent();
        String location = testCase.getMetadata().get("filename") + ":" + testCase.getMetadata().get("fileline");
        List<String> locations = allTestInputs.get(input);
        if (locations == null) {
            locations = new ArrayList<>();
        }
        locations.add(location);
        if (locations.size() > 1) {
            duplicates.add(input);
        }
        allTestInputs.put(input, locations);
    }
    if (duplicates.size() == 0) {
        LOG.info("No duplicate tests were found.");
        // We're done and all is fine.
        return;
    }
    StringBuilder sb = new StringBuilder(1024);
    for (String duplicate : duplicates) {
        sb.append("======================================================\n").append("Testcase > ").append(duplicate).append("\n");
        int count = 0;
        for (String location : allTestInputs.get(duplicate)) {
            sb.append(">Location ").append(++count).append(".(").append(location).append(")\n");
        }
    }
    fail("Found " + duplicates.size() + " testcases multiple times: \n" + sb);
}
Also used : UserAgentAnalyzerTester(nl.basjes.parse.useragent.debug.UserAgentAnalyzerTester) HashMap(java.util.HashMap) TestCase(nl.basjes.parse.useragent.config.TestCase) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 7 with TestCase

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

the class TestPredefinedBrowsers method validateAllPredefinedBrowsersViaTestCase.

@Test
void validateAllPredefinedBrowsersViaTestCase() {
    UserAgentAnalyzerTester uaa;
    uaa = UserAgentAnalyzerTester.newBuilder().immediateInitialization().build();
    LOG.info("==============================================================");
    LOG.info("Validating when getting all fields");
    LOG.info("--------------------------------------------------------------");
    List<TestCase> testCases = uaa.getTestCases();
    long start = System.nanoTime();
    assertEquals(0, testCases.stream().filter(testCase -> !testCase.verify(uaa)).count());
    long stop = System.nanoTime();
    LOG.info("All %d tests passed in %dms (average %4.3fms per testcase).", testCases.size(), (stop - start) / 1_000_000, ((stop - start) / 1_000_000D / testCases.size()));
    LOG.info("--------------------------------------------------------------");
    LOG.info("Running all tests again which should return the cached values");
    assertEquals(0, testCases.stream().filter(testCase -> !testCase.verify(uaa)).count());
    // Only here for ensuring the code being tested with "all fields".
    uaa.destroy();
}
Also used : UserAgentAnalyzerTester(nl.basjes.parse.useragent.debug.UserAgentAnalyzerTester) TestCase(nl.basjes.parse.useragent.config.TestCase) Test(org.junit.jupiter.api.Test)

Example 8 with TestCase

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

the class TestTestCase method basicTestCase.

@Test
void basicTestCase() {
    TestCase testCase = new TestCase(USERAGENT, "My test");
    // The EXPLICITLY requested fields
    testCase.expect("DeviceClass", "Phone");
    testCase.expect("DeviceBrand", "Google");
    testCase.expect("DeviceName", "Google Nexus 6");
    testCase.expect("AgentNameVersionMajor", "Chrome 53");
    // The IMPLICITLY requested fields (i.e. partials of the actually requested ones)
    testCase.expect("AgentName", "Chrome");
    testCase.expect("AgentVersion", "53.0.2785.124");
    testCase.expect("AgentVersionMajor", "53");
    // The NOT requested fields are not there
    testCase.expect("OperatingSystemClass", null);
    testCase.expect("OperatingSystemName", null);
    testCase.expect("OperatingSystemNameVersion", null);
    testCase.expect("OperatingSystemNameVersionMajor", null);
    testCase.expect("OperatingSystemVersion", null);
    testCase.expect("OperatingSystemVersionBuild", null);
    testCase.expect("OperatingSystemVersionMajor", null);
    testCase.expect("LayoutEngineClass", null);
    testCase.expect("LayoutEngineName", null);
    testCase.expect("LayoutEngineNameVersion", null);
    testCase.expect("LayoutEngineNameVersionMajor", null);
    testCase.expect("LayoutEngineVersion", null);
    testCase.expect("LayoutEngineVersionMajor", null);
    testCase.expect("AgentClass", null);
    testCase.expect("AgentNameVersion", null);
    assertTrue(testCase.verify(userAgentAnalyzer));
}
Also used : TestCase(nl.basjes.parse.useragent.config.TestCase) Test(org.junit.jupiter.api.Test)

Aggregations

TestCase (nl.basjes.parse.useragent.config.TestCase)8 Test (org.junit.jupiter.api.Test)5 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)2 List (java.util.List)2 UserAgentAnalyzer (nl.basjes.parse.useragent.UserAgentAnalyzer)2 UserAgentAnalyzerTester (nl.basjes.parse.useragent.debug.UserAgentAnalyzerTester)2 Operation (io.swagger.v3.oas.annotations.Operation)1 Content (io.swagger.v3.oas.annotations.media.Content)1 ExampleObject (io.swagger.v3.oas.annotations.media.ExampleObject)1 ApiResponse (io.swagger.v3.oas.annotations.responses.ApiResponse)1 Tag (io.swagger.v3.oas.annotations.tags.Tag)1 HashSet (java.util.HashSet)1 TreeMap (java.util.TreeMap)1 Collectors (java.util.stream.Collectors)1 UserAgent (nl.basjes.parse.useragent.UserAgent)1 MutableUserAgent (nl.basjes.parse.useragent.UserAgent.MutableUserAgent)1 Matcher (nl.basjes.parse.useragent.analyze.Matcher)1 ParseService (nl.basjes.parse.useragent.servlet.ParseService)1 ParseService.ensureStartedForApis (nl.basjes.parse.useragent.servlet.ParseService.ensureStartedForApis)1