Search in sources :

Example 1 with ScopeEndpointException

use of org.wso2.carbon.identity.oauth.scope.endpoint.exceptions.ScopeEndpointException in project identity-inbound-auth-oauth by wso2-extensions.

the class ScopesApiServiceImplTest method testUpdateScope.

@Test(dataProvider = "BuildUpdateScope")
public void testUpdateScope(Response.Status expectation, Throwable throwable) throws Exception {
    ScopeToUpdateDTO scopeToUpdateDTO = new ScopeToUpdateDTO();
    scopeToUpdateDTO.setDescription("some description");
    scopeToUpdateDTO.setBindings(Collections.<String>emptyList());
    if (Response.Status.OK.equals(expectation)) {
        when(ScopeUtils.getScopeDTO(any(Scope.class))).thenReturn(any(ScopeDTO.class));
        assertEquals(scopesApiService.updateScope(scopeToUpdateDTO, someScopeName).getStatus(), Response.Status.OK.getStatusCode(), "Error occurred while updating scopes");
    } else if (Response.Status.BAD_REQUEST.equals(expectation)) {
        when(oAuth2ScopeService.updateScope(any(Scope.class))).thenThrow(IdentityOAuth2ScopeClientException.class);
        callRealMethod();
        try {
            scopesApiService.updateScope(scopeToUpdateDTO, someScopeName);
        } catch (ScopeEndpointException e) {
            assertEquals(e.getResponse().getStatus(), Response.Status.BAD_REQUEST.getStatusCode(), "Cannot find HTTP Response, Bad Request in Case of " + "IdentityOAuth2ScopeClientException");
            assertEquals(((ErrorDTO) (e.getResponse().getEntity())).getMessage(), Response.Status.BAD_REQUEST.getReasonPhrase(), "Cannot find appropriate error message " + "for HTTP Response, Bad Request");
        } finally {
            reset(oAuth2ScopeService);
        }
    } else if (Response.Status.NOT_FOUND.equals(expectation)) {
        ((IdentityOAuth2ScopeException) throwable).setErrorCode(Oauth2ScopeConstants.ErrorMessages.ERROR_CODE_NOT_FOUND_SCOPE.getCode());
        when(oAuth2ScopeService.updateScope(any(Scope.class))).thenThrow(throwable);
        callRealMethod();
        try {
            scopesApiService.updateScope(scopeToUpdateDTO, someScopeName);
        } catch (ScopeEndpointException e) {
            assertEquals(e.getResponse().getStatus(), Response.Status.NOT_FOUND.getStatusCode(), "Cannot find HTTP Response, Not Found in Case of " + "IdentityOAuth2ScopeClientException");
            assertEquals(((ErrorDTO) (e.getResponse().getEntity())).getMessage(), Response.Status.NOT_FOUND.getReasonPhrase(), "Cannot find appropriate error message " + "for HTTP Response, Not Found");
        } finally {
            reset(oAuth2ScopeService);
        }
    } else if (Response.Status.INTERNAL_SERVER_ERROR.equals(expectation)) {
        when(oAuth2ScopeService.updateScope(any(Scope.class))).thenThrow(IdentityOAuth2ScopeException.class);
        callRealMethod();
        try {
            scopesApiService.updateScope(scopeToUpdateDTO, someScopeName);
        } catch (ScopeEndpointException e) {
            assertEquals(e.getResponse().getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "Cannot find HTTP Response, Internal Server Error in case of " + "IdentityOAuth2ScopeException");
            assertNull(e.getResponse().getEntity(), "Do not include error message in case of " + "Server Exception");
        } finally {
            reset(oAuth2ScopeService);
        }
    }
}
Also used : ScopeEndpointException(org.wso2.carbon.identity.oauth.scope.endpoint.exceptions.ScopeEndpointException) Scope(org.wso2.carbon.identity.oauth2.bean.Scope) ScopeDTO(org.wso2.carbon.identity.oauth.scope.endpoint.dto.ScopeDTO) ErrorDTO(org.wso2.carbon.identity.oauth.scope.endpoint.dto.ErrorDTO) ScopeToUpdateDTO(org.wso2.carbon.identity.oauth.scope.endpoint.dto.ScopeToUpdateDTO) IdentityOAuth2ScopeClientException(org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeClientException) IdentityOAuth2ScopeException(org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeException) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)

Example 2 with ScopeEndpointException

use of org.wso2.carbon.identity.oauth.scope.endpoint.exceptions.ScopeEndpointException in project identity-inbound-auth-oauth by wso2-extensions.

the class ScopesApiServiceImplTest method testGetScopes.

@Test(dataProvider = "BuildgetScopes")
public void testGetScopes(Response.Status expectation) throws Exception {
    Set<Scope> scopes = new HashSet<>();
    scopes.add(new Scope(someScopeName, someScopeName, someScopeDescription));
    int startIndex = 0;
    int count = 1;
    if (Response.Status.OK.equals(expectation)) {
        when(oAuth2ScopeService.getScopes(any(Integer.class), any(Integer.class), any(Boolean.class), any(String.class))).thenReturn(scopes);
        when(ScopeUtils.class, "getScopeDTOs", any(Set.class)).thenCallRealMethod();
        Response response = scopesApiService.getScopes(startIndex, count);
        assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), "Error occurred while getting scopes");
        assertEquals(((HashSet) response.getEntity()).size(), count, "Cannot Retrieve Expected Scopes");
    } else if (Response.Status.INTERNAL_SERVER_ERROR.equals(expectation)) {
        when(oAuth2ScopeService.getScopes(any(Integer.class), any(Integer.class))).thenThrow(IdentityOAuth2ScopeException.class);
        callRealMethod();
        try {
            scopesApiService.getScopes(startIndex, count);
        } catch (ScopeEndpointException e) {
            assertEquals(e.getResponse().getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "Cannot find HTTP Response, Internal Server Error in case of " + "IdentityOAuth2ScopeException");
            assertNull(e.getResponse().getEntity(), "Do not include error message in case of " + "Server Exception");
        }
    }
}
Also used : Response(javax.ws.rs.core.Response) ScopeEndpointException(org.wso2.carbon.identity.oauth.scope.endpoint.exceptions.ScopeEndpointException) Scope(org.wso2.carbon.identity.oauth2.bean.Scope) IdentityOAuth2ScopeException(org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeException) ScopeUtils(org.wso2.carbon.identity.oauth.scope.endpoint.util.ScopeUtils) HashSet(java.util.HashSet) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)

Example 3 with ScopeEndpointException

use of org.wso2.carbon.identity.oauth.scope.endpoint.exceptions.ScopeEndpointException in project identity-inbound-auth-oauth by wso2-extensions.

the class ScopesApiServiceImplTest method testRegisterScope.

@Test(dataProvider = "BuildRegisterScope")
public void testRegisterScope(Response.Status expectation, Throwable throwable) throws Exception {
    ScopeDTO scopeDTO = new ScopeDTO();
    scopeDTO.setDescription("some description");
    scopeDTO.setBindings(Collections.<String>emptyList());
    if (Response.Status.OK.equals(expectation)) {
        when(oAuth2ScopeService.registerScope(any(Scope.class))).thenReturn(any(Scope.class));
        assertEquals(scopesApiService.registerScope(scopeDTO).getStatus(), Response.Status.CREATED.getStatusCode(), "Error occurred while registering scopes");
    } else if (Response.Status.BAD_REQUEST.equals(expectation)) {
        when(oAuth2ScopeService.registerScope(any(Scope.class))).thenThrow(throwable);
        callRealMethod();
        try {
            scopesApiService.registerScope(scopeDTO);
        } catch (ScopeEndpointException e) {
            assertEquals(e.getResponse().getStatus(), Response.Status.BAD_REQUEST.getStatusCode(), "Cannot find HTTP Response, Bad Request in Case of " + "IdentityOAuth2ScopeClientException");
            assertEquals(((ErrorDTO) (e.getResponse().getEntity())).getMessage(), Response.Status.BAD_REQUEST.getReasonPhrase(), "Cannot find appropriate error message " + "for HTTP Response, Bad Request");
        } finally {
            reset(oAuth2ScopeService);
        }
    } else if (Response.Status.CONFLICT.equals(expectation)) {
        ((IdentityOAuth2ScopeException) throwable).setErrorCode(Oauth2ScopeConstants.ErrorMessages.ERROR_CODE_CONFLICT_REQUEST_EXISTING_SCOPE.getCode());
        when(oAuth2ScopeService.registerScope(any(Scope.class))).thenThrow(throwable);
        callRealMethod();
        try {
            scopesApiService.registerScope(scopeDTO);
        } catch (ScopeEndpointException e) {
            assertEquals(e.getResponse().getStatus(), Response.Status.CONFLICT.getStatusCode(), "Cannot find HTTP Response, Conflict in Case of " + "IdentityOAuth2ScopeClientException");
            assertEquals(((ErrorDTO) (e.getResponse().getEntity())).getMessage(), Response.Status.CONFLICT.getReasonPhrase(), "Cannot find appropriate error message " + "for HTTP Response, Conflict");
        } finally {
            reset(oAuth2ScopeService);
        }
    } else if (Response.Status.INTERNAL_SERVER_ERROR.equals(expectation)) {
        when(oAuth2ScopeService.registerScope(any(Scope.class))).thenThrow(IdentityOAuth2ScopeException.class);
        callRealMethod();
        try {
            scopesApiService.registerScope(scopeDTO);
        } catch (ScopeEndpointException e) {
            assertEquals(e.getResponse().getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "Cannot find HTTP Response, Internal Server Error in case of " + "IdentityOAuth2ScopeException");
            assertNull(e.getResponse().getEntity(), "Do not include error message in case of " + "Server Exception");
        } finally {
            reset(oAuth2ScopeService);
        }
    }
}
Also used : ScopeEndpointException(org.wso2.carbon.identity.oauth.scope.endpoint.exceptions.ScopeEndpointException) Scope(org.wso2.carbon.identity.oauth2.bean.Scope) ScopeDTO(org.wso2.carbon.identity.oauth.scope.endpoint.dto.ScopeDTO) ErrorDTO(org.wso2.carbon.identity.oauth.scope.endpoint.dto.ErrorDTO) IdentityOAuth2ScopeException(org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeException) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)

Aggregations

PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 Test (org.testng.annotations.Test)3 ScopeEndpointException (org.wso2.carbon.identity.oauth.scope.endpoint.exceptions.ScopeEndpointException)3 IdentityOAuth2ScopeException (org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeException)3 Scope (org.wso2.carbon.identity.oauth2.bean.Scope)3 PowerMockIdentityBaseTest (org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)3 ErrorDTO (org.wso2.carbon.identity.oauth.scope.endpoint.dto.ErrorDTO)2 ScopeDTO (org.wso2.carbon.identity.oauth.scope.endpoint.dto.ScopeDTO)2 HashSet (java.util.HashSet)1 Response (javax.ws.rs.core.Response)1 ScopeToUpdateDTO (org.wso2.carbon.identity.oauth.scope.endpoint.dto.ScopeToUpdateDTO)1 ScopeUtils (org.wso2.carbon.identity.oauth.scope.endpoint.util.ScopeUtils)1 IdentityOAuth2ScopeClientException (org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeClientException)1