Search in sources :

Example 1 with StripeAgreementEntity

use of uk.gov.pay.adminusers.persistence.entity.StripeAgreementEntity in project pay-adminusers by alphagov.

the class StripeAgreementService method doCreate.

public void doCreate(String serviceExternalId, InetAddress ipAddress) {
    ServiceEntity serviceEntity = serviceDao.findByExternalId(serviceExternalId).orElseThrow(() -> new ServiceNotFoundException(serviceExternalId));
    if (stripeAgreementDao.findByServiceExternalId(serviceExternalId).isPresent()) {
        throw new StripeAgreementExistsException();
    }
    logger.info(format("Creating stripe agreement for service %s", serviceExternalId));
    ZonedDateTime agreementTime = ZonedDateTime.now(ZoneId.of("UTC"));
    stripeAgreementDao.persist(new StripeAgreementEntity(serviceEntity, ipAddress.getHostAddress(), agreementTime));
}
Also used : StripeAgreementExistsException(uk.gov.pay.adminusers.exception.StripeAgreementExistsException) ZonedDateTime(java.time.ZonedDateTime) ServiceNotFoundException(uk.gov.pay.adminusers.exception.ServiceNotFoundException) StripeAgreementEntity(uk.gov.pay.adminusers.persistence.entity.StripeAgreementEntity) ServiceEntity(uk.gov.pay.adminusers.persistence.entity.ServiceEntity)

Example 2 with StripeAgreementEntity

use of uk.gov.pay.adminusers.persistence.entity.StripeAgreementEntity in project pay-adminusers by alphagov.

the class StripeAgreementServiceTest method shouldReturnStripeAgreement.

@Test
public void shouldReturnStripeAgreement() {
    String serviceExternalId = "abc123";
    String ipAddress = "192.0.2.0";
    ZonedDateTime agreementTime = ZonedDateTime.now();
    ServiceEntity mockServiceEntity = mock(ServiceEntity.class);
    StripeAgreementEntity stripeAgreementEntity = new StripeAgreementEntity(mockServiceEntity, ipAddress, agreementTime);
    when(mockedStripeAgreementDao.findByServiceExternalId(serviceExternalId)).thenReturn(Optional.of(stripeAgreementEntity));
    Optional<StripeAgreement> maybeStripeAgreement = stripeAgreementService.findStripeAgreementByServiceId(serviceExternalId);
    assertTrue(maybeStripeAgreement.isPresent());
    assertThat(maybeStripeAgreement.get().getIpAddress().getHostAddress(), is(ipAddress));
    assertThat(maybeStripeAgreement.get().getAgreementTime(), is(agreementTime));
}
Also used : ZonedDateTime(java.time.ZonedDateTime) StripeAgreement(uk.gov.pay.adminusers.model.StripeAgreement) StripeAgreementEntity(uk.gov.pay.adminusers.persistence.entity.StripeAgreementEntity) ServiceEntity(uk.gov.pay.adminusers.persistence.entity.ServiceEntity) Test(org.junit.jupiter.api.Test)

Example 3 with StripeAgreementEntity

use of uk.gov.pay.adminusers.persistence.entity.StripeAgreementEntity in project pay-adminusers by alphagov.

the class StripeAgreementServiceTest method shouldThrowException_whenStripeAgreementAlreadyExists.

@Test
public void shouldThrowException_whenStripeAgreementAlreadyExists() throws UnknownHostException {
    String serviceExternalId = "abc123";
    ServiceEntity mockServiceEntity = mock(ServiceEntity.class);
    when(mockedServiceDao.findByExternalId(serviceExternalId)).thenReturn(Optional.of(mockServiceEntity));
    StripeAgreementEntity mockStripeAgreementEntity = mock(StripeAgreementEntity.class);
    when(mockedStripeAgreementDao.findByServiceExternalId(serviceExternalId)).thenReturn(Optional.of(mockStripeAgreementEntity));
    StripeAgreementExistsException exception = assertThrows(StripeAgreementExistsException.class, () -> stripeAgreementService.doCreate(serviceExternalId, InetAddress.getByName("192.0.2.0")));
    assertThat(exception.getMessage(), is("Stripe agreement information is already stored for this service"));
}
Also used : StripeAgreementExistsException(uk.gov.pay.adminusers.exception.StripeAgreementExistsException) StripeAgreementEntity(uk.gov.pay.adminusers.persistence.entity.StripeAgreementEntity) ServiceEntity(uk.gov.pay.adminusers.persistence.entity.ServiceEntity) Test(org.junit.jupiter.api.Test)

Example 4 with StripeAgreementEntity

use of uk.gov.pay.adminusers.persistence.entity.StripeAgreementEntity in project pay-adminusers by alphagov.

the class StripeAgreementDaoIT method shouldNotPersistStripeAgreementEntityWhenOneAlreadyExistsForService.

@Test
public void shouldNotPersistStripeAgreementEntityWhenOneAlreadyExistsForService() {
    Service service = serviceDbFixture(databaseHelper).withGatewayAccountIds(randomInt().toString()).insertService();
    Optional<ServiceEntity> serviceEntity = serviceDao.findByExternalId(service.getExternalId());
    assertThat(serviceEntity.isPresent(), is(true));
    StripeAgreementEntity stripeAgreementEntity = new StripeAgreementEntity(serviceEntity.get(), "192.0.2.0", ZonedDateTime.now(ZoneId.of("UTC")));
    stripeAgreementDao.persist(stripeAgreementEntity);
    StripeAgreementEntity anotherStripeAgreementEntity = new StripeAgreementEntity(serviceEntity.get(), "192.0.2.1", ZonedDateTime.now(ZoneId.of("UTC")));
    try {
        stripeAgreementDao.persist(anotherStripeAgreementEntity);
        fail();
    } catch (RollbackException e) {
        assertTrue(e.getMessage().contains("Key (service_id)=(" + serviceEntity.get().getId() + ") already exists."));
    }
}
Also used : StripeAgreementEntity(uk.gov.pay.adminusers.persistence.entity.StripeAgreementEntity) Service(uk.gov.pay.adminusers.model.Service) ServiceEntity(uk.gov.pay.adminusers.persistence.entity.ServiceEntity) RollbackException(javax.persistence.RollbackException) Test(org.junit.jupiter.api.Test)

Example 5 with StripeAgreementEntity

use of uk.gov.pay.adminusers.persistence.entity.StripeAgreementEntity in project pay-adminusers by alphagov.

the class StripeAgreementDaoIT method shouldNotFindStripeAgreementEntityWhenNoneExistForServiceId.

@Test
public void shouldNotFindStripeAgreementEntityWhenNoneExistForServiceId() {
    Service service = serviceDbFixture(databaseHelper).insertService();
    stripeAgreementDbFixture(databaseHelper).withServiceId(service.getId()).insert();
    Optional<StripeAgreementEntity> maybeStripeAgreementEntity = stripeAgreementDao.findByServiceExternalId("123");
    assertFalse(maybeStripeAgreementEntity.isPresent());
}
Also used : StripeAgreementEntity(uk.gov.pay.adminusers.persistence.entity.StripeAgreementEntity) Service(uk.gov.pay.adminusers.model.Service) Test(org.junit.jupiter.api.Test)

Aggregations

StripeAgreementEntity (uk.gov.pay.adminusers.persistence.entity.StripeAgreementEntity)7 Test (org.junit.jupiter.api.Test)6 ServiceEntity (uk.gov.pay.adminusers.persistence.entity.ServiceEntity)5 Service (uk.gov.pay.adminusers.model.Service)4 ZonedDateTime (java.time.ZonedDateTime)3 StripeAgreementExistsException (uk.gov.pay.adminusers.exception.StripeAgreementExistsException)2 Map (java.util.Map)1 RollbackException (javax.persistence.RollbackException)1 ServiceNotFoundException (uk.gov.pay.adminusers.exception.ServiceNotFoundException)1 StripeAgreement (uk.gov.pay.adminusers.model.StripeAgreement)1