/*
 * Copyright 2013 - Elian ORIOU
 *
 * 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.
 *
 * 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.vino.backend.db;

import com.google.common.base.Optional;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.vino.backend.model.EntityKey;
import com.vino.backend.model.WineDomain;
import com.vino.backend.persistence.Persistor;
import com.vino.backend.persistence.mongo.MongoPersistor;
import org.junit.Test;
import restx.factory.Factory;
import restx.factory.Name;
import restx.jongo.JongoCollection;

import java.util.Collection;
import java.util.List;
import java.util.Map;

public class DBConsistencyTest {

    private static final Factory FACTORY = Factory.builder().addFromServiceLoader().build();
    private static final Persistor PERSISTOR = FACTORY.getComponent(Name.of(MongoPersistor.class));

    private static final List<WineDomain> DOMAINS = PERSISTOR.getAllDomains();
    private static final JongoCollection KEYS = FACTORY.getComponent(Name.of(JongoCollection.class, "keys"));

    @Test
    public void domains_should_appears_in_keys() throws Exception {

        for (WineDomain domain : DOMAINS) {

            Optional<EntityKey> domainKey = PERSISTOR.getEntityKey(domain.getKey());
            if (!domainKey.isPresent()) {
                KEYS.get().insert(new EntityKey(domain.getKey(), "domains"));
            }
        }
    }

    @Test
    public void retrieve_right_aocs() throws Exception {

        Multimap<String, String> domains = HashMultimap.create();
        for (WineDomain domain : DOMAINS) {
            String aocKey = domain.getOrigin().getKey();
            if (domains.get(aocKey).size() <= 10) {
                domains.put(aocKey, domain.getName());
            }
        }
        for (Map.Entry<String, Collection<String>> entry : domains.asMap().entrySet()) {
            System.out.println(entry.getKey() + " : " + entry.getValue());
        }

    }
}

@@ -790,16 +790,55 @@
 ityKey;%0A
+import com.vino.backend.model.WineAOC;%0A
 import c
@@ -991,18 +991,107 @@
 org.
-junit.Test
+bson.types.ObjectId;%0Aimport org.junit.Test;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory
 ;%0Aim
@@ -1287,24 +1287,126 @@
 encyTest %7B%0A%0A
+    private final Logger logger = LoggerFactory.getLogger(DomainAOCReferenceReplacementTest.class);%0A%0A%0A
     private 
@@ -1662,24 +1662,94 @@
 lDomains();%0A
+    private static final List%3CWineAOC%3E AOCS = PERSISTOR.getAllAOCS();%0A
     private 
@@ -2753,11 +2753,325 @@
 %7D%0A%0A    %7D
+%0A%0A    @Test%0A    public void check_aocs_consistency() %7B%0A%0A        for (WineAOC aoc : AOCS) %7B%0A%0A            if (KEYS.get().count(%22%7B_id: #%7D%22, new ObjectId(aoc.getKey())) %3C= 0) %7B%0A                logger.error(%22AOC is missing into the keys collection : %7B%7D (%7B%7D)%22, aoc.getName(), aoc.getKey());%0A            %7D%0A        %7D%0A    %7D
 %0A%7D%0A

Update test