@Test
  public void testAddApplication() {
    /* Adding dummy application to the application store */
    String testAppIdentifier = "test sample1";
    try {
      DeviceManagementDAOFactory.openConnection();
      applicationDAO.addApplication(
          TestDataHolder.generateApplicationDummyData(testAppIdentifier), -1234);
    } catch (DeviceManagementDAOException | SQLException e) {
      log.error("Error occurred while adding application test sample1", e);
    } finally {
      DeviceManagementDAOFactory.closeConnection();
    }
    /* Retrieving the application by its name */
    Application target = null;
    try {
      target = this.getApplication(testAppIdentifier, -1234);
    } catch (DeviceManagementDAOException e) {
      String msg = "Error occurred while retrieving application info";
      log.error(msg, e);
      Assert.fail(msg, e);
    }

    Assert.assertEquals(
        target.getApplicationIdentifier(),
        testAppIdentifier,
        "Application added is not as same as " + "what's " + "retrieved");
  }
  @Override
  public void updateApplicationListInstalledInDevice(
      DeviceIdentifier deviceIdentifier, List<Application> applications)
      throws ApplicationManagementException {
    List<Application> installedAppList = getApplicationListForDevice(deviceIdentifier);
    try {
      int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
      DeviceManagementDAOFactory.beginTransaction();
      Device device = deviceDAO.getDevice(deviceIdentifier, tenantId);

      if (log.isDebugEnabled()) {
        log.debug("Device:" + device.getId() + ":identifier:" + deviceIdentifier.getId());
      }

      if (log.isDebugEnabled()) {
        log.debug("num of apps installed:" + installedAppList.size());
      }
      List<Application> appsToAdd = new ArrayList<>();
      List<Integer> appIdsToRemove = new ArrayList<>(installedAppList.size());

      for (Application installedApp : installedAppList) {
        if (!applications.contains(installedApp)) {
          if (log.isDebugEnabled()) {
            log.debug("Remove app Id:" + installedApp.getId());
          }
          appIdsToRemove.add(installedApp.getId());
        }
      }
      applicationMappingDAO.removeApplicationMapping(device.getId(), appIdsToRemove, tenantId);
      Application installedApp;
      List<Integer> applicationIds = new ArrayList<>();

      for (Application application : applications) {
        if (!installedAppList.contains(application)) {
          installedApp =
              applicationDAO.getApplication(
                  application.getApplicationIdentifier(), application.getVersion(), tenantId);
          if (installedApp == null) {
            appsToAdd.add(application);
          } else {
            applicationIds.add(installedApp.getId());
          }
        }
      }
      if (log.isDebugEnabled()) {
        log.debug("num of apps add:" + appsToAdd.size());
      }
      applicationIds.addAll(applicationDAO.addApplications(appsToAdd, tenantId));

      if (log.isDebugEnabled()) {
        log.debug("num of app Ids:" + applicationIds.size());
      }
      applicationMappingDAO.addApplicationMappings(device.getId(), applicationIds, tenantId);

      if (log.isDebugEnabled()) {
        log.debug("num of remove app Ids:" + appIdsToRemove.size());
      }

      DeviceManagementDAOFactory.commitTransaction();
    } catch (DeviceManagementDAOException e) {
      DeviceManagementDAOFactory.rollbackTransaction();
      throw new ApplicationManagementException(
          "Error occurred saving application list to the device", e);
    } catch (TransactionManagementException e) {
      throw new ApplicationManagementException("Error occurred while initializing transaction", e);
    } finally {
      DeviceManagementDAOFactory.closeConnection();
    }
  }