Merge branch 'master' into 'master'

Persisting profile operations as string values instead of Objects

See merge request entgra/carbon-device-mgt!30
4.x.x
Charitha Goonetilleke 6 years ago
commit 61e5cd4b53

@ -23,6 +23,8 @@ import java.util.Properties;
public class Operation implements Serializable { public class Operation implements Serializable {
private static final long serialVersionUID = -353614864628915060L;
public enum Type { public enum Type {
CONFIG, MESSAGE, INFO, COMMAND, PROFILE , POLICY CONFIG, MESSAGE, INFO, COMMAND, PROFILE , POLICY
} }

@ -53,7 +53,7 @@ public class ProfileOperationDAOImpl extends GenericOperationDAOImpl {
bao = new ByteArrayOutputStream(); bao = new ByteArrayOutputStream();
oos = new ObjectOutputStream(bao); oos = new ObjectOutputStream(bao);
oos.writeObject(operation); oos.writeObject(operation.getPayLoad());
stmt.setInt(1, operationId); stmt.setInt(1, operationId);
stmt.setBytes(2, bao.toByteArray()); stmt.setBytes(2, bao.toByteArray());
@ -91,7 +91,8 @@ public class ProfileOperationDAOImpl extends GenericOperationDAOImpl {
ObjectInputStream ois; ObjectInputStream ois;
try { try {
Connection conn = OperationManagementDAOFactory.getConnection(); Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "SELECT OPERATION_ID, ENABLED, OPERATION_DETAILS FROM DM_PROFILE_OPERATION WHERE OPERATION_ID=?"; String sql = "SELECT o.ID, po.ENABLED, po.OPERATION_DETAILS, o.CREATED_TIMESTAMP, o.OPERATION_CODE " +
"FROM DM_PROFILE_OPERATION po INNER JOIN DM_OPERATION o ON po.OPERATION_ID = O.ID WHERE po.OPERATION_ID=?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, id); stmt.setInt(1, id);
@ -99,9 +100,20 @@ public class ProfileOperationDAOImpl extends GenericOperationDAOImpl {
if (rs.next()) { if (rs.next()) {
byte[] operationDetails = rs.getBytes("OPERATION_DETAILS"); byte[] operationDetails = rs.getBytes("OPERATION_DETAILS");
int oppId = rs.getInt("ID");
bais = new ByteArrayInputStream(operationDetails); bais = new ByteArrayInputStream(operationDetails);
ois = new ObjectInputStream(bais); ois = new ObjectInputStream(bais);
profileOperation = (ProfileOperation) ois.readObject(); Object obj = ois.readObject();
if(obj instanceof String){
profileOperation = new ProfileOperation();
profileOperation.setCode(rs.getString("OPERATION_CODE"));
profileOperation.setId(oppId);
profileOperation.setCreatedTimeStamp(rs.getString("CREATED_TIMESTAMP"));
profileOperation.setId(oppId);
profileOperation.setPayLoad(obj);
} else {
profileOperation = (ProfileOperation) obj;
}
} }
} catch (IOException e) { } catch (IOException e) {
throw new OperationManagementDAOException("IO Error occurred while de serialize the profile " + throw new OperationManagementDAOException("IO Error occurred while de serialize the profile " +
@ -110,7 +122,7 @@ public class ProfileOperationDAOImpl extends GenericOperationDAOImpl {
throw new OperationManagementDAOException("Class not found error occurred while de serialize the " + throw new OperationManagementDAOException("Class not found error occurred while de serialize the " +
"profile operation object", e); "profile operation object", e);
} catch (SQLException e) { } catch (SQLException e) {
throw new OperationManagementDAOException("SQL Error occurred while retrieving the command " + throw new OperationManagementDAOException("SQL Error occurred while retrieving the profile " +
"operation object " + "available for the id '" + id, e); "operation object " + "available for the id '" + id, e);
} finally { } finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs); OperationManagementDAOUtil.cleanupResources(stmt, rs);
@ -120,7 +132,7 @@ public class ProfileOperationDAOImpl extends GenericOperationDAOImpl {
@Override @Override
public List<? extends Operation> getOperationsByDeviceAndStatus(int enrolmentId, public List<? extends Operation> getOperationsByDeviceAndStatus(int enrolmentId,
Operation.Status status) throws OperationManagementDAOException { Operation.Status status) throws OperationManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
ProfileOperation profileOperation; ProfileOperation profileOperation;
@ -132,10 +144,12 @@ public class ProfileOperationDAOImpl extends GenericOperationDAOImpl {
try { try {
Connection conn = OperationManagementDAOFactory.getConnection(); Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "Select po.OPERATION_ID, ENABLED, OPERATION_DETAILS from DM_PROFILE_OPERATION po " + String sql = "SELECT o.ID, po1.ENABLED, po1.STATUS, o.TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, " +
"INNER JOIN " + "o.OPERATION_CODE, po1.OPERATION_DETAILS " +
"(Select * From DM_ENROLMENT_OP_MAPPING WHERE ENROLMENT_ID=? " + "FROM (SELECT po.OPERATION_ID, po.ENABLED, po.OPERATION_DETAILS, dm.STATUS " +
"AND STATUS=?) dm ON dm.OPERATION_ID = po.OPERATION_ID"; "FROM DM_PROFILE_OPERATION po INNER JOIN (SELECT ENROLMENT_ID, OPERATION_ID, STATUS FROM DM_ENROLMENT_OP_MAPPING " +
"WHERE ENROLMENT_ID = ? AND STATUS = ?) dm ON dm.OPERATION_ID = po.OPERATION_ID) po1 " +
"INNER JOIN DM_OPERATION o ON po1.OPERATION_ID = o.ID ";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, enrolmentId); stmt.setInt(1, enrolmentId);
@ -147,9 +161,20 @@ public class ProfileOperationDAOImpl extends GenericOperationDAOImpl {
byte[] operationDetails = rs.getBytes("OPERATION_DETAILS"); byte[] operationDetails = rs.getBytes("OPERATION_DETAILS");
bais = new ByteArrayInputStream(operationDetails); bais = new ByteArrayInputStream(operationDetails);
ois = new ObjectInputStream(bais); ois = new ObjectInputStream(bais);
profileOperation = (ProfileOperation) ois.readObject(); Object obj = ois.readObject();
profileOperation.setStatus(status); if(obj instanceof String){
operationList.add(profileOperation); profileOperation = new ProfileOperation();
profileOperation.setCode(rs.getString("OPERATION_CODE"));
profileOperation.setId(rs.getInt("ID"));
profileOperation.setCreatedTimeStamp(rs.getString("CREATED_TIMESTAMP"));
profileOperation.setPayLoad(obj);
profileOperation.setStatus(status);
operationList.add(profileOperation);
} else {
profileOperation = (ProfileOperation) obj;
profileOperation.setStatus(status);
operationList.add(profileOperation);
}
} }
} catch (IOException e) { } catch (IOException e) {

@ -67,6 +67,7 @@ public class OperationManagementTests extends BaseDeviceManagementTest {
private static final String POLICY_OPERATION_CODE = "POLICY-TEST"; private static final String POLICY_OPERATION_CODE = "POLICY-TEST";
private static final String CONFIG_OPERATION_CODE = "CONFIG-TEST"; private static final String CONFIG_OPERATION_CODE = "CONFIG-TEST";
private static final String PROFILE_OPERATION_CODE = "PROFILE-TEST"; private static final String PROFILE_OPERATION_CODE = "PROFILE-TEST";
private static final String PROFILE_NOTIFICATION_CODE = "NOTIFICATION";
private static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss"; private static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
private static final int NO_OF_DEVICES = 5; private static final int NO_OF_DEVICES = 5;
private static final String ADMIN_USER = "admin"; private static final String ADMIN_USER = "admin";
@ -101,14 +102,14 @@ public class OperationManagementTests extends BaseDeviceManagementTest {
} }
this.deviceMgmtProvider = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider(); this.deviceMgmtProvider = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider();
deviceManagementService = new TestDeviceManagementService(DEVICE_TYPE, deviceManagementService = new TestDeviceManagementService(DEVICE_TYPE,
MultitenantConstants.SUPER_TENANT_DOMAIN_NAME); MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
this.operationMgtService = PowerMockito.spy(new OperationManagerImpl(DEVICE_TYPE, deviceManagementService)); this.operationMgtService = PowerMockito.spy(new OperationManagerImpl(DEVICE_TYPE, deviceManagementService));
PowerMockito.when(this.operationMgtService, "getNotificationStrategy").thenReturn(new TestNotificationStrategy()); PowerMockito.when(this.operationMgtService, "getNotificationStrategy").thenReturn(new TestNotificationStrategy());
} }
@Test @Test
public void addCommandOperation() throws DeviceManagementException, OperationManagementException, public void addCommandOperation() throws DeviceManagementException, OperationManagementException,
InvalidDeviceException { InvalidDeviceException {
OperationManager operationManager = PowerMockito.spy( OperationManager operationManager = PowerMockito.spy(
new OperationManagerImpl(DEVICE_TYPE, deviceManagementService)); new OperationManagerImpl(DEVICE_TYPE, deviceManagementService));
try { try {
@ -154,7 +155,7 @@ public class OperationManagementTests extends BaseDeviceManagementTest {
public void addEmptyDevicesCommandOperation() throws DeviceManagementException, OperationManagementException, public void addEmptyDevicesCommandOperation() throws DeviceManagementException, OperationManagementException,
InvalidDeviceException { InvalidDeviceException {
this.operationMgtService.addOperation(getOperation(new CommandOperation(), Operation.Type.COMMAND, this.operationMgtService.addOperation(getOperation(new CommandOperation(), Operation.Type.COMMAND,
COMMAND_OPERATION_CODE), new ArrayList<>()); COMMAND_OPERATION_CODE), new ArrayList<>());
} }
@Test(expectedExceptions = InvalidDeviceException.class) @Test(expectedExceptions = InvalidDeviceException.class)
@ -205,8 +206,10 @@ public class OperationManagementTests extends BaseDeviceManagementTest {
@Test(dependsOnMethods = "addConfigOperation") @Test(dependsOnMethods = "addConfigOperation")
public void addProfileOperation() throws DeviceManagementException, OperationManagementException, public void addProfileOperation() throws DeviceManagementException, OperationManagementException,
InvalidDeviceException { InvalidDeviceException {
Activity activity = this.operationMgtService.addOperation(getOperation(new ProfileOperation(), Operation opp = getOperation(new ProfileOperation(),
Operation.Type.PROFILE, PROFILE_OPERATION_CODE), Operation.Type.PROFILE, PROFILE_NOTIFICATION_CODE);
opp.setPayLoad("{\"messageText\":\"xyz\",\"messageTitle\":\"abc\"}");
Activity activity = this.operationMgtService.addOperation(opp ,
this.deviceIds); this.deviceIds);
validateOperationResponse(activity, ActivityStatus.Status.PENDING); validateOperationResponse(activity, ActivityStatus.Status.PENDING);
} }
@ -531,7 +534,7 @@ public class OperationManagementTests extends BaseDeviceManagementTest {
expectedExceptions = OperationManagementException.class) expectedExceptions = OperationManagementException.class)
public void getUpdateOperationForInvalidDevice() throws DeviceManagementException, OperationManagementException { public void getUpdateOperationForInvalidDevice() throws DeviceManagementException, OperationManagementException {
this.operationMgtService.updateOperation(new DeviceIdentifier(INVALID_DEVICE, DEVICE_TYPE), this.operationMgtService.updateOperation(new DeviceIdentifier(INVALID_DEVICE, DEVICE_TYPE),
getOperation(new CommandOperation(), Operation.Type.COMMAND, COMMAND_OPERATION_CODE)); getOperation(new CommandOperation(), Operation.Type.COMMAND, COMMAND_OPERATION_CODE));
} }
@Test(dependsOnMethods = "getUpdateOperationForInvalidDevice", @Test(dependsOnMethods = "getUpdateOperationForInvalidDevice",

Loading…
Cancel
Save