<commit_before>/**
 * Copyright (C) 2009 - present by OpenGamma Inc. and the OpenGamma group of companies
 *
 * Please see distribution for license.
 */
package com.opengamma.masterdb.historicaltimeseries;

import static com.opengamma.util.db.DbDateUtils.MAX_SQL_TIMESTAMP;
import static com.opengamma.util.db.DbDateUtils.toSqlTimestamp;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertNotNull;

import javax.time.Instant;
import javax.time.TimeSource;
import javax.time.calendar.LocalDate;
import javax.time.calendar.OffsetDateTime;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeMethod;

import com.opengamma.id.ExternalId;
import com.opengamma.id.ExternalIdBundleWithDates;
import com.opengamma.id.ExternalIdWithDates;
import com.opengamma.id.UniqueId;
import com.opengamma.master.historicaltimeseries.HistoricalTimeSeriesInfoDocument;
import com.opengamma.master.historicaltimeseries.ManageableHistoricalTimeSeriesInfo;
import com.opengamma.masterdb.DbMasterTestUtils;
import com.opengamma.util.db.DbDateUtils;
import com.opengamma.util.test.DbTest;

/**
 * Base tests for DbHistoricalTimeSeriesMasterWorker via DbHistoricalTimeSeriesMaster.
 */
public abstract class AbstractDbHistoricalTimeSeriesMasterWorkerTest extends DbTest {

  private static final Logger s_logger = LoggerFactory.getLogger(AbstractDbHistoricalTimeSeriesMasterWorkerTest.class);

  protected DbHistoricalTimeSeriesMaster _htsMaster;
  protected Instant _version1Instant;
  protected Instant _version2Instant;
  protected Instant _version3Instant;
  protected Instant _version4Instant;
  protected int _totalPortfolios;
  protected int _totalHistoricalTimeSeries;
  protected OffsetDateTime _now;

  public AbstractDbHistoricalTimeSeriesMasterWorkerTest(String databaseType, String databaseVersion) {
    super(databaseType, databaseVersion);
    s_logger.info("running testcases for {}", databaseType);
  }

  @BeforeMethod
  public void setUp() throws Exception {
    super.setUp();
    ConfigurableApplicationContext context = DbMasterTestUtils.getContext(getDatabaseType());
    _htsMaster = (DbHistoricalTimeSeriesMaster) context.getBean(getDatabaseType() + "DbHistoricalTimeSeriesMaster");
    
    _now = OffsetDateTime.now();
    _htsMaster.setTimeSource(TimeSource.fixed(_now.toInstant()));
    _version1Instant = _now.toInstant().minusSeconds(100);
    _version2Instant = _now.toInstant().minusSeconds(50);
    _version3Instant = _now.toInstant().minusSeconds(40);
    _version4Instant = _now.toInstant().minusSeconds(30);
    s_logger.debug("test data now:   {}", _now);
    s_logger.debug("test data 1: {}", _version1Instant);
    s_logger.debug("test data 2: {}", _version2Instant);
    s_logger.debug("test data 3: {}", _version3Instant);
    s_logger.debug("test data 4: {}", _version4Instant);
    final SimpleJdbcTemplate template = _htsMaster.getDbConnector().getJdbcTemplate();
    template.update("INSERT INTO hts_name VALUES (?,?)",
        1, "N101");
    template.update("INSERT INTO hts_name VALUES (?,?)",
        2, "N102");
    template.update("INSERT INTO hts_name VALUES (?,?)",
        3, "N201");
    template.update("INSERT INTO hts_name VALUES (?,?)",
        4, "N202");
    template.update("INSERT INTO hts_name VALUES (?,?)",
        5, "N203");
    template.update("INSERT INTO hts_data_field VALUES (?,?)",
        11, "DF11");
    template.update("INSERT INTO hts_data_field VALUES (?,?)",
        12, "DF12");
    template.update("INSERT INTO hts_data_source VALUES (?,?)",
        21, "DS21");
    template.update("INSERT INTO hts_data_source VALUES (?,?)",
        22, "DS22");
    template.update("INSERT INTO hts_data_provider VALUES (?,?)",
        31, "DP31");
    template.update("INSERT INTO hts_data_provider VALUES (?,?)",
        32, "DP32");
    template.update("INSERT INTO hts_observation_time VALUES (?,?)",
        41, "OT41");
    template.update("INSERT INTO hts_observation_time VALUES (?,?)",
        42, "OT42");
    
    template.update("INSERT INTO hts_document VALUES (?,?,?,?,?, ?,?,?,?,?, ?)",
        101, 101, toSqlTimestamp(_version1Instant), MAX_SQL_TIMESTAMP, toSqlTimestamp(_version1Instant), MAX_SQL_TIMESTAMP, 1, 11, 21, 31, 41);
    template.update("INSERT INTO hts_document VALUES (?,?,?,?,?, ?,?,?,?,?, ?)",
        102, 102, toSqlTimestamp(_version1Instant), MAX_SQL_TIMESTAMP, toSqlTimestamp(_version1Instant), MAX_SQL_TIMESTAMP, 2, 12, 22, 32, 42);
    template.update("INSERT INTO hts_document VALUES (?,?,?,?,?, ?,?,?,?,?, ?)",
        201, 201, toSqlTimestamp(_version1Instant), toSqlTimestamp(_version2Instant), toSqlTimestamp(_version1Instant), MAX_SQL_TIMESTAMP, 3, 11, 21, 31, 41);
    template.update("INSERT INTO hts_document VALUES (?,?,?,?,?, ?,?,?,?,?, ?)",
        202, 201, toSqlTimestamp(_version2Instant), MAX_SQL_TIMESTAMP, toSqlTimestamp(_version2Instant), toSqlTimestamp(_version3Instant), 4, 11, 21, 31, 42);
    template.update("INSERT INTO hts_document VALUES (?,?,?,?,?, ?,?,?,?,?, ?)",
        203, 201, toSqlTimestamp(_version2Instant), MAX_SQL_TIMESTAMP, toSqlTimestamp(_version3Instant), MAX_SQL_TIMESTAMP, 5, 11, 21, 31, 42);
    _totalHistoricalTimeSeries = 3;
    
    template.update("INSERT INTO hts_idkey VALUES (?,?,?)",
        501, "TICKER", "V501");
    template.update("INSERT INTO hts_idkey VALUES (?,?,?)",
        502, "NASDAQ", "V502");
    template.update("INSERT INTO hts_idkey VALUES (?,?,?)",
        503, "TICKER", "V503");
    template.update("INSERT INTO hts_idkey VALUES (?,?,?)",
        504, "NASDAQ", "V504");
    template.update("INSERT INTO hts_idkey VALUES (?,?,?)",
        505, "TICKER", "V505");
    template.update("INSERT INTO hts_idkey VALUES (?,?,?)",
        506, "NASDAQ", "V506");
    
    template.update("INSERT INTO hts_doc2idkey (doc_id, idkey_id, valid_from, valid_to) VALUES (?,?,?,?)",
        101, 501, DbDateUtils.MIN_SQL_DATE, DbDateUtils.MAX_SQL_DATE);
    template.update("INSERT INTO hts_doc2idkey (doc_id, idkey_id, valid_from, valid_to) VALUES (?,?,?,?)",
        101, 502, DbDateUtils.MIN_SQL_DATE, DbDateUtils.MAX_SQL_DATE);
    template.update("INSERT INTO hts_doc2idkey (doc_id, idkey_id, valid_from, valid_to) VALUES (?,?,?,?)",
        102, 503, DbDateUtils.MIN_SQL_DATE, DbDateUtils.MAX_SQL_DATE);
    template.update("INSERT INTO hts_doc2idkey (doc_id, idkey_id, valid_from, valid_to) VALUES (?,?,?,?)",
        102, 504, DbDateUtils.toSqlDate(LocalDate.of(2011, 6, 30)), DbDateUtils.MAX_SQL_DATE);
    template.update("INSERT INTO hts_doc2idkey (doc_id, idkey_id, valid_from, valid_to) VALUES (?,?,?,?)",
        201, 505, DbDateUtils.MIN_SQL_DATE, DbDateUtils.MAX_SQL_DATE);
    template.update("INSERT INTO hts_doc2idkey (doc_id, idkey_id, valid_from, valid_to) VALUES (?,?,?,?)",
        201, 506, DbDateUtils.MIN_SQL_DATE, DbDateUtils.MAX_SQL_DATE);
    template.update("INSERT INTO hts_doc2idkey (doc_id, idkey_id, valid_from, valid_to) VALUES (?,?,?,?)",
        202, 505, DbDateUtils.MIN_SQL_DATE, DbDateUtils.MAX_SQL_DATE);
    template.update("INSERT INTO hts_doc2idkey (doc_id, idkey_id, valid_from, valid_to) VALUES (?,?,?,?)",
        202, 506, DbDateUtils.MIN_SQL_DATE, DbDateUtils.MAX_SQL_DATE);
    template.update("INSERT INTO hts_doc2idkey (doc_id, idkey_id, valid_from, valid_to) VALUES (?,?,?,?)",
        203, 505, DbDateUtils.MIN_SQL_DATE, DbDateUtils.MAX_SQL_DATE);
    template.update("INSERT INTO hts_doc2idkey (doc_id, idkey_id, valid_from, valid_to) VALUES (?,?,?,?)",
        203, 506, DbDateUtils.MIN_SQL_DATE, DbDateUtils.MAX_SQL_DATE);
    <commit_msg>Explicitly supply id value for INSERT INTO hts_doc2idkey rather than use auto-generation in inline SQL, as not all databases (mumble mumble SQL Server 2008) support this feature.<commit_after>/**
 * Copyright (C) 2009 - present by OpenGamma Inc. and the OpenGamma group of companies
 *
 * Please see distribution for license.
 */
package com.opengamma.masterdb.historicaltimeseries;

import static com.opengamma.util.db.DbDateUtils.MAX_SQL_TIMESTAMP;
import static com.opengamma.util.db.DbDateUtils.toSqlTimestamp;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertNotNull;

import javax.time.Instant;
import javax.time.TimeSource;
import javax.time.calendar.LocalDate;
import javax.time.calendar.OffsetDateTime;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeMethod;

import com.opengamma.id.ExternalId;
import com.opengamma.id.ExternalIdBundleWithDates;
import com.opengamma.id.ExternalIdWithDates;
import com.opengamma.id.UniqueId;
import com.opengamma.master.historicaltimeseries.HistoricalTimeSeriesInfoDocument;
import com.opengamma.master.historicaltimeseries.ManageableHistoricalTimeSeriesInfo;
import com.opengamma.masterdb.DbMasterTestUtils;
import com.opengamma.util.db.DbDateUtils;
import com.opengamma.util.test.DbTest;

/**
 * Base tests for DbHistoricalTimeSeriesMasterWorker via DbHistoricalTimeSeriesMaster.
 */
public abstract class AbstractDbHistoricalTimeSeriesMasterWorkerTest extends DbTest {

  private static final Logger s_logger = LoggerFactory.getLogger(AbstractDbHistoricalTimeSeriesMasterWorkerTest.class);

  protected DbHistoricalTimeSeriesMaster _htsMaster;
  protected Instant _version1Instant;
  protected Instant _version2Instant;
  protected Instant _version3Instant;
  protected Instant _version4Instant;
  protected int _totalPortfolios;
  protected int _totalHistoricalTimeSeries;
  protected OffsetDateTime _now;

  public AbstractDbHistoricalTimeSeriesMasterWorkerTest(String databaseType, String databaseVersion) {
    super(databaseType, databaseVersion);
    s_logger.info("running testcases for {}", databaseType);
  }

  @BeforeMethod
  public void setUp() throws Exception {
    super.setUp();
    ConfigurableApplicationContext context = DbMasterTestUtils.getContext(getDatabaseType());
    _htsMaster = (DbHistoricalTimeSeriesMaster) context.getBean(getDatabaseType() + "DbHistoricalTimeSeriesMaster");
    
    _now = OffsetDateTime.now();
    _htsMaster.setTimeSource(TimeSource.fixed(_now.toInstant()));
    _version1Instant = _now.toInstant().minusSeconds(100);
    _version2Instant = _now.toInstant().minusSeconds(50);
    _version3Instant = _now.toInstant().minusSeconds(40);
    _version4Instant = _now.toInstant().minusSeconds(30);
    s_logger.debug("test data now:   {}", _now);
    s_logger.debug("test data 1: {}", _version1Instant);
    s_logger.debug("test data 2: {}", _version2Instant);
    s_logger.debug("test data 3: {}", _version3Instant);
    s_logger.debug("test data 4: {}", _version4Instant);
    final SimpleJdbcTemplate template = _htsMaster.getDbConnector().getJdbcTemplate();
    template.update("INSERT INTO hts_name VALUES (?,?)",
        1, "N101");
    template.update("INSERT INTO hts_name VALUES (?,?)",
        2, "N102");
    template.update("INSERT INTO hts_name VALUES (?,?)",
        3, "N201");
    template.update("INSERT INTO hts_name VALUES (?,?)",
        4, "N202");
    template.update("INSERT INTO hts_name VALUES (?,?)",
        5, "N203");
    template.update("INSERT INTO hts_data_field VALUES (?,?)",
        11, "DF11");
    template.update("INSERT INTO hts_data_field VALUES (?,?)",
        12, "DF12");
    template.update("INSERT INTO hts_data_source VALUES (?,?)",
        21, "DS21");
    template.update("INSERT INTO hts_data_source VALUES (?,?)",
        22, "DS22");
    template.update("INSERT INTO hts_data_provider VALUES (?,?)",
        31, "DP31");
    template.update("INSERT INTO hts_data_provider VALUES (?,?)",
        32, "DP32");
    template.update("INSERT INTO hts_observation_time VALUES (?,?)",
        41, "OT41");
    template.update("INSERT INTO hts_observation_time VALUES (?,?)",
        42, "OT42");
    
    template.update("INSERT INTO hts_document VALUES (?,?,?,?,?, ?,?,?,?,?, ?)",
        101, 101, toSqlTimestamp(_version1Instant), MAX_SQL_TIMESTAMP, toSqlTimestamp(_version1Instant), MAX_SQL_TIMESTAMP, 1, 11, 21, 31, 41);
    template.update("INSERT INTO hts_document VALUES (?,?,?,?,?, ?,?,?,?,?, ?)",
        102, 102, toSqlTimestamp(_version1Instant), MAX_SQL_TIMESTAMP, toSqlTimestamp(_version1Instant), MAX_SQL_TIMESTAMP, 2, 12, 22, 32, 42);
    template.update("INSERT INTO hts_document VALUES (?,?,?,?,?, ?,?,?,?,?, ?)",
        201, 201, toSqlTimestamp(_version1Instant), toSqlTimestamp(_version2Instant), toSqlTimestamp(_version1Instant), MAX_SQL_TIMESTAMP, 3, 11, 21, 31, 41);
    template.update("INSERT INTO hts_document VALUES (?,?,?,?,?, ?,?,?,?,?, ?)",
        202, 201, toSqlTimestamp(_version2Instant), MAX_SQL_TIMESTAMP, toSqlTimestamp(_version2Instant), toSqlTimestamp(_version3Instant), 4, 11, 21, 31, 42);
    template.update("INSERT INTO hts_document VALUES (?,?,?,?,?, ?,?,?,?,?, ?)",
        203, 201, toSqlTimestamp(_version2Instant), MAX_SQL_TIMESTAMP, toSqlTimestamp(_version3Instant), MAX_SQL_TIMESTAMP, 5, 11, 21, 31, 42);
    _totalHistoricalTimeSeries = 3;
    
    template.update("INSERT INTO hts_idkey VALUES (?,?,?)",
        501, "TICKER", "V501");
    template.update("INSERT INTO hts_idkey VALUES (?,?,?)",
        502, "NASDAQ", "V502");
    template.update("INSERT INTO hts_idkey VALUES (?,?,?)",
        503, "TICKER", "V503");
    template.update("INSERT INTO hts_idkey VALUES (?,?,?)",
        504, "NASDAQ", "V504");
    template.update("INSERT INTO hts_idkey VALUES (?,?,?)",
        505, "TICKER", "V505");
    template.update("INSERT INTO hts_idkey VALUES (?,?,?)",
        506, "NASDAQ", "V506");
    
    template.update("INSERT INTO hts_doc2idkey (id, doc_id, idkey_id, valid_from, valid_to) VALUES (?,?,?,?,?)",
        1, 101, 501, DbDateUtils.MIN_SQL_DATE, DbDateUtils.MAX_SQL_DATE);
    template.update("INSERT INTO hts_doc2idkey (id, doc_id, idkey_id, valid_from, valid_to) VALUES (?,?,?,?,?)",
        2, 101, 502, DbDateUtils.MIN_SQL_DATE, DbDateUtils.MAX_SQL_DATE);
    template.update("INSERT INTO hts_doc2idkey (id, doc_id, idkey_id, valid_from, valid_to) VALUES (?,?,?,?,?)",
        3, 102, 503, DbDateUtils.MIN_SQL_DATE, DbDateUtils.MAX_SQL_DATE);
    template.update("INSERT INTO hts_doc2idkey (id, doc_id, idkey_id, valid_from, valid_to) VALUES (?,?,?,?,?)",
        4, 102, 504, DbDateUtils.toSqlDate(LocalDate.of(2011, 6, 30)), DbDateUtils.MAX_SQL_DATE);
    template.update("INSERT INTO hts_doc2idkey (id, doc_id, idkey_id, valid_from, valid_to) VALUES (?,?,?,?,?)",
        5, 201, 505, DbDateUtils.MIN_SQL_DATE, DbDateUtils.MAX_SQL_DATE);
    template.update("INSERT INTO hts_doc2idkey (id, doc_id, idkey_id, valid_from, valid_to) VALUES (?,?,?,?,?)",
        6, 201, 506, DbDateUtils.MIN_SQL_DATE, DbDateUtils.MAX_SQL_DATE);
    template.update("INSERT INTO hts_doc2idkey (id, doc_id, idkey_id, valid_from, valid_to) VALUES (?,?,?,?,?)",
        7, 202, 505, DbDateUtils.MIN_SQL_DATE, DbDateUtils.MAX_SQL_DATE);
    template.update("INSERT INTO hts_doc2idkey (id, doc_id, idkey_id, valid_from, valid_to) VALUES (?,?,?,?,?)",
        8, 202, 506, DbDateUtils.MIN_SQL_DATE, DbDateUtils.MAX_SQL_DATE);
    template.update("INSERT INTO hts_doc2idkey (id, doc_id, idkey_id, valid_from, valid_to) VALUES (?,?,?,?,?)",
        9, 203, 505, DbDateUtils.MIN_SQL_DATE, DbDateUtils.MAX_SQL_DATE);
    template.update("INSERT INTO hts_doc2idkey (id, doc_id, idkey_id, valid_from, valid_to) VALUES (?,?,?,?,?)",
        10, 203, 506, DbDateUtils.MIN_SQL_DATE, DbDateUtils.MAX_SQL_DATE);
    
{'fraction_non_alphanumeric': 0.1411624403715792, 'fraction_numerical': 0.03665578709515441, 'mean_word_length': 5.092925430210325, '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': '6006047', 'n_tokens_mistral': 5808, 'n_tokens_neox': 5179, 'n_words': 1034}