<commit_before>package fi.otavanopisto.kuntaapi.test.server.integration.management;

import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNot.not;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.hamcrest.core.StringContains.containsString;
import static org.junit.Assert.assertEquals;

import java.time.ZoneId;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.jayway.restassured.http.ContentType;

import fi.otavanopisto.kuntaapi.server.integrations.management.ManagementConsts;
import fi.otavanopisto.kuntaapi.test.AbstractIntegrationTest;

@SuppressWarnings ("squid:S1192")
public class PostTestsIT extends AbstractIntegrationTest {

  private static final ZoneId TIMEZONE_ID = ZoneId.of("Europe/Helsinki");
  private static final String IMAGE_JPEG = "image/jpeg";
  
  /**
   * Starts WireMock
   */
  @Rule
  public WireMockRule wireMockRule = new WireMockRule(getWireMockPort());
  
  @Before
  public void beforeTest() throws InterruptedException {
    getPtvOrganizationMocker()
      .mock("9355a207-efd3-4cfb-a02b-67187f34c822");
    
    getManagementTagMocker()
      .mockTags(9002);
    
    getManagementCategoryMocker()
      .mockCategories(9001, 9003);
    
    getManagementPostMocker()
      .mockPosts(789, 890, 901);
    
    getManagementPostMenuOrderMocker()
      .mockMenuOrders(789, 890, 901);
    
    getManagementMediaMocker()
      .mockMedias(3001, 3002);
    
    startMocks();

    waitApiListCount("/organizations", 1);
    
    createManagementSettings(getOrganizationId(0));

    waitApiListCount(String.format("/organizations/%s/news", getOrganizationId(0)), 3); 
  }

  @After
  public void afterClass() {
    String organizationId = getOrganizationId(0);
    deleteManagementSettings(organizationId);
  }

  @Test
  public void testFindPosts() {
    String organizationId = getOrganizationId(0);
    givenReadonly()
      .contentType(ContentType.JSON)
      .get("/organizations/{organizationId}/news/{newsArticleId}", organizationId, getNewsArticleId(organizationId, 0))
      .then()
      .assertThat()
      .statusCode(200)
      .body("id", notNullValue())
      .body("title", is("Lorem ipsum dolor sit amet"))
      .body("slug", is("lorem-ipsum-dolor-sit-amet"))
      .body("abstract", containsString("Consectetur adipiscing elit"))
      .body("contents", containsString("Aenean a pellentesque erat"))
      .body("published", sameInstant(getInstant(2017, 01, 12, 11, 38, TIMEZONE_ID)));
  }
  
  @Test
  public void testMoreLink() {
    String organizationId = getOrganizationId(0);
    givenReadonly()
      .contentType(ContentType.JSON)
      .get("/organizations/{organizationId}/news/{newsArticleId}", organizationId, getNewsArticleId(organizationId, 0))
      .then()
      .assertThat()
      .statusCode(200)
      .body("abstract", containsString("Consectetur adipiscing elit"))
      .body("abstract", not(containsString("more-link")));
  }
  
  @Test
  public void testListPosts() {
    String organizationId = getOrganizationId(0);
    givenReadonly()
      .contentType(ContentType.JSON)
      .get("/organizations/{organizationId}/news", organizationId)
      .then()
      .assertThat()
      .statusCode(200)
      .body("slug[0]", is("lorem-ipsum-dolor-sit-amet"))
      .body("slug[1]", is("test-2"))
      .body("slug[2]", is("test-3"));
  } 
  
  @Test
  public void testListPostsPublishedBefore() {
    String organizationId = getOrganizationId(0);
    givenReadonly()
      .contentType(ContentType.JSON)
      .get(String.format("/organizations/{organizationId}/news?publishedBefore=%s", getIsoDateTime(2017, 1, 12, 14, 00, TIMEZONE_ID)), organizationId)
      .then()
      .assertThat()
      .statusCode(200)
      .body("id.size()", is(2))
      .body("slug[0]", is("lorem-ipsum-dolor-sit-amet"))
      .body("slug[1]", is("test-2"));
  } 
  
  @Test
  public void testListPostsPublishedAfter() {
    String organizationId = getOrganizationId(0);
    givenReadonly()
      .contentType(ContentType.JSON)
      .get(String.format("/organizations/{organizationId}/news?publishedAfter=%s", getIsoDateTime(2017, 1, 12, 14, 00, TIMEZONE_ID)), organizationId)
      .then()
      .assertThat()
      .statusCode(200)
      .body("id.size()", is(1))
      .body("slug[0]", is("test-3"));
  }
  
  @Test
  public void testListPostsPublishedBetween() {
    String organizationId = getOrganizationId(0);
    String after = getIsoDateTime(2017, 1, 12, 12, 00, TIMEZONE_ID);
    String before = getIsoDateTime(2017, 1, 12, 14, 00, TIMEZONE_ID);
    
    givenReadonly()
      .contentType(ContentType.JSON)
      .get(String.format("/organizations/{organizationId}/news?publishedBefore=%s&publishedAfter=%s", before, after), organizationId)
      .then()
      .assertThat()
      .statusCode(200)
      .body("id.size()", is(1))
      .body("slug[0]", is("test-2"));
  }
  
  @Test
  public void testListPostsOrderLow() throws InterruptedException {
    getManagementPostMenuOrderMocker().mockMenuOrder(789, 20);
    String organizationId = getOrganizationId(0);
    waitNewsArticleSlug(organizationId, 0, "ORDER_NUMBER_PUBLISHED", "test-2");

    givenReadonly()
      .contentType(ContentType.JSON)
      .get("/organizations/{organizationId}/news?sortBy=ORDER_NUMBER_PUBLISHED", organizationId)
      .then()
      .assertThat()
      .statusCode(200)
      .body("slug[0]", is("test-2"))
      .body("slug[1]", is("test-3"))
      .body("slug[2]", is("lorem-ipsum-dolor-sit-amet"));
  } 
  
  @Test
  public void testListPostsOrderHigh() throws InterruptedException {
    getManagementPostMenuOrderMocker().mockMenuOrder(901, -20);
    String organizationId = getOrganizationId(0);
    waitNewsArticleSlug(organizationId, 0, "ORDER_NUMBER_PUBLISHED", "test-3");
    
    givenReadonly()
      .contentType(ContentType.JSON)
      .get("/organizations/{organizationId}/news?sortBy=ORDER_NUMBER_PUBLISHED", organizationId)
      .then()
      .assertThat()
      .statusCode(200)
      .body("slug[0]", is("test-3"))
      .body("slug[1]", is("lorem-ipsum-dolor-sit-amet"))
      .body("slug[2]", is("test-2"));
  } 
  
  @Test
  public void testListPostsByPath() {
    givenReadonly()
      .contentType(ContentType.JSON)
      .get("/organizations/{organizationId}/news?slug=test-3", getOrganizationId(0))
      .then()<commit_msg>Fixed tests to work with new news ordering<commit_after>package fi.otavanopisto.kuntaapi.test.server.integration.management;

import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNot.not;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.hamcrest.core.StringContains.containsString;
import static org.junit.Assert.assertEquals;

import java.time.ZoneId;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.jayway.restassured.http.ContentType;

import fi.otavanopisto.kuntaapi.server.integrations.management.ManagementConsts;
import fi.otavanopisto.kuntaapi.test.AbstractIntegrationTest;

@SuppressWarnings ("squid:S1192")
public class PostTestsIT extends AbstractIntegrationTest {

  private static final ZoneId TIMEZONE_ID = ZoneId.of("Europe/Helsinki");
  private static final String IMAGE_JPEG = "image/jpeg";
  
  /**
   * Starts WireMock
   */
  @Rule
  public WireMockRule wireMockRule = new WireMockRule(getWireMockPort());
  
  @Before
  public void beforeTest() throws InterruptedException {
    getPtvOrganizationMocker()
      .mock("9355a207-efd3-4cfb-a02b-67187f34c822");
    
    getManagementTagMocker()
      .mockTags(9002);
    
    getManagementCategoryMocker()
      .mockCategories(9001, 9003);
    
    getManagementPostMocker()
      .mockPosts(789, 890, 901);
    
    getManagementPostMenuOrderMocker()
      .mockMenuOrders(789, 890, 901);
    
    getManagementMediaMocker()
      .mockMedias(3001, 3002);
    
    startMocks();

    waitApiListCount("/organizations", 1);
    
    createManagementSettings(getOrganizationId(0));

    waitApiListCount(String.format("/organizations/%s/news", getOrganizationId(0)), 3); 
  }

  @After
  public void afterClass() {
    String organizationId = getOrganizationId(0);
    deleteManagementSettings(organizationId);
  }

  @Test
  public void testFindPosts() {
    String organizationId = getOrganizationId(0);
    givenReadonly()
      .contentType(ContentType.JSON)
      .get("/organizations/{organizationId}/news/{newsArticleId}", organizationId, getNewsArticleId(organizationId, 0))
      .then()
      .assertThat()
      .statusCode(200)
      .body("id", notNullValue())
      .body("title", is("Lorem ipsum dolor sit amet"))
      .body("slug", is("lorem-ipsum-dolor-sit-amet"))
      .body("abstract", containsString("Consectetur adipiscing elit"))
      .body("contents", containsString("Aenean a pellentesque erat"))
      .body("published", sameInstant(getInstant(2017, 01, 12, 11, 38, TIMEZONE_ID)));
  }
  
  @Test
  public void testMoreLink() {
    String organizationId = getOrganizationId(0);
    givenReadonly()
      .contentType(ContentType.JSON)
      .get("/organizations/{organizationId}/news/{newsArticleId}", organizationId, getNewsArticleId(organizationId, 0))
      .then()
      .assertThat()
      .statusCode(200)
      .body("abstract", containsString("Consectetur adipiscing elit"))
      .body("abstract", not(containsString("more-link")));
  }
  
  @Test
  public void testListPosts() {
    String organizationId = getOrganizationId(0);
    givenReadonly()
      .contentType(ContentType.JSON)
      .get("/organizations/{organizationId}/news", organizationId)
      .then()
      .assertThat()
      .statusCode(200)
      .body("slug[0]", is("lorem-ipsum-dolor-sit-amet"))
      .body("slug[1]", is("test-2"))
      .body("slug[2]", is("test-3"));
  } 
  
  @Test
  public void testListPostsPublishedBefore() {
    String organizationId = getOrganizationId(0);
    givenReadonly()
      .contentType(ContentType.JSON)
      .get(String.format("/organizations/{organizationId}/news?publishedBefore=%s", getIsoDateTime(2017, 1, 12, 14, 00, TIMEZONE_ID)), organizationId)
      .then()
      .assertThat()
      .statusCode(200)
      .body("id.size()", is(2))
      .body("slug[0]", is("lorem-ipsum-dolor-sit-amet"))
      .body("slug[1]", is("test-2"));
  } 
  
  @Test
  public void testListPostsPublishedAfter() {
    String organizationId = getOrganizationId(0);
    givenReadonly()
      .contentType(ContentType.JSON)
      .get(String.format("/organizations/{organizationId}/news?publishedAfter=%s", getIsoDateTime(2017, 1, 12, 14, 00, TIMEZONE_ID)), organizationId)
      .then()
      .assertThat()
      .statusCode(200)
      .body("id.size()", is(1))
      .body("slug[0]", is("test-3"));
  }
  
  @Test
  public void testListPostsPublishedBetween() {
    String organizationId = getOrganizationId(0);
    String after = getIsoDateTime(2017, 1, 12, 12, 00, TIMEZONE_ID);
    String before = getIsoDateTime(2017, 1, 12, 14, 00, TIMEZONE_ID);
    
    givenReadonly()
      .contentType(ContentType.JSON)
      .get(String.format("/organizations/{organizationId}/news?publishedBefore=%s&publishedAfter=%s", before, after), organizationId)
      .then()
      .assertThat()
      .statusCode(200)
      .body("id.size()", is(1))
      .body("slug[0]", is("test-2"));
  }
  
  @Test
  public void testListPostsOrderLow() throws InterruptedException {
    getManagementPostMenuOrderMocker().mockMenuOrder(789, 20);
    String organizationId = getOrganizationId(0);
    waitNewsArticleSlug(organizationId, 0, "ORDER_NUMBER_PUBLISHED", "test-3");

    givenReadonly()
      .contentType(ContentType.JSON)
      .get("/organizations/{organizationId}/news?sortBy=ORDER_NUMBER_PUBLISHED", organizationId)
      .then()
      .assertThat()
      .statusCode(200)
      .body("slug[0]", is("test-3"))
      .body("slug[1]", is("test-2"))
      .body("slug[2]", is("lorem-ipsum-dolor-sit-amet"));
  } 
  
  @Test
  public void testListPostsOrderHigh() throws InterruptedException {
    getManagementPostMenuOrderMocker().mockMenuOrder(890, -20);
    String organizationId = getOrganizationId(0);
    waitNewsArticleSlug(organizationId, 0, "ORDER_NUMBER_PUBLISHED", "test-2");

    givenReadonly()
      .contentType(ContentType.JSON)
      .get("/organizations/{organizationId}/news?sortBy=ORDER_NUMBER_PUBLISHED", organizationId)
      .then()
      .assertThat()
      .statusCode(200)
      .body("slug[0]", is("test-2"))
      .body("slug[1]", is("test-3"))
      .body("slug[2]", is("lorem-ipsum-dolor-sit-amet"));
  } 
  
  @Test
  public void testListPostsByPath() {
    givenReadonly()
      .contentType(ContentType.JSON)
      .get("/organizations/{organizationId}/news?slug=test-3", getOrganizationId(0))
      .then()
{'fraction_non_alphanumeric': 0.144213695668957, 'fraction_numerical': 0.030726472631822745, 'mean_word_length': 3.8084632516703785, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5532336', 'n_tokens_mistral': 4527, 'n_tokens_neox': 4184, 'n_words': 678}