Skip to content

Commit

Permalink
Merge f696ed2 into d0f788d
Browse files Browse the repository at this point in the history
  • Loading branch information
labamba63 authored Jul 3, 2018
2 parents d0f788d + f696ed2 commit c14ad84
Show file tree
Hide file tree
Showing 13 changed files with 389 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package pl.gov.coi.cascades.server;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyVararg;
import static org.mockito.Mockito.when;

/**
* @author <a href="mailto:lukasz.malek@coi.gov.pl">Łukasz Małek</a>
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = AppMain.class)
@ProductionHibernateTest
public class AppMainStartTestIT {

@Mock
private SpringApplicationFactory applicationFactory;

@Mock
private SpringApplication application;

@Mock
private ConfigurableApplicationContext applicationContext;

@Test
public void shouldApplicationStartOnProductionProfile(){
//given
AppMain.setAppMainSupplier(() -> new AppMain(applicationFactory));

when(applicationFactory.create(any())).thenReturn(application);
when(application.run(anyVararg())).thenReturn(applicationContext);
when(applicationContext.isActive()).thenReturn(true);

// when
try {
AppMain.main(new String[0]);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package pl.gov.coi.cascades.server;

import org.springframework.test.context.ActiveProfiles;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* @author <a href="mailto:lukasz.malek@coi.gov.pl">Łukasz Małek</a>
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@ActiveProfiles({Environment.PRODUCTION_NAME, ProfileType.HIBERNATE_NAME})
@interface ProductionHibernateTest {
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import org.springframework.context.annotation.Profile;
import pl.gov.coi.cascades.server.ProfileType;
import pl.gov.coi.cascades.server.domain.DatabaseIdGateway;
import pl.gov.coi.cascades.server.domain.DatabaseInstanceGateway;
import pl.gov.coi.cascades.server.domain.DatabaseLimitGateway;
import pl.gov.coi.cascades.server.domain.DatabaseTypeClassNameService;
import pl.gov.coi.cascades.server.domain.TemplateIdGateway;
import pl.gov.coi.cascades.server.domain.UserGateway;

import javax.transaction.Transactional;
import pl.gov.coi.cascades.server.persistance.hibernate.mapper.DatabaseInstanceMapper;
import pl.gov.coi.cascades.server.persistance.hibernate.mapper.UserMapper;

/**
* @author <a href="agnieszka.celuch@coi.gov.pl">Agnieszka Celuch</a>
Expand All @@ -20,23 +22,37 @@
public class HibernateConfiguration {

@Bean
@Transactional
TemplateIdGateway createTemplateIdGateway() {
return new TemplateIdGatewayImpl();
}

@Bean
@Transactional
UserGateway createUserGateway(DatabaseTypeClassNameService databaseTypeClassNameService) {
return new UserGatewayImpl(databaseTypeClassNameService);
UserGateway createUserGateway(UserMapper userMapper) {
return new UserGatewayImpl(userMapper);
}

@Bean
DatabaseIdGateway createDatabaseIdGateway(DatabaseInstanceMapper databaseInstanceMapper) {
return new DatabaseIdGatewayImpl(databaseInstanceMapper);
}

@Bean
DatabaseInstanceGateway createDatabaseInstanceGateway() {
return new DatabaseInstanceGatewayImpl();
}

@Bean
DatabaseLimitGateway createDatabaseLimitGateway() {
return new DatabaseLimitGatewayImpl();
}

@Bean
@Transactional
DatabaseIdGateway createDatabaseIdGateway(DatabaseTypeClassNameService databaseTypeClassNameService) {
return new DatabaseIdGatewayImpl(
databaseTypeClassNameService
);
UserMapper createUserMapper(DatabaseTypeClassNameService databaseTypeClassNameService) {
return new UserMapper(databaseTypeClassNameService);
}

@Bean
DatabaseInstanceMapper createDatabaseInstanceMapper(DatabaseTypeClassNameService databaseTypeClassNameService) {
return new DatabaseInstanceMapper(databaseTypeClassNameService);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import pl.gov.coi.cascades.server.domain.DatabaseIdGateway;
import pl.gov.coi.cascades.server.domain.DatabaseInstanceGateway;
import pl.gov.coi.cascades.server.domain.DatabaseLimitGateway;
import pl.gov.coi.cascades.server.domain.DatabaseTypeClassNameService;
import pl.gov.coi.cascades.server.domain.TemplateIdGateway;
import pl.gov.coi.cascades.server.domain.UserGateway;
import pl.gov.coi.cascades.server.persistance.hibernate.mapper.DatabaseInstanceMapper;
import pl.gov.coi.cascades.server.persistance.hibernate.mapper.UserMapper;

import static org.assertj.core.api.Assertions.assertThat;

Expand All @@ -18,50 +22,88 @@
*/
public class HibernateConfigurationTest {

@Mock
private UserMapper userMapper;

@Mock
private DatabaseInstanceMapper databaseInstanceMapper;

@Mock
private DatabaseTypeClassNameService databaseTypeClassNameService;

@Rule
public MockitoRule mockitoRule = MockitoJUnit.rule();

private HibernateConfiguration hibernateConfiguration = new HibernateConfiguration();

@Test
public void testCreateTemplateIdGateway() throws Exception {
// given
HibernateConfiguration hibernateConfiguration = new HibernateConfiguration();

// when
TemplateIdGateway actual = hibernateConfiguration.createTemplateIdGateway();

// then
assertThat(actual).isInstanceOf(TemplateIdGatewayImpl.class);
assertThat(actual).isNotNull();
}

@Test
public void testCreateUserGateway() throws Exception {
// given
HibernateConfiguration hibernateConfiguration = new HibernateConfiguration();

// when
UserGateway actual = hibernateConfiguration.createUserGateway(
databaseTypeClassNameService
userMapper
);

// then
assertThat(actual).isInstanceOf(UserGatewayImpl.class);
assertThat(actual).isNotNull();
}

@Test
public void testCreateDatabaseIdGateway() throws Exception {
// given
HibernateConfiguration hibernateConfiguration = new HibernateConfiguration();

// when
DatabaseIdGateway actual = hibernateConfiguration.createDatabaseIdGateway(
databaseInstanceMapper
);

// then
assertThat(actual).isNotNull();
}

@Test
public void testDatabaseInstanceGateway() throws Exception {
// when
DatabaseInstanceGateway actual = hibernateConfiguration.createDatabaseInstanceGateway();

// then
assertThat(actual).isNotNull();
}

@Test
public void testDatabaseLimitGateway() throws Exception {
// when
DatabaseLimitGateway actual = hibernateConfiguration.createDatabaseLimitGateway();

// then
assertThat(actual).isNotNull();
}

@Test
public void testUserMapper() throws Exception {
// when
UserMapper actual = hibernateConfiguration.createUserMapper(
databaseTypeClassNameService
);

// then
assertThat(actual).isInstanceOf(DatabaseIdGatewayImpl.class);
assertThat(actual).isNotNull();
}

@Test
public void testDatabaseInstanceMapper() throws Exception {
// when
DatabaseInstanceMapper actual = hibernateConfiguration.createDatabaseInstanceMapper(
databaseTypeClassNameService
);

// then
assertThat(actual).isNotNull();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@
import org.slf4j.LoggerFactory;
import pl.gov.coi.cascades.contract.domain.DatabaseId;
import pl.gov.coi.cascades.server.domain.DatabaseIdGateway;
import pl.gov.coi.cascades.server.domain.DatabaseTypeClassNameService;
import pl.gov.coi.cascades.server.persistance.hibernate.entity.DatabaseInstance;
import pl.gov.coi.cascades.server.persistance.hibernate.mapper.DatabaseInstanceMapper;
import pl.wavesoftware.eid.exceptions.Eid;

import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.transaction.Transactional;
import java.util.Optional;

/**
* @author <a href="agnieszka.celuch@coi.gov.pl">Agnieszka Celuch</a>
* @since 02.04.17.
*/
@Transactional
public class DatabaseIdGatewayImpl implements DatabaseIdGateway {

private static final Logger DEFAULT_LOGGER = LoggerFactory.getLogger(DatabaseIdGatewayImpl.class);
Expand All @@ -31,12 +31,9 @@ public class DatabaseIdGatewayImpl implements DatabaseIdGateway {
private Logger logger;
private final DatabaseInstanceMapper databaseInstanceMapper;

@Inject
public DatabaseIdGatewayImpl(DatabaseTypeClassNameService databaseTypeClassNameService) {
public DatabaseIdGatewayImpl(DatabaseInstanceMapper databaseInstanceMapper) {
this(
new DatabaseInstanceMapper(
databaseTypeClassNameService
),
databaseInstanceMapper,
DEFAULT_LOGGER
);
}
Expand Down Expand Up @@ -65,8 +62,8 @@ public Optional<pl.gov.coi.cascades.server.domain.DatabaseInstance> findInstance
"WHERE instance.id = :databaseIdAsLong",
DatabaseInstance.class
)
.setParameter(DATABASE_ID_FIELD, databaseIdAsLong)
.setMaxResults(1);
.setParameter(DATABASE_ID_FIELD, databaseIdAsLong)
.setMaxResults(1);

return Optional.of(databaseInstanceMapper.fromHibernateEntity(query.getSingleResult()));
} catch (NoResultException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package pl.gov.coi.cascades.server.persistance.hibernate;

import pl.gov.coi.cascades.server.domain.DatabaseInstance;
import pl.gov.coi.cascades.server.domain.DatabaseInstanceGateway;

import javax.transaction.Transactional;

/**
* @author <a href="mailto:lukasz.malek@coi.gov.pl">Łukasz Małek</a>
*/
@Transactional
public class DatabaseInstanceGatewayImpl implements DatabaseInstanceGateway {

@Override
@Deprecated
public DatabaseInstance launchDatabase(DatabaseInstance databaseInstance) {
// TODO: write an implementation
throw new UnsupportedOperationException("Not yet implemented!");
}

@Override
@Deprecated
public void deleteDatabase(DatabaseInstance databaseInstance) {
// TODO: write an implementation
throw new UnsupportedOperationException("Not yet implemented!");
}

@Override
@Deprecated
public String getRemoteServerId() {
// TODO: write an implementation
throw new UnsupportedOperationException("Not yet implemented!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package pl.gov.coi.cascades.server.persistance.hibernate;

import pl.gov.coi.cascades.server.domain.DatabaseLimitGateway;
import pl.gov.coi.cascades.server.domain.User;

import javax.transaction.Transactional;

/**
* @author <a href="mailto:lukasz.malek@coi.gov.pl">Łukasz Małek</a>
*/
@Transactional
public class DatabaseLimitGatewayImpl implements DatabaseLimitGateway {

@Override
@Deprecated
public boolean isPersonalLimitExceeded(User user) {
// TODO: write an implementation
throw new UnsupportedOperationException("Not yet implemented!");
}

@Override
@Deprecated
public int getPersonalLimitPerUser(User user) {
// TODO: write an implementation
throw new UnsupportedOperationException("Not yet implemented!");
}

@Override
@Deprecated
public boolean isGlobalLimitExceeded() {
// TODO: write an implementation
throw new UnsupportedOperationException("Not yet implemented!");
}

@Override
@Deprecated
public int getGlobalLimit() {
// TODO: write an implementation
throw new UnsupportedOperationException("Not yet implemented!");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.transaction.Transactional;
import java.util.Optional;

/**
* @author <a href="agnieszka.celuch@coi.gov.pl">Agnieszka Celuch</a>
* @since 30.03.17.
*/
@Transactional
public class TemplateIdGatewayImpl implements TemplateIdGateway {

private static final Logger DEFAULT_LOGGER = LoggerFactory.getLogger(TemplateIdGatewayImpl.class);
Expand Down
Loading

0 comments on commit c14ad84

Please sign in to comment.