<commit_before>/*
 * Copyright 2017 Datamountaineer.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.datamountaineer.streamreactor.connect.redis.sink.writer

import com.datamountaineer.streamreactor.connect.redis.sink.config.{RedisConfig, RedisConfigConstants, RedisSinkSettings}
import com.google.gson.Gson
import org.apache.kafka.connect.data.{Schema, SchemaBuilder, Struct}
import org.apache.kafka.connect.sink.SinkRecord
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{BeforeAndAfterAll, Matchers, WordSpec}
import java.io.File
import java.net.URI

import org.apache.kafka.common.config.SslConfigs
import redis.clients.jedis.Jedis

import scala.collection.JavaConverters._

/*
README BEFORE THE TEST

Since Redis natively doesn't support ssl connections
we use tunneling via port 6390 and the Jedis client https://github.com/xetorthio/jedis

The test requires to:
1) Start the server by executing `make` on https://github.com/xetorthio/jedis/blob/master/Makefile
2) set the truststoreFilePath below with the location of truststore.jceks file
3) set the runTests to true
*/

class RedisSslTest extends WordSpec with Matchers with BeforeAndAfterAll with MockitoSugar {

  val runTests = false;

  val truststoreFilePath = "src/test/resources/truststore.jceks"

  val gson = new Gson()

  val TOPIC = "topic"
  val baseProps = Map(
    RedisConfigConstants.REDIS_HOST -> "localhost",
    RedisConfigConstants.REDIS_PORT -> "6390",
    RedisConfigConstants.REDIS_PASSWORD -> "<PASSWORD>",
    RedisConfigConstants.REDIS_SSL_ENABLED -> "true",
    SslConfigs.SSL_TRUSTSTORE_LOCATION_CONFIG -> truststoreFilePath
  )

  def setupTrustStore(): Unit = {
    setJvmTrustStore(truststoreFilePath, "jceks")
  }

  private def setJvmTrustStore(trustStoreFilePath: String, trustStoreType: String): Unit = {
    new File(trustStoreFilePath).exists shouldBe true
    System.setProperty("javax.net.ssl.trustStore", trustStoreFilePath)
    System.setProperty("javax.net.ssl.trustStoreType", trustStoreType)
  }

  override def beforeAll() = {
    if (runTests) {
      setupTrustStore()
    }
  }

  override def afterAll() = {
  }

  "JedisSslClient" should {

    "establish ssl connection" in {

      if (runTests) {

        val jedis = new Jedis(URI.create(s"rediss://${baseProps(RedisConfigConstants.REDIS_HOST)}:${baseProps(RedisConfigConstants.REDIS_PORT)}"))
        jedis.auth(baseProps(RedisConfigConstants.REDIS_PASSWORD))
        jedis.ping() shouldBe "PONG"

      }
    }

//    "establish tls elasticcache" in {
//        val jedis = new Jedis("", 6379, true)
//        jedis.ping() shouldBe "PONG"
//    }
  }<commit_msg>Remove comment test case for AWS elasticache test<commit_after>/*
 * Copyright 2017 Datamountaineer.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.datamountaineer.streamreactor.connect.redis.sink.writer

import com.datamountaineer.streamreactor.connect.redis.sink.config.{RedisConfig, RedisConfigConstants, RedisSinkSettings}
import com.google.gson.Gson
import org.apache.kafka.connect.data.{Schema, SchemaBuilder, Struct}
import org.apache.kafka.connect.sink.SinkRecord
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{BeforeAndAfterAll, Matchers, WordSpec}
import java.io.File
import java.net.URI

import org.apache.kafka.common.config.SslConfigs
import redis.clients.jedis.Jedis

import scala.collection.JavaConverters._

/*
README BEFORE THE TEST

Since Redis natively doesn't support ssl connections
we use tunneling via port 6390 and the Jedis client https://github.com/xetorthio/jedis

The test requires to:
1) Start the server by executing `make` on https://github.com/xetorthio/jedis/blob/master/Makefile
2) set the truststoreFilePath below with the location of truststore.jceks file
3) set the runTests to true
*/

class RedisSslTest extends WordSpec with Matchers with BeforeAndAfterAll with MockitoSugar {

  val runTests = false;

  val truststoreFilePath = "src/test/resources/truststore.jceks"

  val gson = new Gson()

  val TOPIC = "topic"
  val baseProps = Map(
    RedisConfigConstants.REDIS_HOST -> "localhost",
    RedisConfigConstants.REDIS_PORT -> "6390",
    RedisConfigConstants.REDIS_PASSWORD -> "<PASSWORD>",
    RedisConfigConstants.REDIS_SSL_ENABLED -> "true",
    SslConfigs.SSL_TRUSTSTORE_LOCATION_CONFIG -> truststoreFilePath
  )

  def setupTrustStore(): Unit = {
    setJvmTrustStore(truststoreFilePath, "jceks")
  }

  private def setJvmTrustStore(trustStoreFilePath: String, trustStoreType: String): Unit = {
    new File(trustStoreFilePath).exists shouldBe true
    System.setProperty("javax.net.ssl.trustStore", trustStoreFilePath)
    System.setProperty("javax.net.ssl.trustStoreType", trustStoreType)
  }

  override def beforeAll() = {
    if (runTests) {
      setupTrustStore()
    }
  }

  override def afterAll() = {
  }

  "JedisSslClient" should {

    "establish ssl connection" in {

      if (runTests) {

        val jedis = new Jedis(URI.create(s"rediss://${baseProps(RedisConfigConstants.REDIS_HOST)}:${baseProps(RedisConfigConstants.REDIS_PORT)}"))
        jedis.auth(baseProps(RedisConfigConstants.REDIS_PASSWORD))
        jedis.ping() shouldBe "PONG"

      }
    }
  }
{'fraction_non_alphanumeric': 0.09438202247191012, 'fraction_numerical': 0.006741573033707865, 'mean_word_length': 4.271573604060913, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 4, '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': '1897150', 'n_tokens_mistral': 1977, 'n_tokens_neox': 1916, 'n_words': 510}