Search in sources :

Example 71 with ServerApiException

use of org.apache.cloudstack.api.ServerApiException in project cloudstack by apache.

the class ListAndSwitchSAMLAccountCmdTest method testListAndSwitchSAMLAccountCmd.

@Test
public void testListAndSwitchSAMLAccountCmd() throws Exception {
    // Setup
    final Map<String, Object[]> params = new HashMap<String, Object[]>();
    final String sessionKeyValue = "someSessionIDValue";
    Mockito.when(session.getAttribute(ApiConstants.SESSIONKEY)).thenReturn(sessionKeyValue);
    Mockito.when(session.getAttribute("userid")).thenReturn(2L);
    params.put(ApiConstants.USER_ID, new String[] { "2" });
    params.put(ApiConstants.DOMAIN_ID, new String[] { "1" });
    Mockito.when(userDao.findByUuid(Mockito.anyString())).thenReturn(new UserVO(2L));
    Mockito.when(domainDao.findByUuid(Mockito.anyString())).thenReturn(new DomainVO());
    // Mock/field setup
    ListAndSwitchSAMLAccountCmd cmd = new ListAndSwitchSAMLAccountCmd();
    Field apiServerField = ListAndSwitchSAMLAccountCmd.class.getDeclaredField("_apiServer");
    apiServerField.setAccessible(true);
    apiServerField.set(cmd, apiServer);
    Field managerField = ListAndSwitchSAMLAccountCmd.class.getDeclaredField("_samlAuthManager");
    managerField.setAccessible(true);
    managerField.set(cmd, samlAuthManager);
    Field accountServiceField = BaseCmd.class.getDeclaredField("_accountService");
    accountServiceField.setAccessible(true);
    accountServiceField.set(cmd, accountService);
    Field userAccountDaoField = ListAndSwitchSAMLAccountCmd.class.getDeclaredField("_userAccountDao");
    userAccountDaoField.setAccessible(true);
    userAccountDaoField.set(cmd, userAccountDao);
    Field userDaoField = ListAndSwitchSAMLAccountCmd.class.getDeclaredField("_userDao");
    userDaoField.setAccessible(true);
    userDaoField.set(cmd, userDao);
    Field domainDaoField = ListAndSwitchSAMLAccountCmd.class.getDeclaredField("_domainDao");
    domainDaoField.setAccessible(true);
    domainDaoField.set(cmd, domainDao);
    // invalid session test
    try {
        cmd.authenticate("command", params, null, null, HttpUtils.RESPONSE_TYPE_JSON, new StringBuilder(), req, resp);
    } catch (ServerApiException exception) {
        assertEquals(exception.getErrorCode(), ApiErrorCode.UNAUTHORIZED);
    } finally {
        Mockito.verify(accountService, Mockito.times(0)).getUserAccountById(Mockito.anyLong());
    }
    // invalid sessionkey value test
    params.put(ApiConstants.SESSIONKEY, new String[] { "someOtherValue" });
    try {
        Mockito.when(session.isNew()).thenReturn(false);
        cmd.authenticate("command", params, session, null, HttpUtils.RESPONSE_TYPE_JSON, new StringBuilder(), req, resp);
    } catch (ServerApiException exception) {
        assertEquals(exception.getErrorCode(), ApiErrorCode.UNAUTHORIZED);
    } finally {
        Mockito.verify(accountService, Mockito.times(0)).getUserAccountById(Mockito.anyLong());
    }
    // valid sessionkey value test
    params.put(ApiConstants.SESSIONKEY, new String[] { sessionKeyValue });
    try {
        cmd.authenticate("command", params, session, null, HttpUtils.RESPONSE_TYPE_JSON, new StringBuilder(), req, resp);
    } catch (ServerApiException exception) {
        assertEquals(exception.getErrorCode(), ApiErrorCode.ACCOUNT_ERROR);
    } finally {
        Mockito.verify(accountService, Mockito.times(1)).getUserAccountById(Mockito.anyLong());
    }
    // valid sessionkey, invalid useraccount type (non-saml) value test
    UserAccountVO mockedUserAccount = new UserAccountVO();
    mockedUserAccount.setId(2L);
    mockedUserAccount.setAccountState(Account.State.enabled.toString());
    mockedUserAccount.setUsername("someUsername");
    mockedUserAccount.setExternalEntity("some IDP ID");
    mockedUserAccount.setDomainId(0L);
    mockedUserAccount.setSource(User.Source.UNKNOWN);
    Mockito.when(accountService.getUserAccountById(Mockito.anyLong())).thenReturn(mockedUserAccount);
    try {
        cmd.authenticate("command", params, session, null, HttpUtils.RESPONSE_TYPE_JSON, new StringBuilder(), req, resp);
    } catch (ServerApiException exception) {
        assertEquals(exception.getErrorCode(), ApiErrorCode.ACCOUNT_ERROR);
    } finally {
        // accountService should have been called twice by now, for this case and the case above
        Mockito.verify(accountService, Mockito.times(2)).getUserAccountById(Mockito.anyLong());
    }
    // all valid test
    mockedUserAccount.setSource(User.Source.SAML2);
    Mockito.when(accountService.getUserAccountById(Mockito.anyLong())).thenReturn(mockedUserAccount);
    Mockito.when(apiServer.verifyUser(Mockito.anyLong())).thenReturn(true);
    LoginCmdResponse loginCmdResponse = new LoginCmdResponse();
    loginCmdResponse.setUserId("1");
    loginCmdResponse.setDomainId("1");
    loginCmdResponse.setType("1");
    loginCmdResponse.setUsername("userName");
    loginCmdResponse.setAccount("someAccount");
    loginCmdResponse.setFirstName("firstName");
    loginCmdResponse.setLastName("lastName");
    loginCmdResponse.setSessionKey("newSessionKeyString");
    Mockito.when(apiServer.loginUser(Mockito.any(HttpSession.class), Mockito.anyString(), Mockito.anyString(), Mockito.anyLong(), Mockito.anyString(), Mockito.any(InetAddress.class), Mockito.anyMap())).thenReturn(loginCmdResponse);
    try {
        cmd.authenticate("command", params, session, null, HttpUtils.RESPONSE_TYPE_JSON, new StringBuilder(), req, resp);
    } catch (ServerApiException exception) {
        fail("SAML list and switch account API failed to pass for all valid data: " + exception.getMessage());
    } finally {
        // accountService should have been called 4 times by now, for this case twice and 2 for cases above
        Mockito.verify(accountService, Mockito.times(4)).getUserAccountById(Mockito.anyLong());
        Mockito.verify(resp, Mockito.times(1)).sendRedirect(Mockito.anyString());
    }
}
Also used : HashMap(java.util.HashMap) HttpSession(javax.servlet.http.HttpSession) DomainVO(com.cloud.domain.DomainVO) Field(java.lang.reflect.Field) UserAccountVO(com.cloud.user.UserAccountVO) UserVO(com.cloud.user.UserVO) ServerApiException(org.apache.cloudstack.api.ServerApiException) InetAddress(java.net.InetAddress) LoginCmdResponse(org.apache.cloudstack.api.response.LoginCmdResponse) Test(org.junit.Test)

Example 72 with ServerApiException

use of org.apache.cloudstack.api.ServerApiException in project cloudstack by apache.

the class DeleteLoadBalancerRuleCmd method execute.

@Override
public void execute() {
    CallContext.current().setEventDetails("Load balancer ID: " + getId());
    boolean result = _firewallService.revokeRelatedFirewallRule(id, true);
    result = result && _lbService.deleteLoadBalancerRule(id, true);
    if (result) {
        SuccessResponse response = new SuccessResponse(getCommandName());
        this.setResponseObject(response);
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to delete load balancer");
    }
}
Also used : SuccessResponse(org.apache.cloudstack.api.response.SuccessResponse) ServerApiException(org.apache.cloudstack.api.ServerApiException)

Example 73 with ServerApiException

use of org.apache.cloudstack.api.ServerApiException in project cloudstack by apache.

the class RegisterIsoCmd method execute.

@Override
public void execute() throws ResourceAllocationException {
    VirtualMachineTemplate template = _templateService.registerIso(this);
    if (template != null) {
        ListResponse<TemplateResponse> response = new ListResponse<TemplateResponse>();
        List<TemplateResponse> templateResponses = _responseGenerator.createIsoResponses(ResponseView.Restricted, template, zoneId, false);
        response.setResponses(templateResponses);
        response.setResponseName(getCommandName());
        setResponseObject(response);
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to register ISO");
    }
}
Also used : VirtualMachineTemplate(com.cloud.template.VirtualMachineTemplate) ListResponse(org.apache.cloudstack.api.response.ListResponse) ServerApiException(org.apache.cloudstack.api.ServerApiException) TemplateResponse(org.apache.cloudstack.api.response.TemplateResponse)

Example 74 with ServerApiException

use of org.apache.cloudstack.api.ServerApiException in project cloudstack by apache.

the class UpdateIsoCmd method execute.

@Override
public void execute() {
    VirtualMachineTemplate result = _templateService.updateTemplate(this);
    if (result != null) {
        TemplateResponse response = _responseGenerator.createTemplateUpdateResponse(ResponseView.Restricted, result);
        response.setResponseName(getCommandName());
        setResponseObject(response);
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to update ISO");
    }
}
Also used : VirtualMachineTemplate(com.cloud.template.VirtualMachineTemplate) ServerApiException(org.apache.cloudstack.api.ServerApiException) TemplateResponse(org.apache.cloudstack.api.response.TemplateResponse)

Example 75 with ServerApiException

use of org.apache.cloudstack.api.ServerApiException in project cloudstack by apache.

the class AssignCertToLoadBalancerCmd method execute.

@Override
public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException, NetworkRuleConflictException {
    //To change body of implemented methods use File | Settings | File Templates.
    if (_lbService.assignCertToLoadBalancer(getLbRuleId(), getCertId())) {
        SuccessResponse response = new SuccessResponse(getCommandName());
        this.setResponseObject(response);
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to assign certificate to load balancer");
    }
}
Also used : SuccessResponse(org.apache.cloudstack.api.response.SuccessResponse) ServerApiException(org.apache.cloudstack.api.ServerApiException)

Aggregations

ServerApiException (org.apache.cloudstack.api.ServerApiException)513 SuccessResponse (org.apache.cloudstack.api.response.SuccessResponse)125 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)116 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)61 ArrayList (java.util.ArrayList)58 UserVm (com.cloud.uservm.UserVm)44 ListResponse (org.apache.cloudstack.api.response.ListResponse)44 UserVmResponse (org.apache.cloudstack.api.response.UserVmResponse)42 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)39 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)33 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)26 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)25 Account (com.cloud.user.Account)24 Host (com.cloud.host.Host)20 NetworkRuleConflictException (com.cloud.exception.NetworkRuleConflictException)18 Volume (com.cloud.storage.Volume)16 Test (org.junit.Test)16 VirtualMachineTemplate (com.cloud.template.VirtualMachineTemplate)15 VolumeResponse (org.apache.cloudstack.api.response.VolumeResponse)15 UserAccount (com.cloud.user.UserAccount)13