/*
 * Copyright 2015-2017 OpenCB
 *
 * 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 org.opencb.opencga.storage.core.manager.variant;

import org.junit.Before;
import org.junit.Test;
import org.opencb.commons.datastore.core.Query;
import org.opencb.opencga.catalog.managers.CatalogManager;

import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.opencb.opencga.storage.core.variant.adaptors.VariantQueryParam.STUDIES;

/**
 * Created on 28/02/17.
 *
 * @author Jacobo Coll &lt;jacobo167@gmail.com&gt;
 */
public class VariantCatalogQueryUtilsTest {

    private CatalogManager catalogManager;

    private Map<String, Long> studyNameMap;
    private VariantCatalogQueryUtils catalogUtils;

    @Before
    public void setUp() throws Exception {
        studyNameMap = new HashMap<>();
        addStudyId(1L);
        addStudyId(2L);
        addStudyId(3L);

        catalogManager = mock(CatalogManager.class);
        CatalogManager catalogManager1 = doAnswer(invocation -> studyNameMap.get(invocation.getArgument(0).toString()))
                .when(catalogManager);
        String userId = catalogManager1.getUserManager().getId(anyString());
        catalogManager1.getStudyManager().getId(userId, anyString());
        catalogUtils = new VariantCatalogQueryUtils(catalogManager);
    }

    private void addStudyId(long studyId) {
        studyNameMap.put(String.valueOf(studyId), studyId);
        studyNameMap.put("s"+studyId, studyId);
        studyNameMap.put("p1:s"+studyId, studyId);
        studyNameMap.put("u@p1:s"+studyId, studyId);
    }

    @Test
    public void parseQuery() throws Exception {
        assertEquals("1", catalogUtils.parseQuery(new Query(STUDIES.key(), "s1"), "sessionId").getString(STUDIES.key()));
        assertEquals("1,2", catalogUtils.parseQuery(new Query(STUDIES.key(), "s1,s2"), "sessionId").getString(STUDIES.key()));
        assertEquals("!1,2", catalogUtils.parseQuery(new Query(STUDIES.key(), "!s1,2"), "sessionId").getString(STUDIES.key()));
        assertEquals("2;!1;3", catalogUtils.parseQuery(new Query(STUDIES.key(), "u@p1:s2;!s1;p1:s3"), "sessionId").getString(STUDIES.key()));
    }

}
@@ -801,16 +801,129 @@
 Manager;
+%0Aimport org.opencb.opencga.catalog.managers.StudyManager;%0Aimport org.opencb.opencga.catalog.managers.UserManager;
 %0A%0Aimport
@@ -1102,56 +1102,9 @@
 ito.
-doAnswer;%0Aimport static org.mockito.Mockito.mock
+*
 ;%0Aim
@@ -1692,31 +1692,29 @@
         
-Catalog
+Study
 Manager 
 catalogM
@@ -1709,118 +1709,116 @@
 ger 
-catalog
+study
 Manager
-1
  = 
-doAnswer(invocation -%3E studyNameMap.get(invocation.getArgument(0).toString()))%0A     
+mock(StudyManager.class);%0A        UserManager userManager = mock(UserManager.class);%0A
         
    .
@@ -1817,12 +1817,8 @@
     
-   .
 when
@@ -1836,35 +1836,67 @@
 ager
-);%0A        String userId = 
+.getStudyManager()).thenReturn(studyManager);%0A        when(
 cata
@@ -1901,25 +1901,24 @@
 talogManager
-1
 .getUserMana
@@ -1926,57 +1926,211 @@
 er()
-.getId(anyString());%0A        catalogManager1.getS
+).thenReturn(userManager);%0A        when(userManager.getId(eq(%22sessionId%22))).thenReturn(%22u%22);%0A        doAnswer(invocation -%3E studyNameMap.get(invocation.getArgument(1).toString()))%0A                .when(s
 tudy
@@ -2140,17 +2140,16 @@
 ager
-(
 ).getId(
 user
@@ -2148,14 +2148,19 @@
 tId(
-userId
+anyString()
 , an
@@ -2167,24 +2167,25 @@
 yString());%0A
+%0A
         cata

Fix mock CatalogManager