use of org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationTokenDTO in project carbon-apimgt by wso2.
the class ApplicationKeyMappingUtilTestCase method testFromApplicationTokenToDTO.
@Test
public void testFromApplicationTokenToDTO() {
String accessToken = "123123123123123123132";
ApplicationToken applicationToken = new ApplicationToken();
applicationToken.setAccessToken(accessToken);
applicationToken.setScopes("Scope1");
applicationToken.setValidityPeriod(100000);
ApplicationTokenDTO applicationTokenDTO = ApplicationKeyMappingUtil.fromApplicationTokenToDTO(applicationToken);
Assert.assertEquals(applicationTokenDTO.getAccessToken(), accessToken);
}
use of org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationTokenDTO in project carbon-apimgt by wso2.
the class ApplicationsApiServiceImpl method applicationsApplicationIdGenerateTokenPost.
/**
* Generate an application token
*
* @param applicationId Application ID
* @param body Application information which are required to generate tokens
* @param ifMatch If-Match header value
* @param ifUnmodifiedSince If-UnModified-Since header value
* @param request msf4j request object
* @return Generated application key detials
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response applicationsApplicationIdGenerateTokenPost(String applicationId, ApplicationTokenGenerateRequestDTO body, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
try {
String username = RestApiUtil.getLoggedInUsername(request);
APIStore apiConsumer = RestApiUtil.getConsumer(username);
ApplicationToken token = apiConsumer.generateApplicationToken(body.getConsumerKey(), body.getConsumerSecret(), body.getScopes(), body.getValidityPeriod(), body.getRevokeToken());
ApplicationTokenDTO appToken = ApplicationKeyMappingUtil.fromApplicationTokenToDTO(token);
return Response.ok().entity(appToken).build();
} catch (APIManagementException e) {
String errorMessage = "Error occurred while generating application tokens for application: " + applicationId;
Map<String, String> paramList = new HashMap<>();
paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_ID, applicationId);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationTokenDTO in project carbon-apimgt by wso2.
the class ApplicationKeyMappingUtil method fromApplicationTokenToDTO.
public static ApplicationTokenDTO fromApplicationTokenToDTO(ApplicationToken applicationToken) {
if (applicationToken == null) {
return null;
}
ApplicationTokenDTO applicationTokenDTO = new ApplicationTokenDTO();
applicationTokenDTO.setAccessToken(applicationToken.getAccessToken());
applicationTokenDTO.setTokenScopes(applicationToken.getScopes());
applicationTokenDTO.setValidityTime(applicationToken.getValidityPeriod());
return applicationTokenDTO;
}
use of org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationTokenDTO in project carbon-apimgt by wso2.
the class ApplicationsApiServiceImplTestCase method testApplicationsPostErrorCase.
@Test
public void testApplicationsPostErrorCase() throws APIManagementException, NotFoundException {
TestUtil.printTestMethodName();
String applicationId = UUID.randomUUID().toString();
String accessToken = UUID.randomUUID().toString();
String clientID = UUID.randomUUID().toString();
String clientSecret = UUID.randomUUID().toString();
ApplicationsApiServiceImpl applicationsApiService = new ApplicationsApiServiceImpl();
APIStore apiStore = Mockito.mock(APIStoreImpl.class);
PowerMockito.mockStatic(RestApiUtil.class);
PowerMockito.when(RestApiUtil.getConsumer(USER)).thenReturn(apiStore);
Request request = getRequest();
PowerMockito.when(RestApiUtil.getLoggedInUsername(request)).thenReturn(USER);
Application application = getSampleApplication(applicationId);
WorkflowResponse workflowResponse = new GeneralWorkflowResponse();
workflowResponse.setWorkflowStatus(WorkflowStatus.APPROVED);
ApplicationCreationResponse creationResponse = new ApplicationCreationResponse(UUID.randomUUID().toString(), workflowResponse);
Mockito.when(apiStore.addApplication(application)).thenReturn(creationResponse);
Mockito.when(apiStore.getApplication(creationResponse.getApplicationUUID(), USER)).thenReturn(application);
ApplicationTokenDTO applicationTokenDTO = new ApplicationTokenDTO();
applicationTokenDTO.setAccessToken(accessToken);
applicationTokenDTO.setTokenScopes("SCOPE1");
applicationTokenDTO.setValidityTime((long) 100000);
List<String> grantTypes = new ArrayList<>();
grantTypes.add("password");
grantTypes.add("jwt");
ApplicationKeysDTO applicationKeysDTO = new ApplicationKeysDTO();
applicationKeysDTO.setConsumerKey(clientID);
applicationKeysDTO.setConsumerSecret(clientSecret);
applicationKeysDTO.setKeyType(ApplicationKeysDTO.KeyTypeEnum.PRODUCTION);
applicationKeysDTO.setCallbackUrl(null);
applicationKeysDTO.setSupportedGrantTypes(grantTypes);
List<ApplicationKeysDTO> applicationKeysDTOList = new ArrayList<>();
applicationKeysDTOList.add(applicationKeysDTO);
ApplicationDTO applicationDTO = new ApplicationDTO();
applicationDTO.setApplicationId(applicationId);
applicationDTO.setDescription("sample application");
applicationDTO.setName("app1");
applicationDTO.setSubscriber("subscriber");
applicationDTO.setPermission("permission");
applicationDTO.setLifeCycleStatus("APPROVED");
applicationDTO.setThrottlingTier("UNLIMITED");
applicationDTO.setToken(applicationTokenDTO);
applicationDTO.setKeys(applicationKeysDTOList);
Response response = applicationsApiService.applicationsPost(applicationDTO, request);
Assert.assertEquals(201, response.getStatus());
}
use of org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationTokenDTO in project carbon-apimgt by wso2.
the class ApplicationsApiServiceImplTestCase method testApplicationsApplicationIdPut.
@Test
public void testApplicationsApplicationIdPut() throws APIManagementException, NotFoundException {
TestUtil.printTestMethodName();
String applicationId = UUID.randomUUID().toString();
String accessToken = UUID.randomUUID().toString();
String clientID = UUID.randomUUID().toString();
String clientSecret = UUID.randomUUID().toString();
ApplicationsApiServiceImpl applicationsApiService = new ApplicationsApiServiceImpl();
APIStore apiStore = Mockito.mock(APIStoreImpl.class);
PowerMockito.mockStatic(RestApiUtil.class);
PowerMockito.when(RestApiUtil.getConsumer(USER)).thenReturn(apiStore);
Request request = getRequest();
PowerMockito.when(RestApiUtil.getLoggedInUsername(request)).thenReturn(USER);
ApplicationTokenDTO applicationTokenDTO = new ApplicationTokenDTO();
applicationTokenDTO.setAccessToken(accessToken);
applicationTokenDTO.setTokenScopes("SCOPE1");
applicationTokenDTO.setValidityTime((long) 100000);
List<String> grantTypes = new ArrayList<>();
grantTypes.add("password");
grantTypes.add("jwt");
ApplicationKeysDTO applicationKeysDTO = new ApplicationKeysDTO();
applicationKeysDTO.setConsumerKey(clientID);
applicationKeysDTO.setConsumerSecret(clientSecret);
applicationKeysDTO.setKeyType(ApplicationKeysDTO.KeyTypeEnum.PRODUCTION);
applicationKeysDTO.setCallbackUrl(null);
applicationKeysDTO.setSupportedGrantTypes(grantTypes);
List<ApplicationKeysDTO> applicationKeysDTOList = new ArrayList<>();
applicationKeysDTOList.add(applicationKeysDTO);
ApplicationDTO applicationDTO = new ApplicationDTO();
applicationDTO.setApplicationId(applicationId);
applicationDTO.setDescription("sample application");
applicationDTO.setName("app1");
applicationDTO.setSubscriber("subscriber");
applicationDTO.setPermission("permission");
applicationDTO.setLifeCycleStatus("APPROVED");
applicationDTO.setThrottlingTier("UNLIMITED");
applicationDTO.setToken(applicationTokenDTO);
applicationDTO.setKeys(applicationKeysDTOList);
WorkflowResponse workflowResponse = new GeneralWorkflowResponse();
workflowResponse.setWorkflowStatus(WorkflowStatus.APPROVED);
Mockito.when(apiStore.getApplication(applicationId, USER)).thenReturn(getSampleApplication(applicationId));
Mockito.when(apiStore.updateApplication(applicationId, getSampleApplication(applicationId))).thenReturn(workflowResponse);
Mockito.when(apiStore.getApplication(applicationId, USER)).thenReturn(getSampleApplication(applicationId));
Response response = applicationsApiService.applicationsApplicationIdPut(applicationId, applicationDTO, null, null, request);
Assert.assertEquals(200, response.getStatus());
}
Aggregations