package org.bench4q.master.unitTest.controller;

import java.util.HashMap;
import java.util.Map;

import org.bench4q.share.communication.HttpRequester;
import org.bench4q.share.communication.HttpRequester.HttpResponse;
import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.master.AuthorizeResponseModel;

public class TestBase {
	protected HttpRequester httpRequester = new HttpRequester();
	private final String USERNAME = "admin";
	private final String PASSWORD = "admin";
	public static final String BASE_URL = "http://localhost:7979";
	// public static final String BASE_URL =
	// "http://bench4q_master.jd-app.com:80";
	protected final String AUTH_HEADER_PROPERTY = "Authorization";
	protected final String ACCES_TOCKEN_STARTER = "Bearer ";
	protected String accessTocken;

	public String getAccessTocken() {
		return accessTocken;
	}

	public void setAccessTocken(String accessTocken) {
		this.accessTocken = accessTocken;
	}

	public TestBase() {
	}

	public String login() {
		String urlString = BASE_URL + "/user/adminAuthorize";
		Map<String, String> map = new HashMap<String, String>();
		map.put("userName", USERNAME);
		map.put("password", PASSWORD);
		HttpResponse httpResponse;
		try {
			httpResponse = this.httpRequester.sendGet(urlString, map, null);
			System.out.println("http content:" + httpResponse.getContent());
			return ((AuthorizeResponseModel) MarshalHelper.unmarshal(
					AuthorizeResponseModel.class, httpResponse.getContent()))
					.getAccessToken();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	public Map<String, String> makeAccessTockenMap(String accessToken) {
		Map<String, String> properties = new HashMap<String, String>();
		properties
				.put(AUTH_HEADER_PROPERTY, ACCES_TOCKEN_STARTER + accessToken);
		return properties;
	}
}
