### Question:
JpaRatingPersistenceService implements RatingPersistenceService { @Override public void mergeRatingsForContributors(String contributorIdFrom, String contributorIdTo) { if (contributorIdFrom == null || contributorIdTo == null) { return; } List<String> contentIds = em.createQuery("select r.contentId from Rating r where r.contributorId = ?1",String.class) .setParameter(1, contributorIdTo) .setLockMode(LockModeType.PESSIMISTIC_WRITE) .getResultList(); if(contentIds==null || contentIds.size()==0) { return; } em.createQuery( "update Rating r set r.contributorId = ?1 where r.contributorId = ?2 and r.contentId not in (?3)") .setParameter(1, contributorIdTo).setParameter(2, contributorIdFrom).setParameter(3, contentIds) .executeUpdate(); em.createQuery("delete from Rating r where r.contributorId = ?1").setParameter(1, contributorIdFrom) .executeUpdate(); }  @Override List<Rating> getRatings(String contributorId, String... contentId); @Override void rate(String contributorId, String contentId, int rating); @Override RatingStats countRatingStats(String contentId); @Override void mergeRatingsForContributors(String contributorIdFrom, String contributorIdTo); @Override void deleteRatingsForContributor(String contributorId); @Override void deleteRatingsForContent(String... contentId);  }### Answer:
@Test public void mergeRatingsForContributors() { JpaRatingPersistenceService tested = getTested(); em.getTransaction().begin(); tested.rate(CONTRIB_ID_1, CONTENT_ID_1, 1); tested.rate(CONTRIB_ID_1, CONTENT_ID_3, 4); tested.rate(CONTRIB_ID_2, CONTENT_ID_1, 5); tested.rate(CONTRIB_ID_2, CONTENT_ID_2, 3); tested.rate(CONTRIB_ID_3, CONTENT_ID_1, 2); tested.rate(CONTRIB_ID_3, CONTENT_ID_2, 2); em.getTransaction().commit(); try { em.getTransaction().begin(); tested.mergeRatingsForContributors(CONTRIB_ID_4, CONTRIB_ID_5); } finally { em.getTransaction().commit(); } em.getTransaction().begin(); tested.mergeRatingsForContributors(CONTRIB_ID_1, CONTRIB_ID_2); em.getTransaction().commit(); em.getTransaction().begin(); Assert.assertEquals(0,tested.getRatings(CONTRIB_ID_4, CONTENT_ID_1, CONTENT_ID_2, CONTENT_ID_3).size()); Assert.assertEquals(0,tested.getRatings(CONTRIB_ID_5, CONTENT_ID_1, CONTENT_ID_2, CONTENT_ID_3).size()); Assert.assertEquals(0, tested.getRatings(CONTRIB_ID_1, CONTENT_ID_1, CONTENT_ID_2, CONTENT_ID_3).size()); Assert.assertEquals(3, tested.getRatings(CONTRIB_ID_2, CONTENT_ID_1, CONTENT_ID_2, CONTENT_ID_3).size()); Assert.assertEquals(5, tested.getRatings(CONTRIB_ID_2, CONTENT_ID_1).get(0).getRating()); Assert.assertEquals(3, tested.getRatings(CONTRIB_ID_2, CONTENT_ID_2).get(0).getRating()); Assert.assertEquals(4, tested.getRatings(CONTRIB_ID_2, CONTENT_ID_3).get(0).getRating()); Assert.assertEquals(2, tested.getRatings(CONTRIB_ID_3, CONTENT_ID_1, CONTENT_ID_2, CONTENT_ID_3).size()); em.getTransaction().commit(); }