<commit_before>/**
 * Copyright (C) 2016 Orbeon, Inc.
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the
 * GNU Lesser General Public License as published by the Free Software Foundation; either version
 * 2.1 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * The full text of the license is available at http://www.gnu.org/copyleft/lesser.html
 */
package org.orbeon.oxf.fr.persistence.rest

import java.sql.Connection

import org.junit.Test
import org.orbeon.oxf.fr.persistence.db.Connect
import org.orbeon.oxf.fr.persistence.relational.crud.{Organization, OrganizationId}
import org.orbeon.oxf.test.{ResourceManagerTestBase, XMLSupport}
import org.orbeon.oxf.util.IOUtils._
import org.orbeon.oxf.util.CollectionUtils._
import org.orbeon.oxf.util.{IndentedLogger, LoggerFactory, Logging}
import org.scalatest.junit.AssertionsForJUnit


// Test organization-related code used by the REST API
class OrganizationTest extends ResourceManagerTestBase with AssertionsForJUnit with XMLSupport with Logging {

  private implicit val Logger = new IndentedLogger(LoggerFactory.createLogger(classOf[OrganizationTest]), true)

  private val CA = Organization(List("usa", "ca"))
  private val SF = Organization(List("usa", "ca", "sf"))

  def recordsCount(connection: Connection): Int = {
    val Sql = "SELECT count(*) FROM orbeon_form_organization"
    useAndClose(connection.prepareStatement(Sql)) { statement ⇒
      useAndClose(statement.executeQuery()) { resultSet ⇒
        resultSet.next()
        resultSet.getInt(1)
      }
    }
  }

  def assertRecordsCountIs(connection: Connection, count: Int): Unit =
    assert(recordsCount(connection) === count)

  // Basic test: write organization and read it back
  @Test def createAndRead(): Unit = {
    Connect.withOrbeonTables("create and read") { (connection, provider) ⇒
      List(CA, SF).foreach { writtenOrganization ⇒
        val orgId = Organization.createIfNecessary(connection, provider, writtenOrganization)
        val readOrganization = Organization.read(connection, orgId)
        assert(readOrganization.get === writtenOrganization)
      }<commit_msg>Move test orgs to object, to make them reusable<commit_after>/**
 * Copyright (C) 2016 Orbeon, Inc.
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the
 * GNU Lesser General Public License as published by the Free Software Foundation; either version
 * 2.1 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * The full text of the license is available at http://www.gnu.org/copyleft/lesser.html
 */
package org.orbeon.oxf.fr.persistence.rest

import java.sql.Connection

import org.junit.Test
import org.orbeon.oxf.fr.persistence.db.Connect
import org.orbeon.oxf.fr.persistence.relational.crud.{Organization, OrganizationId}
import org.orbeon.oxf.fr.persistence.rest.OrganizationTest.{CA, SF}
import org.orbeon.oxf.test.{ResourceManagerTestBase, XMLSupport}
import org.orbeon.oxf.util.IOUtils._
import org.orbeon.oxf.util.{IndentedLogger, LoggerFactory, Logging}
import org.scalatest.junit.AssertionsForJUnit

object OrganizationTest {
  val CA = Organization(List("usa", "ca"))
  val SF = Organization(List("usa", "ca", "sf"))
}


// Test organization-related code used by the REST API
class OrganizationTest extends ResourceManagerTestBase with AssertionsForJUnit with XMLSupport with Logging {

  private implicit val Logger = new IndentedLogger(LoggerFactory.createLogger(classOf[OrganizationTest]), true)

  def recordsCount(connection: Connection): Int = {
    val Sql = "SELECT count(*) FROM orbeon_form_organization"
    useAndClose(connection.prepareStatement(Sql)) { statement ⇒
      useAndClose(statement.executeQuery()) { resultSet ⇒
        resultSet.next()
        resultSet.getInt(1)
      }
    }
  }

  def assertRecordsCountIs(connection: Connection, count: Int): Unit =
    assert(recordsCount(connection) === count)

  // Basic test: write organization and read it back
  @Test def createAndRead(): Unit = {
    Connect.withOrbeonTables("create and read") { (connection, provider) ⇒
      List(CA, SF).foreach { writtenOrganization ⇒
        val orgId = Organization.createIfNecessary(connection, provider, writtenOrganization)
        val readOrganization = Organization.read(connection, orgId)
        assert(readOrganization.get === writtenOrganization)
      }
{'fraction_non_alphanumeric': 0.09270176865216508, 'fraction_numerical': 0.0028461069323033137, 'mean_word_length': 4.442477876106195, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4046505', 'n_tokens_mistral': 1388, 'n_tokens_neox': 1377, 'n_words': 441}