/*
 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * 
 * $Id: JAXRSClient.java 73913 2014-07-29 12:31:10Z supol $  
 */

package com.sun.ts.tests.jaxrs.spec.provider.standardnotnull;

import java.io.File;
import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;

import javax.activation.DataSource;
import javax.ws.rs.ProcessingException;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.NoContentException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.xml.bind.JAXBElement;
import javax.xml.transform.Source;

import com.sun.ts.tests.jaxrs.common.client.JaxrsCommonClient;
import com.sun.ts.tests.jaxrs.common.util.JaxrsUtil;

/*
 * @class.setup_props: webServerHost;
 *                     webServerPort;
 *                     ts_home;
 */

public class JAXRSClient extends JaxrsCommonClient {

	private static final long serialVersionUID = -3528892672938533655L;

	public JAXRSClient() {
		setContextRoot("/jaxrs_spec_provider_standardnotnull_web/resource");
	}
	
	/**
	 * Entry point for different-VM execution. It should delegate to method
	 * run(String[], PrintWriter, PrintWriter), and this method should not
	 * contain any test configuration.
	 */
	public static void main(String[] args) {
		new JAXRSClient().run(args);
	}	

	/*
	 * @testName: serverByteArrayProviderTest
	 * @assertion_ids: JAXRS:SPEC:33; JAXRS:SPEC:33.1; JAXRS:SPEC:75;
	 * @test_Strategy: byte[] All media types.
	 * 
	 * 					When reading zero-length request entities, all 
	 * 					implementation-supplied MessageBodyReader implementations 
	 * 					except the JAXB-related one MUST create a corresponding 
	 * 					Java object that represents zero-length data; 
	 * 					they MUST NOT return null.
	 */
	public void serverByteArrayProviderTest() throws Fault {
		setProperty(Property.SEARCH_STRING, Resource.NOTNULL);
		setProperty(Property.REQUEST, buildRequest(Request.POST, "bytearray"));
		invoke();
	}

	/*
	 * @testName: clientByteArrayProviderTest
	 * @assertion_ids: JAXRS:JAVADOC:863;
	 * @test_Strategy: byte[] All media types.
	 * 
	 * 					for a zero-length response entities returns null or a 
	 * 					corresponding Java object that represents zero-length 
	 * 					data.
	 */
	public void clientByteArrayProviderTest() throws Fault {
		setProperty(Property.REQUEST, buildRequest(Request.GET, "entity"));
		invoke();
		byte[] array = new byte[0];
		array = getResponseBody(array.getClass());
		assertTrue(array == null || array.length == 0,
				"byte [] reader gets unexpected", array);
	}

	/*
	 * @testName: serverStringProviderTest
	 * @assertion_ids: JAXRS:SPEC:33; JAXRS:SPEC:33.2; JAXRS:SPEC:75;
	 * @test_Strategy: java.lang.String All media types.
	 * 
	 * 					When reading zero-length request entities, all 
	 * 					implementation-supplied MessageBodyReader implementations 
	 * 					except the JAXB-related one MUST create a corresponding 
	 * 					Java object that represents zero-length data; 
	 * 					they MUST NOT return null.
	 */
	public void serverStringProviderTest() throws Fault {
		setProperty(Property.SEARCH_STRING, Resource.NOTNULL);
		setProperty(Property.REQUEST, buildRequest(Request.POST, "string"));
		invoke();
	}

	/*
	 * @testName: clientStringProviderTest
	 * @assertion_ids: JAXRS:JAVADOC:863;
	 * @test_Strategy: java.lang.String All media types.
	 * 
	 * 					for a zero-length response entities returns null or a 
	 * 					corresponding Java object that represents zero-length 
	 * 					data.
	 */
	public void clientStringProviderTest() throws Fault {
		setProperty(Property.REQUEST, buildRequest(Request.GET, "entity"));
		invoke();
		String entity = getResponseBody(String.class);
		assertTrue(entity == null || entity.length() == 0,
				"String reader gets unexpected", entity);
	}

	/*
	 * @testName: serverInputStreamProviderTest
	 * @assertion_ids: JAXRS:SPEC:33; JAXRS:SPEC:33.3; JAXRS:SPEC:75;
	 * @test_Strategy: java.io.InputStream All media types.
	 * 
	 * 					When reading zero-length request entities, all 
	 * 					implementation-supplied MessageBodyReader implementations 
	 * 					except the JAXB-related one MUST create a corresponding 
	 * 					Java object that represents zero-length data; 
	 * 					they MUST NOT return null.
	 */
	public void serverInputStreamProviderTest() throws Fault {
		setProperty(Property.SEARCH_STRING, Resource.NOTNULL);
		setProperty(Property.REQUEST, buildRequest(Request.POST, "inputstream"));
		invoke();
	}

	/*
	 * @testName: clientInputStreamProviderTest
	 * @assertion_ids: JAXRS:JAVADOC:863;
	 * @test_Strategy: java.io.InputStream All media types.
	 * 
	 * 					for a zero-length response entities returns null or a 
	 * 					corresponding Java object that represents zero-length 
	 * 					data.
	 */
	public void clientInputStreamProviderTest() throws Fault {
		setProperty(Property.REQUEST, buildRequest(Request.GET, "entity"));
		invoke();
		InputStream entity = getResponseBody(InputStream.class);
		String content = null;
		if (entity != null)
			try {
				content = JaxrsUtil.readFromStream(entity);
			} catch (Exception e) {
				throw new Fault(e);
			}
		assertTrue(content == null || content.length() == 0,
				"InputStream reader gets unexpected", content);
	}

	/*
	 * @testName: serverReaderProviderTest
	 * @assertion_ids: JAXRS:SPEC:33; JAXRS:SPEC:33.4; JAXRS:SPEC:75;
	 * @test_Strategy: java.io.Reader All media types.
	 * 
	 * 					When reading zero-length request entities, all 
	 * 					implementation-supplied MessageBodyReader implementations 
	 * 					except the JAXB-related one MUST create a corresponding 
	 * 					Java object that represents zero-length data; 
	 * 					they MUST NOT return null.
	 */
	public void serverReaderProviderTest() throws Fault {
		setProperty(Property.SEARCH_STRING, Resource.NOTNULL);
		setProperty(Property.REQUEST, buildRequest(Request.POST, "reader"));
		invoke();
	}

	/*
	 * @testName: clientReaderProviderTest
	 * @assertion_ids: JAXRS:JAVADOC:863;
	 * @test_Strategy: java.io.Reader All media types.
	 * 
	 * 					for a zero-length response entities returns null or a 
	 * 					corresponding Java object that represents zero-length 
	 * 					data.
	 */
	public void clientReaderProviderTest() throws Fault {
		setProperty(Property.REQUEST, buildRequest(Request.GET, "entity"));
		invoke();
		Reader entity = getResponseBody(Reader.class);
		String content = null;
		if (entity != null)
			try {
				content = JaxrsUtil.readFromReader(entity);
			} catch (Exception e) {
				throw new Fault(e);
			}
		assertTrue(content == null || content.length() == 0,
				"Reader reader gets unexpected", content);
	}

	/*
	 * @testName: serverFileProviderTest
	 * @assertion_ids: JAXRS:SPEC:33; JAXRS:SPEC:33.5; JAXRS:SPEC:75;
	 * @test_Strategy: java.io.File All media types.
	 * 
	 * 					When reading zero-length request entities, all 
	 * 					implementation-supplied MessageBodyReader implementations 
	 * 					except the JAXB-related one MUST create a corresponding 
	 * 					Java object that represents zero-length data; 
	 * 					they MUST NOT return null.
	 */
	public void serverFileProviderTest() throws Fault {
		setProperty(Property.SEARCH_STRING, Resource.NOTNULL);
		setProperty(Property.REQUEST, buildRequest(Request.POST, "file"));
		invoke();
	}

	/*
	 * @testName: clientFileProviderTest
	 * @assertion_ids: JAXRS:JAVADOC:863;
	 * @test_Strategy: java.io.File All media types.
	 * 
	 * 					for a zero-length response entities returns null or a 
	 * 					corresponding Java object that represents zero-length 
	 * 					data.
	 */
	public void clientFileProviderTest() throws Fault {
		setProperty(Property.REQUEST, buildRequest(Request.GET, "entity"));
		invoke();
		File entity = getResponseBody(File.class);
		String content = null;
		if (entity != null)
			try {
				content = JaxrsUtil.readFromFile(entity);
			} catch (Exception e) {
				throw new Fault(e);
			}
		assertTrue(content == null || content.length() == 0,
				"File reader gets unexpected", content);
	}

	/*
	 * @testName: serverDataSourceProviderTest
	 * @assertion_ids: JAXRS:SPEC:33; JAXRS:SPEC:33.6; JAXRS:SPEC:75;
	 * @test_Strategy: DataSource All media types.
	 * 
	 * 					When reading zero-length request entities, all 
	 * 					implementation-supplied MessageBodyReader implementations 
	 * 					except the JAXB-related one MUST create a corresponding 
	 * 					Java object that represents zero-length data; 
	 * 					they MUST NOT return null.
	 */
	public void serverDataSourceProviderTest() throws Fault {
		setProperty(Property.SEARCH_STRING, Resource.NOTNULL);
		setProperty(Property.REQUEST, buildRequest(Request.POST, "datasource"));
		invoke();
	}

	/*
	 * @testName: clientDataSourceProviderTest
	 * @assertion_ids: JAXRS:JAVADOC:863;
	 * @test_Strategy: DataSource All media types.
	 * 
	 * 					for a zero-length response entities returns null or a 
	 * 					corresponding Java object that represents zero-length 
	 * 					data.
	 */
	public void clientDataSourceProviderTest() throws Fault {
		setProperty(Property.REQUEST, buildRequest(Request.GET, "entity"));
		invoke();
		DataSource entity = getResponseBody(DataSource.class);
		String content = null;
		if (entity != null)
			try {
				content = JaxrsUtil.readFromStream(entity.getInputStream());
			} catch (Exception e) {
				throw new Fault(e);
			}
		assertTrue(content == null || content.length() == 0,
				"DataSource reader gets unexpected", content);
	}

	/*
	 * @testName: serverSourceProviderTest
	 * @assertion_ids: JAXRS:SPEC:33; JAXRS:SPEC:33.9; JAXRS:SPEC:75;
	 * @test_Strategy: Source XML types.
	 * 
	 * 					When reading zero-length request entities, all 
	 * 					implementation-supplied MessageBodyReader implementations 
	 * 					except the JAXB-related one MUST create a corresponding 
	 * 					Java object that represents zero-length data; 
	 * 					they MUST NOT return null.
	 */
	public void serverSourceProviderTest() throws Fault {
		setProperty(Property.REQUEST_HEADERS,
				buildContentType(MediaType.APPLICATION_XML_TYPE));
		setProperty(Property.REQUEST, buildRequest(Request.POST, "source"));
		invoke();
	}

	/*
	 * @testName: clientSourceProviderTest
	 * @assertion_ids: JAXRS:JAVADOC:863;
	 * @test_Strategy: Source XML types.
	 * 
	 * 					for a zero-length response entities returns null or a 
	 * 					corresponding Java object that represents zero-length 
	 * 					data.
	 */
	public void clientSourceProviderTest() throws Fault {
		setProperty(Property.REQUEST, buildRequest(Request.GET, "entity"));
		invoke();
		Response response = getResponse();
		response.getHeaders().add(HttpHeaders.CONTENT_TYPE,
				MediaType.APPLICATION_XML);
		Source entity = getResponseBody(Source.class);
		String content = null;
		if (entity != null)
			content = entity.getSystemId();
		assertTrue(content == null || content.length() == 0,
				"Source reader gets unexpected", content);
	}

	/*
	 * @testName: serverMultivaluedMapProviderTest
	 * @assertion_ids: JAXRS:SPEC:33; JAXRS:SPEC:33.9; JAXRS:SPEC:75;
	 * @test_Strategy: MultivaluedMap<String,String> Form content 
	 * 						(application/x-www-form-urlencoded).
	 * 
	 * 					When reading zero-length request entities, all 
	 * 					implementation-supplied MessageBodyReader implementations 
	 * 					except the JAXB-related one MUST create a corresponding 
	 * 					Java object that represents zero-length data; 
	 * 					they MUST NOT return null.
	 */
	public void serverMultivaluedMapProviderTest() throws Fault {
		setProperty(Property.REQUEST_HEADERS,
				buildContentType(MediaType.APPLICATION_FORM_URLENCODED_TYPE));
		setProperty(Property.REQUEST, buildRequest(Request.POST, "map"));
		invoke();
	}

	/*
	 * @testName: clientMultivaluedMapProviderTest
	 * @assertion_ids: JAXRS:JAVADOC:863;
	 * @test_Strategy: MultivaluedMap<String,String> Form content 
	 * 						(application/x-www-form-urlencoded).
	 * 
	 * 					for a zero-length response entities returns null or a 
	 * 					corresponding Java object that represents zero-length 
	 * 					data.
	 */
	public void clientMultivaluedMapProviderTest() throws Fault {
		setProperty(Property.REQUEST, buildRequest(Request.GET, "entity"));
		invoke();
		Response response = getResponse();
		response.getHeaders().add(HttpHeaders.CONTENT_TYPE,
				MediaType.APPLICATION_FORM_URLENCODED);
		@SuppressWarnings("unchecked")
		MultivaluedMap<String, String> entity = getResponseBody(MultivaluedMap.class);
		assertTrue(entity == null || entity.size() == 0,
				"MultivaluedMap reader gets unexpected", entity);
	}

	/*
	 * @testName: serverJaxbProviderTest
	 * @assertion_ids: JAXRS:SPEC:33; JAXRS:SPEC:33.8; JAXRS:SPEC:76;
	 * @test_Strategy: JAXBElement XML media types	
	 *   
	 * 					The pre-packaged JAXB MessageBodyReader implementation 
	 * 					MUST throw a WebApplicationException with a client error 
	 * 					response (HTTP 400) for zero-length request entities. 
	 */
	public void serverJaxbProviderTest() throws Fault {
		setProperty(Property.REQUEST_HEADERS,
				buildContentType(MediaType.APPLICATION_XML_TYPE));
		setProperty(Property.REQUEST, buildRequest(Request.POST, "jaxb"));
		setProperty(Property.STATUS_CODE, getStatusCode(Status.BAD_REQUEST));
		invoke();
	}

	/*
	 * @testName: clientJaxbProviderTest
	 * @assertion_ids: JAXRS:JAVADOC:863;
	 * @test_Strategy: JAXBElement XML media types 
	 * 
	 * 					for a zero-length response entities returns null or a 
	 * 					corresponding Java object that represents zero-length 
	 * 					data.
	 */
	public void clientJaxbProviderTest() throws Fault {
		setProperty(Property.REQUEST, buildRequest(Request.GET, "null"));
		setProperty(Property.REQUEST_HEADERS,
				buildAccept(MediaType.APPLICATION_XML_TYPE));
		setProperty(Property.STATUS_CODE, getStatusCode(Status.NO_CONTENT));
		invoke();
		GenericType<JAXBElement<String>> type = new GenericType<JAXBElement<String>>() {
		};
		JAXBElement<String> entity = getResponse().readEntity(type);
		assertNull(entity,
				"JAXBElement of content type is unexpectedly not null", entity);
		logMsg("Read JAXBElement entity is null as expected");
	}

	/*
	 * @testName: serverStreamingOutputProviderTest
	 * @assertion_ids: JAXRS:SPEC:33; JAXRS:SPEC:33.8; JAXRS:SPEC:75;
	 * @test_Strategy: JAXBElement XML media types	
	 *   
	 * 					The pre-packaged JAXB MessageBodyReader implementation 
	 * 					MUST throw a WebApplicationException with a client error 
	 * 					response (HTTP 400) for zero-length request entities. 
	 */
	public void serverStreamingOutputProviderTest() throws Fault {
		String content = "StreamingOutput";
		setProperty(Property.REQUEST,
				buildRequest(Request.POST, "streamingoutput"));
		setProperty(Property.CONTENT, content);
		setProperty(Property.SEARCH_STRING, content);
		invoke();
	}

	/*
	 * @testName: serverBooleanProviderTest
	 * @assertion_ids: JAXRS:SPEC:33; JAXRS:SPEC:33.8; JAXRS:SPEC:76;
	 * @test_Strategy: Boolean, text/plain media type	
	 *   
	 * 					The pre-packaged JAXB and the prepackaged primitive type 
	 * 					MessageBodyReaders MUST throw a BadRequestException 
	 * 					(400 status) for zero-length request entities. 
	 */
	public void serverBooleanProviderTest() throws Fault {
		setProperty(Property.REQUEST_HEADERS,
				buildContentType(MediaType.TEXT_PLAIN_TYPE));
		setProperty(Property.REQUEST, buildRequest(Request.POST, "boolean"));
		setProperty(Property.STATUS_CODE, getStatusCode(Status.BAD_REQUEST));
		invoke();
	}

	/*
	 * @testName: clientBooleanProviderTest
	 * @assertion_ids: JAXRS:JAVADOC:863;
	 * @test_Strategy: Boolean, text/plain media type
	 * 
	 * 				   the pre-packaged primitive type MessageBodyReader 
	 * 					implementations MUST throw a NoContentException for 
	 * 					zero-length message entities.
	 * 
	 * 					readEntity throws Processing Exception
	 */
	public void clientBooleanProviderTest() throws Fault {
		setProperty(Property.REQUEST, buildRequest(Request.GET, "entity"));
		invoke();
		Response response = getResponse();
		response.getHeaders().add(HttpHeaders.CONTENT_TYPE,
				MediaType.TEXT_PLAIN_TYPE);
		try {
			Boolean entity = getResponseBody(Boolean.class);
			fault("No expcetion thrown and entity is", entity);
		} catch (Exception e) {
			ProcessingException p = assertCause(e, ProcessingException.class,
					"Processing exception has not been thrown", e);
			NoContentException nce = assertCause(e, NoContentException.class,
					"NoContentException has not been thrown", p);
			logMsg("Found expected NoContentException", nce);
		}
	}
	
	/*
	 * @testName: serverCharProviderTest
	 * @assertion_ids: JAXRS:SPEC:33; JAXRS:SPEC:33.8; JAXRS:SPEC:76;
	 * @test_Strategy: Character, text/plain media type	
	 *   
	 * 					The pre-packaged JAXB and the prepackaged primitive type 
	 * 					MessageBodyReaders MUST throw a BadRequestException 
	 * 					(400 status) for zero-length request entities. 
	 */
	public void serverCharProviderTest() throws Fault {
		setProperty(Property.REQUEST_HEADERS,
				buildContentType(MediaType.TEXT_PLAIN_TYPE));
		setProperty(Property.REQUEST, buildRequest(Request.POST, "character"));
		setProperty(Property.STATUS_CODE, getStatusCode(Status.BAD_REQUEST));
		invoke();
	}

	/*
	 * @testName: clientCharProviderTest
	 * @assertion_ids: JAXRS:JAVADOC:863;
	 * @test_Strategy: Character, text/plain media type 
	 * 
	 * 				   the pre-packaged primitive type MessageBodyReader 
	 * 					implementations MUST throw a NoContentException for 
	 * 					zero-length message entities.
	 * 
	 * 					readEntity throws Processing Exception
	 */
	public void clientCharProviderTest() throws Fault {
		setProperty(Property.REQUEST, buildRequest(Request.GET, "entity"));
		invoke();
		Response response = getResponse();
		response.getHeaders().add(HttpHeaders.CONTENT_TYPE,
				MediaType.TEXT_PLAIN_TYPE);
		try {
			Character entity = getResponseBody(Character.class);
			fault("No expcetion thrown and entity is", entity);
		} catch (Exception e) {
			ProcessingException p = assertCause(e, ProcessingException.class,
					"Processing exception has not been thrown", e);
			NoContentException nce = assertCause(e, NoContentException.class,
					"NoContentException has not been thrown", p);
			logMsg("Found expected NoContentException", nce);
		}
	}

	/*
	 * @testName: serverIntegerProviderTest
	 * @assertion_ids: JAXRS:SPEC:33; JAXRS:SPEC:33.8; JAXRS:SPEC:76;
	 * @test_Strategy: Integer, text/plain media type	
	 *   
	 * 					The pre-packaged JAXB and the prepackaged primitive type 
	 * 					MessageBodyReaders MUST throw a BadRequestException 
	 * 					(400 status) for zero-length request entities. 
	 */
	public void serverIntegerProviderTest() throws Fault {
		setProperty(Property.REQUEST_HEADERS,
				buildContentType(MediaType.TEXT_PLAIN_TYPE));
		setProperty(Property.REQUEST, buildRequest(Request.POST, "integer"));
		setProperty(Property.STATUS_CODE, getStatusCode(Status.BAD_REQUEST));
		invoke();
	}

	/*
	 * @testName: clientIntegerProviderTest
	 * @assertion_ids: JAXRS:JAVADOC:863;
	 * @test_Strategy: Integer, text/plain media type 
	 * 
	 * 				   the pre-packaged primitive type MessageBodyReader 
	 * 					implementations MUST throw a NoContentException for 
	 * 					zero-length message entities.
	 * 
	 * 					readEntity throws Processing Exception
	 */
	public void clientIntegerProviderTest() throws Fault {
		setProperty(Property.REQUEST, buildRequest(Request.GET, "entity"));
		invoke();
		Response response = getResponse();
		response.getHeaders().add(HttpHeaders.CONTENT_TYPE,
				MediaType.TEXT_PLAIN_TYPE);
		try {
			Integer entity = getResponseBody(Integer.class);
			fault("No expcetion thrown and entity is", entity);
		} catch (Exception e) {
			ProcessingException p = assertCause(e, ProcessingException.class,
					"Processing exception has not been thrown", e);
			NoContentException nce = assertCause(e, NoContentException.class,
					"NoContentException has not been thrown", p);
			logMsg("Found expected NoContentException", nce);
		}
	}

	/*
	 * @testName: serverBigDecimalProviderTest
	 * @assertion_ids: JAXRS:SPEC:33; JAXRS:SPEC:33.8; JAXRS:SPEC:76;
	 * @test_Strategy: BigDecimal, text/plain media type	
	 *   
	 * 					The pre-packaged JAXB and the prepackaged primitive type 
	 * 					MessageBodyReaders MUST throw a BadRequestException 
	 * 					(400 status) for zero-length request entities. 
	 */
	public void serverBigDecimalProviderTest() throws Fault {
		setProperty(Property.REQUEST_HEADERS,
				buildContentType(MediaType.TEXT_PLAIN_TYPE));
		setProperty(Property.REQUEST, buildRequest(Request.POST, "bigdecimal"));
		setProperty(Property.STATUS_CODE, getStatusCode(Status.BAD_REQUEST));
		invoke();
	}

	/*
	 * @testName: clientBigDecimalProviderTest
	 * @assertion_ids: JAXRS:JAVADOC:863;
	 * @test_Strategy: Integer, text/plain media type 
	 * 
	 * 				   the pre-packaged primitive type MessageBodyReader 
	 * 					implementations MUST throw a NoContentException for 
	 * 					zero-length message entities.
	 * 
	 * 					readEntity throws Processing Exception
	 */
	public void clientBigDecimalProviderTest() throws Fault {
		setProperty(Property.REQUEST, buildRequest(Request.GET, "entity"));
		invoke();
		Response response = getResponse();
		response.getHeaders().add(HttpHeaders.CONTENT_TYPE,
				MediaType.TEXT_PLAIN_TYPE);
		try {
			BigDecimal entity = getResponseBody(BigDecimal.class);
			fault("No expcetion thrown and entity is", entity);
		} catch (Exception e) {
			ProcessingException p = assertCause(e, ProcessingException.class,
					"Processing exception has not been thrown", e);
			NoContentException nce = assertCause(e, NoContentException.class,
					"NoContentException has not been thrown", p);
			logMsg("Found expected NoContentException", nce);
		}
	}

	// ////////////////////////////////////////////////////////////////////////

}
