use of nl.basjes.parse.useragent.UserAgentAnalyzer in project yauaa by nielsbasjes.
the class ApiYamlOutput method createOutput.
// -------------------------------------------------
private String createOutput(String userAgentString) {
if (userAgentString == null) {
throw new MissingUserAgentException();
}
ensureStartedForApis(OutputType.JSON);
if (userAgentAnalyzerIsAvailable()) {
UserAgentAnalyzer userAgentAnalyzer = ParseService.getUserAgentAnalyzer();
List<String> result = new ArrayList<>(2048);
for (String input : splitPerFilledLine(userAgentString)) {
if (input.startsWith("#")) {
result.add(input);
} else {
UserAgent userAgent = userAgentAnalyzer.parse(input);
result.add(userAgent.toYamlTestCase(userAgent.getCleanedAvailableFieldNamesSorted()));
}
}
return String.join("\n", result);
}
return "";
}
use of nl.basjes.parse.useragent.UserAgentAnalyzer in project yauaa by nielsbasjes.
the class RunTests method getRunTests.
// ===========================================
@Operation(description = "Fire all available test cases against the analyzer and return 200 if all tests were good")
@ApiResponse(// HttpStatus.OK
responseCode = "200", description = "All tests were good", content = @Content(mediaType = TEXT_PLAIN_VALUE, examples = @ExampleObject("All 3866 tests passed in 2994ms (average 0.775ms per testcase).")))
@ApiResponse(// HttpStatus.INTERNAL_SERVER_ERROR
responseCode = "500", description = "A test failed", content = @Content(mediaType = TEXT_PLAIN_VALUE, examples = @ExampleObject("Extensive text describing what went wrong in the test that failed")))
@GetMapping(value = "/runtests", produces = TEXT_PLAIN_VALUE)
public String getRunTests() {
UserAgentAnalyzer userAgentAnalyzer = ParseService.getUserAgentAnalyzer();
List<TestCase> testCases = userAgentAnalyzer.getTestCases();
long start = System.nanoTime();
List<TestCase> failedTests = testCases.stream().filter(testCase -> !testCase.verify(userAgentAnalyzer)).collect(Collectors.toList());
long stop = System.nanoTime();
if (failedTests.isEmpty()) {
return String.format("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()));
}
throw new YauaaTestsFailed("There were " + failedTests.size() + " failed tests " + "(~" + ((100.0D * failedTests.size()) / testCases.size()) + "%)");
}
use of nl.basjes.parse.useragent.UserAgentAnalyzer in project yauaa by nielsbasjes.
the class RunTests method getPreHeat.
@Operation(description = "Fire all available test cases against the analyzer to heat up the JVM")
@ApiResponse(// HttpStatus.OK
responseCode = "200", description = "The number of reported tests were done to preheat the engine", content = @Content(mediaType = APPLICATION_JSON_VALUE, examples = @ExampleObject("{\n" + " \"status\": \"Ran tests\",\n" + " \"testsDone\": 2337,\n" + " \"timeInMs\": 3123\n" + "}")))
@GetMapping(value = "/preheat", produces = APPLICATION_JSON_VALUE)
public String getPreHeat() {
ensureStartedForApis(OutputType.JSON);
UserAgentAnalyzer userAgentAnalyzer = ParseService.getUserAgentAnalyzer();
final int cacheSize = userAgentAnalyzer.getCacheSize();
userAgentAnalyzer.disableCaching();
long start = System.nanoTime();
final long testsDone = userAgentAnalyzer.preHeat();
long stop = System.nanoTime();
userAgentAnalyzer.setCacheSize(cacheSize);
if (testsDone == 0) {
return "{ \"status\": \"No testcases available\", \"testsDone\": 0 , \"timeInMs\" : -1 } ";
}
return "{ \"status\": \"Ran tests\", \"testsDone\": " + testsDone + " , \"timeInMs\" : " + (stop - start) / 1000000 + " } ";
}
use of nl.basjes.parse.useragent.UserAgentAnalyzer in project yauaa by nielsbasjes.
the class TestYauaaProcessor method testExtraRules.
@Test
void testExtraRules() {
Map<String, Object> document = new HashMap<>();
document.put(SOURCE_FIELD, "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");
IngestDocument ingestDocument = new IngestDocument("index", "type", "id", null, 42L, VersionType.EXTERNAL, document);
UserAgentAnalyzer userAgentAnalyzer = UserAgentAnalyzer.newBuilder().withFields(Arrays.asList("DeviceClass", "DeviceBrand", "DeviceName", "AgentNameVersionMajor", "FirstProductName")).withCache(10).preheat(10).addYamlRule("config:\n- matcher:\n extract:\n - 'FirstProductName : 1 :agent.(1)product.(1)name'\n").build();
YauaaProcessor processor = new YauaaProcessor("tag", "description", SOURCE_FIELD, TARGET_FIELD, userAgentAnalyzer);
Map<String, Object> data = processor.execute(ingestDocument).getSourceAndMetadata();
MatcherAssert.assertThat(data, hasKey(TARGET_FIELD));
Object targetFieldData = data.get(TARGET_FIELD);
assertTrue(targetFieldData instanceof Map, "Wrong type");
Map<?, ?> results = (Map<?, ?>) targetFieldData;
// The EXPLICITLY requested fields
assertHasKValue(results, "FirstProductName", "Mozilla");
assertHasKValue(results, "DeviceClass", "Phone");
assertHasKValue(results, "DeviceBrand", "Google");
assertHasKValue(results, "DeviceName", "Google Nexus 6");
assertHasKValue(results, "AgentNameVersionMajor", "Chrome 53");
// The IMPLICITLY requested fields (i.e. partials of the actually requested ones)
assertHasKValue(results, "AgentName", "Chrome");
assertHasKValue(results, "AgentVersion", "53.0.2785.124");
assertHasKValue(results, "AgentVersionMajor", "53");
// The NOT requested fields
assertHasNotKey(results, "OperatingSystemClass");
assertHasNotKey(results, "OperatingSystemName");
assertHasNotKey(results, "OperatingSystemNameVersion");
assertHasNotKey(results, "OperatingSystemNameVersionMajor");
assertHasNotKey(results, "OperatingSystemVersion");
assertHasNotKey(results, "OperatingSystemVersionBuild");
assertHasNotKey(results, "OperatingSystemVersionMajor");
assertHasNotKey(results, "LayoutEngineClass");
assertHasNotKey(results, "LayoutEngineName");
assertHasNotKey(results, "LayoutEngineNameVersion");
assertHasNotKey(results, "LayoutEngineNameVersionMajor");
assertHasNotKey(results, "LayoutEngineVersion");
assertHasNotKey(results, "LayoutEngineVersionMajor");
assertHasNotKey(results, "AgentClass");
assertHasNotKey(results, "AgentNameVersion");
}
use of nl.basjes.parse.useragent.UserAgentAnalyzer in project yauaa by nielsbasjes.
the class TestYauaaProcessor method testThatProcessorWorks.
@Test
void testThatProcessorWorks() {
Map<String, Object> document = new HashMap<>();
document.put(SOURCE_FIELD, "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");
IngestDocument ingestDocument = new IngestDocument("index", "type", "id", null, 42L, VersionType.EXTERNAL, document);
UserAgentAnalyzer userAgentAnalyzer = UserAgentAnalyzer.newBuilder().build();
YauaaProcessor processor = new YauaaProcessor("tag", "description", SOURCE_FIELD, TARGET_FIELD, userAgentAnalyzer);
Map<String, Object> data = processor.execute(ingestDocument).getSourceAndMetadata();
MatcherAssert.assertThat(data, hasKey(TARGET_FIELD));
Object targetFieldData = data.get(TARGET_FIELD);
assertTrue(targetFieldData instanceof Map, "Wrong type");
Map<?, ?> results = (Map<?, ?>) targetFieldData;
assertHasKValue(results, "DeviceClass", "Phone");
assertHasKValue(results, "DeviceBrand", "Google");
assertHasKValue(results, "DeviceName", "Google Nexus 6");
assertHasKValue(results, "OperatingSystemClass", "Mobile");
assertHasKValue(results, "OperatingSystemName", "Android");
assertHasKValue(results, "OperatingSystemNameVersion", "Android 7.0");
assertHasKValue(results, "OperatingSystemNameVersionMajor", "Android 7");
assertHasKValue(results, "OperatingSystemVersion", "7.0");
assertHasKValue(results, "OperatingSystemVersionBuild", "NBD90Z");
assertHasKValue(results, "OperatingSystemVersionMajor", "7");
assertHasKValue(results, "LayoutEngineClass", "Browser");
assertHasKValue(results, "LayoutEngineName", "Blink");
assertHasKValue(results, "LayoutEngineNameVersion", "Blink 53.0");
assertHasKValue(results, "LayoutEngineNameVersionMajor", "Blink 53");
assertHasKValue(results, "LayoutEngineVersion", "53.0");
assertHasKValue(results, "LayoutEngineVersionMajor", "53");
assertHasKValue(results, "AgentClass", "Browser");
assertHasKValue(results, "AgentName", "Chrome");
assertHasKValue(results, "AgentNameVersion", "Chrome 53.0.2785.124");
assertHasKValue(results, "AgentNameVersionMajor", "Chrome 53");
assertHasKValue(results, "AgentVersion", "53.0.2785.124");
assertHasKValue(results, "AgentVersionMajor", "53");
}
Aggregations