package org.bench4q.web.test.masterMessager;

import static com.github.tomakehurst.wiremock.client.WireMock.*;

import static org.junit.Assert.*;

import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.master.AuthorizeResponseModel;
import org.bench4q.share.models.master.RegisterResponseModel;

import org.bench4q.web.masterMessager.UserMessager;
import org.junit.AfterClass;

import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("file:src/test/resources/bench4qweb-servlet.xml")
public class UserMessageTest extends MessagerTestBase {
	private UserMessager userMessager;
	private String baseUrl = "/user";

	public UserMessager getUserMessager() {
		return userMessager;
	}

	@Autowired
	public void setUserMessager(UserMessager userMessager) {
		this.userMessager = userMessager;
	}

	@BeforeClass
	public static void setUp() {
		startServer();
	}

	@AfterClass
	public static void clear() {
		stopServer();
	}

	@Test
	public void test_register() {
		String url = baseUrl + "/register?scope=&userName=&password=";
		RegisterResponseModel registerResponseModel = new RegisterResponseModel();
		registerResponseModel.setSuccess(true);
		String response = MarshalHelper.tryMarshal(registerResponseModel);
		this.getWireMock().register(
				get(urlEqualTo(url)).willReturn(
						aResponse().withStatus(200)
								.withHeader("Content-Type", "text/xml")
								.withBody(response)));
		assertNotNull(this.userMessager.register("", "", ""));
		assertTrue(this.userMessager.register("", "", "").isSuccess());
	}

	@Test
	public void test_adminLogin() {
		String url = baseUrl + "/adminAuthorize?userName=&password=";
		this.getWireMock().register(
				get(urlEqualTo(url)).willReturn(
						aResponse().withStatus(200)
								.withHeader("Content-Type", "text/xml")
								.withBody(this.createResponse())));
		assertNotNull(this.userMessager.adminLogin("", ""));
		assertTrue(this.userMessager.adminLogin("", "").isSuccess());
	}

	@Test
	public void test_normalLogin() {
		String url = baseUrl + "/normalAuthorize?userName=&password=";
		this.getWireMock().register(
				get(urlEqualTo(url)).willReturn(
						aResponse().withStatus(200)
								.withHeader("Content-Type", "text/xml")
								.withBody(this.createResponse())));
		assertNotNull(this.userMessager.normalLogin("", ""));
		assertTrue(this.userMessager.normalLogin("", "").isSuccess());
	}

	@Test
	public void test_deleteUser() {
		String userName = "userName";
		String url = baseUrl + "/deleteUser/" + userName;
		this.getWireMock().register(
				get(urlEqualTo(url)).willReturn(
						aResponse().withStatus(200)
								.withHeader("Content-Type", "text/xml")
								.withBody(this.createResponse())));
		assertNotNull(this.userMessager.deleteUser(userName));
		assertTrue(this.userMessager.deleteUser(userName).isSuccess());
	}

	public String createResponse() {
		AuthorizeResponseModel authorizeResponseModel = new AuthorizeResponseModel();
		authorizeResponseModel.setSuccess(true);
		return MarshalHelper.tryMarshal(authorizeResponseModel);
	}
}
