Search in sources :

Example 31 with Matchers.containsString

use of org.hamcrest.Matchers.containsString in project jetty.project by eclipse.

the class HttpFieldsTest method testPutTo.

@Test
public void testPutTo() throws Exception {
    HttpFields header = new HttpFields();
    header.put("name0", "value0");
    header.put("name1", "value:A");
    header.add("name1", "value:B");
    header.add("name2", "");
    ByteBuffer buffer = BufferUtil.allocate(1024);
    BufferUtil.flipToFill(buffer);
    HttpGenerator.putTo(header, buffer);
    BufferUtil.flipToFlush(buffer, 0);
    String result = BufferUtil.toString(buffer);
    assertThat(result, Matchers.containsString("name0: value0"));
    assertThat(result, Matchers.containsString("name1: value:A"));
    assertThat(result, Matchers.containsString("name1: value:B"));
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 32 with Matchers.containsString

use of org.hamcrest.Matchers.containsString in project spring-integration by spring-projects.

the class MessagingAnnotationsWithBeanAnnotationTests method testMessagingAnnotationsFlow.

@Test
public void testMessagingAnnotationsFlow() {
    Stream.of(this.sourcePollingChannelAdapters).forEach(a -> a.start());
    // this.sourcePollingChannelAdapter.start();
    for (int i = 0; i < 10; i++) {
        Message<?> receive = this.discardChannel.receive(10000);
        assertNotNull(receive);
        assertTrue(((Integer) receive.getPayload()) % 2 == 0);
        receive = this.counterErrorChannel.receive(10000);
        assertNotNull(receive);
        assertThat(receive, instanceOf(ErrorMessage.class));
        assertThat(receive.getPayload(), instanceOf(MessageRejectedException.class));
        MessageRejectedException exception = (MessageRejectedException) receive.getPayload();
        assertThat(exception.getMessage(), containsString("MessageFilter " + "'messagingAnnotationsWithBeanAnnotationTests.ContextConfiguration.filter.filter.handler'" + " rejected Message"));
    }
    for (Message<?> message : collector) {
        assertFalse(((Integer) message.getPayload()) % 2 == 0);
        MessageHistory messageHistory = MessageHistory.read(message);
        assertNotNull(messageHistory);
        String messageHistoryString = messageHistory.toString();
        assertThat(messageHistoryString, Matchers.containsString("routerChannel"));
        assertThat(messageHistoryString, Matchers.containsString("filterChannel"));
        assertThat(messageHistoryString, Matchers.containsString("aggregatorChannel"));
        assertThat(messageHistoryString, Matchers.containsString("splitterChannel"));
        assertThat(messageHistoryString, Matchers.containsString("serviceChannel"));
        assertThat(messageHistoryString, Matchers.not(Matchers.containsString("discardChannel")));
    }
    assertNull(this.skippedServiceActivator);
    assertNull(this.skippedMessageHandler);
    assertNull(this.skippedChannel);
    assertNull(this.skippedChannel2);
    assertNull(this.skippedMessageSource);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessageHistory(org.springframework.integration.history.MessageHistory) EnableMessageHistory(org.springframework.integration.config.EnableMessageHistory) Matchers.containsString(org.hamcrest.Matchers.containsString) ErrorMessage(org.springframework.messaging.support.ErrorMessage) MessageRejectedException(org.springframework.integration.MessageRejectedException) Test(org.junit.Test)

Example 33 with Matchers.containsString

use of org.hamcrest.Matchers.containsString in project spring-integration by spring-projects.

the class SftpTests method testSftpMgetFlow.

@Test
@SuppressWarnings("unchecked")
public void testSftpMgetFlow() {
    QueueChannel out = new QueueChannel();
    IntegrationFlow flow = f -> f.handle(Sftp.outboundGateway(sessionFactory(), AbstractRemoteFileOutboundGateway.Command.MGET, "payload").options(AbstractRemoteFileOutboundGateway.Option.RECURSIVE).regexFileNameFilter("(subSftpSource|.*1.txt)").localDirectoryExpression("'" + getTargetLocalDirectoryName() + "' + #remoteDirectory").localFilenameExpression("#remoteFileName.replaceFirst('sftpSource', 'localTarget')")).channel(out);
    String dir = "sftpSource/";
    IntegrationFlowRegistration registration = this.flowContext.registration(flow).register();
    registration.getInputChannel().send(new GenericMessage<>(dir + "*"));
    Message<?> result = out.receive(10_000);
    assertNotNull(result);
    List<File> localFiles = (List<File>) result.getPayload();
    // should have filtered sftpSource2.txt
    assertEquals(2, localFiles.size());
    for (File file : localFiles) {
        assertThat(file.getPath().replaceAll(Matcher.quoteReplacement(File.separator), "/"), Matchers.containsString(dir));
    }
    assertThat(localFiles.get(1).getPath().replaceAll(Matcher.quoteReplacement(File.separator), "/"), Matchers.containsString(dir + "subSftpSource"));
    registration.destroy();
}
Also used : DirtiesContext(org.springframework.test.annotation.DirtiesContext) QueueChannel(org.springframework.integration.channel.QueueChannel) IntegrationFlow(org.springframework.integration.dsl.IntegrationFlow) RunWith(org.junit.runner.RunWith) FileHeaders(org.springframework.integration.file.FileHeaders) Autowired(org.springframework.beans.factory.annotation.Autowired) IntegrationFlowContext(org.springframework.integration.dsl.context.IntegrationFlowContext) FileExistsMode(org.springframework.integration.file.support.FileExistsMode) Assert.assertThat(org.junit.Assert.assertThat) MessageBuilder(org.springframework.integration.support.MessageBuilder) Matcher(java.util.regex.Matcher) Pollers(org.springframework.integration.dsl.Pollers) IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) IntegrationFlows(org.springframework.integration.dsl.IntegrationFlows) Message(org.springframework.messaging.Message) SpringRunner(org.springframework.test.context.junit4.SpringRunner) RemoteFileTemplate(org.springframework.integration.file.remote.RemoteFileTemplate) SftpRemoteFileTemplate(org.springframework.integration.sftp.session.SftpRemoteFileTemplate) ChannelSftp(com.jcraft.jsch.ChannelSftp) Matchers.isOneOf(org.hamcrest.Matchers.isOneOf) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Matchers(org.hamcrest.Matchers) AbstractRemoteFileOutboundGateway(org.springframework.integration.file.remote.gateway.AbstractRemoteFileOutboundGateway) Test(org.junit.Test) EnableIntegration(org.springframework.integration.config.EnableIntegration) IntegrationFlowRegistration(org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration) File(java.io.File) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Configuration(org.springframework.context.annotation.Configuration) List(java.util.List) SftpTestSupport(org.springframework.integration.sftp.SftpTestSupport) GenericMessage(org.springframework.messaging.support.GenericMessage) Matchers.containsString(org.hamcrest.Matchers.containsString) Assert.assertEquals(org.junit.Assert.assertEquals) StandardIntegrationFlow(org.springframework.integration.dsl.StandardIntegrationFlow) InputStream(java.io.InputStream) QueueChannel(org.springframework.integration.channel.QueueChannel) IntegrationFlowRegistration(org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration) List(java.util.List) IntegrationFlow(org.springframework.integration.dsl.IntegrationFlow) StandardIntegrationFlow(org.springframework.integration.dsl.StandardIntegrationFlow) Matchers.containsString(org.hamcrest.Matchers.containsString) File(java.io.File) Test(org.junit.Test)

Example 34 with Matchers.containsString

use of org.hamcrest.Matchers.containsString in project gocd by gocd.

the class DiskSpaceWarningCheckerTest method shouldFormatLowDiskSpaceWarningMailWithHelpLinksHttpAndSiteUrl.

@Test
public void shouldFormatLowDiskSpaceWarningMailWithHelpLinksHttpAndSiteUrl() throws URISyntaxException {
    String expectedHelpUrl = docsUrl("/installation/configuring_server_details.html");
    GoConfigService goConfigService = mockGoConfigServiceToHaveSiteUrl();
    SystemDiskSpaceChecker checker = mock(SystemDiskSpaceChecker.class);
    when(checker.getUsableSpace(any(File.class))).thenReturn(1000000L);
    ArtifactsDiskSpaceWarningChecker diskSpaceWarningChecker = new ArtifactsDiskSpaceWarningChecker(new SystemEnvironment(), null, goConfigService, checker, serverHealthService) {

        @Override
        protected String targetFolderCanonicalPath() {
            return ".";
        }
    };
    SendEmailMessage actual = diskSpaceWarningChecker.createEmail();
    ServerHealthStateOperationResult result = new ServerHealthStateOperationResult();
    diskSpaceWarningChecker.check(result);
    assertThat(actual.getBody(), Matchers.containsString(expectedHelpUrl));
    assertThat(result.getServerHealthState().isSuccess(), is(true));
    assertThat(result.getServerHealthState().getMessage(), is("GoCD Server's artifact repository is running low on disk space"));
    assertThat(result.getServerHealthState().getType(), is(HealthStateType.artifactsDiskFull()));
}
Also used : SystemEnvironment(com.thoughtworks.go.util.SystemEnvironment) SendEmailMessage(com.thoughtworks.go.server.messaging.SendEmailMessage) Matchers.containsString(org.hamcrest.Matchers.containsString) ServerHealthStateOperationResult(com.thoughtworks.go.server.service.result.ServerHealthStateOperationResult) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 35 with Matchers.containsString

use of org.hamcrest.Matchers.containsString in project knox by apache.

the class GatewayBasicFuncTest method testXForwardHeadersRewrite.

@Test(timeout = TestUtils.MEDIUM_TIMEOUT)
public void testXForwardHeadersRewrite() throws Exception {
    LOG_ENTER();
    String username = "hdfs";
    String password = "hdfs-password";
    String host = "whatsinaname";
    String port = "8889";
    String scheme = "https";
    // Test rewriting of body with X-Forwarded headers (using storm)
    String resourceName = "storm/topology-id.json";
    String path = "/api/v1/topology/WordCount-1-1424792039";
    String gatewayPath = driver.getUrl("STORM") + path;
    driver.getMock("STORM").expect().method("GET").header("X-Forwarded-Host", host).header("X-Forwarded-Proto", scheme).header("X-Forwarded-Port", port).header("X-Forwarded-Context", "/gateway/cluster").header("X-Forwarded-Server", host).header("X-Forwarded-For", Matchers.containsString("what, boo")).pathInfo(path).queryParam("user.name", username).respond().status(HttpStatus.SC_OK).content(driver.getResourceBytes(resourceName)).contentType(ContentType.JSON.toString());
    Response response = given().auth().preemptive().basic(username, password).header("X-XSRF-Header", "jksdhfkhdsf").header("Accept", ContentType.JSON.toString()).header("X-Forwarded-Host", host).header("X-Forwarded-Proto", scheme).header("X-Forwarded-Port", port).header("X-Forwarded-Server", "what").header("X-Forwarded-For", "what, boo").then().statusCode(HttpStatus.SC_OK).contentType(ContentType.JSON.toString()).when().get(gatewayPath);
    String link = response.getBody().jsonPath().getString("spouts[0].errorWorkerLogLink");
    assertThat(link, is(startsWith(scheme + "://" + host + ":" + port + "/")));
    assertThat(link, containsString("/storm/logviewer"));
    driver.assertComplete();
    resourceName = "storm/topology-component-id.json";
    path = "/api/v1/topology/WordCount-1-1424792039/component/spout";
    gatewayPath = driver.getUrl("STORM") + path;
    driver.getMock("STORM").expect().method("GET").header("X-Forwarded-Host", host).header("X-Forwarded-Proto", scheme).header("X-Forwarded-Port", port).header("X-Forwarded-Context", "/gateway/cluster").header("X-Forwarded-Server", host).header("X-Forwarded-For", Matchers.containsString("what, boo")).pathInfo(path).queryParam("user.name", username).respond().status(HttpStatus.SC_OK).content(driver.getResourceBytes(resourceName)).contentType(ContentType.JSON.toString());
    response = given().auth().preemptive().basic(username, password).header("X-XSRF-Header", "jksdhfkhdsf").header("Accept", ContentType.JSON.toString()).header("X-Forwarded-Host", host).header("X-Forwarded-Proto", scheme).header("X-Forwarded-Port", port).header("X-Forwarded-Server", host).header("X-Forwarded-For", "what, boo").then().statusCode(HttpStatus.SC_OK).contentType(ContentType.JSON.toString()).when().get(gatewayPath);
    link = response.getBody().jsonPath().getString("executorStats[0].workerLogLink");
    assertThat(link, is(startsWith(scheme + "://" + host + ":" + port + "/")));
    assertThat(link, containsString("/storm/logviewer"));
    driver.assertComplete();
    // Test header rewrite using webhdfs
    String root = "/tmp/GatewayBasicFuncTest/testBasicOutboundHeaderUseCase";
    driver.getMock("WEBHDFS").expect().method("PUT").pathInfo("/v1" + root + "/dir/file").header("Host", driver.getRealAddr("WEBHDFS")).header("X-Forwarded-Host", host).header("X-Forwarded-Proto", scheme).header("X-Forwarded-Port", port).header("X-Forwarded-Context", "/gateway/cluster").header("X-Forwarded-Server", host).header("X-Forwarded-For", Matchers.containsString("what, boo")).queryParam("op", "CREATE").queryParam("user.name", username).respond().status(HttpStatus.SC_TEMPORARY_REDIRECT).header("Location", driver.getRealUrl("DATANODE") + "/v1" + root + "/dir/file?op=CREATE&user.name=hdfs");
    response = given().auth().preemptive().basic(username, password).header("X-XSRF-Header", "jksdhfkhdsf").header("X-Forwarded-Host", host).header("X-Forwarded-Proto", scheme).header("X-Forwarded-Port", port).header("X-Forwarded-Server", host).header("X-Forwarded-For", "what, boo").queryParam("op", "CREATE").then().statusCode(HttpStatus.SC_TEMPORARY_REDIRECT).when().put(driver.getUrl("WEBHDFS") + "/v1" + root + "/dir/file");
    String location = response.getHeader("Location");
    log.debug("Redirect location: " + response.getHeader("Location"));
    if (driver.isUseGateway()) {
        assertThat(location, is(startsWith(scheme + "://" + host + ":" + port + "/")));
        assertThat(location, containsString("?_="));
    }
    assertThat(location, not(containsString("host=")));
    assertThat(location, not(containsString("port=")));
    LOG_EXIT();
}
Also used : Response(io.restassured.response.Response) HttpResponse(org.apache.http.HttpResponse) Matchers.emptyString(org.hamcrest.Matchers.emptyString) Matchers.containsString(org.hamcrest.Matchers.containsString) VerifyTest(org.apache.knox.test.category.VerifyTest) Test(org.junit.Test)

Aggregations

Matchers.containsString (org.hamcrest.Matchers.containsString)35 Test (org.junit.Test)32 Socket (java.net.Socket)12 OutputStream (java.io.OutputStream)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 ServletOutputStream (javax.servlet.ServletOutputStream)9 StacklessLogging (org.eclipse.jetty.util.log.StacklessLogging)6 IOException (java.io.IOException)5 InputStream (java.io.InputStream)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 ServletException (javax.servlet.ServletException)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)4 File (java.io.File)3 ByteBuffer (java.nio.ByteBuffer)3 List (java.util.List)3 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)3 AbstractLogger (org.eclipse.jetty.util.log.AbstractLogger)3 Matchers (org.hamcrest.Matchers)3 Assert.assertEquals (org.junit.Assert.assertEquals)3