content: /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You 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 org.apache.camel.component.infinispan.embedded;
  
  import java.util.function.BiFunction;
  
  import org.apache.camel.BindToRegistry;
  import org.apache.camel.builder.RouteBuilder;
  import org.apache.camel.component.infinispan.InfinispanConstants;
  import org.apache.camel.component.infinispan.InfinispanOperation;
  import org.apache.camel.component.infinispan.InfinispanProducerTestSupport;
  import org.infinispan.commons.api.BasicCache;
  import org.infinispan.configuration.cache.ConfigurationBuilder;
  import org.infinispan.stats.Stats;
  import org.junit.jupiter.api.BeforeEach;
  import org.junit.jupiter.api.Test;
  
  import static org.junit.jupiter.api.Assertions.assertEquals;
  
  public class InfinispanEmbeddedProducerTest extends InfinispanEmbeddedTestSupport implements InfinispanProducerTestSupport {
  
      @BindToRegistry("mappingFunction")
      public BiFunction<String, String, String> mappingFunction() {
          return (k, v) -> v + "replay";
      }
  
      @Override
      protected ConfigurationBuilder getConfiguration() {
          ConfigurationBuilder builder = super.getConfiguration();
          builder.statistics().enable();
  
          return builder;
      }
  
      @Test
      public void statsOperation() {
          fluentTemplate()
                  .to("direct:start")
                  .withHeader(InfinispanConstants.KEY, InfinispanProducerTestSupport.KEY_ONE)
                  .withHeader(InfinispanConstants.VALUE, InfinispanProducerTestSupport.VALUE_ONE)
                  .withHeader(InfinispanConstants.OPERATION, InfinispanOperation.PUT)
                  .send();
  
          assertEquals(InfinispanProducerTestSupport.VALUE_ONE, getCache().get(InfinispanProducerTestSupport.KEY_ONE));
  
          fluentTemplate()
                  .to("direct:start")
                  .withHeader(InfinispanConstants.KEY, InfinispanProducerTestSupport.KEY_TWO)
                  .withHeader(InfinispanConstants.VALUE, InfinispanProducerTestSupport.VALUE_TWO)
                  .withHeader(InfinispanConstants.OPERATION, InfinispanOperation.PUT)
                  .send();
  
          assertEquals(InfinispanProducerTestSupport.VALUE_TWO, getCache().get(InfinispanProducerTestSupport.KEY_TWO));
  
          assertEquals(
                  2L,
                  fluentTemplate()
                          .to("direct:start")
                          .withHeader(InfinispanConstants.OPERATION, InfinispanOperation.STATS)
                          .request(Stats.class)
                          .getApproximateEntries());
      }
  
      // *****************************
      //
      // *****************************
  
      @BeforeEach
      protected void beforeEach() {
          // cleanup the default test cache before each run
          getCache().clear();
      }
  
      @Override
      public BasicCache<Object, Object> getCache() {
          return super.getCache();
      }
  
      @Override
      public BasicCache<Object, Object> getCache(String name) {
          return super.getCache(name);
      }
  
      @Override
      protected RouteBuilder createRouteBuilder() {
          return new RouteBuilder() {
              @Override
              public void configure() {
                  from("direct:start")
                          .toF("infinispan-embedded:%s", getCacheName());
                  from("direct:compute")
                          .toF("infinispan-embedded:%s?remappingFunction=#mappingFunction", getCacheName());
                  from("direct:explicitput")
                          .toF("infinispan-embedded:%s?operation=PUT&key=a&value=3", getCacheName());
              }
          };
      }
  }
  