<commit_before>/**
 * Copyright (C) 2010-2012 LShift Ltd.
 *
 * 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 net.lshift.diffa.kernel.config

import org.junit.{Before, Test}
import org.junit.Assert._
import org.easymock.EasyMock._
import net.lshift.diffa.schema.jooq.{DatabaseFacade => JooqDatabaseFacade}
import net.lshift.diffa.kernel.hooks.HookManager
import net.lshift.diffa.kernel.util.cache.HazelcastCacheProvider
import org.easymock.classextension.{EasyMock => E4}
import org.jooq.impl.Factory
import net.lshift.diffa.kernel.frontend._
import scala.collection.JavaConversions._
import net.lshift.diffa.kernel.frontend.DomainPairDef
import scala.Some
import net.lshift.diffa.kernel.frontend.EndpointDef
import net.lshift.diffa.kernel.frontend.EscalationDef
import net.lshift.diffa.kernel.frontend.PairReportDef
import net.lshift.diffa.kernel.util.MissingObjectException

class CachedDomainConfigStoreTest {

  val jooq = E4.createStrictMock(classOf[JooqDatabaseFacade])
  val hm = E4.createNiceMock(classOf[HookManager])
  val ml = createStrictMock(classOf[DomainMembershipAware])

  val sc = E4.createNiceMock(classOf[SpacePathCache])

  val cp = new HazelcastCacheProvider

  val domainConfig = new JooqDomainConfigStore(jooq, hm, cp, ml)
  
  val spaceId = System.currentTimeMillis()

  @Before
  def resetCaches {
    domainConfig.reset
  }

  @Test
  def shouldCacheDomainMembershipsAndThenInvalidateOnUpdate {

    val members = new java.util.ArrayList[String]()
    members.add("m1")
    members.add("m2")

    expect(jooq.execute(anyObject[Function1[Factory,java.util.List[String]]]())).andReturn(members).once()

    E4.replay(jooq)

    // The first call to get maybeConfigOption should propagate against the DB, but the second call will be cached

    val firstCall = domainConfig.listDomainMembers(spaceId)
    assertEquals(members.toList, firstCall)

    val secondCall = domainConfig.listDomainMembers(spaceId)
    assertEquals(members.toList, secondCall)

    E4.verify(jooq)

    // Reset the mocks control and an intermediate step to verify the calls to the underlying mock are all in order

    E4.reset(jooq)

    // Remove one of the underlying values and expect the DB to be updated. A subsequent call to
    // get listDomainMembers should also propagate against the DB.

    members.remove("m1")
    expect(jooq.execute(anyObject[Function1[Factory,Unit]]())).andReturn(Unit).once()
    expect(jooq.execute(anyObject[Function1[Factory,java.util.List[String]]]())).andReturn(members).once()

    E4.replay(jooq)

    domainConfig.removeDomainMembership(spaceId, "m1")

    val thirdCall = domainConfig.listDomainMembers(spaceId)
    assertEquals(members.toList, thirdCall)

    E4.verify(jooq)

    // Reset the mocks control and an intermediate step to verify the calls to the underlying mock are all in order

    E4.reset(jooq)

    // Add a new underlying value values and expect the DB to be updated. A subsequent call to
    // get listDomainMembers should also propagate against the DB.

    members.remove("m3")
    expect(jooq.execute(anyObject[Function1[Factory,Unit]]())).andReturn(Unit).once()
    expect(jooq.execute(anyObject[Function1[Factory,java.util.List[String]]]())).andReturn(members).once()

    E4.replay(jooq)

    domainConfig.makeDomainMember(spaceId, "m3")

    val fourthCall = domainConfig.listDomainMembers(spaceId)
    assertEquals(members.toList, fourthCall)
<commit_msg>Fixed expectation in caching test<commit_after>/**
 * Copyright (C) 2010-2012 LShift Ltd.
 *
 * 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 net.lshift.diffa.kernel.config

import org.junit.{Before, Test}
import org.junit.Assert._
import org.easymock.EasyMock._
import net.lshift.diffa.schema.jooq.{DatabaseFacade => JooqDatabaseFacade}
import net.lshift.diffa.kernel.hooks.HookManager
import net.lshift.diffa.kernel.util.cache.HazelcastCacheProvider
import org.easymock.classextension.{EasyMock => E4}
import org.jooq.impl.Factory
import net.lshift.diffa.kernel.frontend._
import scala.collection.JavaConversions._
import net.lshift.diffa.kernel.frontend.DomainPairDef
import scala.Some
import net.lshift.diffa.kernel.frontend.EndpointDef
import net.lshift.diffa.kernel.frontend.EscalationDef
import net.lshift.diffa.kernel.frontend.PairReportDef
import net.lshift.diffa.kernel.util.MissingObjectException

class CachedDomainConfigStoreTest {

  val jooq = E4.createStrictMock(classOf[JooqDatabaseFacade])
  val hm = E4.createNiceMock(classOf[HookManager])
  val ml = createStrictMock(classOf[DomainMembershipAware])

  val sc = E4.createNiceMock(classOf[SpacePathCache])

  val cp = new HazelcastCacheProvider

  val domainConfig = new JooqDomainConfigStore(jooq, hm, cp, ml)
  
  val spaceId = System.currentTimeMillis()

  @Before
  def resetCaches {
    domainConfig.reset
  }

  @Test
  def shouldCacheDomainMembershipsAndThenInvalidateOnUpdate {

    val members = new java.util.ArrayList[String]()
    members.add("m1")
    members.add("m2")

    expect(jooq.execute(anyObject[Function1[Factory,java.util.List[String]]]())).andReturn(members).once()

    E4.replay(jooq)

    // The first call to get maybeConfigOption should propagate against the DB, but the second call will be cached

    val firstCall = domainConfig.listDomainMembers(spaceId)
    assertEquals(members.toList, firstCall)

    val secondCall = domainConfig.listDomainMembers(spaceId)
    assertEquals(members.toList, secondCall)

    E4.verify(jooq)

    // Reset the mocks control and an intermediate step to verify the calls to the underlying mock are all in order

    E4.reset(jooq)

    // Remove one of the underlying values and expect the DB to be updated. A subsequent call to
    // get listDomainMembers should also propagate against the DB.

    members.remove("m1")
    expect(jooq.execute(anyObject[Function1[Factory,Unit]]())).andReturn(Unit).once()
    expect(jooq.execute(anyObject[Function1[Factory,String]]())).andReturn("domain").once() // resolveSpaceName is deprecated, BTW
    expect(jooq.execute(anyObject[Function1[Factory,java.util.List[String]]]())).andReturn(members).once()

    E4.replay(jooq)

    domainConfig.removeDomainMembership(spaceId, "m1")

    val thirdCall = domainConfig.listDomainMembers(spaceId)
    assertEquals(members.toList, thirdCall)

    E4.verify(jooq)

    // Reset the mocks control and an intermediate step to verify the calls to the underlying mock are all in order

    E4.reset(jooq)

    // Add a new underlying value values and expect the DB to be updated. A subsequent call to
    // get listDomainMembers should also propagate against the DB.

    members.remove("m3")
    expect(jooq.execute(anyObject[Function1[Factory,Unit]]())).andReturn(Unit).once()
    expect(jooq.execute(anyObject[Function1[Factory,String]]())).andReturn("domain").once() // resolveSpaceName is deprecated, BTW
    expect(jooq.execute(anyObject[Function1[Factory,java.util.List[String]]]())).andReturn(members).once()

    E4.replay(jooq)

    domainConfig.makeDomainMember(spaceId, "m3")

    val fourthCall = domainConfig.listDomainMembers(spaceId)
    assertEquals(members.toList, fourthCall)

{'fraction_non_alphanumeric': 0.09894942584901051, 'fraction_numerical': 0.008551184949914489, 'mean_word_length': 4.573179033356024, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '1698865', 'n_tokens_mistral': 2542, 'n_tokens_neox': 2444, 'n_words': 630}