<commit_before>
package com.matchandtrade.doc.document;

import com.github.rafasantos.restapidoc.SpecificationFilter;
import com.github.rafasantos.restapidoc.SpecificationParser;
import com.matchandtrade.doc.util.TemplateUtil;
import com.matchandtrade.doc.util.MatchAndTradeApiFacade;
import com.matchandtrade.doc.util.MatchAndTradeRestUtil;
import com.matchandtrade.doc.util.PaginationTemplateUtil;
import com.matchandtrade.rest.v1.json.ArticleJson;
import com.matchandtrade.rest.v1.json.MembershipJson;
import com.matchandtrade.rest.v1.json.TradeJson;
import com.matchandtrade.rest.v1.json.UserJson;
import com.matchandtrade.rest.v1.json.search.Matcher;
import com.matchandtrade.rest.v1.json.search.Operator;
import com.matchandtrade.rest.v1.json.search.Recipe;
import com.matchandtrade.rest.v1.json.search.SearchCriteriaJson;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;

import java.util.Date;

import static org.hamcrest.Matchers.equalTo;


public class SearchDocument implements Document {
	
	private static final String SEARCH_POST_PLACEHOLDER = "SEARCH_POST_PLACEHOLDER";
	private static final String TRADE_ID_PLACEHOLDER = "TRADE_ID";
	private static final String MEMBER_ID_PLACEHOLDER = "MEMBERSHIP_ID";

	@Override
	public String contentFilePath() {
		return "search.html";
	}

	@Override
	public String content() {
		TradeJson trade = buildTrade();
		initOwnerData(trade);
		MembershipJson secondMember = initMemberData(trade);

		// SEARCH_POST_PLACEHOLDER
		SpecificationParser parser = parsePostSearch(trade, secondMember);
//
		String template = TemplateUtil.buildTemplate(contentFilePath());
		// TODO
		template = TemplateUtil.replacePlaceholder(template, TRADE_ID_PLACEHOLDER, trade.getTradeId().toString());
		template = TemplateUtil.replacePlaceholder(template, MEMBER_ID_PLACEHOLDER, secondMember.getMembershipId().toString());
		template = TemplateUtil.replacePlaceholder(template, SEARCH_POST_PLACEHOLDER, parser.asHtmlSnippet());
		template = PaginationTemplateUtil.replacePaginationTable(template);
		return TemplateUtil.appendHeaderAndFooter(template);
	}

	private SpecificationParser parsePostSearch(TradeJson trade, MembershipJson secondMember) {
		SearchCriteriaJson search = new SearchCriteriaJson();
		search.setRecipe(Recipe.ARTICLES);
		search.addCriterion("Trade.tradeId", trade.getTradeId());
		search.addCriterion("Membership.membershipId", secondMember.getMembershipId(), Operator.AND, Matcher.NOT_EQUALS);

		SpecificationFilter filter = new SpecificationFilter();
		SpecificationParser parser = new SpecificationParser(filter);
		RestAssured.given()
			.filter(filter)
			.header(MatchAndTradeRestUtil.getLastAuthorizationHeader())
			.queryParam("_pageNumber", "2")
			.queryParam("_pageSize", "2")
			.contentType(ContentType.JSON)
			.body(search)
			.post(MatchAndTradeRestUtil.searchUrl());
		parser.getResponse().then().statusCode(200).and().header("X-Pagination-Total-Count", equalTo("5"));
		return parser;
	}

	private MembershipJson initMemberData(TradeJson trade) {
		MatchAndTradeRestUtil.nextAuthorizationHeader();
		UserJson user = MatchAndTradeRestUtil.getLastAuthenticatedUser();
		MatchAndTradeApiFacade apiFacade = new MatchAndTradeApiFacade(MatchAndTradeRestUtil.getLastAuthorizationHeader());
		MembershipJson membership = apiFacade.subscribeToTrade(user.getUserId(), trade.getTradeId());
		apiFacade.createArticle(membership, "DO NOT DISPLAY THIS ARTICLE");
		return membership;
	}

	private void initOwnerData(TradeJson trade) {
		MatchAndTradeRestUtil.nextAuthorizationHeader();
		UserJson user = MatchAndTradeRestUtil.getLastAuthenticatedUser();
		MatchAndTradeApiFacade apiFacade = new MatchAndTradeApiFacade(MatchAndTradeRestUtil.getLastAuthorizationHeader());
		MembershipJson memberMembership = apiFacade.subscribeToTrade(user.getUserId(), trade.getTradeId());
		ArticleJson elysiumArticle = apiFacade.createArticle(memberMembership, "Elysium");
		apiFacade.createListing(memberMembership.getMembershipId(), elysiumArticle.getArticleId());
		ArticleJson voyagesOfMarcoPoloArticle = apiFacade.createArticle(memberMembership, "The Voyages of Marco Polo");
		apiFacade.createListing(memberMembership.getMembershipId(), voyagesOfMarcoPoloArticle.getArticleId());
		ArticleJson deusArticle = apiFacade.createArticle(memberMembership, "Deus");
		apiFacade.createListing(memberMembership.getMembershipId(), deusArticle.getArticleId());
	}

	private TradeJson buildTrade() {
		UserJson user = MatchAndTradeRestUtil.getLastAuthenticatedUser();
		MatchAndTradeApiFacade apiFacade = new MatchAndTradeApiFacade();
		TradeJson trade = apiFacade.createTrade("Search Recipe ARTICLES - " + new Date().getTime() + hashCode());
		MembershipJson membership = apiFacade.findMembershipByUserIdAndTradeId(user.getUserId(), trade.getTradeId());
		ArticleJson imperialSettlersArticle = apiFacade.createArticle(membership, "Imperial Settlers");
		apiFacade.createListing(membership.getMembershipId(), imperialSettlersArticle.getArticleId());
		ArticleJson deadOfWinterArticle = apiFacade.createArticle(membership, "Dead of Winter: A Crossroads Game");
		apiFacade.createListing(membership.getMembershipId(), deadOfWinterArticle.getArticleId());
		return trade;
	}

}<commit_msg>Refactor SearchDocument to use MatchAndTradeClient<commit_after>
package com.matchandtrade.doc.document;

import com.github.rafasantos.restapidoc.SpecificationFilter;
import com.github.rafasantos.restapidoc.SpecificationParser;
import com.matchandtrade.doc.util.MatchAndTradeClient;
import com.matchandtrade.doc.util.MatchAndTradeRestUtil;
import com.matchandtrade.doc.util.PaginationTemplateUtil;
import com.matchandtrade.doc.util.TemplateUtil;
import com.matchandtrade.rest.v1.json.*;
import com.matchandtrade.rest.v1.json.search.Recipe;
import com.matchandtrade.rest.v1.json.search.SearchCriteriaJson;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;

import static org.hamcrest.Matchers.equalTo;


public class SearchDocument implements Document {
	
	private static final String SEARCH_POST_PLACEHOLDER = "SEARCH_POST_PLACEHOLDER";
	private static final String TRADE_ID_PLACEHOLDER = "TRADE_ID";
	private static final String MEMBER_ID_PLACEHOLDER = "MEMBERSHIP_ID";

	private final MatchAndTradeClient clientApi;
	private String template;
	private UserJson user;

	public SearchDocument() {
		clientApi = new MatchAndTradeClient();
		user = clientApi.findUser().getResponse().as(UserJson.class);
		template = TemplateUtil.buildTemplate(contentFilePath());
	}

	@Override
	public String contentFilePath() {
		return "search.html";
	}

	@Override
	public String content() {
		TradeJson trade = new TradeJson();
		trade.setName("Trading books - " + System.currentTimeMillis());
		trade = clientApi.create(trade).getResponse().as(TradeJson.class);

		MembershipJson membership = clientApi.findMembershipByUserIdAndTradeIdAsMembership(
			user.getUserId(),
			trade.getTradeId());

		createAndListArticle(membership, "American Gods");
		createAndListArticle(membership, "Neverwhere");
		createAndListArticle(membership, "Stardust");
		createAndListArticle(membership, "The Sandman");

		// SEARCH_POST_PLACEHOLDER
		SpecificationParser parser = postSearchParser(trade, membership);
		template = TemplateUtil.replacePlaceholder(template, TRADE_ID_PLACEHOLDER, trade.getTradeId().toString());
		template = TemplateUtil.replacePlaceholder(template, MEMBER_ID_PLACEHOLDER, membership.getMembershipId().toString());
		template = TemplateUtil.replacePlaceholder(template, SEARCH_POST_PLACEHOLDER, parser.asHtmlSnippet());

		template = PaginationTemplateUtil.replacePaginationTable(template);
		return TemplateUtil.appendHeaderAndFooter(template);
	}

	private void createAndListArticle(MembershipJson membership, String articleName) {
		ArticleJson article = new ArticleJson();
		article.setName(articleName);
		article = clientApi.create(article).getResponse().as(ArticleJson.class);
		ListingJson listing = new ListingJson();
		listing.setMembershipId(membership.getMembershipId());
		listing.setArticleId(article.getArticleId());
		clientApi.create(listing);
	}

	private SpecificationParser postSearchParser(TradeJson trade, MembershipJson secondMember) {
		SearchCriteriaJson search = new SearchCriteriaJson();
		search.setRecipe(Recipe.ARTICLES);
		search.addCriterion("Trade.tradeId", trade.getTradeId());
		search.addCriterion("Membership.membershipId", secondMember.getMembershipId());
		SpecificationFilter filter = new SpecificationFilter();
		SpecificationParser parser = new SpecificationParser(filter);
		RestAssured.given()
			.filter(filter)
			.header(clientApi.getAuthorizationHeader())
			.queryParam("_pageNumber", "2")
			.queryParam("_pageSize", "2")
			.contentType(ContentType.JSON)
			.body(search)
			.post(MatchAndTradeRestUtil.searchUrl());
		parser.getResponse().then().statusCode(200).and().header("X-Pagination-Total-Count", equalTo("4"));
		return parser;
	}

}
{'fraction_non_alphanumeric': 0.10429447852760736, 'fraction_numerical': 0.00256553262688232, 'mean_word_length': 7.271217712177122, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3955827', 'n_tokens_mistral': 2924, 'n_tokens_neox': 2713, 'n_words': 434}