KeyMapper { public Key convert(ApiKeyEntity apiKeyEntity) { final Key keyItem = new Key(); final String plan = apiKeyEntity.getPlan(); try { PlanEntity planEntity = planService.findById(plan); keyItem.setApi(planEntity.getApi()); } catch (PlanNotFoundException e) { LOGGER.warn("plan does not exist : {}", plan); } keyItem.setApplication(apiKeyEntity.getApplication()); keyItem.setCreatedAt(apiKeyEntity.getCreatedAt().toInstant().atOffset(ZoneOffset.UTC)); keyItem.setId(apiKeyEntity.getKey()); keyItem.setPaused(apiKeyEntity.isPaused()); keyItem.setPlan(plan); keyItem.setRevoked(apiKeyEntity.isRevoked()); if (apiKeyEntity.isRevoked()) { keyItem.setRevokedAt(apiKeyEntity.getRevokedAt().toInstant().atOffset(ZoneOffset.UTC)); } keyItem.setExpired(apiKeyEntity.isExpired()); if (apiKeyEntity.getExpireAt() != null) { keyItem.setExpireAt(apiKeyEntity.getExpireAt().toInstant().atOffset(ZoneOffset.UTC)); } return keyItem; }  Key convert(ApiKeyEntity apiKeyEntity);  }
@Test public void testConvert() { Key key = keyMapper.convert(apiKeyEntity); assertNotNull(key); assertEquals(API, key.getApi()); assertEquals(APPLICATION, key.getApplication()); assertEquals(now.toEpochMilli(), key.getCreatedAt().toInstant().toEpochMilli()); assertEquals(KEY, key.getId()); assertEquals(Boolean.FALSE, key.getPaused()); assertEquals(PLAN, key.getPlan()); assertEquals(Boolean.FALSE, key.getRevoked()); assertNull(key.getRevokedAt()); }
@Test public void testConvertWithoutPlan() { apiKeyEntity.setPlan(UNKNOWN_PLAN); Key key = keyMapper.convert(apiKeyEntity); assertNotNull(key); assertNull(key.getApi()); assertEquals(APPLICATION, key.getApplication()); assertEquals(now.toEpochMilli(), key.getCreatedAt().toInstant().toEpochMilli()); assertEquals(KEY, key.getId()); assertEquals(Boolean.FALSE, key.getPaused()); assertEquals(UNKNOWN_PLAN, key.getPlan()); assertEquals(Boolean.FALSE, key.getRevoked()); assertNull(key.getRevokedAt()); }