Sync master branch changes into appm_improvement branch

appm_improvement
Lasantha Dharmakeerthi 5 months ago
commit 47ac0774ad

@ -107,6 +107,23 @@ public interface GrafanaAPIProxyService {
)
Response frontendMetrics(JsonObject body, @Context HttpHeaders headers, @Context UriInfo requestUriInfo);
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/user/auth-tokens/rotate")
@ApiOperation(
produces = MediaType.APPLICATION_JSON,
httpMethod = "POST",
value = "Rotate authentication tokens",
tags = "Analytics",
extensions = {
@Extension(properties = {
@ExtensionProperty(name = SCOPE, value = "grafana:api:view")
})
}
)
Response rotateAuthToken(JsonObject body, @Context HttpHeaders headers, @Context UriInfo requestUriInfo);
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/dashboards/uid/{uid}")
@ -123,6 +140,22 @@ public interface GrafanaAPIProxyService {
)
Response getDashboard(@Context HttpHeaders headers, @Context UriInfo requestUriInfo) throws ClassNotFoundException;
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/folders/{uid}")
@ApiOperation(
produces = MediaType.APPLICATION_JSON,
httpMethod = "GET",
value = "Grafana dashboard folder information",
tags = "Analytics",
extensions = {
@Extension(properties = {
@ExtensionProperty(name = SCOPE, value = "grafana:api:view")
})
}
)
Response getFolders(@Context HttpHeaders headers, @Context UriInfo requestUriInfo) throws ClassNotFoundException;
@GET
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@ -140,6 +173,23 @@ public interface GrafanaAPIProxyService {
)
Response getAnnotations(@Context HttpHeaders headers, @Context UriInfo requestUriInfo) throws ClassNotFoundException;
@GET
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/prometheus/grafana/api/v1/rules")
@ApiOperation(
produces = MediaType.APPLICATION_JSON,
httpMethod = "GET",
value = "Accessing Grafana Prometheus rule information",
tags = "Analytics",
extensions = {
@Extension(properties = {
@ExtensionProperty(name = SCOPE, value = "grafana:api:view")
})
}
)
Response prometheusRuleInfo(@Context HttpHeaders headers, @Context UriInfo requestUriInfo) throws ClassNotFoundException;
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/alerts/states-for-dashboard")

@ -26,6 +26,8 @@ import io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.api.impl.util.Grafa
import io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.api.impl.util.GrafanaRequestHandlerUtil;
import io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.common.exception.GrafanaManagementException;
import io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.core.bean.GrafanaPanelIdentifier;
import io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.core.config.GrafanaConfiguration;
import io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.core.config.GrafanaConfigurationManager;
import io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.core.exception.MaliciousQueryAttempt;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -56,9 +58,13 @@ public class GrafanaAPIProxyServiceImpl implements GrafanaAPIProxyService {
@Override
public Response queryDatasource(JsonObject body, @Context HttpHeaders headers, @Context UriInfo requestUriInfo) {
try {
GrafanaConfiguration configuration = GrafanaConfigurationManager.getInstance().getGrafanaConfiguration();
GrafanaPanelIdentifier panelIdentifier = GrafanaRequestHandlerUtil.getPanelIdentifier(headers);
boolean queryValidationConfig = configuration.getValidationConfig().getDSQueryValidation();
if (queryValidationConfig) {
GrafanaMgtAPIUtils.getGrafanaQueryService().buildSafeQuery(body, panelIdentifier.getDashboardId(),
panelIdentifier.getPanelId(), requestUriInfo.getRequestUri());
}
return GrafanaRequestHandlerUtil.proxyPassPostRequest(body, requestUriInfo, panelIdentifier.getOrgId());
} catch (MaliciousQueryAttempt e) {
return Response.status(Response.Status.BAD_REQUEST).entity(
@ -83,6 +89,15 @@ public class GrafanaAPIProxyServiceImpl implements GrafanaAPIProxyService {
return proxyPassPostRequest(body, headers, requestUriInfo);
}
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/user/auth-tokens/rotate")
@Override
public Response rotateAuthToken(JsonObject body, @Context HttpHeaders headers, @Context UriInfo requestUriInfo) {
return proxyPassPostRequest(body, headers, requestUriInfo);
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/dashboards/uid/{uid}")
@ -91,6 +106,14 @@ public class GrafanaAPIProxyServiceImpl implements GrafanaAPIProxyService {
return proxyPassGetRequest(headers, requestUriInfo);
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/folders/{uid}")
@Override
public Response getFolders(@Context HttpHeaders headers, @Context UriInfo requestUriInfo) {
return proxyPassGetRequest(headers, requestUriInfo);
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@ -99,6 +122,16 @@ public class GrafanaAPIProxyServiceImpl implements GrafanaAPIProxyService {
public Response getAnnotations(@Context HttpHeaders headers, @Context UriInfo requestUriInfo) {
return proxyPassGetRequest(headers, requestUriInfo);
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/prometheus/grafana/api/v1/rules")
@Override
public Response prometheusRuleInfo(@Context HttpHeaders headers, @Context UriInfo requestUriInfo) {
return proxyPassGetRequest(headers, requestUriInfo);
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/alerts/states-for-dashboard")

@ -22,6 +22,8 @@ import io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.api.bean.ErrorRespo
import io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.api.exception.RefererNotValid;
import io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.common.exception.GrafanaManagementException;
import io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.core.bean.GrafanaPanelIdentifier;
import io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.core.config.GrafanaConfiguration;
import io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.core.config.GrafanaConfigurationManager;
import io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.core.exception.GrafanaEnvVariablesNotDefined;
import io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.core.util.GrafanaConstants;
import io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.core.util.GrafanaUtil;
@ -120,20 +122,24 @@ public class GrafanaRequestHandlerUtil {
return path;
}
public static GrafanaPanelIdentifier getPanelIdentifier(HttpHeaders headers) throws RefererNotValid {
public static GrafanaPanelIdentifier getPanelIdentifier(HttpHeaders headers) throws RefererNotValid, GrafanaManagementException {
String referer = headers.getHeaderString(GrafanaConstants.REFERER_HEADER);
if (referer == null) {
String errMsg = "Request does not contain Referer header";
log.error(errMsg);
throw new RefererNotValid(errMsg);
}
GrafanaConfiguration configuration = GrafanaConfigurationManager.getInstance().getGrafanaConfiguration();
boolean dashboardIntegrationConfig = configuration.getValidationConfig().getDashboardIntegration();
GrafanaPanelIdentifier panelIdentifier = GrafanaUtil.getPanelIdentifierFromReferer(referer);
if (!dashboardIntegrationConfig) {
if (panelIdentifier.getDashboardId() == null ||
panelIdentifier.getPanelId() == null || panelIdentifier.getOrgId() == null) {
String errMsg = "Referer must contain dashboardId, panelId and orgId";
String errMsg = "Referer must contain dashboardId, panelId, and orgId";
log.error(errMsg);
throw new RefererNotValid(errMsg);
}
}
return panelIdentifier;
}

@ -19,6 +19,7 @@
package io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.core.config;
import io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.core.config.xml.bean.CacheConfiguration;
import io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.core.config.xml.bean.ValidationConfig;
import io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.core.config.xml.bean.User;
import javax.xml.bind.annotation.XmlElement;
@ -30,6 +31,7 @@ import java.util.List;
public class GrafanaConfiguration {
private User adminUser;
private ValidationConfig validationConfig;
private List<CacheConfiguration> caches;
@XmlElement(name = "AdminUser")
@ -37,6 +39,15 @@ public class GrafanaConfiguration {
return adminUser;
}
@XmlElement(name = "ValidationConfig")
public ValidationConfig getValidationConfig() {
return validationConfig;
}
public void setValidationConfig(ValidationConfig validationConfig) {
this.validationConfig = validationConfig;
}
public void setAdminUser(User user) {
this.adminUser = user;
}

@ -0,0 +1,45 @@
/*
* Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.core.config.xml.bean;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "ValidationConfig")
public class ValidationConfig {
private boolean dsQueryValidation;
private boolean dashboardIntegration;
@XmlElement(name = "DSQueryValidation")
public boolean getDSQueryValidation() {
return dsQueryValidation;
}
public void setDSQueryValidation(boolean dsQueryValidation) {
this.dsQueryValidation = dsQueryValidation;
}
@XmlElement(name = "DashboardIntegration")
public boolean getDashboardIntegration() {
return dashboardIntegration;
}
public void setDashboardIntegration(boolean dashboardIntegration) {
this.dashboardIntegration = dashboardIntegration;
}
}

@ -120,7 +120,9 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
throw new BadRequestException(msg);
} else if (HttpStatus.SC_NOT_FOUND == response.code()) {
String msg = "Shared scope key not found : " + key;
log.info(msg);
if (log.isDebugEnabled()) {
log.debug(msg);
}
return false;
} else {
String msg = "Response : " + response.code() + response.body();

@ -205,7 +205,9 @@
org.wso2.carbon.utils;version="4.6",
org.wso2.carbon.utils.multitenancy;version="4.6",
org.apache.commons.lang,
org.json
org.json,
io.entgra.device.mgt.core.device.mgt.common.permission.mgt,
io.entgra.device.mgt.core.device.mgt.core.permission.mgt
</Import-Package>
<Embed-Dependency>
jsr311-api;scope=compile|runtime;inline=false

@ -46,6 +46,7 @@ import io.entgra.device.mgt.core.device.mgt.core.config.DeviceManagementConfig;
import io.entgra.device.mgt.core.device.mgt.core.config.permission.DefaultPermission;
import io.entgra.device.mgt.core.device.mgt.core.config.permission.DefaultPermissions;
import io.entgra.device.mgt.core.device.mgt.core.config.permission.ScopeMapping;
import io.entgra.device.mgt.core.device.mgt.core.permission.mgt.PermissionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -68,6 +69,8 @@ import org.wso2.carbon.user.core.tenant.TenantSearchResult;
import org.wso2.carbon.utils.CarbonUtils;
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
import org.wso2.carbon.utils.multitenancy.MultitenantUtils;
import io.entgra.device.mgt.core.device.mgt.core.permission.mgt.PermissionUtils;
import io.entgra.device.mgt.core.device.mgt.common.permission.mgt.PermissionManagementException;
import java.io.BufferedReader;
import java.io.File;
@ -610,9 +613,17 @@ public class APIPublisherServiceImpl implements APIPublisherService {
if (publisherRESTAPIServices.isSharedScopeNameExists(apiApplicationKey, accessTokenInfo, scope.getName())) {
publisherRESTAPIServices.updateSharedScope(apiApplicationKey, accessTokenInfo, scope);
// todo: permission changed in update path, is not handled yet.
} else {
// todo: come to this level means, that scope is removed from API, but haven't removed from the scope-role-permission-mappings list
log.warn(scope.getName() + " not available as shared scope");
// This scope doesn't have an api attached.
log.warn(scope.getName() + " not available as shared, add as new scope");
publisherRESTAPIServices.addNewSharedScope(apiApplicationKey, accessTokenInfo, scope);
// add permission if not exist
try {
PermissionUtils.putPermission(permission);
} catch(PermissionManagementException e) {
log.error("Error when adding permission ", e);
}
}
}
for (String role : rolePermissions.keySet()) {

@ -118,6 +118,11 @@ public class Filter {
*/
private boolean isNotRetired;
/**
* To check whether web applications should be returned
*/
private boolean withWebApps;
public int getLimit() {
return limit;
}
@ -221,4 +226,12 @@ public class Filter {
public void setNotRetired(boolean notRetired) {
isNotRetired = notRetired;
}
public boolean isWithWebApps() {
return withWebApps;
}
public void setWithWebApps(boolean withWebApps) {
this.withWebApps = withWebApps;
}
}

@ -178,7 +178,11 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
sql += "AND AP_APP_RELEASE.CURRENT_STATE = ? ";
}
if (deviceTypeId != -1) {
sql += "AND AP_APP.DEVICE_TYPE_ID = ? ";
sql += "AND (AP_APP.DEVICE_TYPE_ID = ? ";
if (filter.isWithWebApps()) {
sql += "OR AP_APP.DEVICE_TYPE_ID = 0 ";
}
sql += ") ";
}
if (filter.isNotRetired()) {
sql += "AND AP_APP.STATUS != 'RETIRED' ";
@ -309,7 +313,11 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
sql += " AND AP_APP_RELEASE.CURRENT_STATE = ?";
}
if (deviceTypeId != -1) {
sql += " AND AP_APP.DEVICE_TYPE_ID = ?";
sql += "AND (AP_APP.DEVICE_TYPE_ID = ? ";
if (filter.isWithWebApps()) {
sql += "OR AP_APP.DEVICE_TYPE_ID = 0 ";
}
sql += ") ";
}
if (filter.isNotRetired()) {
sql += " AND AP_APP.STATUS != 'RETIRED'";

@ -165,6 +165,13 @@ import java.util.Map;
roles = {"Internal/devicemgt-user"},
permissions = {"/device-mgt/devices/change-status"}
),
@Scope(
name = "Update status of a given operation",
description = "Updates the status of a given operation of a given device",
key = "dm:devices:ops:status:update",
roles = {"Internal/devicemgt-user"},
permissions = {"/device-mgt/devices/operations/status-update"}
),
@Scope(
name = "Enroll Device",
description = "Register a device",
@ -2714,12 +2721,12 @@ public interface DeviceManagementService {
@ApiOperation(
produces = MediaType.APPLICATION_JSON,
httpMethod = "PUT",
value = "Update status of a given opeation",
value = "Update status of a given operation",
notes = "Updates the status of a given operation of a given device in Entgra IoT Server.",
tags = "Device Management",
extensions = {
@Extension(properties = {
@ExtensionProperty(name = Constants.SCOPE, value = "dm:devices:ops:view")
@ExtensionProperty(name = Constants.SCOPE, value = "dm:devices:ops:status:update")
})
}
)

@ -1716,7 +1716,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
String sql = "SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME AS DEVICE_NAME, d.LAST_UPDATED_TIMESTAMP, " +
"e.DEVICE_TYPE, e.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, " +
"e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT, e.ENROLMENT_ID FROM " +
"(SELECT e.ID, e.DEVICE_ID, e.DEVICE_TYPE, e.OWNER, " +
"(SELECT e.ID, e.DEVICE_ID, e.DEVICE_TYPE, e.DEVICE_IDENTIFICATION, e.OWNER, " +
"e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, e.DATE_OF_ENROLMENT, e.DATE_OF_LAST_UPDATE, e.ID AS " +
"ENROLMENT_ID FROM DM_ENROLMENT e WHERE TENANT_ID = ? AND STATUS = ?) e, " +
"DM_DEVICE d WHERE d.ID = e.DEVICE_ID AND d.TENANT_ID = ?";

@ -23,6 +23,7 @@ import io.entgra.device.mgt.core.device.mgt.common.EnrolmentInfo;
import io.entgra.device.mgt.core.device.mgt.common.MonitoringOperation;
import io.entgra.device.mgt.core.device.mgt.common.OperationMonitoringTaskConfig;
import io.entgra.device.mgt.core.device.mgt.common.app.mgt.Application;
import io.entgra.device.mgt.core.device.mgt.common.device.details.DeviceData;
import io.entgra.device.mgt.core.device.mgt.common.device.details.DeviceInfo;
import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroup;
import io.entgra.device.mgt.core.device.mgt.common.notification.mgt.Notification;
@ -32,6 +33,7 @@ import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.Calendar;
public class TestDataHolder {
@ -39,11 +41,18 @@ public class TestDataHolder {
public final static Integer SUPER_TENANT_ID = -1234;
public final static String SUPER_TENANT_DOMAIN = "carbon.super";
public final static String initialDeviceIdentifier = "12345";
public final static String initialDeviceName = "TEST-DEVICE";
public final static String OWNER = "admin";
public static final String OPERATION_CONFIG = "TEST-OPERATION-";
public static Device initialTestDevice;
public static DeviceType initialTestDeviceType;
public static Date getTimeBefore(int minutes) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MINUTE, -minutes);
return calendar.getTime();
}
public static Device generateDummyDeviceData(String deviceType) {
Device device = new Device();
device.setEnrolmentInfo(generateEnrollmentInfo(new Date().getTime(), new Date().getTime(), OWNER, EnrolmentInfo
@ -141,6 +150,15 @@ public class TestDataHolder {
return device;
}
public static DeviceData generateDummyDevice(DeviceIdentifier deviceIdentifier) {
DeviceData deviceData = new DeviceData();
deviceData.setDeviceIdentifier(deviceIdentifier);
deviceData.setDeviceOwner(OWNER);
deviceData.setDeviceOwnership(EnrolmentInfo.OwnerShip.BYOD.toString());
deviceData.setLastModifiedDate(getTimeBefore(1));
return deviceData;
}
public static DeviceType generateDeviceTypeData(String devTypeName) {
DeviceType deviceType = new DeviceType();
deviceType.setName(devTypeName);

@ -18,7 +18,13 @@
package io.entgra.device.mgt.core.device.mgt.core.dao;
import io.entgra.device.mgt.core.device.mgt.common.EnrolmentInfo;
import io.entgra.device.mgt.core.device.mgt.common.PaginationRequest;
import io.entgra.device.mgt.core.device.mgt.common.device.details.DeviceData;
import io.entgra.device.mgt.core.device.mgt.common.geo.service.GeoCluster;
import io.entgra.device.mgt.core.device.mgt.common.geo.service.GeoCoordinate;
import io.entgra.device.mgt.core.device.mgt.common.geo.service.GeoQuery;
import org.apache.commons.collections.map.SingletonMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.testng.Assert;
@ -34,6 +40,7 @@ import io.entgra.device.mgt.core.device.mgt.core.common.TestDataHolder;
import io.entgra.device.mgt.core.device.mgt.core.dto.DeviceType;
import java.sql.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@ -89,6 +96,7 @@ public class DevicePersistTests extends BaseDeviceManagementTest {
public void testAddDeviceTest() {
int tenantId = TestDataHolder.SUPER_TENANT_ID;
Device device = TestDataHolder.generateDummyDeviceData(TestDataHolder.TEST_DEVICE_TYPE);
device.setName(TestDataHolder.initialDeviceName);
try {
DeviceManagementDAOFactory.beginTransaction();
@ -275,6 +283,7 @@ public class DevicePersistTests extends BaseDeviceManagementTest {
pr.setStatusList(Collections.singletonList(Status.ACTIVE.name()));
List<Device> results = deviceDAO.getDevicesByStatus(pr, TestDataHolder.SUPER_TENANT_ID);
Assert.assertEquals(1, results.size(), "No device returned");
DeviceManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the current status of the " +
"enrolment", e);
@ -283,4 +292,509 @@ public class DevicePersistTests extends BaseDeviceManagementTest {
}
}
@Test(dependsOnMethods = "testAddDeviceTest")
public void getDevicesByTenantId() throws DeviceManagementDAOException, TransactionManagementException {
try {
DeviceManagementDAOFactory.beginTransaction();
List<Device> results = deviceDAO.getDevices(TestDataHolder.SUPER_TENANT_ID);
Assert.assertEquals(1, results.size(), "No device returned");
DeviceManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = "testAddDeviceTest")
public void getDeviceByDeviceId() throws DeviceManagementDAOException, TransactionManagementException {
Device device = TestDataHolder.initialTestDevice;
try {
DeviceManagementDAOFactory.beginTransaction();
Device results = deviceDAO.getDevice(device.getDeviceIdentifier(), TestDataHolder.SUPER_TENANT_ID);
Assert.assertNotNull(results.getDeviceIdentifier(), "No device returned");
DeviceManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = "testAddDeviceTest")
public void getDeviceByDeviceIdentifierWithTenantId() throws DeviceManagementDAOException, TransactionManagementException {
Device device = TestDataHolder.initialTestDevice;
try {
DeviceManagementDAOFactory.beginTransaction();
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType());
Device results = deviceDAO.getDevice(deviceIdentifier, TestDataHolder.SUPER_TENANT_ID);
Assert.assertNotNull(results.getDeviceIdentifier(), "No device returned");
DeviceManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = "testAddDeviceTest")
public void getDeviceByDeviceData() throws DeviceManagementDAOException, TransactionManagementException {
Device device = TestDataHolder.initialTestDevice;
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType());
DeviceData deviceData = TestDataHolder.generateDummyDevice(deviceIdentifier);
try {
DeviceManagementDAOFactory.beginTransaction();
Device results = deviceDAO.getDevice(deviceData, TestDataHolder.SUPER_TENANT_ID);
Assert.assertNotNull(results.getDeviceIdentifier(), "No device returned");
DeviceManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = "testAddDeviceTest")
public void getDeviceByOwner() throws DeviceManagementDAOException, TransactionManagementException {
Device device = TestDataHolder.initialTestDevice;
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType());
try {
DeviceManagementDAOFactory.beginTransaction();
Device results = deviceDAO.getDevice(deviceIdentifier, TestDataHolder.OWNER, TestDataHolder.SUPER_TENANT_ID);
Assert.assertNotNull(results.getDeviceIdentifier(), "No device returned");
DeviceManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = "testAddDeviceTest")
public void getDeviceByDateSince() throws DeviceManagementDAOException, TransactionManagementException {
Device device = TestDataHolder.initialTestDevice;
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType());
try {
DeviceManagementDAOFactory.beginTransaction();
Device results = deviceDAO.getDevice(deviceIdentifier, TestDataHolder.getTimeBefore(1), TestDataHolder.SUPER_TENANT_ID);
Assert.assertNotNull(results.getDeviceIdentifier(), "No device returned");
DeviceManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = "testAddDeviceTest")
public void getDeviceByDateSinceWithDeviceId() throws DeviceManagementDAOException, TransactionManagementException {
Device device = TestDataHolder.initialTestDevice;
try {
DeviceManagementDAOFactory.beginTransaction();
Device results = deviceDAO.getDevice(device.getDeviceIdentifier(), TestDataHolder.getTimeBefore(1), TestDataHolder.SUPER_TENANT_ID);
Assert.assertNotNull(results.getDeviceIdentifier(), "No device returned");
DeviceManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = "testAddDeviceTest")
public void getDeviceByEnrollmentStatus() throws DeviceManagementDAOException, TransactionManagementException {
Device device = TestDataHolder.initialTestDevice;
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType());
try {
DeviceManagementDAOFactory.beginTransaction();
Device results = deviceDAO.getDevice(deviceIdentifier, Status.ACTIVE, TestDataHolder.SUPER_TENANT_ID);
Assert.assertEquals(device.getDeviceIdentifier(), results.getDeviceIdentifier(), "No device returned");
DeviceManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = "testAddDeviceTest")
public void getDeviceByDeviceIdentifier() throws DeviceManagementDAOException, TransactionManagementException {
Device device = TestDataHolder.initialTestDevice;
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType());
try {
DeviceManagementDAOFactory.beginTransaction();
SingletonMap results = deviceDAO.getDevice(deviceIdentifier);
Assert.assertNotNull(results, "No device returned");
DeviceManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = "testAddDeviceTest")
public void getDeviceByDeviceType() throws DeviceManagementDAOException, TransactionManagementException {
Device device = TestDataHolder.initialTestDevice;
try {
DeviceManagementDAOFactory.beginTransaction();
List<Device> results = deviceDAO.getDevices(device.getType(), TestDataHolder.SUPER_TENANT_ID);
Assert.assertEquals(1, results.size(), "No device returned");
DeviceManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = "testAddDeviceTest")
public void getAllocatedDevices() throws DeviceManagementDAOException, TransactionManagementException {
Device device = TestDataHolder.initialTestDevice;
try {
DeviceManagementDAOFactory.beginTransaction();
List<Device> results = deviceDAO.getAllocatedDevices(device.getType(), TestDataHolder.SUPER_TENANT_ID, 1, 0);
Assert.assertEquals(1, results.size(), "No device returned");
DeviceManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = "testAddDeviceTest")
public void getDevicesOfUser() throws DeviceManagementDAOException, TransactionManagementException {
try {
DeviceManagementDAOFactory.beginTransaction();
List<Device> results = deviceDAO.getDevicesOfUser(TestDataHolder.OWNER, TestDataHolder.SUPER_TENANT_ID);
Assert.assertEquals(1, results.size(), "No device returned");
DeviceManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = "testAddDeviceTest")
public void getDevicesOfUserWithDeviceType() throws DeviceManagementDAOException, TransactionManagementException {
Device device = TestDataHolder.initialTestDevice;
try {
DeviceManagementDAOFactory.beginTransaction();
List<Device> results = deviceDAO.getDevicesOfUser(TestDataHolder.OWNER, device.getType(), TestDataHolder.SUPER_TENANT_ID);
Assert.assertEquals(1, results.size(), "No device returned");
DeviceManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = "testAddDeviceTest")
public void getDevicesOfUserWithDeviceStatus() throws DeviceManagementDAOException, TransactionManagementException {
List<String> status = new ArrayList<>() ;
status.add(Status.ACTIVE.name());
try {
DeviceManagementDAOFactory.beginTransaction();
List<Device> results = deviceDAO.getDevicesOfUser(TestDataHolder.OWNER, TestDataHolder.SUPER_TENANT_ID, status);
Assert.assertEquals(1, results.size(), "No device returned");
DeviceManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = "testAddDeviceTest")
public void getCountOfDevicesInGroup() throws DeviceManagementDAOException, TransactionManagementException {
PaginationRequest pr = new PaginationRequest(0, 10);
pr.setGroupId(1);
try {
DeviceManagementDAOFactory.beginTransaction();
int results = deviceDAO.getCountOfDevicesInGroup(pr, TestDataHolder.SUPER_TENANT_ID);
Assert.assertEquals(0, results, "No device count returned in group");
DeviceManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the device count" + e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = "testAddDeviceTest")
public void getDeviceCountWithOwner() throws DeviceManagementDAOException, TransactionManagementException {
try {
DeviceManagementDAOFactory.beginTransaction();
int results = deviceDAO.getDeviceCount(TestDataHolder.OWNER, TestDataHolder.SUPER_TENANT_ID);
Assert.assertEquals(1, results, "No device returned");
DeviceManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = "testAddDeviceTest")
public void getDeviceCountWithType() throws DeviceManagementDAOException, TransactionManagementException {
Device device = TestDataHolder.initialTestDevice;
try {
DeviceManagementDAOFactory.beginTransaction();
int results = deviceDAO.getDeviceCount(device.getType(), Status.ACTIVE.name(), TestDataHolder.SUPER_TENANT_ID);
Assert.assertEquals(1, results, "No device returned");
DeviceManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = "testAddDeviceTest")
public void setEnrolmentStatusInBulk() throws DeviceManagementDAOException, TransactionManagementException {
Device device = TestDataHolder.initialTestDevice;
List<String> devices = new ArrayList<>() ;
devices.add(device.getDeviceIdentifier());
try {
DeviceManagementDAOFactory.beginTransaction();
boolean results = deviceDAO.setEnrolmentStatusInBulk(device.getType(), Status.ACTIVE.name(),TestDataHolder.SUPER_TENANT_ID, devices );
Assert.assertTrue(results, "No device returned");
DeviceManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = "testAddDeviceTest")
public void getDeviceCount() throws DeviceManagementDAOException, TransactionManagementException {
try {
DeviceManagementDAOFactory.beginTransaction();
int results = deviceDAO.getDeviceCount(TestDataHolder.SUPER_TENANT_ID);
Assert.assertEquals(1, results, "No device returned");
DeviceManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = "testAddDeviceTest")
public void getDeviceCountWithPagination() throws DeviceManagementDAOException, TransactionManagementException {
Device device = TestDataHolder.initialTestDevice;
PaginationRequest pr = new PaginationRequest(0, 10);
pr.setDeviceName(device.getName());
pr.setDeviceType(device.getType());
pr.setOwner(TestDataHolder.OWNER);
try {
DeviceManagementDAOFactory.beginTransaction();
int results = deviceDAO.getDeviceCount(pr, TestDataHolder.SUPER_TENANT_ID);
Assert.assertEquals(1, results, "No device returned");
DeviceManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = "testAddDeviceTest")
public void getDeviceCountByType() throws DeviceManagementDAOException, TransactionManagementException {
Device device = TestDataHolder.initialTestDevice;
try {
DeviceManagementDAOFactory.beginTransaction();
int results = deviceDAO.getDeviceCountByType(device.getType(), TestDataHolder.SUPER_TENANT_ID);
Assert.assertEquals(1, results, "No device returned");
DeviceManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = "testAddDeviceTest")
public void getDeviceCountByUser() throws DeviceManagementDAOException, TransactionManagementException {
try {
DeviceManagementDAOFactory.beginTransaction();
int results = deviceDAO.getDeviceCountByUser(TestDataHolder.OWNER, TestDataHolder.SUPER_TENANT_ID);
Assert.assertEquals(1, results, "No device returned");
DeviceManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = "testAddDeviceTest")
public void getDeviceCountByName() throws DeviceManagementDAOException, TransactionManagementException {
try {
DeviceManagementDAOFactory.beginTransaction();
int results = deviceDAO.getDeviceCountByName(TestDataHolder.initialDeviceName, TestDataHolder.SUPER_TENANT_ID);
Assert.assertEquals(1, results, "No device returned");
DeviceManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = "testAddDeviceTest")
public void getDeviceCountByOwnership() throws DeviceManagementDAOException, TransactionManagementException {
try {
DeviceManagementDAOFactory.beginTransaction();
int results = deviceDAO.getDeviceCountByOwnership(EnrolmentInfo.OwnerShip.BYOD.name(), TestDataHolder.SUPER_TENANT_ID);
Assert.assertEquals(1, results, "No device returned");
DeviceManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = "testAddDeviceTest")
public void getDeviceCountByStatus() throws DeviceManagementDAOException, TransactionManagementException {
try {
DeviceManagementDAOFactory.beginTransaction();
int results = deviceDAO.getDeviceCountByStatus(Status.ACTIVE.name(), TestDataHolder.SUPER_TENANT_ID);
Assert.assertEquals(1, results, "No device returned");
DeviceManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = "testAddDeviceTest")
public void getDeviceCountByStatusWithType() throws DeviceManagementDAOException, TransactionManagementException {
Device device = TestDataHolder.initialTestDevice;
try {
DeviceManagementDAOFactory.beginTransaction();
int results = deviceDAO.getDeviceCountByStatus(device.getType(), Status.ACTIVE.name(), TestDataHolder.SUPER_TENANT_ID);
Assert.assertEquals(1, results, "No device returned");
DeviceManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = "testAddDeviceTest")
public void getActiveEnrolment() throws DeviceManagementDAOException, TransactionManagementException {
Device device = TestDataHolder.initialTestDevice;
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType());
try {
DeviceManagementDAOFactory.beginTransaction();
EnrolmentInfo results = deviceDAO.getActiveEnrolment(deviceIdentifier, TestDataHolder.SUPER_TENANT_ID);
Assert.assertEquals(Status.ACTIVE, results.getStatus(), "No device returned");
DeviceManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = "testAddDeviceTest")
public void getDeviceEnrolledTenants() throws DeviceManagementDAOException, TransactionManagementException {
try {
DeviceManagementDAOFactory.beginTransaction();
List<Integer> results = deviceDAO.getDeviceEnrolledTenants();
Assert.assertEquals(1, results.size(), "No device returned");
DeviceManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = "testAddDeviceTest")
public void findGeoClusters() throws DeviceManagementDAOException, TransactionManagementException {
GeoQuery geoQuery = new GeoQuery(new GeoCoordinate(123, 123), new GeoCoordinate(123, 123), 12345);
try {
DeviceManagementDAOFactory.beginTransaction();
List<GeoCluster> results = deviceDAO.findGeoClusters(geoQuery, TestDataHolder.SUPER_TENANT_ID);
Assert.assertNotNull(results, "No device returned");
DeviceManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = "testAddDeviceTest")
public void getAppNotInstalledDevices() throws DeviceManagementDAOException, TransactionManagementException {
Device device = TestDataHolder.initialTestDevice;
PaginationRequest pr = new PaginationRequest(0, 10);
pr.setDeviceType(device.getType());
try {
DeviceManagementDAOFactory.beginTransaction();
List<Device> results = deviceDAO.getAppNotInstalledDevices(pr, TestDataHolder.SUPER_TENANT_ID, "com.google.calc", "1.0.0");
Assert.assertNotNull(results, "No device returned");
DeviceManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = "testAddDeviceTest")
public void getCountOfAppNotInstalledDevices() throws DeviceManagementDAOException, TransactionManagementException {
Device device = TestDataHolder.initialTestDevice;
PaginationRequest pr = new PaginationRequest(0, 10);
pr.setDeviceType(device.getType());
try {
DeviceManagementDAOFactory.beginTransaction();
int results = deviceDAO.getCountOfAppNotInstalledDevices(pr, TestDataHolder.SUPER_TENANT_ID, "com.google.calc", "1.0.0");
Assert.assertEquals(1, results, "No device returned");
DeviceManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = "testAddDeviceTest")
public void getDevicesByEncryptionStatus() throws DeviceManagementDAOException, TransactionManagementException {
PaginationRequest pr = new PaginationRequest(0, 10);
try {
DeviceManagementDAOFactory.beginTransaction();
List<Device> results = deviceDAO.getDevicesByEncryptionStatus(pr, TestDataHolder.SUPER_TENANT_ID, false);
Assert.assertNotNull(results, "No device returned");
DeviceManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = "testAddDeviceTest")
public void getCountOfDevicesByEncryptionStatus() throws DeviceManagementDAOException, TransactionManagementException {
try {
DeviceManagementDAOFactory.beginTransaction();
int results = deviceDAO.getCountOfDevicesByEncryptionStatus(TestDataHolder.SUPER_TENANT_ID, true);
Assert.assertEquals(0, results, "No device returned");
DeviceManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
}

@ -18,6 +18,8 @@
package io.entgra.device.mgt.core.device.mgt.core.dao;
import io.entgra.device.mgt.core.device.mgt.common.EnrolmentInfo;
import io.entgra.device.mgt.core.device.mgt.common.PaginationRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.testng.Assert;
@ -54,19 +56,18 @@ public class GroupPersistTests extends BaseDeviceManagementTest {
GroupManagementDAOFactory.beginTransaction();
groupId = groupDAO.addGroup(deviceGroup, TestDataHolder.SUPER_TENANT_ID);
GroupManagementDAOFactory.commitTransaction();
GroupManagementDAOFactory.closeConnection();
log.debug("Group added to database. ID: " + groupId);
} catch (GroupManagementDAOException e) {
GroupManagementDAOFactory.rollbackTransaction();
GroupManagementDAOFactory.closeConnection();
String msg = "Error occurred while adding device type '" + deviceGroup.getName() + "'.";
log.error(msg, e);
Assert.fail(msg, e);
} catch (TransactionManagementException e) {
GroupManagementDAOFactory.closeConnection();
String msg = "Error occurred while initiating transaction.";
log.error(msg, e);
Assert.fail(msg, e);
} finally {
GroupManagementDAOFactory.closeConnection();
}
DeviceGroup group = getGroupById(groupId);
if (!isMock()) {
@ -83,22 +84,21 @@ public class GroupPersistTests extends BaseDeviceManagementTest {
request.setGroupName(null);
request.setOwner(null);
List<DeviceGroup> groups = groupDAO.getGroups(request, TestDataHolder.SUPER_TENANT_ID);
GroupManagementDAOFactory.closeConnection();
if (!isMock()) {
Assert.assertNotEquals(groups.size(), 0, "No groups found");
Assert.assertNotNull(groups.get(0), "Group is null");
log.debug("No of Groups found: " + groups.size());
}
} catch (GroupManagementDAOException e) {
GroupManagementDAOFactory.closeConnection();
String msg = "Error occurred while find group by name.";
log.error(msg, e);
Assert.fail(msg, e);
} catch (SQLException e) {
GroupManagementDAOFactory.closeConnection();
String msg = "Error occurred while opening a connection to the data source.";
log.error(msg, e);
Assert.fail(msg, e);
} finally {
GroupManagementDAOFactory.closeConnection();
}
}
@ -114,21 +114,20 @@ public class GroupPersistTests extends BaseDeviceManagementTest {
}
GroupManagementDAOFactory.commitTransaction();
List<String> roles = groupDAO.getRoles(groupId, TestDataHolder.SUPER_TENANT_ID);
GroupManagementDAOFactory.closeConnection();
if (!isMock()) {
Assert.assertEquals(roles, addedRoles, "Added roles are not equal to returned roles.");
}
log.debug("Group shared with roles.");
} catch (GroupManagementDAOException e) {
GroupManagementDAOFactory.closeConnection();
String msg = "Error occurred while find group by name.";
log.error(msg, e);
Assert.fail(msg, e);
} catch (TransactionManagementException e) {
GroupManagementDAOFactory.closeConnection();
String msg = "Error occurred while opening a connection to the data source.";
log.error(msg, e);
Assert.fail(msg, e);
} finally {
GroupManagementDAOFactory.closeConnection();
}
}
@ -141,22 +140,21 @@ public class GroupPersistTests extends BaseDeviceManagementTest {
roles.remove(0);
}
List<DeviceGroup> deviceGroups = groupDAO.getGroups(roles.toArray(new String[roles.size()]), TestDataHolder.SUPER_TENANT_ID);
GroupManagementDAOFactory.closeConnection();
if (!isMock()) {
Assert.assertEquals(deviceGroups.size(), 1, "Unexpected number of device groups found with role.");
Assert.assertEquals(deviceGroups.get(0).getGroupId(), groupId, "Unexpected groupId found with role.");
}
log.debug("Group found for given roles.");
} catch (GroupManagementDAOException e) {
GroupManagementDAOFactory.closeConnection();
String msg = "Error occurred while getting groups shared with roles.";
log.error(msg, e);
Assert.fail(msg, e);
} catch (SQLException e) {
GroupManagementDAOFactory.closeConnection();
String msg = "Error occurred while opening a connection to the data source.";
log.error(msg, e);
Assert.fail(msg, e);
} finally {
GroupManagementDAOFactory.closeConnection();
}
}
@ -271,19 +269,18 @@ public class GroupPersistTests extends BaseDeviceManagementTest {
GroupManagementDAOFactory.beginTransaction();
groupDAO.updateGroup(group, groupId, TestDataHolder.SUPER_TENANT_ID);
GroupManagementDAOFactory.commitTransaction();
GroupManagementDAOFactory.closeConnection();
log.debug("Group updated");
} catch (GroupManagementDAOException e) {
GroupManagementDAOFactory.rollbackTransaction();
GroupManagementDAOFactory.closeConnection();
String msg = "Error occurred while updating group details.";
log.error(msg, e);
Assert.fail(msg, e);
} catch (TransactionManagementException e) {
GroupManagementDAOFactory.closeConnection();
String msg = "Error occurred while initiating transaction.";
log.error(msg, e);
Assert.fail(msg, e);
} finally {
GroupManagementDAOFactory.closeConnection();
}
if (!isMock()) {
group = getGroupById(groupId);
@ -301,20 +298,20 @@ public class GroupPersistTests extends BaseDeviceManagementTest {
GroupManagementDAOFactory.beginTransaction();
groupDAO.deleteGroup(group.getGroupId(), TestDataHolder.SUPER_TENANT_ID);
GroupManagementDAOFactory.commitTransaction();
GroupManagementDAOFactory.closeConnection();
log.debug("Group deleted");
} catch (GroupManagementDAOException e) {
GroupManagementDAOFactory.rollbackTransaction();
GroupManagementDAOFactory.closeConnection();
String msg = "Error occurred while updating group details.";
log.error(msg, e);
Assert.fail(msg, e);
} catch (TransactionManagementException e) {
GroupManagementDAOFactory.closeConnection();
String msg = "Error occurred while initiating transaction.";
log.error(msg, e);
Assert.fail(msg, e);
}
finally {
GroupManagementDAOFactory.closeConnection();
}
group = getGroupById(groupId);
if (!isMock()) {
Assert.assertNull(group, "Group is not deleted");
@ -325,23 +322,290 @@ public class GroupPersistTests extends BaseDeviceManagementTest {
try {
GroupManagementDAOFactory.openConnection();
DeviceGroup deviceGroup = groupDAO.getGroup(groupId, TestDataHolder.SUPER_TENANT_ID);
GroupManagementDAOFactory.closeConnection();
if (deviceGroup == null && isMock()) {
deviceGroup = new DeviceGroup();
deviceGroup.setGroupId(groupId);
}
return deviceGroup;
} catch (GroupManagementDAOException e) {
GroupManagementDAOFactory.closeConnection();
String msg = "Error occurred while retrieving group details.";
log.error(msg, e);
Assert.fail(msg, e);
} catch (SQLException e) {
GroupManagementDAOFactory.closeConnection();
String msg = "Error occurred while opening a connection to the data source.";
log.error(msg, e);
Assert.fail(msg, e);
} finally {
GroupManagementDAOFactory.closeConnection();
}
return null;
}
@Test(dependsOnMethods = {"addDeviceToGroupTest"})
public void getAllDevicesOfGroupWithStatus() {
DeviceGroup deviceGroup = getGroupById(groupId);
Assert.assertNotNull(deviceGroup, "Group is null");
List<String> deviceStatus = new ArrayList<>();
deviceStatus.add(EnrolmentInfo.Status.ACTIVE.name());
try {
GroupManagementDAOFactory.beginTransaction();
groupDAO.getAllDevicesOfGroup(deviceGroup.getName(), deviceStatus, TestDataHolder.SUPER_TENANT_ID);
GroupManagementDAOFactory.commitTransaction();
} catch (GroupManagementDAOException e) {
GroupManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while getting devices of group '" + groupId + "'.";
log.error(msg, e);
Assert.fail(msg, e);
} catch (TransactionManagementException e) {
String msg = "Error occurred while initiating transaction.";
log.error(msg, e);
Assert.fail(msg, e);
} finally {
GroupManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = {"addDeviceToGroupTest"})
public void getAllDevicesOfGroup() {
DeviceGroup deviceGroup = getGroupById(groupId);
Assert.assertNotNull(deviceGroup, "Group is null");
try {
GroupManagementDAOFactory.beginTransaction();
groupDAO.getAllDevicesOfGroup(deviceGroup.getName(), TestDataHolder.SUPER_TENANT_ID);
GroupManagementDAOFactory.commitTransaction();
} catch (GroupManagementDAOException e) {
GroupManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while getting devices of group '" + groupId + "'.";
log.error(msg, e);
Assert.fail(msg, e);
} catch (TransactionManagementException e) {
String msg = "Error occurred while initiating transaction.";
log.error(msg, e);
Assert.fail(msg, e);
} finally {
GroupManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = {"addDeviceToGroupTest"})
public void getGroupUnassignedDevices() {
DeviceGroup deviceGroup = getGroupById(groupId);
Device device = TestDataHolder.initialTestDevice;
Assert.assertNotNull(deviceGroup, "Group is null");
PaginationRequest pr = new PaginationRequest(0,10);
pr.setDeviceType(device.getType());
List<String> groupNames = new ArrayList<>();
groupNames.add(deviceGroup.getName());
try {
GroupManagementDAOFactory.beginTransaction();
groupDAO.getGroupUnassignedDevices(pr, groupNames);
GroupManagementDAOFactory.commitTransaction();
} catch (GroupManagementDAOException e) {
GroupManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while getting devices of group '" + groupId + "'.";
log.error(msg, e);
Assert.fail(msg, e);
} catch (TransactionManagementException e) {
String msg = "Error occurred while initiating transaction.";
log.error(msg, e);
Assert.fail(msg, e);
} finally {
GroupManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = {"addDeviceToGroupTest"})
public void getOwnGroupsCount() {
try {
GroupManagementDAOFactory.beginTransaction();
groupDAO.getOwnGroupsCount(TestDataHolder.OWNER, TestDataHolder.SUPER_TENANT_ID, "/");
GroupManagementDAOFactory.commitTransaction();
} catch (GroupManagementDAOException e) {
GroupManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while getting own group count for '" + TestDataHolder.OWNER + "'.";
log.error(msg, e);
Assert.fail(msg, e);
} catch (TransactionManagementException e) {
String msg = "Error occurred while initiating transaction.";
log.error(msg, e);
Assert.fail(msg, e);
} finally {
GroupManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = {"addDeviceToGroupTest"})
public void getOwnGroups() {
try {
GroupManagementDAOFactory.beginTransaction();
groupDAO.getOwnGroups(TestDataHolder.OWNER, TestDataHolder.SUPER_TENANT_ID);
GroupManagementDAOFactory.commitTransaction();
} catch (GroupManagementDAOException e) {
GroupManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while getting own groups for '" + TestDataHolder.OWNER + "'.";
log.error(msg, e);
Assert.fail(msg, e);
} catch (TransactionManagementException e) {
String msg = "Error occurred while initiating transaction.";
log.error(msg, e);
Assert.fail(msg, e);
} finally {
GroupManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = {"addDeviceToGroupTest"})
public void getOwnGroupIds() {
try {
GroupManagementDAOFactory.beginTransaction();
groupDAO.getOwnGroupIds(TestDataHolder.OWNER, TestDataHolder.SUPER_TENANT_ID);
GroupManagementDAOFactory.commitTransaction();
} catch (GroupManagementDAOException e) {
GroupManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while getting own group Ids for '" + TestDataHolder.OWNER + "'.";
log.error(msg, e);
Assert.fail(msg, e);
} catch (TransactionManagementException e) {
String msg = "Error occurred while initiating transaction.";
log.error(msg, e);
Assert.fail(msg, e);
} finally {
GroupManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = {"addDeviceToGroupTest"})
public void getDeviceCount() {
DeviceGroup deviceGroup = getGroupById(groupId);
Assert.assertNotNull(deviceGroup, "Group is null");
try {
GroupManagementDAOFactory.beginTransaction();
groupDAO.getDeviceCount(deviceGroup.getGroupId(), TestDataHolder.SUPER_TENANT_ID);
GroupManagementDAOFactory.commitTransaction();
} catch (GroupManagementDAOException e) {
GroupManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while getting device count for '" +groupId+ "'.";
log.error(msg, e);
Assert.fail(msg, e);
} catch (TransactionManagementException e) {
String msg = "Error occurred while initiating transaction.";
log.error(msg, e);
Assert.fail(msg, e);
} finally {
GroupManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = {"addDeviceToGroupTest"})
public void isDeviceMappedToGroup() {
DeviceGroup deviceGroup = getGroupById(groupId);
Device device = TestDataHolder.initialTestDevice;
Assert.assertNotNull(deviceGroup, "Group is null");
try {
GroupManagementDAOFactory.beginTransaction();
groupDAO.isDeviceMappedToGroup(deviceGroup.getGroupId(), Integer.parseInt(device.getDeviceIdentifier()), TestDataHolder.SUPER_TENANT_ID);
GroupManagementDAOFactory.commitTransaction();
} catch (GroupManagementDAOException e) {
GroupManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while checking device map to group for '" +groupId+ "'.";
log.error(msg, e);
Assert.fail(msg, e);
} catch (TransactionManagementException e) {
String msg = "Error occurred while initiating transaction.";
log.error(msg, e);
Assert.fail(msg, e);
} finally {
GroupManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = {"addDeviceToGroupTest"})
public void getGroupCount() {
DeviceGroup deviceGroup = getGroupById(groupId);
GroupPaginationRequest pr = new GroupPaginationRequest(0,10);
pr.setGroupName(deviceGroup.getName());
Assert.assertNotNull(deviceGroup, "Group is null");
try {
GroupManagementDAOFactory.beginTransaction();
groupDAO.getGroupCount(pr, TestDataHolder.SUPER_TENANT_ID);
GroupManagementDAOFactory.commitTransaction();
} catch (GroupManagementDAOException e) {
GroupManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while getting group count for '" +TestDataHolder.SUPER_TENANT_ID+ "'.";
log.error(msg, e);
Assert.fail(msg, e);
} catch (TransactionManagementException e) {
String msg = "Error occurred while initiating transaction.";
log.error(msg, e);
Assert.fail(msg, e);
} finally {
GroupManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = {"addDeviceToGroupTest"})
public void getGroupCountWithStatus() {
DeviceGroup deviceGroup = getGroupById(groupId);
Assert.assertNotNull(deviceGroup, "Group is null");
try {
GroupManagementDAOFactory.beginTransaction();
groupDAO.getGroupCount(TestDataHolder.SUPER_TENANT_ID, EnrolmentInfo.Status.ACTIVE.name());
GroupManagementDAOFactory.commitTransaction();
} catch (GroupManagementDAOException e) {
GroupManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while getting group count for" + TestDataHolder.SUPER_TENANT_ID;
log.error(msg, e);
Assert.fail(msg, e);
} catch (TransactionManagementException e) {
String msg = "Error occurred while initiating transaction.";
log.error(msg, e);
Assert.fail(msg, e);
} finally {
GroupManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = {"addDeviceToGroupTest"})
public void getRootGroups() {
DeviceGroup deviceGroup = getGroupById(groupId);
Assert.assertNotNull(deviceGroup, "Group is null");
try {
GroupManagementDAOFactory.beginTransaction();
groupDAO.getRootGroups(TestDataHolder.SUPER_TENANT_ID);
GroupManagementDAOFactory.commitTransaction();
} catch (GroupManagementDAOException e) {
GroupManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while getting group count for " + TestDataHolder.SUPER_TENANT_ID;
log.error(msg, e);
Assert.fail(msg, e);
} catch (TransactionManagementException e) {
String msg = "Error occurred while initiating transaction.";
log.error(msg, e);
Assert.fail(msg, e);
} finally {
GroupManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = {"addDeviceToGroupTest"})
public void getAllGroupProperties() {
DeviceGroup deviceGroup = getGroupById(groupId);
Assert.assertNotNull(deviceGroup, "Group is null");
try {
GroupManagementDAOFactory.beginTransaction();
groupDAO.getAllGroupProperties(groupId, TestDataHolder.SUPER_TENANT_ID);
GroupManagementDAOFactory.commitTransaction();
} catch (GroupManagementDAOException e) {
GroupManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while getting groups for " + TestDataHolder.SUPER_TENANT_ID;
log.error(msg, e);
Assert.fail(msg, e);
} catch (TransactionManagementException e) {
String msg = "Error occurred while initiating transaction.";
log.error(msg, e);
Assert.fail(msg, e);
} finally {
GroupManagementDAOFactory.closeConnection();
}
}
}

@ -32,4 +32,10 @@
<Username>admin</Username>
<Password>admin</Password>
</AdminUser>
<ValidationConfig>
<!-- Enable/Disable data source query validation. -->
<DSQueryValidation>true</DSQueryValidation>
<!-- Enable/Disable dashboard integration. -->
<DashboardIntegration>false</DashboardIntegration>
</ValidationConfig>
</GrafanaConfiguration>

@ -32,4 +32,10 @@
<Username>admin</Username>
<Password>admin</Password>
</AdminUser>
<ValidationConfig>
<!-- Enable/Disable data source query validation. -->
<DSQueryValidation>true</DSQueryValidation>
<!-- Enable/Disable dashboard integration. -->
<DashboardIntegration>false</DashboardIntegration>
</ValidationConfig>
</GrafanaConfiguration>

Loading…
Cancel
Save