Search in sources :

Example 16 with ApiDAO

use of org.wso2.carbon.apimgt.core.dao.ApiDAO in project carbon-apimgt by wso2.

the class ApiDAOImplIT method testUpdateGetDedicatedGateway.

@Test
public void testUpdateGetDedicatedGateway() throws Exception {
    ApiDAO apiDAO = DAOFactory.getApiDAO();
    API.APIBuilder builder = SampleTestObjectCreator.createDefaultAPI().apiDefinition(SampleTestObjectCreator.apiDefinition);
    API api = builder.build();
    testAddGetEndpoint();
    apiDAO.addAPI(api);
    String autoGeneratedLabelName = ContainerBasedGatewayConstants.PRIVATE_JET_API_PREFIX + api.getId();
    List<Label> labelList = new ArrayList<>();
    LabelDAO labelDAO = DAOFactory.getLabelDAO();
    Label autoGenLabel = new Label.Builder().id(UUID.randomUUID().toString()).name(autoGeneratedLabelName).accessUrls(null).build();
    labelList.add(autoGenLabel);
    labelDAO.addLabels(labelList);
    DedicatedGateway dedicatedGateway = new DedicatedGateway();
    dedicatedGateway.setEnabled(true);
    dedicatedGateway.setUpdatedBy(api.getCreatedBy());
    dedicatedGateway.setApiId(api.getId());
    List<String> labels = new ArrayList<>();
    labels.add(autoGeneratedLabelName);
    apiDAO.updateDedicatedGateway(dedicatedGateway, labels);
    DedicatedGateway result = apiDAO.getDedicatedGateway(api.getId());
    Assert.assertEquals(result.isEnabled(), dedicatedGateway.isEnabled());
}
Also used : Label(org.wso2.carbon.apimgt.core.models.Label) ArrayList(java.util.ArrayList) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) LabelDAO(org.wso2.carbon.apimgt.core.dao.LabelDAO) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) DedicatedGateway(org.wso2.carbon.apimgt.core.models.DedicatedGateway) Test(org.testng.annotations.Test)

Example 17 with ApiDAO

use of org.wso2.carbon.apimgt.core.dao.ApiDAO in project carbon-apimgt by wso2.

the class ApiDAOImplIT method testUpdateComment.

@Test
public void testUpdateComment() throws Exception {
    String newCommentText = "updated comment";
    ApiDAO apiDAO = DAOFactory.getApiDAO();
    API.APIBuilder builder = SampleTestObjectCreator.createDefaultAPI().apiDefinition(SampleTestObjectCreator.apiDefinition);
    API api = builder.build();
    testAddGetEndpoint();
    apiDAO.addAPI(api);
    Comment comment1 = SampleTestObjectCreator.createDefaultComment(api.getId());
    apiDAO.addComment(comment1, api.getId());
    String lastUpdatedTime1 = apiDAO.getLastUpdatedTimeOfComment(comment1.getUuid());
    // Keep at least millisecond difference between the two timestamps
    Thread.sleep(1);
    Comment comment2 = SampleTestObjectCreator.createDefaultComment(api.getId());
    comment2.setCommentText(newCommentText);
    apiDAO.updateComment(comment2, comment1.getUuid(), api.getId());
    Comment commentFromDB = apiDAO.getCommentByUUID(comment1.getUuid(), api.getId());
    String lastUpdatedTimeAfterUpdating = apiDAO.getLastUpdatedTimeOfComment(comment1.getUuid());
    Assert.assertNotNull(commentFromDB);
    Assert.assertNotNull(lastUpdatedTime1);
    Assert.assertNotNull(lastUpdatedTimeAfterUpdating);
    Assert.assertNotEquals(lastUpdatedTime1, lastUpdatedTimeAfterUpdating);
    Assert.assertEquals(newCommentText, commentFromDB.getCommentText());
}
Also used : Comment(org.wso2.carbon.apimgt.core.models.Comment) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) Test(org.testng.annotations.Test)

Example 18 with ApiDAO

use of org.wso2.carbon.apimgt.core.dao.ApiDAO in project carbon-apimgt by wso2.

the class ApiDAOImplIT method testGetAllAverageAndUserRatingsOfAPI.

@Test
public void testGetAllAverageAndUserRatingsOfAPI() throws Exception {
    ApiDAO apiDAO = DAOFactory.getApiDAO();
    API.APIBuilder builder = SampleTestObjectCreator.createDefaultAPI().apiDefinition(SampleTestObjectCreator.apiDefinition);
    API api = builder.build();
    testAddGetEndpoint();
    apiDAO.addAPI(api);
    Rating rating1 = SampleTestObjectCreator.createDefaultRating(api.getId());
    apiDAO.addRating(api.getId(), rating1);
    Rating rating2 = SampleTestObjectCreator.createDefaultRating(api.getId());
    rating2.setRating(2);
    rating2.setUsername("andrew");
    apiDAO.addRating(api.getId(), rating2);
    Rating rating3 = SampleTestObjectCreator.createDefaultRating(api.getId());
    rating3.setRating(3);
    rating3.setUsername("smith");
    apiDAO.addRating(api.getId(), rating3);
    List<Rating> ratingsListFromDB = apiDAO.getRatingsListForApi(api.getId());
    Assert.assertNotNull(ratingsListFromDB);
    Assert.assertEquals(ratingsListFromDB.size(), 3);
    Rating ratingOfJohn = apiDAO.getUserRatingForApiFromUser(api.getId(), "john");
    Rating ratingOfAndrew = apiDAO.getUserRatingForApiFromUser(api.getId(), "andrew");
    Rating ratingOfSmith = apiDAO.getUserRatingForApiFromUser(api.getId(), "smith");
    Assert.assertEquals(ratingOfJohn.getRating(), 4);
    Assert.assertEquals(ratingOfAndrew.getRating(), 2);
    Assert.assertEquals(ratingOfSmith.getRating(), 3);
    double averageRating = apiDAO.getAverageRating(api.getId());
    Assert.assertEquals(averageRating, 3, 0.0001);
}
Also used : Rating(org.wso2.carbon.apimgt.core.models.Rating) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) Test(org.testng.annotations.Test)

Example 19 with ApiDAO

use of org.wso2.carbon.apimgt.core.dao.ApiDAO in project carbon-apimgt by wso2.

the class ApiDAOImplIT method testGetAPIsWhenUserRolesInAPIPermissionsWithoutREAD.

@Test(description = "Tests getting the APIs when the user roles are contained in the API permission list " + "but without READ permissions")
public void testGetAPIsWhenUserRolesInAPIPermissionsWithoutREAD() throws Exception {
    ApiDAO apiDAO = DAOFactory.getApiDAO();
    Set<String> rolesOfUser = new HashSet<>();
    rolesOfUser.add(SampleTestObjectCreator.DEVELOPER_ROLE_ID);
    // This user is not the provider of the API
    List<API> apiList = apiDAO.getAPIs(rolesOfUser, ALTERNATIVE_USER);
    Assert.assertTrue(apiList.isEmpty());
    Map map = new HashMap();
    map.put(SampleTestObjectCreator.DEVELOPER_ROLE_ID, 0);
    API.APIBuilder builder = SampleTestObjectCreator.createDefaultAPI().permissionMap(map);
    API api1 = builder.build();
    testAddGetEndpoint();
    apiDAO.addAPI(api1);
    apiList = apiDAO.getAPIs(rolesOfUser, ALTERNATIVE_USER);
    // Since the API has the role ID of the user but without READ permissions, it is not visible to this user
    Assert.assertTrue(apiList.size() == 0);
}
Also used : HashMap(java.util.HashMap) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) HashMap(java.util.HashMap) Map(java.util.Map) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 20 with ApiDAO

use of org.wso2.carbon.apimgt.core.dao.ApiDAO in project carbon-apimgt by wso2.

the class ApiDAOImplIT method testAttributeSearchAPIsStore.

@Test
public void testAttributeSearchAPIsStore() throws Exception {
    // Add few APIs with different attributes.
    List<String> apiIDList = createAPIsAndGetIDsOfAddedAPIs();
    List<String> userRoles = new ArrayList<>();
    Map<String, String> attributeMap = new HashMap<>();
    String[] expectedAPINames;
    // Asserting results for different search queries
    // Attribute search for "provider", for "admin" role
    userRoles.add(ADMIN);
    attributeMap.put("provider", "a");
    expectedAPINames = new String[] { "PublicAPI", "AdminManagerAPI" };
    Assert.assertTrue(compareResults(userRoles, new ArrayList<>(), attributeMap, expectedAPINames));
    userRoles.clear();
    attributeMap.clear();
    // Attribute search for "version", for "manager" role
    userRoles.add(MANAGER_ROLE);
    attributeMap.put("version", "2.3");
    expectedAPINames = new String[] { "PublicAPI", "ManagerOnlyAPI" };
    Assert.assertTrue(compareResults(userRoles, new ArrayList<>(), attributeMap, expectedAPINames));
    userRoles.clear();
    attributeMap.clear();
    // Attribute search for "context", for "manager", "employee" and "customer" roles
    userRoles.add(MANAGER_ROLE);
    userRoles.add(EMPLOYEE_ROLE);
    userRoles.add(CUSTOMER_ROLE);
    attributeMap.put("context", "Man");
    expectedAPINames = new String[] { "ManagerOnlyAPI", "AdminManagerAPI" };
    Assert.assertTrue(compareResults(userRoles, new ArrayList<>(), attributeMap, expectedAPINames));
    userRoles.clear();
    attributeMap.clear();
    // Attribute search for "description", for "admin" role
    userRoles.add(ADMIN);
    attributeMap.put("description", "Admin and manager");
    expectedAPINames = new String[] { "AdminManagerAPI" };
    Assert.assertTrue(compareResults(userRoles, new ArrayList<>(), attributeMap, expectedAPINames));
    userRoles.clear();
    attributeMap.clear();
    // Attribute search for "tags", for "manager", "employee" and "customer" roles
    userRoles.add(MANAGER_ROLE);
    userRoles.add(EMPLOYEE_ROLE);
    userRoles.add(CUSTOMER_ROLE);
    attributeMap.put("tags", "E");
    expectedAPINames = new String[] { "ManagerOnlyAPI", "NonAdminAPI" };
    Assert.assertTrue(compareResults(userRoles, new ArrayList<>(), attributeMap, expectedAPINames));
    userRoles.clear();
    attributeMap.clear();
    // Attribute search for "subcontext", for "manager", "employee" and "customer" roles
    userRoles.add(MANAGER_ROLE);
    userRoles.add(EMPLOYEE_ROLE);
    userRoles.add(CUSTOMER_ROLE);
    attributeMap.put("subcontext", "C");
    expectedAPINames = new String[] { "AdminManagerAPI", "EmployeeAPI", "NonAdminAPI" };
    Assert.assertTrue(compareResults(userRoles, new ArrayList<>(), attributeMap, expectedAPINames));
    userRoles.clear();
    attributeMap.clear();
    // cleanup added APIs
    ApiDAO apiDAO = DAOFactory.getApiDAO();
    for (String apiID : apiIDList) {
        apiDAO.deleteAPI(apiID);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) Test(org.testng.annotations.Test)

Aggregations

ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)315 Test (org.testng.annotations.Test)305 API (org.wso2.carbon.apimgt.core.models.API)210 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)108 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)102 APILifecycleManager (org.wso2.carbon.apimgt.core.api.APILifecycleManager)75 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)65 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)62 APIBuilder (org.wso2.carbon.apimgt.core.models.API.APIBuilder)59 BeforeTest (org.testng.annotations.BeforeTest)50 ArrayList (java.util.ArrayList)48 IdentityProvider (org.wso2.carbon.apimgt.core.api.IdentityProvider)47 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)46 LabelDAO (org.wso2.carbon.apimgt.core.dao.LabelDAO)45 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)43 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)36 HashMap (java.util.HashMap)31 SQLException (java.sql.SQLException)30 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)30 LifecycleState (org.wso2.carbon.lcm.core.impl.LifecycleState)27