<commit_before>package org.fedorahosted.flies.rest.service;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;

import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.core.Response;

import org.dbunit.operation.DatabaseOperation;
import org.fedorahosted.flies.ContentType;
import org.fedorahosted.flies.LocaleId;
import org.fedorahosted.flies.rest.ApiKeyHeaderDecorator;
import org.fedorahosted.flies.rest.client.IDocumentsResource;
import org.fedorahosted.flies.rest.dto.Document;
import org.fedorahosted.flies.rest.dto.Documents;
import org.fedorahosted.flies.rest.dto.Resource;
import org.fedorahosted.flies.rest.dto.TextFlow;
import org.fedorahosted.flies.rest.dto.TextFlowTarget;
import org.hamcrest.MatcherAssert;
import org.jboss.resteasy.client.ClientRequestFactory;
import org.jboss.resteasy.client.ClientResponse;
import org.jboss.resteasy.plugins.providers.RegisterBuiltin;
import org.jboss.resteasy.spi.ResteasyProviderFactory;
import org.jboss.seam.mock.DBUnitSeamTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;


@Test(groups = { "seam-tests" })
public class DocumentsServiceSeamTest extends DBUnitSeamTest {

	ClientRequestFactory clientRequestFactory;
	IDocumentsResource docsService;

	@BeforeClass
	public void prepareRestEasyClientFramework() throws Exception {

		ResteasyProviderFactory instance = ResteasyProviderFactory
				.getInstance();
		RegisterBuiltin.register(instance);

		clientRequestFactory = new ClientRequestFactory(
				new SeamMockClientExecutor(this), (URI)null);

		clientRequestFactory.getPrefixInterceptors().registerInterceptor(
				new ApiKeyHeaderDecorator("admin",
						"12345678901234567890123456789012"));

		docsService = clientRequestFactory
				.createProxy(IDocumentsResource.class, 
					"/restv1/projects/p/sample-project/iterations/i/1.1/documents");

	}

	@Override
	protected void prepareDBUnitOperations() {
	    beforeTestOperations.add(new DataSetOperation(
		    "org/fedorahosted/flies/test/model/DocumentsData.dbunit.xml",
		    DatabaseOperation.CLEAN_INSERT));
	    afterTestOperations.add(new DataSetOperation(
			    "org/fedorahosted/flies/test/model/DocumentsData.dbunit.xml",
			    DatabaseOperation.DELETE_ALL));
	}

	public void getZero() throws Exception {
		expectDocs();
	}

//	private void expectDocs(int expectDocs) {
//	    ClientResponse<Documents> response = docsService.getDocuments();
//
//	    assertThat(response.getStatus(), is(200));
//	    assertThat(response.getEntity(), notNullValue());
//	    assertThat(response.getEntity().getDocuments().size(), is(expectDocs));
//	}
	
	private void expectDocs(Document... docs) {
	    ClientResponse<Documents> response = docsService.getDocuments();
	    
	    assertThat(response.getStatus(), is(200));
	    assertThat(response.getEntity(), notNullValue());
//	    List<String> actual = new ArrayList<String>();
//	    for (Document doc : response.getEntity().getDocuments()) {
//			actual.add(doc.toString());
//		}
//	    assertThat(actual, is(docStrings));
	    assertThat(response.getEntity().getDocuments(), is(Arrays.asList(docs)));

//	    Map<String, Document> expected = new HashMap<String, Document>();
//	    for (Document doc : docs) {
//			expected.put(doc.getId(), doc);
//		}
//	    assertThat(response.getEntity().getDocuments(), is(expected));
	}
	
	private Document newDoc(String id, Resource... resources) {
		ContentType contentType = ContentType.TextPlain;
		Integer version = 1;
		Document doc = new Document(id, id+"name", id+"path", contentType, version, LocaleId.EN);
		for (Resource textFlow : resources) {
			doc.getResources(true).add(textFlow);
		}
		return doc;
	}
	
	private TextFlow newTextFlow(String id, String sourceContent, String targetLocale, String targetContent) {
		TextFlow textFlow = new TextFlow(id, LocaleId.EN);
	    textFlow.setContent(sourceContent);
	    TextFlowTarget target = new TextFlowTarget(textFlow, LocaleId.fromJavaName(targetLocale));
	    target.setContent(targetContent);
		textFlow.addTarget(target);
		return textFlow;
	}

	private Document putDoc1() {
		Documents docs = new Documents();
		Document doc = newDoc("foo.properties", 
				newTextFlow("FOOD", "<NAME>", "de_DE", "Sauerkraut"));
		docs.getDocuments().add(doc);
		Response response = docsService.put(docs);
		assertThat(response.getStatus(), is(200));
		return doc;
	}
	
	private Document postDoc2() {
	    Documents docs = new Documents();
	    Document doc = newDoc("test.properties",
	    		newTextFlow("HELLO", "Hello World", "fr", "Bonjour le Monde"));
		docs.getDocuments().add(doc);
	    Response response = docsService.post(docs);
	    assertThat(response.getStatus(), is(200));
		return doc;
	}
	
	public void putGet() throws Exception {
	    getZero();
	    Document doc1 = putDoc1();
//	    expectDocs(1);
	    expectDocs(doc1);
	}


	
	public void putPostGet() throws Exception {
	    getZero();
	    Document doc1 = putDoc1();
//	    expectDocs(1);
	    expectDocs(doc1);
	    Document doc2 = postDoc2();
//	    expectDocs(2);
	    expectDocs(doc1, doc2);
	}
	
	// TODO expect 404 for non-existent project
//	public void getBadProject() {
//		
//	}

}<commit_msg>Changed test to compare XML representations of Document, since it doesn't implement equals().<commit_after>package org.fedorahosted.flies.rest.service;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;

import java.net.URI;
import java.util.Arrays;
import java.util.Set;
import java.util.TreeSet;

import javax.ws.rs.core.Response;

import org.dbunit.operation.DatabaseOperation;
import org.fedorahosted.flies.ContentType;
import org.fedorahosted.flies.LocaleId;
import org.fedorahosted.flies.rest.ApiKeyHeaderDecorator;
import org.fedorahosted.flies.rest.client.IDocumentsResource;
import org.fedorahosted.flies.rest.dto.Document;
import org.fedorahosted.flies.rest.dto.Documents;
import org.fedorahosted.flies.rest.dto.Resource;
import org.fedorahosted.flies.rest.dto.TextFlow;
import org.fedorahosted.flies.rest.dto.TextFlowTarget;
import org.jboss.resteasy.client.ClientRequestFactory;
import org.jboss.resteasy.client.ClientResponse;
import org.jboss.resteasy.plugins.providers.RegisterBuiltin;
import org.jboss.resteasy.spi.ResteasyProviderFactory;
import org.jboss.seam.mock.DBUnitSeamTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;


@Test(groups = { "seam-tests" })
public class DocumentsServiceSeamTest extends DBUnitSeamTest {

	ClientRequestFactory clientRequestFactory;
	IDocumentsResource docsService;

	@BeforeClass
	public void prepareRestEasyClientFramework() throws Exception {

		ResteasyProviderFactory instance = ResteasyProviderFactory
				.getInstance();
		RegisterBuiltin.register(instance);

		clientRequestFactory = new ClientRequestFactory(
				new SeamMockClientExecutor(this), (URI)null);

		clientRequestFactory.getPrefixInterceptors().registerInterceptor(
				new ApiKeyHeaderDecorator("admin",
						"12345678901234567890123456789012"));

		docsService = clientRequestFactory
				.createProxy(IDocumentsResource.class, 
					"/restv1/projects/p/sample-project/iterations/i/1.1/documents");

	}

	@Override
	protected void prepareDBUnitOperations() {
	    beforeTestOperations.add(new DataSetOperation(
		    "org/fedorahosted/flies/test/model/DocumentsData.dbunit.xml",
		    DatabaseOperation.CLEAN_INSERT));
	    afterTestOperations.add(new DataSetOperation(
			    "org/fedorahosted/flies/test/model/DocumentsData.dbunit.xml",
			    DatabaseOperation.DELETE_ALL));
	}

	public void getZero() throws Exception {
		expectDocs();
	}
	
	private void expectDocs(Document... docs) {
	    ClientResponse<Documents> response = docsService.getDocuments();
	    
	    assertThat(response.getStatus(), is(200));
	    assertThat(response.getEntity(), notNullValue());
	    Set<String> expected = new TreeSet<String>();
	    for (Document doc : docs) {
			expected.add(doc.toString());
		}
	    assertThat(response.getEntity().getDocuments().size(), is(Arrays.asList(docs).size()));
	    Set<String> actual = new TreeSet<String>();
	    for (Document doc : response.getEntity().getDocuments()) {
			actual.add(doc.toString());
		}
	    assertThat(actual, is(expected));
	}
	
	private Document newDoc(String id, Resource... resources) {
		ContentType contentType = ContentType.TextPlain;
		Integer version = 1;
		Document doc = new Document(id, id+"name", id+"path", contentType, version, LocaleId.EN);
		for (Resource textFlow : resources) {
			doc.getResources(true).add(textFlow);
		}
		return doc;
	}
	
	private TextFlow newTextFlow(String id, String sourceContent, String targetLocale, String targetContent) {
		TextFlow textFlow = new TextFlow(id, LocaleId.EN);
	    textFlow.setContent(sourceContent);
	    TextFlowTarget target = new TextFlowTarget(textFlow, LocaleId.fromJavaName(targetLocale));
	    target.setContent(targetContent);
		textFlow.addTarget(target);
		return textFlow;
	}

	private Document putDoc1() {
		Documents docs = new Documents();
		Document doc = newDoc("foo.properties", 
				newTextFlow("FOOD", "<NAME>", "de_DE", "Sauerkraut"));
		docs.getDocuments().add(doc);
		Response response = docsService.put(docs);
		assertThat(response.getStatus(), is(200));
		return doc;
	}
	
	private Document postDoc2() {
	    Documents docs = new Documents();
	    Document doc = newDoc("test.properties",
	    		newTextFlow("HELLO", "Hello World", "fr", "Bonjour le Monde"));
		docs.getDocuments().add(doc);
	    Response response = docsService.post(docs);
	    assertThat(response.getStatus(), is(200));
		return doc;
	}
	
	public void putGet() throws Exception {
	    getZero();
	    Document doc1 = putDoc1();
	    expectDocs(doc1);
	}


	
	public void putPostGet() throws Exception {
	    getZero();
	    Document doc1 = putDoc1();
	    expectDocs(doc1);
	    Document doc2 = postDoc2();
	    expectDocs(doc1, doc2);
	}
	
	// TODO expect 404 for non-existent project
//	public void getBadProject() {
//		
//	}

}
{'fraction_non_alphanumeric': 0.12302593098069799, 'fraction_numerical': 0.012283096120101384, 'mean_word_length': 4.671088999447209, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '1378195', 'n_tokens_mistral': 3530, 'n_tokens_neox': 3153, 'n_words': 591}