/*
 * 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 com.mongodb.DB;
import com.mongodb.Mongo;

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

    @Test
    public void testGetSystemConnection() {
        CurrentTenantHolder.push(null);
        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() {
        String tenantId = "testTenantId";
        CurrentTenantHolder.push(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());
    }
}

@@ -794,16 +794,124 @@
 tHolder;
+%0Aimport org.slc.sli.dal.repository.tenancy.SystemCall;%0Aimport org.slc.sli.dal.repository.tenancy.TenantCall;
 %0A%0Aimport
@@ -1031,32 +1031,48 @@
 st %7B%0A%0A    @Test%0A
+    @SystemCall%0A
     public void 
@@ -1103,48 +1103,8 @@
 ) %7B%0A
-        CurrentTenantHolder.push(null);%0A
     
@@ -1639,26 +1639,32 @@
     
-String tenantId = 
+testGetTenantConnection(
 %22tes
@@ -1677,51 +1677,55 @@
 tId%22
+)
 ;%0A    
+%7D%0A
     
-CurrentTenantHolder.push(
+%0A    @TenantCall(param = %22
 tenantId
 );  
@@ -1720,27 +1720,77 @@
 tenantId
-);
+%22)%0A
     
-    %0A
+public void testGetTenantConnection(String tenantId) %7B
         

Refactor unit test