RangerDataHistService { public void createObjectDataHistory(RangerBaseModelObject baseModelObj, String action) { if(baseModelObj == null || action == null) { throw restErrorUtil .createRESTException("Error while creating DataHistory. " + "Object or Action can not be null.", MessageEnums.DATA_NOT_FOUND); } Integer classType = null; String objectName = null; String content = null; Long objectId = baseModelObj.getId(); String objectGuid = baseModelObj.getGuid(); Date currentDate = DateUtil.getUTCDate(); XXDataHist xDataHist = new XXDataHist(); xDataHist.setObjectId(baseModelObj.getId()); xDataHist.setObjectGuid(objectGuid); xDataHist.setCreateTime(currentDate); xDataHist.setAction(action); xDataHist.setVersion(baseModelObj.getVersion()); xDataHist.setUpdateTime(currentDate); xDataHist.setFromTime(currentDate); if(baseModelObj instanceof RangerServiceDef) { RangerServiceDef serviceDef = (RangerServiceDef) baseModelObj; objectName = serviceDef.getName(); classType = AppConstants.CLASS_TYPE_XA_SERVICE_DEF; content = jsonUtil.writeObjectAsString(serviceDef); } else if(baseModelObj instanceof RangerService) { RangerService service = (RangerService) baseModelObj; objectName = service.getName(); classType = AppConstants.CLASS_TYPE_XA_SERVICE; content = jsonUtil.writeObjectAsString(service); } else if(baseModelObj instanceof RangerPolicy) { RangerPolicy policy = (RangerPolicy) baseModelObj; objectName = policy.getName(); classType = AppConstants.CLASS_TYPE_RANGER_POLICY; policy.setServiceType(policy.getServiceType()); content = jsonUtil.writeObjectAsString(policy); } xDataHist.setObjectClassType(classType); xDataHist.setObjectName(objectName); xDataHist.setContent(content); xDataHist = daoMgr.getXXDataHist().create(xDataHist); if (ACTION_UPDATE.equalsIgnoreCase(action) || ACTION_DELETE.equalsIgnoreCase(action)) { XXDataHist prevHist = daoMgr.getXXDataHist().findLatestByObjectClassTypeAndObjectId(classType, objectId); if(prevHist == null) { throw restErrorUtil.createRESTException( "Error updating DataHistory Object. ObjectName: " + objectName, MessageEnums.DATA_NOT_UPDATABLE); } prevHist.setUpdateTime(currentDate); prevHist.setToTime(currentDate); prevHist.setObjectName(objectName); prevHist = daoMgr.getXXDataHist().update(prevHist); } }  void createObjectDataHistory(RangerBaseModelObject baseModelObj, String action); static final String ACTION_CREATE; static final String ACTION_UPDATE; static final String ACTION_DELETE; }
@Test public void test1CreateObjectDataHistory() { String action = "create"; RangerBaseModelObject baseModelObj = new RangerBaseModelObject(); Mockito.when(daoMgr.getXXDataHist()).thenReturn(XXDataHistDao); rangerDataHistService.createObjectDataHistory(baseModelObj, action); }