<commit_before>/*
 * Copyright 2008 the original author or authors.
 * 
 * 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 org.openehealth.ipf.platform.camel.lbs.http.process;

import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.camel.CamelContext;
import org.apache.camel.EndpointInject;
import org.apache.camel.Exchange;
import org.apache.camel.ExchangePattern;
import org.apache.camel.Processor;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.impl.DefaultExchange;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.ProtocolException;
import org.apache.commons.httpclient.methods.FileRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.io.IOUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openehealth.ipf.commons.lbs.resource.ResourceCompatibleDataSource;
import org.openehealth.ipf.commons.lbs.resource.ResourceDataSource;
import org.openehealth.ipf.commons.lbs.resource.ResourceFactory;
import org.openehealth.ipf.commons.lbs.store.LargeBinaryStore;
import org.openehealth.ipf.commons.lbs.utils.CorruptedInputStream;
import org.openehealth.ipf.platform.camel.core.junit.DirtySpringContextJUnit4ClassRunner;
import org.openehealth.ipf.platform.camel.lbs.http.process.ResourceList;
import org.springframework.beans.factory.annotation.Autowired;


/**
 * This test creates HTTP endpoints implicitly via the routes. These will be
 * not automatically be removed after the test execution. To do this it is
 * necessary to dirty the Spring application context. The best way to do this
 * is to run this test via {@link DirtySpringContextJUnit4ClassRunner}.
 *  
 * @author <NAME>
 */
public abstract class AbstractLbsHttpTest {
    
    public static final String ENDPOINT_NO_EXTRACT = 
        "http://localhost:9452/lbstest_no_extract";

    public static final String ENDPOINT_EXTRACT = 
        "http://localhost:9452/lbstest_extract";
    
    public static final String ENDPOINT_PING = 
        "http://localhost:9452/lbstest_ping";
    
    public static final String ENDPOINT_EXTRACT_FACTORY_VIA_BEAN = 
        "http://localhost:9452/lbstest_extract_factory_via_bean";

    public static final String ENDPOINT_EXTRACT_ROUTER = 
        "http://localhost:9452/lbstest_extract_router";
    
    public static final String ENDPOINT_SEND_ONLY = 
        "direct:lbstest_send_only";
    
    public static final String ENDPOINT_NON_HTTP = 
        "direct:lbstest_non_http";
        
    @EndpointInject(uri="mock:mock")
    protected MockEndpoint mock;

    @Autowired
    private CamelContext camelContext;

    @Autowired
    protected ProducerTemplate producerTemplate;
    
    @Autowired
    protected LargeBinaryStore store;
    
    @Resource(name = "resourceFactory")
    private ResourceFactory factory;
    
    protected File file;

    protected HttpClient httpClient;

    private TestOutputGenerator outputGenerator;
    
    @Before
    public void setUp() throws Exception {
        file = File.createTempFile(getClass().getName(), "txt");
        FileWriter writer = new FileWriter(file);
        writer.write("blu bla");
        writer.close();
        httpClient = new HttpClient();
        outputGenerator = new TestOutputGenerator("testoutput");
    }

    @After
    public void tearDown() throws Exception {        
        mock.reset();
        // MockEndpoint#reset does not set the default message processor back to
        // null (as of Camel 1.5). We have to do that manually.
        mock.whenAnyExchangeReceived(null);
        
        file.delete();
        store.deleteAll();
    }
    
    @Test
    public void testTextWithoutResourceExtract() throws Exception {
        PostMethod method = new PostMethod(ENDPOINT_NO_EXTRACT);
        method.setRequestEntity(new StringRequestEntity("testtext", "text/plain", null));

        mock.expectedMessageCount(1);
        httpClient.executeMethod(method);
        method.getResponseBody();
        method.releaseConnection();
        mock.assertIsSatisfied();
        
        List<Exchange> receivedExchanges = mock.getExchanges();
        Exchange received = receivedExchanges.get(0);
        
        String body = received.getIn().getBody(String.class);
        assertEquals("testtext", body);
    }
<commit_msg>Fix test to work with new Jetty version<commit_after>/*
 * Copyright 2008 the original author or authors.
 * 
 * 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 org.openehealth.ipf.platform.camel.lbs.http.process;

import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.camel.CamelContext;
import org.apache.camel.EndpointInject;
import org.apache.camel.Exchange;
import org.apache.camel.ExchangePattern;
import org.apache.camel.Processor;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.impl.DefaultExchange;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.ProtocolException;
import org.apache.commons.httpclient.methods.FileRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.io.IOUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openehealth.ipf.commons.lbs.resource.ResourceCompatibleDataSource;
import org.openehealth.ipf.commons.lbs.resource.ResourceDataSource;
import org.openehealth.ipf.commons.lbs.resource.ResourceFactory;
import org.openehealth.ipf.commons.lbs.store.LargeBinaryStore;
import org.openehealth.ipf.commons.lbs.utils.CorruptedInputStream;
import org.openehealth.ipf.platform.camel.core.junit.DirtySpringContextJUnit4ClassRunner;
import org.openehealth.ipf.platform.camel.lbs.http.process.ResourceList;
import org.springframework.beans.factory.annotation.Autowired;


/**
 * This test creates HTTP endpoints implicitly via the routes. These will be
 * not automatically be removed after the test execution. To do this it is
 * necessary to dirty the Spring application context. The best way to do this
 * is to run this test via {@link DirtySpringContextJUnit4ClassRunner}.
 *  
 * @author <NAME>
 */
public abstract class AbstractLbsHttpTest {
    
    public static final String ENDPOINT_NO_EXTRACT = 
        "http://localhost:9452/lbstest_no_extract";

    public static final String ENDPOINT_EXTRACT = 
        "http://localhost:9452/lbstest_extract";
    
    public static final String ENDPOINT_PING = 
        "http://localhost:9452/lbstest_ping";
    
    public static final String ENDPOINT_EXTRACT_FACTORY_VIA_BEAN = 
        "http://localhost:9452/lbstest_extract_factory_via_bean";

    public static final String ENDPOINT_EXTRACT_ROUTER = 
        "http://localhost:9452/lbstest_extract_router";
    
    public static final String ENDPOINT_SEND_ONLY = 
        "direct:lbstest_send_only";
    
    public static final String ENDPOINT_NON_HTTP = 
        "direct:lbstest_non_http";
        
    @EndpointInject(uri="mock:mock")
    protected MockEndpoint mock;

    @Autowired
    private CamelContext camelContext;

    @Autowired
    protected ProducerTemplate producerTemplate;
    
    @Autowired
    protected LargeBinaryStore store;
    
    @Resource(name = "resourceFactory")
    private ResourceFactory factory;
    
    protected File file;

    protected HttpClient httpClient;

    private TestOutputGenerator outputGenerator;
    
    @Before
    public void setUp() throws Exception {
        file = File.createTempFile(getClass().getName(), "txt");
        FileWriter writer = new FileWriter(file);
        writer.write("blu bla");
        writer.close();
        httpClient = new HttpClient();
        outputGenerator = new TestOutputGenerator("testoutput");
    }

    @After
    public void tearDown() throws Exception {        
        mock.reset();
        // MockEndpoint#reset does not set the default message processor back to
        // null (as of Camel 1.5). We have to do that manually.
        mock.whenAnyExchangeReceived(null);
        
        file.delete();
        store.deleteAll();
    }
    
    @Test
    public void testTextWithoutResourceExtract() throws Exception {
        PostMethod method = new PostMethod(ENDPOINT_NO_EXTRACT);
        method.setRequestEntity(new StringRequestEntity("testtext", "text/plain", null));

        mock.expectedMessageCount(1);
        mock.whenAnyExchangeReceived(new Processor() {
            @Override
            public void process(Exchange received) throws Exception {
                // Must be done in here because the connection is only valid during
                // processing
                String body = received.getIn().getBody(String.class);
                assertEquals("testtext", body);
            }            
        });
        httpClient.executeMethod(method);
        method.getResponseBody();
        method.releaseConnection();
        mock.assertIsSatisfied();
    }

{'fraction_non_alphanumeric': 0.08824699610089919, 'fraction_numerical': 0.005331423569666587, 'mean_word_length': 3.880776699029126, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, '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': '7255881', 'n_tokens_mistral': 3533, 'n_tokens_neox': 3461, 'n_words': 843}