@Test public void storeCertificateBlankValuesTest() throws Exception { CustomCertificateRepository customCertificateRepository = new MockCustomCertificateRepository(); DefaultCustomCertificateAccessor customCertificateAccessor = new DefaultCustomCertificateAccessor(customCertificateRepository); try { CustomCertificateModel certificateModel = new CustomCertificateModel("", content, testDate.toString()); customCertificateAccessor.storeCertificate(certificateModel); fail(); } catch (AlertDatabaseConstraintException e) { assertNotNull(e); } try { CustomCertificateModel certificateModel = new CustomCertificateModel(alias, "", testDate.toString()); customCertificateAccessor.storeCertificate(certificateModel); fail(); } catch (AlertDatabaseConstraintException e) { assertNotNull(e); } }
@Override public CustomCertificateModel storeCertificate(CustomCertificateModel certificateModel) throws AlertDatabaseConstraintException { if (null == certificateModel) { throw new AlertDatabaseConstraintException("The certificate model cannot be null"); } String alias = certificateModel.getAlias(); if (StringUtils.isBlank(alias)) { throw new AlertDatabaseConstraintException("The field 'alias' cannot be blank"); } String certificateContent = certificateModel.getCertificateContent(); if (StringUtils.isBlank(certificateContent)) { throw new AlertDatabaseConstraintException("The field 'certificateContent' cannot be blank"); } CustomCertificateEntity entityToSave = new CustomCertificateEntity(alias, certificateContent, DateUtils.createCurrentDateTimestamp()); Long id = certificateModel.getNullableId(); if (null == id) { id = customCertificateRepository.findByAlias(alias).map(CustomCertificateEntity::getId).orElse(null); } else { if (!customCertificateRepository.existsById(id)) { throw new AlertDatabaseConstraintException("A custom certificate with that id did not exist"); } } entityToSave.setId(id); CustomCertificateEntity updatedEntity = customCertificateRepository.save(entityToSave); return createModel(updatedEntity); }
DefaultCustomCertificateAccessor implements CustomCertificateAccessor { @Override public CustomCertificateModel storeCertificate(CustomCertificateModel certificateModel) throws AlertDatabaseConstraintException { if (null == certificateModel) { throw new AlertDatabaseConstraintException("The certificate model cannot be null"); } String alias = certificateModel.getAlias(); if (StringUtils.isBlank(alias)) { throw new AlertDatabaseConstraintException("The field 'alias' cannot be blank"); } String certificateContent = certificateModel.getCertificateContent(); if (StringUtils.isBlank(certificateContent)) { throw new AlertDatabaseConstraintException("The field 'certificateContent' cannot be blank"); } CustomCertificateEntity entityToSave = new CustomCertificateEntity(alias, certificateContent, DateUtils.createCurrentDateTimestamp()); Long id = certificateModel.getNullableId(); if (null == id) { id = customCertificateRepository.findByAlias(alias).map(CustomCertificateEntity::getId).orElse(null); } else { if (!customCertificateRepository.existsById(id)) { throw new AlertDatabaseConstraintException("A custom certificate with that id did not exist"); } } entityToSave.setId(id); CustomCertificateEntity updatedEntity = customCertificateRepository.save(entityToSave); return createModel(updatedEntity); } }
DefaultCustomCertificateAccessor implements CustomCertificateAccessor { @Override public CustomCertificateModel storeCertificate(CustomCertificateModel certificateModel) throws AlertDatabaseConstraintException { if (null == certificateModel) { throw new AlertDatabaseConstraintException("The certificate model cannot be null"); } String alias = certificateModel.getAlias(); if (StringUtils.isBlank(alias)) { throw new AlertDatabaseConstraintException("The field 'alias' cannot be blank"); } String certificateContent = certificateModel.getCertificateContent(); if (StringUtils.isBlank(certificateContent)) { throw new AlertDatabaseConstraintException("The field 'certificateContent' cannot be blank"); } CustomCertificateEntity entityToSave = new CustomCertificateEntity(alias, certificateContent, DateUtils.createCurrentDateTimestamp()); Long id = certificateModel.getNullableId(); if (null == id) { id = customCertificateRepository.findByAlias(alias).map(CustomCertificateEntity::getId).orElse(null); } else { if (!customCertificateRepository.existsById(id)) { throw new AlertDatabaseConstraintException("A custom certificate with that id did not exist"); } } entityToSave.setId(id); CustomCertificateEntity updatedEntity = customCertificateRepository.save(entityToSave); return createModel(updatedEntity); } @Autowired  DefaultCustomCertificateAccessor(CustomCertificateRepository customCertificateRepository); }
DefaultCustomCertificateAccessor implements CustomCertificateAccessor { @Override public CustomCertificateModel storeCertificate(CustomCertificateModel certificateModel) throws AlertDatabaseConstraintException { if (null == certificateModel) { throw new AlertDatabaseConstraintException("The certificate model cannot be null"); } String alias = certificateModel.getAlias(); if (StringUtils.isBlank(alias)) { throw new AlertDatabaseConstraintException("The field 'alias' cannot be blank"); } String certificateContent = certificateModel.getCertificateContent(); if (StringUtils.isBlank(certificateContent)) { throw new AlertDatabaseConstraintException("The field 'certificateContent' cannot be blank"); } CustomCertificateEntity entityToSave = new CustomCertificateEntity(alias, certificateContent, DateUtils.createCurrentDateTimestamp()); Long id = certificateModel.getNullableId(); if (null == id) { id = customCertificateRepository.findByAlias(alias).map(CustomCertificateEntity::getId).orElse(null); } else { if (!customCertificateRepository.existsById(id)) { throw new AlertDatabaseConstraintException("A custom certificate with that id did not exist"); } } entityToSave.setId(id); CustomCertificateEntity updatedEntity = customCertificateRepository.save(entityToSave); return createModel(updatedEntity); } @Autowired  DefaultCustomCertificateAccessor(CustomCertificateRepository customCertificateRepository); @Override List<CustomCertificateModel> getCertificates(); @Override Optional<CustomCertificateModel> getCertificate(Long id); @Override CustomCertificateModel storeCertificate(CustomCertificateModel certificateModel); @Override void deleteCertificate(String certificateAlias); @Override void deleteCertificate(Long certificateId); }
DefaultCustomCertificateAccessor implements CustomCertificateAccessor { @Override public CustomCertificateModel storeCertificate(CustomCertificateModel certificateModel) throws AlertDatabaseConstraintException { if (null == certificateModel) { throw new AlertDatabaseConstraintException("The certificate model cannot be null"); } String alias = certificateModel.getAlias(); if (StringUtils.isBlank(alias)) { throw new AlertDatabaseConstraintException("The field 'alias' cannot be blank"); } String certificateContent = certificateModel.getCertificateContent(); if (StringUtils.isBlank(certificateContent)) { throw new AlertDatabaseConstraintException("The field 'certificateContent' cannot be blank"); } CustomCertificateEntity entityToSave = new CustomCertificateEntity(alias, certificateContent, DateUtils.createCurrentDateTimestamp()); Long id = certificateModel.getNullableId(); if (null == id) { id = customCertificateRepository.findByAlias(alias).map(CustomCertificateEntity::getId).orElse(null); } else { if (!customCertificateRepository.existsById(id)) { throw new AlertDatabaseConstraintException("A custom certificate with that id did not exist"); } } entityToSave.setId(id); CustomCertificateEntity updatedEntity = customCertificateRepository.save(entityToSave); return createModel(updatedEntity); } @Autowired  DefaultCustomCertificateAccessor(CustomCertificateRepository customCertificateRepository); @Override List<CustomCertificateModel> getCertificates(); @Override Optional<CustomCertificateModel> getCertificate(Long id); @Override CustomCertificateModel storeCertificate(CustomCertificateModel certificateModel); @Override void deleteCertificate(String certificateAlias); @Override void deleteCertificate(Long certificateId);  }