/*
 * Copyright 2012 Shared Learning Collaborative, LLC
 *
 * 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.slc.sli.dal.repository.connection;

import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
import org.slc.sli.dal.repository.tenancy.CurrentTenantHolder;
import org.slc.sli.dal.repository.tenancy.SystemCall;
import org.slc.sli.dal.repository.tenancy.TenantCall;

import com.mongodb.DB;
import com.mongodb.Mongo;

/**
 * @author okrook
 *
 */
public class TenantAwareMongoDbFactoryTest {

    @Test
    @SystemCall
    public void testGetSystemConnection() {
        Mongo mongo = Mockito.mock(Mongo.class);
        DB db = Mockito.mock(DB.class);

        Mockito.when(db.getMongo()).thenReturn(mongo);
        Mockito.when(db.getName()).thenReturn("System");
        Mockito.when(mongo.getDB(Mockito.anyString())).thenReturn(db);

        TenantAwareMongoDbFactory cm = new TenantAwareMongoDbFactory(mongo, "System");

        Assert.assertNotNull(cm.getDb());
        Assert.assertSame("System", cm.getDb().getName());
    }
    
    @Test
    public void testGetTenantConnection() {
        testGetTenantConnection("testTenantId");
    }
    
    @TenantCall(param = "tenantId")
    public void testGetTenantConnection(String tenantId) {        
        Mongo mongo = Mockito.mock(Mongo.class);
        DB db = Mockito.mock(DB.class);

        Mockito.when(db.getMongo()).thenReturn(mongo);
        Mockito.when(db.getName()).thenReturn("tenant");
        Mockito.when(mongo.getDB(TenantAwareMongoDbFactory.getTenantDatabaseName(tenantId))).thenReturn(db);        
        
        TenantAwareMongoDbFactory cm = new TenantAwareMongoDbFactory(mongo, "System");

        Assert.assertNotNull(cm.getDb());
        Assert.assertSame("tenant", cm.getDb().getName());
    }
}

@@ -732,16 +732,54 @@
 ockito;%0A
+import org.slc.sli.dal.TenantContext;%0A
 import o
@@ -1081,24 +1081,8 @@
 est%0A
-    @SystemCall%0A
     
@@ -1713,44 +1713,8 @@
    %0A
-    @TenantCall(param = %22tenantId%22)%0A
     
@@ -1768,32 +1768,77 @@
 ntId) %7B        %0A
+        TenantContext.setTenantId(tenantId);%0A
         Mongo mo

Fix unit test