<commit_before>package br.com.aurum.astrea.servlet;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.Mockito;

import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.google.gson.reflect.TypeToken;

import br.com.aurum.astrea.domain.Contact;
import br.com.aurum.astrea.service.ContactServlet;
import br.com.aurum.astrea.util.DelegatingServletInputStream;
import br.com.aurum.astrea.util.JsonParse;

public class ContactServletTest extends Mockito {

	private static final String APPLICATION_JSON = "application/json";
	private static final LocalServiceTestHelper helper = new LocalServiceTestHelper(
			new LocalDatastoreServiceTestConfig());

	private HttpServletRequest request;
	private HttpServletResponse response;

	@AfterClass
	public static void afterClass() {
		ContactServletTest.helper.tearDown();
	}

	@BeforeClass
	public static void beforeClass() {
		ContactServletTest.helper.setUp();
	}

	@Before
	public void setUp() {
		this.request = Mockito.mock(HttpServletRequest.class);
		this.response = Mockito.mock(HttpServletResponse.class);
	}

	@Test
	public void saveContactServletTest() throws ServletException, IOException {
		StringWriter out = this.createContactPost();
		Contact contact = (Contact) JsonParse.convertJsonToObject(out.toString(), Contact.class);
		Assert.assertTrue("Deveria ter cadastrado o contato",
				Objects.nonNull(contact) && Objects.nonNull(contact.getId()));
	}

	@SuppressWarnings("unchecked")
	@Test
	public void listContactServletTest() throws Exception {
		this.createContactPost();
		this.request = Mockito.mock(HttpServletRequest.class);
		this.response = Mockito.mock(HttpServletResponse.class);
		Mockito.when(this.request.getParameter(ContactServlet.ACTION)).thenReturn(ContactServlet.ACTION_LISTAGEM);
		StringWriter out = new StringWriter();
		Mockito.when(this.response.getWriter()).thenReturn(new PrintWriter(out, true));
		ContactServlet contactServlet = new ContactServlet();
		contactServlet.doGet(this.request, this.response);
		Type type = new TypeToken<ArrayList<Contact>>() {
		}.getType();
		List<Contact> contacts = (ArrayList<Contact>) JsonParse.convertJsonToObject(out.toString(), type);
		Assert.assertTrue("Deveria conter um contato", Objects.nonNull(contacts) && contacts.size() >= 1);
	}

	@Test
	public void getContactServletTest() throws Exception {
		StringWriter out = this.createContactPost();
		JsonParse.convertJsonToObject(out.toString(), Contact.class);
		this.request = Mockito.mock(HttpServletRequest.class);
		this.response = Mockito.mock(HttpServletResponse.class);
		Mockito.when(this.request.getParameter(ContactServlet.ACTION)).thenReturn(ContactServlet.ACTION_GET_CONTACT);
		Mockito.when(this.request.getParameter(ContactServlet.ID)).thenReturn(null);
		StringWriter outGet = new StringWriter();
		Mockito.when(this.response.getWriter()).thenReturn(new PrintWriter(outGet, true));
		ContactServlet contactServlet = new ContactServlet();
		contactServlet.doGet(this.request, this.response);
		Contact contactGet = (Contact) JsonParse.convertJsonToObject(outGet.toString(), Contact.class);
		Assert.assertTrue("Deveria ter retornado um contato", Objects.nonNull(contactGet));
	}

	private StringWriter createContactPost() throws IOException {
		String contactJson = JsonParse.ObjectToJson(this.createContact());
		ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
				contactJson.getBytes(StandardCharsets.UTF_8));
		DelegatingServletInputStream delegatingServletInputStream = new DelegatingServletInputStream(
				byteArrayInputStream);
		Mockito.when(this.request.getInputStream()).thenReturn(delegatingServletInputStream);<commit_msg>arrumando teste de obterContato<commit_after>package br.com.aurum.astrea.servlet;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.Mockito;

import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.google.gson.reflect.TypeToken;

import br.com.aurum.astrea.domain.Contact;
import br.com.aurum.astrea.service.ContactServlet;
import br.com.aurum.astrea.util.DelegatingServletInputStream;
import br.com.aurum.astrea.util.JsonParse;

public class ContactServletTest extends Mockito {

	private static final String APPLICATION_JSON = "application/json";
	private static final LocalServiceTestHelper helper = new LocalServiceTestHelper(
			new LocalDatastoreServiceTestConfig());

	private HttpServletRequest request;
	private HttpServletResponse response;

	@AfterClass
	public static void afterClass() {
		ContactServletTest.helper.tearDown();
	}

	@BeforeClass
	public static void beforeClass() {
		ContactServletTest.helper.setUp();
	}

	@Before
	public void setUp() {
		this.request = Mockito.mock(HttpServletRequest.class);
		this.response = Mockito.mock(HttpServletResponse.class);
	}

	@Test
	public void saveContactServletTest() throws ServletException, IOException {
		StringWriter out = this.createContactPost();
		Contact contact = (Contact) JsonParse.convertJsonToObject(out.toString(), Contact.class);
		Assert.assertTrue("Deveria ter cadastrado o contato",
				Objects.nonNull(contact) && Objects.nonNull(contact.getId()));
	}

	@SuppressWarnings("unchecked")
	@Test
	public void listContactServletTest() throws Exception {
		this.createContactPost();
		this.request = Mockito.mock(HttpServletRequest.class);
		this.response = Mockito.mock(HttpServletResponse.class);
		Mockito.when(this.request.getParameter(ContactServlet.ACTION)).thenReturn(ContactServlet.ACTION_LISTAGEM);
		StringWriter out = new StringWriter();
		Mockito.when(this.response.getWriter()).thenReturn(new PrintWriter(out, true));
		ContactServlet contactServlet = new ContactServlet();
		contactServlet.doGet(this.request, this.response);
		Type type = new TypeToken<ArrayList<Contact>>() {
		}.getType();
		List<Contact> contacts = (ArrayList<Contact>) JsonParse.convertJsonToObject(out.toString(), type);
		Assert.assertTrue("Deveria conter um contato", Objects.nonNull(contacts) && contacts.size() >= 1);
	}

	@Test
	public void getContactServletTest() throws Exception {
		StringWriter out = this.createContactPost();
		Contact contact = (Contact) JsonParse.convertJsonToObject(out.toString(), Contact.class);
		this.request = Mockito.mock(HttpServletRequest.class);
		this.response = Mockito.mock(HttpServletResponse.class);
		Mockito.when(this.request.getParameter(ContactServlet.ACTION)).thenReturn(ContactServlet.ACTION_GET_CONTACT);
		Mockito.when(this.request.getParameter(ContactServlet.ID)).thenReturn(contact.getId().toString());
		StringWriter outGet = new StringWriter();
		Mockito.when(this.response.getWriter()).thenReturn(new PrintWriter(outGet, true));
		ContactServlet contactServlet = new ContactServlet();
		contactServlet.doGet(this.request, this.response);
		Contact contactGet = (Contact) JsonParse.convertJsonToObject(outGet.toString(), Contact.class);
		Assert.assertTrue("Deveria ter retornado um contato", Objects.nonNull(contactGet));
	}

	private StringWriter createContactPost() throws IOException {
		String contactJson = JsonParse.ObjectToJson(this.createContact());
		ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
				contactJson.getBytes(StandardCharsets.UTF_8));
		DelegatingServletInputStream delegatingServletInputStream = new DelegatingServletInputStream(
				byteArrayInputStream);
		Mockito.when(this.request.getInputStream()).thenReturn(delegatingServletInputStream);
{'fraction_non_alphanumeric': 0.10807986997910378, 'fraction_numerical': 0.0004643603436266543, 'mean_word_length': 6.747302158273381, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6614610', 'n_tokens_mistral': 2522, 'n_tokens_neox': 2428, 'n_words': 451}