/*
 * 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.processor.aggregator;

import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.camel.CamelContext;
import org.apache.camel.ContextTestSupport;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.processor.BodyInAggregatingStrategy;
import org.apache.camel.processor.aggregate.MemoryAggregationRepository;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class AggregateCompletionOnlyTwoTest extends ContextTestSupport {

    private MyRepo repo = new MyRepo();

    @Test
    public void testOnlyTwo() throws Exception {
        MockEndpoint mock = getMockEndpoint("mock:aggregated");
        mock.expectedBodiesReceived("A+B", "C+END");

        template.sendBodyAndHeader("direct:start", "A", "id", "foo");
        template.sendBodyAndHeader("direct:start", "B", "id", "foo");
        template.sendBodyAndHeader("direct:start", "C", "id", "foo");
        template.sendBodyAndHeader("direct:start", "END", "id", "foo");

        assertMockEndpointsSatisfied();

        assertEquals(4, repo.getGet());
        assertEquals(2, repo.getAdd());
        assertEquals(2, repo.getRemove());
        assertEquals(2, repo.getConfirm());
    }

    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("direct:start").aggregate(header("id"), new BodyInAggregatingStrategy()).aggregationRepository(repo)
                        .completionSize(2).to("mock:aggregated");
            }
        };
    }

    private class MyRepo extends MemoryAggregationRepository {

        private int add;
        private int get;
        private int remove;
        private AtomicInteger confirm = new AtomicInteger(0);

        @Override
        public Exchange add(CamelContext camelContext, String key, Exchange exchange) {
            add++;
            return super.add(camelContext, key, exchange);
        }

        @Override
        public Exchange get(CamelContext camelContext, String key) {
            get++;
            return super.get(camelContext, key);
        }

        @Override
        public void remove(CamelContext camelContext, String key, Exchange exchange) {
            remove++;
            super.remove(camelContext, key, exchange);
        }

        @Override
        public void confirm(CamelContext camelContext, String exchangeId) {
            confirm.incrementAndGet();
            super.confirm(camelContext, exchangeId);
        }

        @Override
        public Set<String> getKeys() {
            return super.getKeys();
        }

        public int getAdd() {
            return add;
        }

        public int getGet() {
            return get;
        }

        public int getRemove() {
            return remove;
        }

        public int getConfirm() {
            return confirm.get();
        }
    }
}

@@ -866,16 +866,54 @@
 il.Set;%0A
+import java.util.concurrent.TimeUnit;%0A
 import j
@@ -1340,16 +1340,63 @@
 .Test;%0A%0A
+import static org.awaitility.Awaitility.await;%0A
 import s
@@ -1449,23 +1449,16 @@
 quals;%0A%0A
-public 
 class Ag
@@ -1528,16 +1528,22 @@
 private 
+final 
 MyRepo r
@@ -1573,31 +1573,24 @@
   @Test%0A    
-public 
 void testOnl
@@ -2179,32 +2179,188 @@
 move());%0A       
+ // A second thread is involved so let's use awaitility to add more flexibility to the test%0A        await().atMost(10, TimeUnit.SECONDS).untilAsserted(() -%3E
  assertEquals(2,
@@ -2378,16 +2378,17 @@
 nfirm())
+)
 ;%0A    %7D%0A
@@ -2562,33 +2562,16 @@
 figure()
- throws Exception
  %7B%0A     
@@ -2797,16 +2797,23 @@
 private 
+static 
 class My
@@ -2950,16 +2950,22 @@
 private 
+final 
 AtomicIn
@@ -2998,17 +2998,16 @@
 Integer(
-0
 );%0A%0A    

Fix the test AggregateCompletionOnlyTwoTest.testOnlyTwo