use of org.wso2.carbon.apimgt.core.dao.LabelDAO in project carbon-apimgt by wso2.
the class LabelDAOImpl method addLabels.
/**
* @see LabelDAO#addLabels(List)
*/
@Override
public void addLabels(List<Label> labels) throws APIMgtDAOException {
if (!labels.isEmpty()) {
final String query = "INSERT INTO AM_LABELS (LABEL_ID, NAME) VALUES (?,?)";
Map<String, List<String>> urlMap = new HashMap<>();
try (Connection connection = DAOUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(query)) {
for (Label label : labels) {
statement.setString(1, label.getId());
statement.setString(2, label.getName());
statement.addBatch();
urlMap.put(label.getId(), label.getAccessUrls());
}
statement.executeBatch();
if (!urlMap.isEmpty()) {
for (Map.Entry<String, List<String>> entry : urlMap.entrySet()) {
if (entry.getValue() != null) {
insertAccessUrlMappings(entry.getKey(), entry.getValue());
}
}
}
} catch (SQLException e) {
String message = DAOUtil.DAO_ERROR_PREFIX + "adding label data";
throw new APIMgtDAOException(message, e);
}
}
}
use of org.wso2.carbon.apimgt.core.dao.LabelDAO in project carbon-apimgt by wso2.
the class LabelDAOImpl method getLabels.
/**
* @see LabelDAO#getLabels()
*/
@Override
public List<Label> getLabels() throws APIMgtDAOException {
final String query = "SELECT LABEL_ID, NAME, TYPE_NAME FROM AM_LABELS";
List<Label> labels = new ArrayList<>();
try (Connection connection = DAOUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(query)) {
try (ResultSet rs = statement.executeQuery()) {
while (rs.next()) {
Label label = new Label.Builder().id(rs.getString("LABEL_ID")).name(rs.getString("NAME")).type(rs.getString("TYPE_NAME")).accessUrls(getLabelAccessUrls(rs.getString("LABEL_ID"))).build();
labels.add(label);
}
}
} catch (SQLException e) {
String message = DAOUtil.DAO_ERROR_PREFIX + "retrieving labels";
throw new APIMgtDAOException(message, e);
}
return labels;
}
use of org.wso2.carbon.apimgt.core.dao.LabelDAO in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testUpdateApiEndpointName.
@Test(description = "Test add api with production endpoint")
public void testUpdateApiEndpointName() throws APIManagementException {
/**
* this test method verify the API Add with correct API object get invoked correctly
*/
Endpoint endpoint1 = new Endpoint.Builder().id(UUID.randomUUID().toString()).endpointConfig("http://localhost").name("endpoint1").applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).build();
Endpoint endpoint2 = new Endpoint.Builder().id(UUID.randomUUID().toString()).endpointConfig("http://localhost").name("endpoint2").applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).build();
Map<String, Endpoint> endpointMap = new HashMap<>();
endpointMap.put(APIMgtConstants.PRODUCTION_ENDPOINT, endpoint1);
endpointMap.put(APIMgtConstants.SANDBOX_ENDPOINT, endpoint2);
API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI().id(UUID.randomUUID().toString()).endpoint(endpointMap);
apiBuilder.apiPermission("");
apiBuilder.permissionMap(null);
apiBuilder.policies(Collections.emptySet());
apiBuilder.apiPolicy(null);
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
Mockito.when(apiDAO.getAPI(apiBuilder.getId())).thenReturn(apiBuilder.build());
GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
APIGateway gateway = Mockito.mock(APIGateway.class);
PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
LabelDAO labelDao = Mockito.mock(LabelDAO.class);
Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.api, APIMgtConstants.DEFAULT_API_POLICY)).thenReturn(new APIPolicy(APIMgtConstants.DEFAULT_API_POLICY));
APIPublisherImpl apiPublisher = getApiPublisherImpl(apiDAO, apiLifecycleManager, gatewaySourceGenerator, gateway, policyDAO, labelDao);
Mockito.when(apiDAO.getEndpoint(endpoint1.getId())).thenReturn(endpoint1);
Mockito.when(apiDAO.getApiSwaggerDefinition(apiBuilder.getId())).thenReturn(SampleTestObjectCreator.apiDefinition);
Mockito.when(apiDAO.getEndpoint(endpoint2.getId())).thenReturn(endpoint2);
Mockito.when(apiDAO.getEndpoint(endpoint1.getName())).thenReturn(endpoint1);
Mockito.when(apiDAO.getEndpointByName(endpoint2.getName())).thenReturn(endpoint2);
Endpoint endpoint3 = new Endpoint.Builder(endpoint2).name("endpoint3").build();
Map<String, Endpoint> updatedEndpointMap = new HashMap<>(endpointMap);
updatedEndpointMap.replace(APIMgtConstants.SANDBOX_ENDPOINT, endpoint3);
apiBuilder.endpoint(updatedEndpointMap);
apiPublisher.updateAPI(apiBuilder);
Mockito.verify(apiDAO, Mockito.times(1)).updateAPI(apiBuilder.getId(), apiBuilder.build());
}
use of org.wso2.carbon.apimgt.core.dao.LabelDAO in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testAddAlreadyAddedEndpointToApi.
@Test(description = "Test add api with Api Specific Endpoint", expectedExceptions = { APIManagementException.class })
public void testAddAlreadyAddedEndpointToApi() throws APIManagementException, LifecycleException {
/**
* this test method verify the API Add with correct API object get invoked correctly
*/
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
LabelDAO labelDao = Mockito.mock(LabelDAO.class);
GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
Endpoint globalEndpoint = new Endpoint.Builder().id(UUID.randomUUID().toString()).name("testEndpoint").applicableLevel(APIMgtConstants.GLOBAL_ENDPOINT).build();
Endpoint apiEndpoint = new Endpoint.Builder().id(UUID.randomUUID().toString()).name("apiEndpoint").applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).build();
Map<String, Endpoint> endpointMap = new HashMap();
endpointMap.put(APIMgtConstants.PRODUCTION_ENDPOINT, globalEndpoint);
endpointMap.put(APIMgtConstants.SANDBOX_ENDPOINT, apiEndpoint);
API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI().endpoint(endpointMap);
Mockito.when(apiLifecycleManager.addLifecycle(APIMgtConstants.API_LIFECYCLE, USER)).thenReturn(new LifecycleState());
APIGateway gateway = Mockito.mock(APIGateway.class);
APIPublisherImpl apiPublisher = getApiPublisherImpl(null, apiDAO, null, null, null, apiLifecycleManager, labelDao, null, null, null, gatewaySourceGenerator, gateway);
Mockito.when(apiDAO.getEndpoint(globalEndpoint.getId())).thenReturn(globalEndpoint);
Mockito.when(apiDAO.getEndpointByName(apiEndpoint.getName())).thenReturn(apiEndpoint);
Mockito.when(apiDAO.isAPINameExists(apiBuilder.getName(), USER)).thenReturn(false);
apiPublisher.addAPI(apiBuilder);
Mockito.verify(apiDAO, Mockito.times(1)).addAPI(apiBuilder.build());
Mockito.verify(apiLifecycleManager, Mockito.times(1)).addLifecycle(APIMgtConstants.API_LIFECYCLE, USER);
Mockito.verify(apiDAO, Mockito.times(1)).getEndpointByName(apiEndpoint.getName());
Mockito.verify(apiDAO, Mockito.times(1)).isAPINameExists(apiBuilder.getName(), USER);
}
use of org.wso2.carbon.apimgt.core.dao.LabelDAO in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testUpdateDedicatedGateway.
@Test(description = "Update dedicated gateway")
public void testUpdateDedicatedGateway() throws APIManagementException {
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
LabelDAO labelDAO = Mockito.mock(LabelDAO.class);
IdentityProvider identityProvider = Mockito.mock(IdentityProvider.class);
API api = SampleTestObjectCreator.createDefaultAPI().build();
String uuid = api.getId();
Mockito.when(apiDAO.getAPI(uuid)).thenReturn(api);
Mockito.when(identityProvider.getRoleName(SampleTestObjectCreator.DEVELOPER_ROLE_ID)).thenReturn(DEVELOPER_ROLE);
Mockito.when(identityProvider.getRoleName(SampleTestObjectCreator.ADMIN_ROLE_ID)).thenReturn(ADMIN_ROLE);
Mockito.when(apiDAO.getApiSwaggerDefinition(api.getId())).thenReturn(SampleTestObjectCreator.apiDefinition);
String autoGenLabelName = ContainerBasedGatewayConstants.PRIVATE_JET_API_PREFIX + uuid;
Mockito.when(labelDAO.getLabelByName(autoGenLabelName)).thenReturn(null);
Mockito.when(labelDAO.getLabelIdByNameAndType(Mockito.anyString(), Mockito.anyString())).thenReturn(UUID.randomUUID().toString());
APIPublisherImpl apiPublisher = getApiPublisherImpl(identityProvider, apiDAO, labelDAO, gatewaySourceGenerator);
DedicatedGateway dedicatedGateway = SampleTestObjectCreator.createDedicatedGateway(uuid, true, api.getCreatedBy());
apiPublisher.updateDedicatedGateway(dedicatedGateway);
Mockito.verify(labelDAO, Mockito.times(1)).addLabels(Mockito.anyList());
}
Aggregations