AccessKeyService { @Transactional public AccessKey create(String appId, AccessKey entity) { long count = accessKeyRepository.countByAppId(appId); if (count >= ACCESSKEY_COUNT_LIMIT) { throw new BadRequestException("AccessKeys count limit exceeded"); } entity.setId(0L); entity.setAppId(appId); entity.setDataChangeLastModifiedBy(entity.getDataChangeCreatedBy()); AccessKey accessKey = accessKeyRepository.save(entity); auditService.audit(AccessKey.class.getSimpleName(), accessKey.getId(), Audit.OP.INSERT, accessKey.getDataChangeCreatedBy()); return accessKey; } AccessKeyService(
      AccessKeyRepository accessKeyRepository,
      AuditService auditService); List<AccessKey> findByAppId(String appId); @Transactional AccessKey create(String appId, AccessKey entity); @Transactional AccessKey update(String appId, AccessKey entity); @Transactional void delete(String appId, long id, String operator);  }
@Test public void testCreate() { String appId = "someAppId"; String secret = "someSecret"; AccessKey entity = assembleAccessKey(appId, secret); AccessKey accessKey = accessKeyService.create(appId, entity); assertNotNull(accessKey); }
@Test(expected = BadRequestException.class) public void testCreateWithException() { String appId = "someAppId"; String secret = "someSecret"; int maxCount = 5; for (int i = 0; i <= maxCount; i++) { AccessKey entity = assembleAccessKey(appId, secret); accessKeyService.create(appId, entity); } }