SubscriptionsRepository { public void saveSubscription(Subscription subscription) throws SubscriptionPersistenceException { try { ObjectWriter writer = mapper.writer(); String susbcribeContextString = writer.writeValueAsString(subscription.getSubscribeContext()); String expirationDate = subscription.getExpirationDate().toString(); jdbcTemplate.update("insert into t_subscriptions(id,expirationDate,subscribeContext) values(?,?,?)", subscription.getSubscriptionId(), expirationDate, susbcribeContextString); } catch (Exception e) { throw new SubscriptionPersistenceException(e); } }  void saveSubscription(Subscription subscription); void updateSubscription(Subscription subscription); Map<String, Subscription> getAllSubscriptions(); void removeSubscription(String subscriptionId);  }
@Test public void saveSubscriptionTest() throws URISyntaxException, SubscriptionPersistenceException { SubscribeContext subscribeContext = createSubscribeContextTemperature(); Subscription subscription = new Subscription("12345", Instant.now().plus(1, ChronoUnit.DAYS), subscribeContext); subscriptionsRepository.saveSubscription(subscription); Map<String, Subscription> subscriptions = subscriptionsRepository.getAllSubscriptions(); Assert.assertEquals(1, subscriptions.size()); Assert.assertEquals(subscription.getSubscriptionId(), subscriptions.get("12345").getSubscriptionId()); Assert.assertEquals(subscription.getExpirationDate(), subscriptions.get("12345").getExpirationDate()); Assert.assertEquals(subscribeContext.getDuration(), subscriptions.get("12345").getSubscribeContext().getDuration()); }
@Test public void saveSubscriptionWithExceptionTest() throws URISyntaxException, SubscriptionPersistenceException { thrown.expect(SubscriptionPersistenceException.class); Subscription subscription = new Subscription(); subscription.setSubscribeContext(new SubscribeContext()); subscriptionsRepository.saveSubscription(subscription); }
@Test public void saveSubscriptionWithDuplicateKeyExceptionTest() throws URISyntaxException, SubscriptionPersistenceException { thrown.expect(SubscriptionPersistenceException.class); SubscribeContext subscribeContext = createSubscribeContextTemperature(); Subscription subscription = new Subscription("12345", Instant.now().plus(1, ChronoUnit.DAYS), subscribeContext); subscriptionsRepository.saveSubscription(subscription); subscriptionsRepository.saveSubscription(subscription); }