package org.ovirt.engine.core.bll;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.Collections;
import java.util.List;

import org.junit.Test;
import org.ovirt.engine.core.common.businessentities.vm_pools;
import org.ovirt.engine.core.common.queries.GetAllVmPoolsAttachedToUserParameters;
import org.ovirt.engine.core.compat.Guid;
import org.ovirt.engine.core.dao.VmPoolDAO;

/**
 * A test class for the {@link GetAllVmPoolsAttachedToUserQuery} class.
 * It mocks the DAO and tests the flow of the query itself.
 */
public class GetAllVmPoolsAttachedToUserQueryTest extends AbstractUserQueryTest<GetAllVmPoolsAttachedToUserParameters, GetAllVmPoolsAttachedToUserQuery<GetAllVmPoolsAttachedToUserParameters>> {

    /** Tests that executing a query with the same user works */
    @Test
    public void testQueryWithSameUser() {
        assertExecuteQueryCommandResult(getUser().getUserId(), true);
    }

    /** Tests that executing a query with a different user returns an empty list */
    @Test
    public void testQueryWithOtherUser() {
        assertExecuteQueryCommandResult(Guid.NewGuid(), false);
    }

    /** Tests that executing a query with a different user works when the query is run in admin mode */
    @Test
    public void testAdminQueryWithOtherUserWithDisks() {
        when(getQueryParameters().isFiltered()).thenReturn(false);
        assertExecuteQueryCommandResult(Guid.NewGuid(), true);
    }

    public void assertExecuteQueryCommandResult(Guid requestedUser, boolean expectedResults) {
        mockQueryParameters(requestedUser);

        // Mock the result of the DAO
        final vm_pools expectedPool = mockVMPoolsFromDAO(requestedUser);

        getQuery().executeQueryCommand();

        @SuppressWarnings("unchecked")
        List<vm_pools> actualPools = (List<vm_pools>) getQuery().getQueryReturnValue().getReturnValue();
        if (!expectedResults) {
            assertTrue("no VMs should have been returned", actualPools.isEmpty());
        } else {
            assertEquals("wrong number of VMs returned", 1, actualPools.size());
            vm_pools actualPool = actualPools.get(0);
            assertEquals("wrong VMs returned", expectedPool, actualPool);
        }
    }

    /**
     * Adds additional parameters to the parameters object
     * @param requestedUser The user to get the VMs for
     */
    private void mockQueryParameters(Guid requestedUser) {
        when(getQueryParameters().getUserId()).thenReturn(requestedUser);
    }

    /**
     * Mocks the DAOs to return a VM
     * @param requestedUser The user on the parameter object to return the VM for
     * @return The VM the mocked DAO will return
     */
    private vm_pools mockVMPoolsFromDAO(Guid requestedUser) {
        vm_pools expectedPool = new vm_pools();
        VmPoolDAO vmDaoPoolMock = mock(VmPoolDAO.class);
        when(vmDaoPoolMock.getAllForUser(requestedUser)).thenReturn(Collections.singletonList(expectedPool));
        when(getDbFacadeMockInstance().getVmPoolDao()).thenReturn(vmDaoPoolMock);

        return expectedPool;
    }
}

@@ -78,51 +78,8 @@
 ls;%0A
-import static org.junit.Assert.assertTrue;%0A
 impo
@@ -671,20 +671,16 @@
 Abstract
-User
 QueryTes
@@ -837,775 +837,150 @@
 ery 
-with the same user works */%0A    @Test%0A    public void testQueryWithSameUser() %7B%0A        assertExecuteQueryCommandResult(getUser().getUserId(), true);%0A    %7D%0A%0A    /** Tests that
+returns the
  ex
+p
 ec
-uting a query with a different user returns an empty list */%0A    @Test%0A    public void testQueryWithOtherUser() %7B%0A        assertExecuteQueryCommandResult(Guid.NewGuid(), false);%0A    %7D%0A%0A    /** Tests that executing a query with a different user works when the query is run in admin mode */%0A    @Test%0A    public void testAdminQueryWithOtherUserWithDisks() %7B%0A        when(getQueryParameters().isFiltered()).thenReturn(false);%0A        assertExecuteQueryCommandResult(Guid.NewGuid(), true);%0A    %7D%0A%0A    public void assertExecuteQueryCommandResult(Guid requestedUser, boolean expectedResults) %7B
+ted result */%0A    @Test%0A    public void testAdminQueryWithOtherUserWithDisks() %7B%0A        Guid requestedUser = Guid.NewGuid();
 %0A   
@@ -1324,144 +1324,8 @@
 ();%0A
-        if (!expectedResults) %7B%0A            assertTrue(%22no VMs should have been returned%22, actualPools.isEmpty());%0A        %7D else %7B%0A    
     
@@ -1401,28 +1401,24 @@
 ));%0A        
-    
 vm_pools act
@@ -1451,28 +1451,24 @@
 0);%0A        
-  
-  
 assertEquals
@@ -1521,18 +1521,8 @@
 l);%0A
-        %7D%0A
     

Fix GetAllVmPoolsAttachedToUserQueryTest