### Question:
PushNotificationSubscriptionServiceImpl implements PushNotificationSubscriptionService { @Transactional @Nonnull @Override public PushNotificationSubscription createPushNotificationSubscription(@Nonnull final Long userId, @Nonnull final PushNotificationSubscriptionDto subscriptionDto) { assertUserIdNotNull(userId); Assert.notNull(subscriptionDto, "Subscription DTO should not be null"); final User user = userService.getUserById(userId); assertNoSubscriptionExistsForUser(user); PushNotificationSubscription subscription = new PushNotificationSubscription(); subscriptionDto.updateDomainEntityProperties(subscription); subscription.setUser(user); subscription = pushNotificationSubscriptionRepository.save(subscription); LOGGER.debug("Successfully created push notification subscription with id - {}, subscription - {}", subscription.getId(), subscription); return subscription; } PushNotificationSubscriptionServiceImpl(); @Nonnull @Override boolean checkIfPushNotificationSubscriptionExistsForUser(@Nonnull final Long userId); @Nonnull @Override PushNotificationSubscription getPushNotificationSubscriptionForUser(@Nonnull final Long userId); @Transactional @Nonnull @Override PushNotificationSubscription createPushNotificationSubscription(@Nonnull final Long userId, @Nonnull final PushNotificationSubscriptionDto subscriptionDto); @Nonnull @Override PushNotificationSubscription getPushNotificationSubscriptionById(@Nonnull final Long subscriptionId); PushNotificationSubscriptionRepository getPushNotificationSubscriptionRepository(); void setPushNotificationSubscriptionRepository(final PushNotificationSubscriptionRepository pushNotificationSubscriptionRepository); UserService getUserService(); void setUserService(final UserService userService);  }### Answer:
@Test public void testCreatePushNotificationSubscriptionWithInvalidArguments() { final Long userId = 1L; final PushNotificationSubscriptionDto subscriptionDto = getServicesImplTestHelper().createPushNotificationSubscriptionDto(); resetAll(); replayAll(); try { pushNotificationSubscriptionService.createPushNotificationSubscription(null, subscriptionDto); fail("Exception should be thrown"); } catch (final IllegalArgumentException ex) { } try { pushNotificationSubscriptionService.createPushNotificationSubscription(userId, null); fail("Exception should be thrown"); } catch (final IllegalArgumentException ex) { } verifyAll(); }
@Test public void testCreatePushNotificationSubscriptionWhenUserAlreadyHasOne() { final Long userId = 1L; final User user = getServicesImplTestHelper().createUser(); user.setId(userId); final PushNotificationSubscriptionDto subscriptionDto = getServicesImplTestHelper().createPushNotificationSubscriptionDto(); final Long existingSubscriptionId = 2L; final PushNotificationSubscription existingSubscription = getServicesImplTestHelper().createPushNotificationSubscription(); existingSubscription.setId(existingSubscriptionId); resetAll(); expect(userService.getUserById(eq(userId))).andReturn(user).once(); expect(pushNotificationSubscriptionRepository.findByUser(eq(user))).andReturn(existingSubscription).once(); replayAll(); try { pushNotificationSubscriptionService.createPushNotificationSubscription(userId, subscriptionDto); fail("Exception should be thrown"); } catch (final PushNotificationSubscriptionAlreadyExistsForUserException ex) { assertPushNotificationSubscriptionAlreadyExistsForUserException(ex, userId, existingSubscriptionId); } verifyAll(); }
@Test public void testCreatePushNotificationSubscription() { final Long userId = 1L; final User user = getServicesImplTestHelper().createUser(); user.setId(userId); final PushNotificationSubscriptionDto subscriptionDto = getServicesImplTestHelper().createPushNotificationSubscriptionDto(); resetAll(); expect(userService.getUserById(eq(userId))).andReturn(user).once(); expect(pushNotificationSubscriptionRepository.findByUser(eq(user))).andReturn(null).once(); expect(pushNotificationSubscriptionRepository.save(isA(PushNotificationSubscription.class))).andAnswer(() -> (PushNotificationSubscription) getCurrentArguments()[0]).once(); replayAll(); final PushNotificationSubscription result = pushNotificationSubscriptionService.createPushNotificationSubscription(userId, subscriptionDto); getServicesImplTestHelper().assertPushNotificationSubscription(result); Assert.assertEquals(user, result.getUser()); verifyAll(); }