-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from CodeKaio/58-make-settings-persistent
🔧 : make settings persistent
- Loading branch information
Showing
8 changed files
with
293 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/java/io/codeka/gaia/repository/SettingsRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package io.codeka.gaia.repository; | ||
|
||
import io.codeka.gaia.bo.Settings; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.mongodb.core.MongoTemplate; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import javax.annotation.PostConstruct; | ||
|
||
|
||
@Repository | ||
public class SettingsRepository { | ||
|
||
private MongoTemplate mongoTemplate; | ||
|
||
private Settings settings; | ||
|
||
@Autowired | ||
public SettingsRepository(MongoTemplate mongoTemplate, Settings settings) { | ||
this.mongoTemplate = mongoTemplate; | ||
this.settings = settings; | ||
} | ||
|
||
/** | ||
* loading settings that are saved in database, and merging into current settings (that are injected) | ||
*/ | ||
@PostConstruct | ||
void postConstruct(){ | ||
var savedSettings = mongoTemplate.findAll(Settings.class).stream() | ||
.findFirst().orElse(null); | ||
|
||
if(savedSettings != null) { | ||
// if settings are found in database, merging them in the current settings | ||
settings.merge(savedSettings); | ||
} | ||
} | ||
|
||
/** | ||
* Saving settings | ||
* We don't need any parameter as settings are already in-memory | ||
*/ | ||
public void save(){ | ||
mongoTemplate.remove(Settings.class).all(); | ||
mongoTemplate.save(settings); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/test/java/io/codeka/gaia/controller/SettingsControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package io.codeka.gaia.controller; | ||
|
||
import io.codeka.gaia.bo.Settings; | ||
import io.codeka.gaia.repository.SettingsRepository; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.ArgumentMatchers.anyList; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.verifyNoMoreInteractions; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class SettingsControllerTest { | ||
|
||
@Mock | ||
private Settings settings; | ||
|
||
@Mock | ||
private SettingsRepository settingsRepository; | ||
|
||
@InjectMocks | ||
private SettingsController settingsController; | ||
|
||
@Test | ||
void put_shouldSaveUpdatedSettings(){ | ||
// given | ||
var editedSettings = new Settings(); | ||
editedSettings.setExternalUrl("edited"); | ||
|
||
var envVar = new Settings.EnvVar(); | ||
envVar.setName("test"); | ||
envVar.setValue("value"); | ||
editedSettings.setEnvVars(List.of(envVar)); | ||
|
||
// when | ||
settingsController.saveSettings(editedSettings); | ||
|
||
// then | ||
verify(settings).setExternalUrl("edited"); | ||
verify(settings).setEnvVars(anyList()); | ||
|
||
verify(settingsRepository).save(); | ||
} | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
src/test/java/io/codeka/gaia/repository/SettingsRepositoryIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package io.codeka.gaia.repository; | ||
|
||
import io.codeka.gaia.bo.Settings; | ||
import io.codeka.gaia.test.MongoContainer; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.data.mongodb.core.MongoTemplate; | ||
import org.springframework.test.context.TestPropertySource; | ||
import org.testcontainers.junit.jupiter.Container; | ||
import org.testcontainers.junit.jupiter.Testcontainers; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
@SpringBootTest | ||
@Testcontainers | ||
@TestPropertySource(properties = {"gaia.externalUrl=http://gaia.io", "gaia.dockerDaemonUrl=unix:///var/run/docker.sock"}) | ||
class SettingsRepositoryIT { | ||
|
||
@Container | ||
private static MongoContainer mongoContainer = new MongoContainer(); | ||
|
||
@Autowired | ||
private MongoTemplate mongoTemplate; | ||
|
||
@Autowired | ||
private SettingsRepository settingsRepository; | ||
|
||
@Autowired | ||
private Settings settings; | ||
|
||
@Test | ||
void itShouldSaveSettings(){ | ||
// given | ||
|
||
// when | ||
settingsRepository.save(); | ||
|
||
// then | ||
var saved = mongoTemplate.findAll(Settings.class).get(0); | ||
assertEquals("http://gaia.io", saved.getExternalUrl()); | ||
assertEquals("unix:///var/run/docker.sock", saved.getDockerDaemonUrl()); | ||
} | ||
|
||
@Test | ||
void settings_shouldOnlyExistOnceInDatabase(){ | ||
// given | ||
|
||
// when | ||
settingsRepository.save(); | ||
settingsRepository.save(); | ||
|
||
// then | ||
assertThat( mongoTemplate.findAll(Settings.class) ).hasSize(1); | ||
} | ||
|
||
} |
76 changes: 76 additions & 0 deletions
76
src/test/java/io/codeka/gaia/repository/SettingsRepositoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package io.codeka.gaia.repository; | ||
|
||
import io.codeka.gaia.bo.Settings; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Answers; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.data.mongodb.core.MongoTemplate; | ||
|
||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.*; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class SettingsRepositoryTest { | ||
|
||
@Mock(answer = Answers.RETURNS_DEEP_STUBS) | ||
private MongoTemplate mongoTemplate; | ||
|
||
private Settings settings; | ||
|
||
private SettingsRepository settingsRepository; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
settings = new Settings(); | ||
settings.setExternalUrl("externalUrl"); | ||
settings.setDockerDaemonUrl("dockerDaemonUrl"); | ||
|
||
settingsRepository = new SettingsRepository(mongoTemplate, settings); | ||
} | ||
|
||
@Test | ||
void settings_shouldBeLoadedFromDatabase(){ | ||
var savedSettings = new Settings(); | ||
savedSettings.setExternalUrl("savedExternalUrl"); | ||
savedSettings.setDockerDaemonUrl("savedDockerDaemonUrl"); | ||
|
||
when(mongoTemplate.findAll(Settings.class)).thenReturn(List.of(savedSettings)); | ||
|
||
//when | ||
settingsRepository.postConstruct(); | ||
|
||
//then | ||
assertEquals("savedExternalUrl", settings.getExternalUrl()); | ||
assertEquals("savedDockerDaemonUrl", settings.getDockerDaemonUrl()); | ||
|
||
verify(mongoTemplate).findAll(Settings.class); | ||
verifyNoMoreInteractions(mongoTemplate); | ||
} | ||
|
||
@Test | ||
void nothingShouldHappen_whenSettingsDoesntExistInDatabase(){ | ||
//when | ||
settingsRepository.postConstruct(); | ||
|
||
//then | ||
verify(mongoTemplate).findAll(Settings.class); | ||
verifyNoMoreInteractions(mongoTemplate); | ||
} | ||
|
||
@Test | ||
void save_shouldSaveSettingsInDatabase(){ | ||
//when | ||
settingsRepository.save(); | ||
|
||
//then | ||
verify(mongoTemplate).remove(Settings.class); | ||
verify(mongoTemplate).save(settings); | ||
verifyNoMoreInteractions(mongoTemplate); | ||
} | ||
|
||
} |