@Test
  public void findByPushApplicationID() {
    final String pushApplicationID1 = "888";

    assertThat(pushApplicationDao.findByPushApplicationID(pushApplicationID1)).isNotNull();
    assertThat(pushApplicationDao.findByPushApplicationID(pushApplicationID1).getName())
        .isEqualTo("Push App 1");
    assertThat(pushApplicationDao.findByPushApplicationID("13245632")).isNull();
  }
  @Test
  public void findAllIDsForDeveloper() {
    assertThat(pushApplicationDao.findAllPushApplicationIDsForDeveloper("Admin")).hasSize(2);
    assertThat(pushApplicationDao.findAllPushApplicationIDsForDeveloper("Dave The Drummer"))
        .hasSize(1);
    assertThat(pushApplicationDao.findAllPushApplicationIDsForDeveloper("Admin The Drummer"))
        .isEmpty();

    // check all:
    assertThat(pushApplicationDao.getNumberOfPushApplicationsForDeveloper()).isEqualTo(3);
    assertThat(pushApplicationDao.findAll(0, 10).getAggregate().getCount()).isEqualTo(3);
  }
  @Test
  public void updatePushApplication() {
    final String pushApplicationID1 = "888";

    final PushApplication pushApplication1 =
        pushApplicationDao.findByPushApplicationID(pushApplicationID1);
    assertThat(pushApplication1).isNotNull();
    assertThat(pushApplication1.getName()).isEqualTo("Push App 1");

    pushApplication1.setName("Cool Push App 1");
    pushApplicationDao.update(pushApplication1);

    assertThat(pushApplicationDao.findByPushApplicationID(pushApplicationID1)).isNotNull();
    assertThat(pushApplicationDao.findByPushApplicationID(pushApplicationID1).getName())
        .isEqualTo("Cool Push App 1");
  }
  @Test
  public void findByPushApplicationIDForDeveloper() {
    final String pushApplicationID1 = "888";

    assertThat(pushApplicationDao.findByPushApplicationIDForDeveloper(pushApplicationID1, "Admin"))
        .isNotNull();

    assertThat(
            pushApplicationDao
                .findByPushApplicationIDForDeveloper(pushApplicationID1, "Admin")
                .getName())
        .isEqualTo("Push App 1");

    assertThat(pushApplicationDao.findByPushApplicationIDForDeveloper("1234", "Admin")).isNull();
    assertThat(pushApplicationDao.findByPushApplicationIDForDeveloper(pushApplicationID1, "FooBar"))
        .isNull();
  }
  /**
   * Loads all the Variant objects where we did notice some failures on sending for the given user
   */
  @Override
  public List<ApplicationVariant> getVariantsWithWarnings() {
    final List<String> warningIDs = pushMessageInformationDao.findVariantIDsWithWarnings();
    if (warningIDs.isEmpty()) {
      return Collections.emptyList();
    }

    return wrapApplicationVariant(pushApplicationDao.findByVariantIds(warningIDs));
  }
  @Test
  public void shouldCountInstallations() {
    final Map<String, Long> result = pushApplicationDao.countInstallationsByType("888");

    assertThat(result).isNotEmpty();
    assertThat(result.get("1")).isEqualTo(2L);
    assertThat(result.get("3")).isEqualTo(1L);
    assertThat(result.get(VariantType.ANDROID.getTypeName())).isEqualTo(2L);
  }
  @Test
  public void primaryKeyUnmodifiedAfterUpdate() {
    final String id = "1";
    PushApplication pa = pushApplicationDao.find(id);

    assertThat(pa.getId()).isEqualTo(id);

    final PushApplication pushApplication1 =
        pushApplicationDao.findByPushApplicationID(pa.getPushApplicationID());
    pushApplication1.setName("Cool Push App 1");
    pushApplicationDao.update(pushApplication1);

    entityManager.flush();
    entityManager.clear();

    pa = pushApplicationDao.find(id);

    assertThat(pa.getName()).isEqualTo("Cool Push App 1");
  }
  @Test
  public void deletePushApplicationIncludingVariantAndInstallations() {
    final String id = "888";

    final PushApplication pa = pushApplicationDao.findByPushApplicationID(id);

    pushApplicationDao.delete(pa);
    // flush to be sure that it's in the database
    entityManager.flush();
    // clear the cache otherwise finding the entity will not perform a select but get the entity
    // from cache
    entityManager.clear();

    // Installation should be gone:
    assertThat(entityManager.find(Installation.class, "1")).isNull();

    // Variant should be gone:
    assertThat(entityManager.find(AndroidVariant.class, "1")).isNull();

    // PushApp should be gone:
    assertThat(pushApplicationDao.find(id)).isNull();
  }
  @Test
  public void shouldFindPushApplicationNameAndIDBasedOnVariantID() {

    // when
    final List<PushApplication> applications =
        pushApplicationDao.findByVariantIds(Arrays.asList("1"));

    // then
    assertThat(applications).isNotEmpty();
    assertThat(applications.size()).isEqualTo(1);

    final PushApplication application = applications.iterator().next();
    assertThat(application.getName()).isEqualTo("Push App 1");
    assertThat(application.getVariants()).isNotEmpty();
    assertThat(application.getVariants().size()).isEqualTo(1);
    assertThat(application.getVariants().iterator().next().getId()).isEqualTo("1");
  }
  @Test
  public void findAllForDeveloper() throws Exception {
    assertThat(pushApplicationDao.findAllForDeveloper("Admin", 0, 10).getResultList()).hasSize(2);
    assertThat(pushApplicationDao.findAllForDeveloper("Dave The Drummer", 0, 10).getResultList())
        .hasSize(1);
    assertThat(pushApplicationDao.findAllForDeveloper("Dave The Drummer", 0, 10).getResultList())
        .extracting("name")
        .containsOnly("Push App 3");
    assertThat(pushApplicationDao.findAllForDeveloper("Admin The Drummer", 0, 10).getResultList())
        .isEmpty();

    assertThat(pushApplicationDao.getNumberOfPushApplicationsForDeveloper("Dave The Drummer"))
        .isEqualTo(1);
    assertThat(pushApplicationDao.getNumberOfPushApplicationsForDeveloper("Admin")).isEqualTo(2);

    // check all:
    assertThat(pushApplicationDao.getNumberOfPushApplicationsForDeveloper()).isEqualTo(3);
    assertThat(pushApplicationDao.findAll(0, 10).getAggregate().getCount()).isEqualTo(3);

    // check exact:
    assertThat(pushApplicationDao.findAllByPushApplicationID("123").getName())
        .isEqualTo("Push App 2");
  }
 private List<Application> wrapApplication(List<PushMessageInformation> pushMessageInformations) {
   final List<Application> applications =
       new ArrayList<Application>(pushMessageInformations.size());
   for (PushMessageInformation pushMessageInformation : pushMessageInformations) {
     String applicationName =
         pushApplicationDao
             .findByPushApplicationID(pushMessageInformation.getPushApplicationId())
             .getName();
     final Application application =
         new Application(
             applicationName,
             pushMessageInformation.getPushApplicationId(),
             pushMessageInformation.getTotalReceivers(),
             pushMessageInformation.getSubmitDate());
     applications.add(application);
   }
   return applications;
 }
 @Override
 public PushApplication findByPushApplicationIDForDeveloper(String pushApplicationID) {
   return pushApplicationDao.findAllByPushApplicationID(pushApplicationID);
 }
 @Override
 public PageResult<PushApplication> findAllPushApplicationsForDeveloper(
     Integer page, Integer pageSize) {
   return pushApplicationDao.findAll(page, pageSize);
 }
 private long totalApplicationNumber() {
   return pushApplicationDao.getNumberOfPushApplicationsForDeveloper();
 }