<commit_before>/**
 * Copyright (C) 2015 The Gravitee team (http://gravitee.io)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gravitee.gateway.core.reactor;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.gravitee.common.event.EventManager;
import io.gravitee.common.http.HttpMethod;
import io.gravitee.common.http.HttpStatusCode;
import io.gravitee.gateway.api.Response;
import io.gravitee.gateway.core.AbstractCoreTest;
import io.gravitee.gateway.core.definition.ApiDefinition;
import io.gravitee.gateway.core.external.ApiExternalResource;
import io.gravitee.gateway.core.external.ApiServlet;
import io.gravitee.gateway.core.http.HttpServerRequest;
import io.gravitee.gateway.core.http.HttpServerResponse;
import io.gravitee.gateway.core.manager.ApiEvent;
import io.gravitee.gateway.core.plugin.PluginHandler;
import io.gravitee.gateway.core.reporter.ConsoleReporter;
import io.gravitee.gateway.core.reporter.ReporterManager;
import io.gravitee.plugin.api.Plugin;
import io.gravitee.plugin.api.PluginManifest;
import io.gravitee.plugin.api.PluginType;
import org.junit.*;
import org.springframework.beans.factory.annotation.Autowired;
import rx.Observable;

import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.nio.file.Path;


/**
 * @author <NAME> (<EMAIL>)
 */
public class GraviteeReactorTest extends AbstractCoreTest {

    @ClassRule
    public static final ApiExternalResource SERVER_MOCK = new ApiExternalResource("8083", ApiServlet.class, "/*", null);

    @Autowired
    private GraviteeReactor<Observable<Response>> reactor;

    @Autowired
    private ReporterManager reporterManager;

    @Autowired
    private EventManager eventManager;

    @Before
    public void setUp() throws Exception {
        reactor.doStart();
    }

    @After
    public void tearDown() throws Exception {
        reactor.clearHandlers();
        reactor.doStop();
    }

    @Test
    public void processRequest_startedApi() throws IOException {
        // Register API endpoint
        ApiDefinition apiDefinition = getApiDefinition();

        eventManager.publishEvent(ApiEvent.CREATE, apiDefinition);

        HttpServerRequest req = new HttpServerRequest();
        HttpServerResponse response = new HttpServerResponse();

        req.setRequestURI(URI.create("http://localhost/team"));
        req.setMethod(HttpMethod.GET);

        Response resp = reactor.process(req, response).toBlocking().single();
        Assert.assertEquals(HttpStatusCode.OK_200, resp.status());
    }

    @Test
    public void processRequest_notYetStartedApi() throws IOException {
        // Register API endpoint
        ApiDefinition apiDefinition = getApiDefinition();
        apiDefinition.setEnabled(false);

        eventManager.publishEvent(ApiEvent.CREATE, apiDefinition);

        HttpServerRequest req = new HttpServerRequest();
        HttpServerResponse response = new HttpServerResponse();

        req.setRequestURI(URI.create("http://localhost/team"));
        req.setMethod(HttpMethod.GET);

        Response resp = reactor.process(req, response).toBlocking().single();
        Assert.assertEquals(HttpStatusCode.NOT_FOUND_404, resp.status());
    }

    @Test
    public void processNotFoundRequest() throws IOException {
        // Register API endpoint
        ApiDefinition apiDefinition = getApiDefinition();

        eventManager.publishEvent(ApiEvent.CREATE, apiDefinition);
<commit_msg>fix(test): Add a test for bad gateway (null outputstream in our case<commit_after>/**
 * Copyright (C) 2015 The Gravitee team (http://gravitee.io)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gravitee.gateway.core.reactor;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.gravitee.common.event.EventManager;
import io.gravitee.common.http.HttpMethod;
import io.gravitee.common.http.HttpStatusCode;
import io.gravitee.gateway.api.Response;
import io.gravitee.gateway.core.AbstractCoreTest;
import io.gravitee.gateway.core.definition.ApiDefinition;
import io.gravitee.gateway.core.external.ApiExternalResource;
import io.gravitee.gateway.core.external.ApiServlet;
import io.gravitee.gateway.core.http.HttpServerRequest;
import io.gravitee.gateway.core.http.HttpServerResponse;
import io.gravitee.gateway.core.manager.ApiEvent;
import io.gravitee.gateway.core.plugin.PluginHandler;
import io.gravitee.gateway.core.reporter.ConsoleReporter;
import io.gravitee.gateway.core.reporter.ReporterManager;
import io.gravitee.plugin.api.Plugin;
import io.gravitee.plugin.api.PluginManifest;
import io.gravitee.plugin.api.PluginType;
import org.junit.*;
import org.springframework.beans.factory.annotation.Autowired;
import rx.Observable;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.nio.file.Path;


/**
 * @author <NAME> (<EMAIL> at <EMAIL>)
 */
public class GraviteeReactorTest extends AbstractCoreTest {

    @ClassRule
    public static final ApiExternalResource SERVER_MOCK = new ApiExternalResource("8083", ApiServlet.class, "/*", null);

    @Autowired
    private GraviteeReactor<Observable<Response>> reactor;

    @Autowired
    private ReporterManager reporterManager;

    @Autowired
    private EventManager eventManager;

    @Before
    public void setUp() throws Exception {
        reactor.doStart();
    }

    @After
    public void tearDown() throws Exception {
        reactor.clearHandlers();
        reactor.doStop();
    }

    @Test
    public void processRequest_startedApi() throws IOException {
        // Register API endpoint
        ApiDefinition apiDefinition = getApiDefinition();

        eventManager.publishEvent(ApiEvent.CREATE, apiDefinition);

        HttpServerRequest req = new HttpServerRequest();
        HttpServerResponse response = new HttpServerResponse();
        response.setOutputStream(new ByteArrayOutputStream());

        req.setRequestURI(URI.create("http://localhost/team"));
        req.setMethod(HttpMethod.GET);

        Response resp = reactor.process(req, response).toBlocking().single();
        Assert.assertEquals(HttpStatusCode.OK_200, resp.status());
    }

    @Test
    public void processRequest_startedApi_gatewayError() throws IOException {
        // Register API endpoint
        ApiDefinition apiDefinition = getApiDefinition();

        eventManager.publishEvent(ApiEvent.CREATE, apiDefinition);

        HttpServerRequest req = new HttpServerRequest();
        HttpServerResponse response = new HttpServerResponse();

        req.setRequestURI(URI.create("http://localhost/team"));
        req.setMethod(HttpMethod.GET);

        Response resp = reactor.process(req, response).toBlocking().single();
        Assert.assertEquals(HttpStatusCode.BAD_GATEWAY_502, resp.status());
    }

    @Test
    public void processRequest_notYetStartedApi() throws IOException {
        // Register API endpoint
        ApiDefinition apiDefinition = getApiDefinition();
        apiDefinition.setEnabled(false);

        eventManager.publishEvent(ApiEvent.CREATE, apiDefinition);

        HttpServerRequest req = new HttpServerRequest();
        HttpServerResponse response = new HttpServerResponse();

        req.setRequestURI(URI.create("http://localhost/team"));
        req.setMethod(HttpMethod.GET);

        Response resp = reactor.process(req, response).toBlocking().single();
        Assert.assertEquals(HttpStatusCode.NOT_FOUND_404, resp.status());
    }

    @Test
    public void processNotFoundRequest() throws IOException {
        // Register API endpoint
        ApiDefinition apiDefinition = getApiDefinition();

        eventManager.publishEvent(ApiEvent.CREATE, apiDefinition);

{'fraction_non_alphanumeric': 0.09860284012826386, 'fraction_numerical': 0.0044663307375171785, 'mean_word_length': 3.93947963800905, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1469184', 'n_tokens_mistral': 2427, 'n_tokens_neox': 2358, 'n_words': 563}