/** * Assert the given string equals the given file's content. * * The white space differences are ignored. * * @param expectedFilePath the expected file path * @param actual the actual string * @throws IOException the io exception */ public static void assertStringEqualFileContent(final String expectedFilePath, final String actual) throws IOException { final String expected = new String(Files.readAllBytes(Paths.get(expectedFilePath)), StandardCharsets.UTF_8); assertThat(actual).isEqualToIgnoringWhitespace(expected); }
@Test @SuppressWarnings("checkstyle:LineLength") public void test_should_trigger_when_other_than_material_color_used_in_allowed_file() throws Exception { String file = "values/colors__allowed_from_palette.xml"; String expectedOutcome = "values/colors__allowed_from_palette.xml:7: Error: Attempting to use a color reference out of the material colors palette file [NonMaterialColors]\n" + " <color name=\"red_400\">@color/red</color>\n" + " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n" + "1 errors, 0 warnings\n"; String outcome = lintFiles(file); // There's a weird invisible char that makes equalTo fail :/ assertThat(outcome).isEqualToIgnoringWhitespace(expectedOutcome); }
@Test public void test_detector_triggering() throws Exception { String file = "AndroidLogExistingTestCase.java"; String expectedOutcome = "AndroidLogExistingTestCase.java:8: Error: Don't use Android Log class methods. [InvalidLogStatement]\n" + " Log.d(\"tag\", \"log message\");\n" + " ~~~~~\n" + "1 errors, 0 warnings\n"; String outcome = lintFiles(file); // There's a weird invisible char that makes equalTo fail :/ assertThat(outcome).isEqualToIgnoringWhitespace(expectedOutcome); }
@Test @SuppressWarnings("checkstyle:LineLength") public void test_should_trigger_when_material_palette_color_referenced_directly_in_code() throws Exception { String file = "layout/direct_material_palette_color_usage_test_case.xml"; String expectedOutcome = "layout/direct_material_palette_color_usage_test_case.xml:15: Error: Attempting to use a palette color directly. [DirectMaterialColorUsage]\n" + " android:textColor=\"@color/material_red_400\" />\n" + " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n" + "1 errors, 0 warnings\n"; String outcome = lintFiles(file); // There's a weird invisible char that makes equalTo fail :/ assertThat(outcome).isEqualToIgnoringWhitespace(expectedOutcome); }
@Test @SuppressWarnings("checkstyle:LineLength") public void test_should_trigger_when_there_is_a_hardcoded_color() throws Exception { String file = "layout/hardcoded_colors_existing_test_case.xml"; String expectedOutcome = "layout/hardcoded_colors_existing_test_case.xml:15: Error: Don't use harcoded colors, link through colors file [HardcodedColors]\n" + " android:textColor=\"#FFFFFFFF\" />\n" + " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n" + "1 errors, 0 warnings\n"; String outcome = lintFiles(file); // There's a weird invisible char that makes equalTo fail :/ assertThat(outcome).isEqualToIgnoringWhitespace(expectedOutcome); }
@Test public void remove_saves_error_message_and_stacktrace_when_exception_is_provided() { Throwable error = new NullPointerException("Fake NPE to test persistence to DB"); CeTask task = submit(CeTaskTypes.REPORT, newProjectDto("PROJECT_1")); Optional<CeTask> peek = underTest.peek(WORKER_UUID_1); underTest.remove(peek.get(), CeActivityDto.Status.FAILED, null, error); Optional<CeActivityDto> activityDto = db.getDbClient().ceActivityDao().selectByUuid(session, task.getUuid()); assertThat(activityDto).isPresent(); assertThat(activityDto.get().getErrorMessage()).isEqualTo(error.getMessage()); assertThat(activityDto.get().getErrorStacktrace()).isEqualToIgnoringWhitespace(stacktraceToString(error)); assertThat(activityDto.get().getErrorType()).isNull(); }
@Test public void remove_saves_error_when_TypedMessageException_is_provided() { Throwable error = new TypedExceptionImpl("aType", "aMessage"); CeTask task = submit(CeTaskTypes.REPORT, newProjectDto("PROJECT_1")); Optional<CeTask> peek = underTest.peek(WORKER_UUID_1); underTest.remove(peek.get(), CeActivityDto.Status.FAILED, null, error); CeActivityDto activityDto = db.getDbClient().ceActivityDao().selectByUuid(session, task.getUuid()).get(); assertThat(activityDto.getErrorType()).isEqualTo("aType"); assertThat(activityDto.getErrorMessage()).isEqualTo("aMessage"); assertThat(activityDto.getErrorStacktrace()).isEqualToIgnoringWhitespace(stacktraceToString(error)); }
@Test public void testProcessTermination() throws Exception { final TriggerInstance triggerInstance = createTriggerInstance(); this.processor.processTermination(triggerInstance); this.sendEmailLatch.await(10L, TimeUnit.SECONDS); verify(this.message).setSubject( "flow trigger for flow 'flowId', project 'proj' has been cancelled on azkaban"); assertThat(TestUtils.readResource("/emailTemplate/flowtriggerfailureemail.html", this)) .isEqualToIgnoringWhitespace(this.message.getBody()); }
@Test public void createFailedUpdateMessage() throws Exception { final ExecutorManagerException exception = createTestStracktrace(); assertTrue(this.mailCreator .createFailedUpdateMessage(Arrays.asList(this.executableFlow, this.executableFlow), this.executor, exception, this.message, this.azkabanName, this.scheme, this.clientHostname, this.clientPortNumber)); assertEquals("Flow status could not be updated from executor1-host on unit-tests", this.message.getSubject()); assertThat(TestUtils.readResource("failedUpdateMessage.html", this)) .isEqualToIgnoringWhitespace(this.message.getBody()); }
@Test public void testSendErrorEmail() throws Exception { final Flow flow = this.project.getFlow("jobe"); flow.addFailureEmails(this.receiveAddrList); Assert.assertNotNull(flow); final ExecutableFlow exFlow = new ExecutableFlow(this.project, flow); final CommonMetrics commonMetrics = new CommonMetrics(new MetricsManager(new MetricRegistry())); final Emailer emailer = new Emailer(this.props, commonMetrics, this.messageCreator, this.executorLoader); emailer.alertOnError(exFlow); verify(this.message).addAllToAddress(this.receiveAddrList); verify(this.message).setSubject("Flow 'jobe' has failed on azkaban"); assertThat(TestUtils.readResource("errorEmail2.html", this)) .isEqualToIgnoringWhitespace(this.message.getBody()); }
@Test public void alertOnFailedUpdate() throws Exception { final Flow flow = this.project.getFlow("jobe"); flow.addFailureEmails(this.receiveAddrList); Assert.assertNotNull(flow); final ExecutableFlow exFlow = new ExecutableFlow(this.project, flow); final CommonMetrics commonMetrics = new CommonMetrics(new MetricsManager(new MetricRegistry())); final Emailer emailer = new Emailer(this.props, commonMetrics, this.messageCreator, this.executorLoader); final Executor executor = new Executor(1, "executor1-host", 1234, true); final List<ExecutableFlow> executions = Arrays.asList(exFlow, exFlow); final ExecutorManagerException exception = DefaultMailCreatorTest.createTestStracktrace(); emailer.alertOnFailedUpdate(executor, executions, exception); verify(this.message).addAllToAddress(this.receiveAddrList); verify(this.message) .setSubject("Flow status could not be updated from executor1-host on azkaban"); assertThat(TestUtils.readResource("failedUpdateMessage2.html", this)) .isEqualToIgnoringWhitespace(this.message.getBody()); }
private void assertTriggerInstancesEqual(final TriggerInstance actual, final TriggerInstance expected, final boolean ignoreFlowTrigger) { if (!ignoreFlowTrigger) { if (actual.getFlowTrigger() != null && expected.getFlowTrigger() != null) { assertThat(actual.getFlowTrigger().toString()) .isEqualToIgnoringWhitespace(expected.getFlowTrigger().toString()); } else { assertThat(actual.getFlowTrigger()).isNull(); assertThat(expected.getFlowTrigger()).isNull(); } } assertThat(actual).isEqualToIgnoringGivenFields(expected, "depInstances", "flowTrigger"); assertThat(actual.getDepInstances()) .usingComparatorForElementFieldsWithType((d1, d2) -> { if (d1 == null && d2 == null) { return 0; } else if (d1 != null && d2 != null && d1.getTime() == d2.getTime()) { return 0; } else { return -1; } }, Date.class) .usingElementComparatorIgnoringFields("triggerInstance", "context") .containsExactlyInAnyOrder(expected.getDepInstances() .toArray(new DependencyInstance[expected.getDepInstances().size()])); }
@Test public void createFirstErrorMessage() throws Exception { setJobStatus(Status.FAILED); this.executableFlow.setStatus(Status.FAILED_FINISHING); assertTrue(this.mailCreator.createFirstErrorMessage( this.executableFlow, this.message, this.azkabanName, this.scheme, this.clientHostname, this.clientPortNumber)); assertEquals("Flow 'mail-creator-test' has encountered a failure on unit-tests", this.message.getSubject()); assertThat(TestUtils.readResource("firstErrorMessage.html", this)) .isEqualToIgnoringWhitespace(this.message.getBody()); }
@Test public void createSuccessEmail() throws Exception { setJobStatus(Status.SUCCEEDED); this.executableFlow.setEndTime(END_TIME_MILLIS); this.executableFlow.setStatus(Status.SUCCEEDED); assertTrue(this.mailCreator.createSuccessEmail( this.executableFlow, this.message, this.azkabanName, this.scheme, this.clientHostname, this.clientPortNumber)); assertEquals("Flow 'mail-creator-test' has succeeded on unit-tests", this.message.getSubject()); assertThat(TestUtils.readResource("successEmail.html", this)) .isEqualToIgnoringWhitespace(this.message.getBody()); }
@Test public void testUploadTriggerInstance() { final TriggerInstance expectedTriggerInst = this.createTriggerInstance(this.flowTrigger, this .flow_id, this.flow_version, this.submitUser, this.project, System.currentTimeMillis()); this.triggerInstLoader.uploadTriggerInstance(expectedTriggerInst); final TriggerInstance actualTriggerInst = this.triggerInstLoader .getTriggerInstanceById(expectedTriggerInst.getId()); assertThat(expectedTriggerInst.getFlowTrigger().toString()) .isEqualToIgnoringWhitespace(actualTriggerInst.getFlowTrigger().toString()); assertThat(expectedTriggerInst).isEqualToIgnoringGivenFields(actualTriggerInst, "depInstances", "flowTrigger"); assertThat(expectedTriggerInst.getDepInstances()) .usingElementComparatorIgnoringFields("triggerInstance", "context") .containsAll(actualTriggerInst.getDepInstances()) .hasSameSizeAs(actualTriggerInst.getDepInstances()); }
@Test public void createErrorEmail() throws Exception { setJobStatus(Status.FAILED); this.executableFlow.setEndTime(END_TIME_MILLIS); this.executableFlow.setStatus(Status.FAILED); final List<ExecutableFlow> executableFlows = new ArrayList<>(); final ExecutableFlow executableFlow1 = new ExecutableFlow(this.project, this.flow); executableFlow1.setExecutionId(1); executableFlow1.setStartTime(START_TIME_MILLIS); executableFlow1.setEndTime(END_TIME_MILLIS); executableFlow1.setStatus(Status.FAILED); executableFlows.add(executableFlow1); final ExecutableFlow executableFlow2 = new ExecutableFlow(this.project, this.flow); executableFlow2.setExecutionId(2); executableFlow2.setStartTime(START_TIME_MILLIS); executableFlow2.setEndTime(END_TIME_MILLIS); executableFlow2.setStatus(Status.SUCCEEDED); executableFlows.add(executableFlow2); assertTrue(this.mailCreator.createErrorEmail( this.executableFlow, executableFlows, this.message, this.azkabanName, this.scheme, this .clientHostname, this.clientPortNumber)); assertEquals("Flow 'mail-creator-test' has failed on unit-tests", this.message.getSubject()); assertThat(TestUtils.readResource("errorEmail.html", this)) .isEqualToIgnoringWhitespace(this.message.getBody()); }
@Test public void testExpandDRL() throws Exception { String dsl = "[condition]Something=Something()" + NL + "[then]another=another();"; String drl = "rule 'foo' " + NL + " when " + NL + " Something " + NL + " then " + NL + " another " + NL + "end"; DrlParser parser = new DrlParser(LanguageLevelOption.DRL5); String result = parser.getExpandedDRL( drl, new StringReader(dsl)); Assertions.assertThat("rule 'foo' " + NL + " when " + NL + " Something() " + NL + " then " + NL + " another(); " + NL + "end") .isEqualToIgnoringWhitespace(result); }
public static void DrlFile(String filename) throws Exception { DrlParser parser = new DrlParser(LanguageLevelOption.DRL5); final PackageDescr pkgOriginal = parser.parse( new InputStreamReader( DumperTestHelper.class.getResourceAsStream( filename ) ) ); final DrlDumper dumper = new DrlDumper(); String result1 = dumper.dump( pkgOriginal ); final PackageDescr pkgDerivated = parser.parse( new StringReader( result1 ) ); String result2 = dumper.dump( pkgDerivated ); System.out.println( result1 ); Assertions.assertThat(result1).isEqualToIgnoringWhitespace(result2); }
@Test public void testParseRule() throws Exception { final XmlPackageReader xmlPackageReader = getXmReader(); xmlPackageReader.read( new InputStreamReader( getClass().getResourceAsStream( "test_ParseRule.xml" ) ) ); final PackageDescr packageDescr = xmlPackageReader.getPackageDescr(); String expected = StringUtils.readFileAsString( new InputStreamReader( getClass().getResourceAsStream( "test_ParseRule.drl" ) ) ); // remove license header as that one is not stored in the XML String expectedWithoutHeader = removeLicenseHeader(expected); System.out.println(expectedWithoutHeader); String actual = new DrlDumper().dump( packageDescr ); Assertions.assertThat(expectedWithoutHeader).isEqualToIgnoringWhitespace(actual); }
@Test public void testParseCollect() throws Exception { final XmlPackageReader xmlPackageReader = getXmReader(); xmlPackageReader.read( new InputStreamReader( getClass().getResourceAsStream( "test_ParseCollect.xml" ) ) ); final PackageDescr packageDescr = xmlPackageReader.getPackageDescr(); String expected = StringUtils.readFileAsString( new InputStreamReader( getClass().getResourceAsStream( "test_ParseCollect.drl" ) ) ); String expectedWithoutHeader = removeLicenseHeader( expected ); String actual = new DrlDumper().dump( packageDescr ); Assertions.assertThat(expectedWithoutHeader).isEqualToIgnoringWhitespace(actual); }