/*
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 * $Id: JAXRSClient.java 70976 2013-03-22 14:22:25Z supol $
 */

package com.sun.ts.tests.jaxrs.spec.context.client;

import com.sun.ts.tests.jaxrs.common.client.JaxrsCommonClient;
import com.sun.ts.tests.jaxrs.common.provider.StringBean;
import com.sun.ts.tests.jaxrs.spec.context.server.StringBeanEntityProviderWithInjectables;

import static com.sun.ts.tests.jaxrs.spec.context.server.StringBeanEntityProviderWithInjectables.*;

/*
 * @class.setup_props: webServerHost;
 *                     webServerPort;
 *                     ts_home;
 */
public class JAXRSClient extends JaxrsCommonClient {

	private static final long serialVersionUID = -2921113736906329195L;

	public JAXRSClient() {
		setContextRoot("/jaxrs_spec_context_client_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);
	}

	/* Run test */
	/*
	 * @testName: clientWriterTest
	 * @assertion_ids: JAXRS:SPEC:93; JAXRS:SPEC:93.3;
	 * 					JAXRS:SPEC:94; JAXRS:SPEC:95; JAXRS:SPEC:96;
	 * 					JAXRS:SPEC:97; JAXRS:SPEC:98; JAXRS:SPEC:99;
	 * 					JAXRS:SPEC:100;
	 * 					JAXRS:JAVADOC:754;
	 * @test_Strategy: @Context available to providers
	 * 
	 * 					An instance can be injected into a class field or 
	 * 					method parameter using the @Context annotation.
	 * 
	 * 					The lifecycle of components registered using this 
	 * 					class-based register(...) method is fully managed 
	 * 					by the JAX-RS implementation or any underlying IoC 
	 * 					container supported by the implementation.
	 */
	public void clientWriterTest() throws Fault {
		addProvider(StringBeanEntityProviderWithInjectables.class);
		setRequestContentEntity(new StringBean("stringbean"));
		setProperty(Property.REQUEST, buildRequest(Request.POST, "echo"));
		invoke();
		assertInjection("@Context injection did not work properly:");
	}

	/*
	 * @testName: clientReaderTest
	 * @assertion_ids: JAXRS:SPEC:93; JAXRS:SPEC:93.3;
	 * 					JAXRS:SPEC:94; JAXRS:SPEC:95; JAXRS:SPEC:96;
	 * 					JAXRS:SPEC:97; JAXRS:SPEC:98; JAXRS:SPEC:99;
	 * 					JAXRS:SPEC:100;
	 * 					JAXRS:JAVADOC:754;
	 * @test_Strategy: @Context available to providers
	 * 
	 * 					An instance can be injected into a class field or 
	 * 					method parameter using the @Context annotation.
	 * 
	 * 					The lifecycle of components registered using this 
	 * 					class-based register(...) method is fully managed 
	 * 					by the JAX-RS implementation or any underlying IoC 
	 * 					container supported by the implementation.
	 */
	public void clientReaderTest() throws Fault {
		addProvider(StringBeanEntityProviderWithInjectables.class);
		setRequestContentEntity("stringbean");
		setProperty(Property.REQUEST, buildRequest(Request.POST, "echo"));
		invoke();
		StringBean content = getResponseBody(StringBean.class);
		assertNotNull(content, "response body is null");
		logMsg("Injectables are", content.get());
		assertInjection(content.get(),
				"@Context injection did not work properly:");
	}
	
	// ////////////////////////////////////////////////////////////////////
	private void assertInjection(String body, Object failMessage) throws Fault {			
		assertEquals('1', body.charAt(6), failMessage, notInjected(body, 6),
				"has not been injected"); // Providers				
		assertEquals('1', body.charAt(8), failMessage, notInjected(body, 8),
				"has not been injected"); // Configuration			
		logMsg("@Context injected as expected");
	}

	private void assertInjection(Object failMessage) throws Fault {
		String body = getResponseBody();
		assertInjection(body, failMessage);
	}
}
