/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.apache.tika.server;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import javax.ws.rs.core.Response;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;
import org.apache.tika.server.resource.LanguageResource;
import org.apache.tika.server.writer.TarWriter;
import org.apache.tika.server.writer.ZipWriter;
import org.junit.Test;

public class LanguageResourceTest extends CXFTestBase {

	private static final String LANG_PATH = "/language";
	private static final String LANG_STREAM_PATH = LANG_PATH + "/stream";
	private static final String LANG_STRING_PATH = LANG_PATH + "/string";
	private static final String ENGLISH_STRING = "This is English!";
	private static final String FRENCH_STRING = "c’est comme ci comme ça";

	@Override
	protected void setUpResources(JAXRSServerFactoryBean sf) {
		sf.setResourceClasses(LanguageResource.class);
		sf.setResourceProvider(LanguageResource.class,
				new SingletonResourceProvider(new LanguageResource()));

	}

	@Override
	protected void setUpProviders(JAXRSServerFactoryBean sf) {
		List<Object> providers = new ArrayList<Object>();
		providers.add(new TarWriter());
		providers.add(new ZipWriter());
		providers.add(new TikaServerParseExceptionMapper(false));
		sf.setProviders(providers);

	}

	@Test
	public void testDetectEnglishString() throws Exception {
		String url = endPoint + LANG_STRING_PATH;
		Response response = WebClient.create(url).type("text/plain")
				.accept("text/plain").put(ENGLISH_STRING);
		assertNotNull(response);
		String readLang = getStringFromInputStream((InputStream) response
				.getEntity());
		assertEquals("en", readLang);
	}

	@Test
	public void testDetectFrenchString() throws Exception {
		String url = endPoint + LANG_STRING_PATH;
		Response response = WebClient.create(url).type("text/plain")
				.accept("text/plain").put(FRENCH_STRING);
		assertNotNull(response);
		String readLang = getStringFromInputStream((InputStream) response
				.getEntity());
		assertEquals("fr", readLang);
	}

	@Test
	public void testDetectEnglishFile() throws Exception {
		String url = endPoint + LANG_STREAM_PATH;
		Response response = WebClient.create(url).type("text/plain")
				.accept("text/plain")
				.put(ClassLoader.getSystemResourceAsStream("english.txt"));
		assertNotNull(response);
		String readLang = getStringFromInputStream((InputStream) response
				.getEntity());
		assertEquals("en", readLang);
	}

	@Test
	public void testDetectFrenchFile() throws Exception {
		String url = endPoint + LANG_STREAM_PATH;
		Response response = WebClient.create(url).type("text/plain")
				.accept("text/plain")
				.put(ClassLoader.getSystemResourceAsStream("french.txt"));
		assertNotNull(response);
		String readLang = getStringFromInputStream((InputStream) response
				.getEntity());
		assertEquals("fr", readLang);
	}

}
