From f5702114d4b1f4e38a5071179f404b278ae58d12 Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Sat, 3 Aug 2024 16:04:41 -0700 Subject: [PATCH] Integrated ComparatorService to handler --- src/core/comparator.go | 18 +++- src/core/comparator_test.go | 195 +++++++++++++++++++----------------- src/core/core_test.go | 6 +- src/core/git.go | 6 -- src/core/git_test.go | 23 ----- src/core/handler.go | 56 +++++++++-- src/core/handler_test.go | 5 +- src/model/account.go | 1 + 8 files changed, 177 insertions(+), 133 deletions(-) diff --git a/src/core/comparator.go b/src/core/comparator.go index ef08040..0c8257c 100644 --- a/src/core/comparator.go +++ b/src/core/comparator.go @@ -10,6 +10,14 @@ import ( type DiffType string type DiffResult string +type IComparatorService interface { + CheckSnapshotDiff(left model.Snapshot, right model.Snapshot, diffType DiffType) model.DiffResult + MergeResults(diffResults []model.DiffResult) model.DiffResult + NewSnapshotFromJson(jsonData []byte) model.Snapshot +} + +type ComparatorService struct{} + const ( NEW DiffType = "NEW" CHANGED DiffType = "CHANGED" @@ -22,18 +30,22 @@ const ( COMPONENT DiffResult = "COMPONENT" ) -func NewSnapshotFromJson(jsonData []byte) model.Snapshot { +func NewComparatorService() *ComparatorService { + return &ComparatorService{} +} + +func (c *ComparatorService) NewSnapshotFromJson(jsonData []byte) model.Snapshot { var snapshot model.Snapshot json.Unmarshal(jsonData, &snapshot) return snapshot } -func CheckSnapshotDiff(left model.Snapshot, right model.Snapshot, diffType DiffType) model.DiffResult { +func (c *ComparatorService) CheckSnapshotDiff(left model.Snapshot, right model.Snapshot, diffType DiffType) model.DiffResult { diffResult := model.DiffResult{} return checkGroupDiff(left, right, diffType, diffResult) } -func MergeResults(diffResults []model.DiffResult) model.DiffResult { +func (c *ComparatorService) MergeResults(diffResults []model.DiffResult) model.DiffResult { var result model.DiffResult for _, diffResult := range diffResults { diff --git a/src/core/comparator_test.go b/src/core/comparator_test.go index 7e4c2d5..310bed3 100644 --- a/src/core/comparator_test.go +++ b/src/core/comparator_test.go @@ -13,16 +13,17 @@ const DEFAULT_JSON = "../../resources/fixtures/default.json" func TestCheckSnapshotDiffGroupChange(t *testing.T) { // Given + c := NewComparatorService() jsonLeft := utils.ReadJsonFromFile(DEFAULT_JSON) jsonRight := utils.ReadJsonFromFile("../../resources/fixtures/changed_group.json") - snapshotLeft := NewSnapshotFromJson([]byte(jsonLeft)) - snapshotRight := NewSnapshotFromJson([]byte(jsonRight)) + snapshotLeft := c.NewSnapshotFromJson([]byte(jsonLeft)) + snapshotRight := c.NewSnapshotFromJson([]byte(jsonRight)) // Test Check/Merge changes - diffChanged := CheckSnapshotDiff(snapshotLeft, snapshotRight, CHANGED) - diffNew := CheckSnapshotDiff(snapshotRight, snapshotLeft, NEW) - diffDeleted := CheckSnapshotDiff(snapshotLeft, snapshotRight, DELETED) - actual := MergeResults([]model.DiffResult{diffChanged, diffNew, diffDeleted}) + diffChanged := c.CheckSnapshotDiff(snapshotLeft, snapshotRight, CHANGED) + diffNew := c.CheckSnapshotDiff(snapshotRight, snapshotLeft, NEW) + diffDeleted := c.CheckSnapshotDiff(snapshotLeft, snapshotRight, DELETED) + actual := c.MergeResults([]model.DiffResult{diffChanged, diffNew, diffDeleted}) AssertNotNil(t, actual) assert.JSONEq(t, `{ @@ -44,16 +45,17 @@ func TestCheckSnapshotDiffGroupChange(t *testing.T) { func TestCheckSnapshotDiffNewGroup(t *testing.T) { // Given + c := NewComparatorService() jsonLeft := utils.ReadJsonFromFile(DEFAULT_JSON) jsonRight := utils.ReadJsonFromFile("../../resources/fixtures/new_group.json") - snapshotLeft := NewSnapshotFromJson([]byte(jsonLeft)) - snapshotRight := NewSnapshotFromJson([]byte(jsonRight)) + snapshotLeft := c.NewSnapshotFromJson([]byte(jsonLeft)) + snapshotRight := c.NewSnapshotFromJson([]byte(jsonRight)) // Test Check/Merge changes - diffChanged := CheckSnapshotDiff(snapshotLeft, snapshotRight, CHANGED) - diffNew := CheckSnapshotDiff(snapshotRight, snapshotLeft, NEW) - diffDeleted := CheckSnapshotDiff(snapshotLeft, snapshotRight, DELETED) - actual := MergeResults([]model.DiffResult{diffChanged, diffNew, diffDeleted}) + diffChanged := c.CheckSnapshotDiff(snapshotLeft, snapshotRight, CHANGED) + diffNew := c.CheckSnapshotDiff(snapshotRight, snapshotLeft, NEW) + diffDeleted := c.CheckSnapshotDiff(snapshotLeft, snapshotRight, DELETED) + actual := c.MergeResults([]model.DiffResult{diffChanged, diffNew, diffDeleted}) AssertNotNil(t, actual) assert.JSONEq(t, `{ @@ -83,16 +85,17 @@ func TestCheckSnapshotDiffNewGroup(t *testing.T) { func TestCheckSnapshotDiffNewGroupFromEmptyGroup(t *testing.T) { // Given + c := NewComparatorService() jsonLeft := utils.ReadJsonFromFile("../../resources/fixtures/default_empty.json") jsonRight := utils.ReadJsonFromFile(DEFAULT_JSON) - snapshotLeft := NewSnapshotFromJson([]byte(jsonLeft)) - snapshotRight := NewSnapshotFromJson([]byte(jsonRight)) + snapshotLeft := c.NewSnapshotFromJson([]byte(jsonLeft)) + snapshotRight := c.NewSnapshotFromJson([]byte(jsonRight)) // Test Check/Merge changes - diffChanged := CheckSnapshotDiff(snapshotLeft, snapshotRight, CHANGED) - diffNew := CheckSnapshotDiff(snapshotRight, snapshotLeft, NEW) - diffDeleted := CheckSnapshotDiff(snapshotLeft, snapshotRight, DELETED) - actual := MergeResults([]model.DiffResult{diffChanged, diffNew, diffDeleted}) + diffChanged := c.CheckSnapshotDiff(snapshotLeft, snapshotRight, CHANGED) + diffNew := c.CheckSnapshotDiff(snapshotRight, snapshotLeft, NEW) + diffDeleted := c.CheckSnapshotDiff(snapshotLeft, snapshotRight, DELETED) + actual := c.MergeResults([]model.DiffResult{diffChanged, diffNew, diffDeleted}) AssertNotNil(t, actual) assert.Equal(t, "NEW", actual.Changes[0].Action) @@ -101,16 +104,17 @@ func TestCheckSnapshotDiffNewGroupFromEmptyGroup(t *testing.T) { func TestCheckSnapshotDiffNewGroupFromEmptyConfig(t *testing.T) { // Given + c := NewComparatorService() jsonLeft := utils.ReadJsonFromFile("../../resources/fixtures/default_empty_config.json") jsonRight := utils.ReadJsonFromFile(DEFAULT_JSON) - snapshotLeft := NewSnapshotFromJson([]byte(jsonLeft)) - snapshotRight := NewSnapshotFromJson([]byte(jsonRight)) + snapshotLeft := c.NewSnapshotFromJson([]byte(jsonLeft)) + snapshotRight := c.NewSnapshotFromJson([]byte(jsonRight)) // Test Check/Merge changes - diffChanged := CheckSnapshotDiff(snapshotLeft, snapshotRight, CHANGED) - diffNew := CheckSnapshotDiff(snapshotRight, snapshotLeft, NEW) - diffDeleted := CheckSnapshotDiff(snapshotLeft, snapshotRight, DELETED) - actual := MergeResults([]model.DiffResult{diffChanged, diffNew, diffDeleted}) + diffChanged := c.CheckSnapshotDiff(snapshotLeft, snapshotRight, CHANGED) + diffNew := c.CheckSnapshotDiff(snapshotRight, snapshotLeft, NEW) + diffDeleted := c.CheckSnapshotDiff(snapshotLeft, snapshotRight, DELETED) + actual := c.MergeResults([]model.DiffResult{diffChanged, diffNew, diffDeleted}) AssertNotNil(t, actual) assert.Equal(t, "NEW", actual.Changes[0].Action) @@ -119,16 +123,17 @@ func TestCheckSnapshotDiffNewGroupFromEmptyConfig(t *testing.T) { func TestCheckSnapshotDiffDeletedGroup(t *testing.T) { // Given + c := NewComparatorService() jsonLeft := utils.ReadJsonFromFile(DEFAULT_JSON) jsonRight := utils.ReadJsonFromFile("../../resources/fixtures/deleted_group.json") - snapshotLeft := NewSnapshotFromJson([]byte(jsonLeft)) - snapshotRight := NewSnapshotFromJson([]byte(jsonRight)) + snapshotLeft := c.NewSnapshotFromJson([]byte(jsonLeft)) + snapshotRight := c.NewSnapshotFromJson([]byte(jsonRight)) // Test Check/Merge changes - diffChanged := CheckSnapshotDiff(snapshotLeft, snapshotRight, CHANGED) - diffNew := CheckSnapshotDiff(snapshotRight, snapshotLeft, NEW) - diffDeleted := CheckSnapshotDiff(snapshotLeft, snapshotRight, DELETED) - actual := MergeResults([]model.DiffResult{diffChanged, diffNew, diffDeleted}) + diffChanged := c.CheckSnapshotDiff(snapshotLeft, snapshotRight, CHANGED) + diffNew := c.CheckSnapshotDiff(snapshotRight, snapshotLeft, NEW) + diffDeleted := c.CheckSnapshotDiff(snapshotLeft, snapshotRight, DELETED) + actual := c.MergeResults([]model.DiffResult{diffChanged, diffNew, diffDeleted}) AssertNotNil(t, actual) assert.JSONEq(t, `{ @@ -147,16 +152,17 @@ func TestCheckSnapshotDiffDeletedGroup(t *testing.T) { func TestCheckSnapshotDiffConfigChange(t *testing.T) { // Given + c := NewComparatorService() jsonLeft := utils.ReadJsonFromFile(DEFAULT_JSON) jsonRight := utils.ReadJsonFromFile("../../resources/fixtures/changed_config.json") - snapshotLeft := NewSnapshotFromJson([]byte(jsonLeft)) - snapshotRight := NewSnapshotFromJson([]byte(jsonRight)) + snapshotLeft := c.NewSnapshotFromJson([]byte(jsonLeft)) + snapshotRight := c.NewSnapshotFromJson([]byte(jsonRight)) // Test Check/Merge changes - diffChanged := CheckSnapshotDiff(snapshotLeft, snapshotRight, CHANGED) - diffNew := CheckSnapshotDiff(snapshotRight, snapshotLeft, NEW) - diffDeleted := CheckSnapshotDiff(snapshotLeft, snapshotRight, DELETED) - actual := MergeResults([]model.DiffResult{diffChanged, diffNew, diffDeleted}) + diffChanged := c.CheckSnapshotDiff(snapshotLeft, snapshotRight, CHANGED) + diffNew := c.CheckSnapshotDiff(snapshotRight, snapshotLeft, NEW) + diffDeleted := c.CheckSnapshotDiff(snapshotLeft, snapshotRight, DELETED) + actual := c.MergeResults([]model.DiffResult{diffChanged, diffNew, diffDeleted}) AssertNotNil(t, actual) assert.JSONEq(t, `{ @@ -179,16 +185,17 @@ func TestCheckSnapshotDiffConfigChange(t *testing.T) { func TestCheckSnapshotDiffNewConfig(t *testing.T) { // Given + c := NewComparatorService() jsonLeft := utils.ReadJsonFromFile(DEFAULT_JSON) jsonRight := utils.ReadJsonFromFile("../../resources/fixtures/new_config.json") - snapshotLeft := NewSnapshotFromJson([]byte(jsonLeft)) - snapshotRight := NewSnapshotFromJson([]byte(jsonRight)) + snapshotLeft := c.NewSnapshotFromJson([]byte(jsonLeft)) + snapshotRight := c.NewSnapshotFromJson([]byte(jsonRight)) // Test Check/Merge changes - diffChanged := CheckSnapshotDiff(snapshotLeft, snapshotRight, CHANGED) - diffNew := CheckSnapshotDiff(snapshotRight, snapshotLeft, NEW) - diffDeleted := CheckSnapshotDiff(snapshotLeft, snapshotRight, DELETED) - actual := MergeResults([]model.DiffResult{diffChanged, diffNew, diffDeleted}) + diffChanged := c.CheckSnapshotDiff(snapshotLeft, snapshotRight, CHANGED) + diffNew := c.CheckSnapshotDiff(snapshotRight, snapshotLeft, NEW) + diffDeleted := c.CheckSnapshotDiff(snapshotLeft, snapshotRight, DELETED) + actual := c.MergeResults([]model.DiffResult{diffChanged, diffNew, diffDeleted}) AssertNotNil(t, actual) assert.JSONEq(t, `{ @@ -213,16 +220,17 @@ func TestCheckSnapshotDiffNewConfig(t *testing.T) { func TestCheckSnapshotDiffDeletedConfig(t *testing.T) { // Given + c := NewComparatorService() jsonLeft := utils.ReadJsonFromFile(DEFAULT_JSON) jsonRight := utils.ReadJsonFromFile("../../resources/fixtures/deleted_config.json") - snapshotLeft := NewSnapshotFromJson([]byte(jsonLeft)) - snapshotRight := NewSnapshotFromJson([]byte(jsonRight)) + snapshotLeft := c.NewSnapshotFromJson([]byte(jsonLeft)) + snapshotRight := c.NewSnapshotFromJson([]byte(jsonRight)) // Test Check/Merge changes - diffChanged := CheckSnapshotDiff(snapshotLeft, snapshotRight, CHANGED) - diffNew := CheckSnapshotDiff(snapshotRight, snapshotLeft, NEW) - diffDeleted := CheckSnapshotDiff(snapshotLeft, snapshotRight, DELETED) - actual := MergeResults([]model.DiffResult{diffChanged, diffNew, diffDeleted}) + diffChanged := c.CheckSnapshotDiff(snapshotLeft, snapshotRight, CHANGED) + diffNew := c.CheckSnapshotDiff(snapshotRight, snapshotLeft, NEW) + diffDeleted := c.CheckSnapshotDiff(snapshotLeft, snapshotRight, DELETED) + actual := c.MergeResults([]model.DiffResult{diffChanged, diffNew, diffDeleted}) AssertNotNil(t, actual) assert.JSONEq(t, `{ @@ -242,16 +250,17 @@ func TestCheckSnapshotDiffDeletedConfig(t *testing.T) { func TestCheckSnapshotDiffStrategyChange(t *testing.T) { // Given + c := NewComparatorService() jsonLeft := utils.ReadJsonFromFile(DEFAULT_JSON) jsonRight := utils.ReadJsonFromFile("../../resources/fixtures/changed_strategy.json") - snapshotLeft := NewSnapshotFromJson([]byte(jsonLeft)) - snapshotRight := NewSnapshotFromJson([]byte(jsonRight)) + snapshotLeft := c.NewSnapshotFromJson([]byte(jsonLeft)) + snapshotRight := c.NewSnapshotFromJson([]byte(jsonRight)) // Test Check/Merge changes - diffChanged := CheckSnapshotDiff(snapshotLeft, snapshotRight, CHANGED) - diffNew := CheckSnapshotDiff(snapshotRight, snapshotLeft, NEW) - diffDeleted := CheckSnapshotDiff(snapshotLeft, snapshotRight, DELETED) - actual := MergeResults([]model.DiffResult{diffChanged, diffNew, diffDeleted}) + diffChanged := c.CheckSnapshotDiff(snapshotLeft, snapshotRight, CHANGED) + diffNew := c.CheckSnapshotDiff(snapshotRight, snapshotLeft, NEW) + diffDeleted := c.CheckSnapshotDiff(snapshotLeft, snapshotRight, DELETED) + actual := c.MergeResults([]model.DiffResult{diffChanged, diffNew, diffDeleted}) AssertNotNil(t, actual) assert.JSONEq(t, `{ @@ -274,16 +283,17 @@ func TestCheckSnapshotDiffStrategyChange(t *testing.T) { func TestCheckSnapshotDiffNewStrategy(t *testing.T) { // Given + c := NewComparatorService() jsonLeft := utils.ReadJsonFromFile(DEFAULT_JSON) jsonRight := utils.ReadJsonFromFile("../../resources/fixtures/new_strategy.json") - snapshotLeft := NewSnapshotFromJson([]byte(jsonLeft)) - snapshotRight := NewSnapshotFromJson([]byte(jsonRight)) + snapshotLeft := c.NewSnapshotFromJson([]byte(jsonLeft)) + snapshotRight := c.NewSnapshotFromJson([]byte(jsonRight)) // Test Check/Merge changes - diffChanged := CheckSnapshotDiff(snapshotLeft, snapshotRight, CHANGED) - diffNew := CheckSnapshotDiff(snapshotRight, snapshotLeft, NEW) - diffDeleted := CheckSnapshotDiff(snapshotLeft, snapshotRight, DELETED) - actual := MergeResults([]model.DiffResult{diffChanged, diffNew, diffDeleted}) + diffChanged := c.CheckSnapshotDiff(snapshotLeft, snapshotRight, CHANGED) + diffNew := c.CheckSnapshotDiff(snapshotRight, snapshotLeft, NEW) + diffDeleted := c.CheckSnapshotDiff(snapshotLeft, snapshotRight, DELETED) + actual := c.MergeResults([]model.DiffResult{diffChanged, diffNew, diffDeleted}) AssertNotNil(t, actual) assert.JSONEq(t, `{ @@ -310,16 +320,17 @@ func TestCheckSnapshotDiffNewStrategy(t *testing.T) { func TestCheckSnapshotDiffDeletedStrategy(t *testing.T) { // Given + c := NewComparatorService() jsonLeft := utils.ReadJsonFromFile(DEFAULT_JSON) jsonRight := utils.ReadJsonFromFile("../../resources/fixtures/deleted_strategy.json") - snapshotLeft := NewSnapshotFromJson([]byte(jsonLeft)) - snapshotRight := NewSnapshotFromJson([]byte(jsonRight)) + snapshotLeft := c.NewSnapshotFromJson([]byte(jsonLeft)) + snapshotRight := c.NewSnapshotFromJson([]byte(jsonRight)) // Test Check/Merge changes - diffChanged := CheckSnapshotDiff(snapshotLeft, snapshotRight, CHANGED) - diffNew := CheckSnapshotDiff(snapshotRight, snapshotLeft, NEW) - diffDeleted := CheckSnapshotDiff(snapshotLeft, snapshotRight, DELETED) - actual := MergeResults([]model.DiffResult{diffChanged, diffNew, diffDeleted}) + diffChanged := c.CheckSnapshotDiff(snapshotLeft, snapshotRight, CHANGED) + diffNew := c.CheckSnapshotDiff(snapshotRight, snapshotLeft, NEW) + diffDeleted := c.CheckSnapshotDiff(snapshotLeft, snapshotRight, DELETED) + actual := c.MergeResults([]model.DiffResult{diffChanged, diffNew, diffDeleted}) AssertNotNil(t, actual) assert.JSONEq(t, `{ @@ -340,16 +351,17 @@ func TestCheckSnapshotDiffDeletedStrategy(t *testing.T) { func TestCheckSnapshotDiffStrategyValueChange(t *testing.T) { // Given + c := NewComparatorService() jsonLeft := utils.ReadJsonFromFile(DEFAULT_JSON) jsonRight := utils.ReadJsonFromFile("../../resources/fixtures/new_strategy_value.json") - snapshotLeft := NewSnapshotFromJson([]byte(jsonLeft)) - snapshotRight := NewSnapshotFromJson([]byte(jsonRight)) + snapshotLeft := c.NewSnapshotFromJson([]byte(jsonLeft)) + snapshotRight := c.NewSnapshotFromJson([]byte(jsonRight)) // Test Check/Merge changes - diffChanged := CheckSnapshotDiff(snapshotLeft, snapshotRight, CHANGED) - diffNew := CheckSnapshotDiff(snapshotRight, snapshotLeft, NEW) - diffDeleted := CheckSnapshotDiff(snapshotLeft, snapshotRight, DELETED) - actual := MergeResults([]model.DiffResult{diffChanged, diffNew, diffDeleted}) + diffChanged := c.CheckSnapshotDiff(snapshotLeft, snapshotRight, CHANGED) + diffNew := c.CheckSnapshotDiff(snapshotRight, snapshotLeft, NEW) + diffDeleted := c.CheckSnapshotDiff(snapshotLeft, snapshotRight, DELETED) + actual := c.MergeResults([]model.DiffResult{diffChanged, diffNew, diffDeleted}) AssertNotNil(t, actual) assert.JSONEq(t, `{ @@ -372,16 +384,17 @@ func TestCheckSnapshotDiffStrategyValueChange(t *testing.T) { func TestCheckSnapshotDiffDeletedStrategyValue(t *testing.T) { // Given + c := NewComparatorService() jsonLeft := utils.ReadJsonFromFile(DEFAULT_JSON) jsonRight := utils.ReadJsonFromFile("../../resources/fixtures/deleted_strategy_value.json") - snapshotLeft := NewSnapshotFromJson([]byte(jsonLeft)) - snapshotRight := NewSnapshotFromJson([]byte(jsonRight)) + snapshotLeft := c.NewSnapshotFromJson([]byte(jsonLeft)) + snapshotRight := c.NewSnapshotFromJson([]byte(jsonRight)) // Test Check/Merge changes - diffChanged := CheckSnapshotDiff(snapshotLeft, snapshotRight, CHANGED) - diffNew := CheckSnapshotDiff(snapshotRight, snapshotLeft, NEW) - diffDeleted := CheckSnapshotDiff(snapshotLeft, snapshotRight, DELETED) - actual := MergeResults([]model.DiffResult{diffChanged, diffNew, diffDeleted}) + diffChanged := c.CheckSnapshotDiff(snapshotLeft, snapshotRight, CHANGED) + diffNew := c.CheckSnapshotDiff(snapshotRight, snapshotLeft, NEW) + diffDeleted := c.CheckSnapshotDiff(snapshotLeft, snapshotRight, DELETED) + actual := c.MergeResults([]model.DiffResult{diffChanged, diffNew, diffDeleted}) AssertNotNil(t, actual) assert.JSONEq(t, `{ @@ -404,16 +417,17 @@ func TestCheckSnapshotDiffDeletedStrategyValue(t *testing.T) { func TestCheckSnapshotDiffNewComponent(t *testing.T) { // Given + c := NewComparatorService() jsonLeft := utils.ReadJsonFromFile(DEFAULT_JSON) jsonRight := utils.ReadJsonFromFile("../../resources/fixtures/new_component.json") - snapshotLeft := NewSnapshotFromJson([]byte(jsonLeft)) - snapshotRight := NewSnapshotFromJson([]byte(jsonRight)) + snapshotLeft := c.NewSnapshotFromJson([]byte(jsonLeft)) + snapshotRight := c.NewSnapshotFromJson([]byte(jsonRight)) // Test Check/Merge changes - diffChanged := CheckSnapshotDiff(snapshotLeft, snapshotRight, CHANGED) - diffNew := CheckSnapshotDiff(snapshotRight, snapshotLeft, NEW) - diffDeleted := CheckSnapshotDiff(snapshotLeft, snapshotRight, DELETED) - actual := MergeResults([]model.DiffResult{diffChanged, diffNew, diffDeleted}) + diffChanged := c.CheckSnapshotDiff(snapshotLeft, snapshotRight, CHANGED) + diffNew := c.CheckSnapshotDiff(snapshotRight, snapshotLeft, NEW) + diffDeleted := c.CheckSnapshotDiff(snapshotLeft, snapshotRight, DELETED) + actual := c.MergeResults([]model.DiffResult{diffChanged, diffNew, diffDeleted}) AssertNotNil(t, actual) assert.JSONEq(t, `{ @@ -435,16 +449,17 @@ func TestCheckSnapshotDiffNewComponent(t *testing.T) { func TestCheckSnapshotDiffDeletedComponent(t *testing.T) { // Given + c := NewComparatorService() jsonLeft := utils.ReadJsonFromFile(DEFAULT_JSON) jsonRight := utils.ReadJsonFromFile("../../resources/fixtures/deleted_component.json") - snapshotLeft := NewSnapshotFromJson([]byte(jsonLeft)) - snapshotRight := NewSnapshotFromJson([]byte(jsonRight)) + snapshotLeft := c.NewSnapshotFromJson([]byte(jsonLeft)) + snapshotRight := c.NewSnapshotFromJson([]byte(jsonRight)) // Test Check/Merge changes - diffChanged := CheckSnapshotDiff(snapshotLeft, snapshotRight, CHANGED) - diffNew := CheckSnapshotDiff(snapshotRight, snapshotLeft, NEW) - diffDeleted := CheckSnapshotDiff(snapshotLeft, snapshotRight, DELETED) - actual := MergeResults([]model.DiffResult{diffChanged, diffNew, diffDeleted}) + diffChanged := c.CheckSnapshotDiff(snapshotLeft, snapshotRight, CHANGED) + diffNew := c.CheckSnapshotDiff(snapshotRight, snapshotLeft, NEW) + diffDeleted := c.CheckSnapshotDiff(snapshotLeft, snapshotRight, DELETED) + actual := c.MergeResults([]model.DiffResult{diffChanged, diffNew, diffDeleted}) AssertNotNil(t, actual) assert.JSONEq(t, `{ diff --git a/src/core/core_test.go b/src/core/core_test.go index 84218f6..756be5d 100644 --- a/src/core/core_test.go +++ b/src/core/core_test.go @@ -29,7 +29,8 @@ func setup() { accountRepository := repository.NewAccountRepositoryMongo(mongoDb) gitService := NewGitService("repoURL", "token", "main") - coreHandler = NewCoreHandler(accountRepository, gitService) + comparatorService := NewComparatorService() + coreHandler = NewCoreHandler(accountRepository, gitService, comparatorService) } func shutdown() { @@ -49,12 +50,13 @@ func givenAccount() model.Account { Name: "Switcher GitOps", Version: "", LastCommit: "", + LastDate: "", Status: "", Message: "", }, Settings: model.Settings{ Active: true, - Window: "10m", + Window: "5s", ForcePrune: false, }, } diff --git a/src/core/git.go b/src/core/git.go index f0e0d44..5f4be9b 100644 --- a/src/core/git.go +++ b/src/core/git.go @@ -14,7 +14,6 @@ import ( type IGitService interface { GetRepositoryData(environment string) (string, string, string, error) - CheckForChanges(account model.Account, lastCommit string, date string, content string) (status string, message string) } type GitService struct { @@ -41,11 +40,6 @@ func (g *GitService) GetRepositoryData(environment string) (string, string, stri return commitHash, commitDate.Format(time.ANSIC), content, nil } -func (g *GitService) CheckForChanges(account model.Account, lastCommit string, - date string, content string) (status string, message string) { - return model.StatusSynced, "Synced successfully" -} - func (g *GitService) getLastCommitData(filePath string) (string, time.Time, string, error) { c, err := g.getCommitObject() diff --git a/src/core/git_test.go b/src/core/git_test.go index 5dd13e4..1f61cb7 100644 --- a/src/core/git_test.go +++ b/src/core/git_test.go @@ -5,7 +5,6 @@ import ( "github.com/stretchr/testify/assert" "github.com/switcherapi/switcher-gitops/src/config" - "github.com/switcherapi/switcher-gitops/src/model" ) const ( @@ -90,28 +89,6 @@ func TestGetRepositoryDataErrorInvalidToken(t *testing.T) { assert.Empty(t, content) } -func TestCheckForChanges(t *testing.T) { - if !canRunTests() { - t.Skip(SkipMessage) - } - - // Given - gitService := NewGitService( - config.GetEnv("GIT_REPO_URL"), - config.GetEnv("GIT_TOKEN"), - config.GetEnv("GIT_BRANCH")) - - account := givenAccount() - lastCommit, date, content, _ := gitService.GetRepositoryData(account.Environment) - - // Test - status, message := gitService.CheckForChanges(account, lastCommit, date, content) - - // Assert - assert.Equal(t, model.StatusSynced, status) - assert.Equal(t, "Synced successfully", message) -} - // Helpers func canRunTests() bool { diff --git a/src/core/handler.go b/src/core/handler.go index 8281d70..a5f7346 100644 --- a/src/core/handler.go +++ b/src/core/handler.go @@ -11,13 +11,15 @@ import ( type CoreHandler struct { AccountRepository repository.AccountRepository GitService IGitService + ComparatorService IComparatorService status int } -func NewCoreHandler(repo repository.AccountRepository, gitService IGitService) *CoreHandler { +func NewCoreHandler(repo repository.AccountRepository, gitService IGitService, comparatorService IComparatorService) *CoreHandler { return &CoreHandler{ AccountRepository: repo, GitService: gitService, + ComparatorService: comparatorService, } } @@ -44,7 +46,7 @@ func (c *CoreHandler) StartAccountHandler(account model.Account, quit chan bool, } // Reads Window setting - sleep := 1 + timeWindow, unitWindow := getTimeWindow(account.Settings.Window) for { select { @@ -58,19 +60,59 @@ func (c *CoreHandler) StartAccountHandler(account model.Account, quit chan bool, } } - time.Sleep(time.Duration(sleep) * time.Second) + time.Sleep(time.Duration(timeWindow) * unitWindow) } } func (c *CoreHandler) syncUp(account model.Account, lastCommit string, date string, content string) { - status, message := c.GitService.CheckForChanges(account, lastCommit, date, content) - + // Update account status: Out of sync account.Domain.LastCommit = lastCommit - account.Domain.Status = status - account.Domain.Message = message + account.Domain.LastDate = date + account.Domain.Status = model.StatusOutSync + account.Domain.Message = "Syncing up..." + c.AccountRepository.Update(&account) + + // Check for changes + diff := c.checkForChanges(content) + + // Apply changes + c.applyChanges(account, diff) + + // Update account status: Synced + account.Domain.Status = model.StatusSynced + account.Domain.Message = "Synced successfully" c.AccountRepository.Update(&account) } +func (c *CoreHandler) checkForChanges(content string) model.DiffResult { + // Get Snapshot from API + + // Convert API JSON to model.Snapshot + jsonLeft := []byte(content) + left := c.ComparatorService.NewSnapshotFromJson(jsonLeft) + + // Convert content to model.Snapshot + jsonRight := []byte(content) + right := c.ComparatorService.NewSnapshotFromJson(jsonRight) + + // Compare Snapshots and get diff + diffNew := c.ComparatorService.CheckSnapshotDiff(left, right, NEW) + diffChanged := c.ComparatorService.CheckSnapshotDiff(left, right, CHANGED) + diffDeleted := c.ComparatorService.CheckSnapshotDiff(left, right, DELETED) + + return c.ComparatorService.MergeResults([]model.DiffResult{diffNew, diffChanged, diffDeleted}) +} + +func (c *CoreHandler) applyChanges(account model.Account, diff model.DiffResult) { + // Apply changes +} + func isRepositoryOutSync(account model.Account, lastCommit string) bool { return account.Domain.LastCommit == "" || account.Domain.LastCommit != lastCommit } + +func getTimeWindow(window string) (int, time.Duration) { + // Convert window string to time.Duration + duration, _ := time.ParseDuration(window) + return 1, duration +} diff --git a/src/core/handler_test.go b/src/core/handler_test.go index 1b0cf9d..0c4ce9f 100644 --- a/src/core/handler_test.go +++ b/src/core/handler_test.go @@ -13,7 +13,7 @@ import ( func TestInitCoreHandlerCoroutine(t *testing.T) { // Given fakeGitService := NewFakeGitService() - coreHandler = NewCoreHandler(coreHandler.AccountRepository, fakeGitService) + coreHandler = NewCoreHandler(coreHandler.AccountRepository, fakeGitService, NewComparatorService()) account := givenAccount() coreHandler.AccountRepository.Create(&account) @@ -58,7 +58,7 @@ func TestStartAccountHandler(t *testing.T) { fakeGitService := NewFakeGitService() fakeGitService.status = model.StatusSynced fakeGitService.message = "Synced successfully" - coreHandler = NewCoreHandler(coreHandler.AccountRepository, fakeGitService) + coreHandler = NewCoreHandler(coreHandler.AccountRepository, fakeGitService, NewComparatorService()) account := givenAccount() coreHandler.AccountRepository.Create(&account) @@ -81,6 +81,7 @@ func TestStartAccountHandler(t *testing.T) { assert.Equal(t, model.StatusSynced, accountFromDb.Domain.Status) assert.Equal(t, "Synced successfully", accountFromDb.Domain.Message) assert.Equal(t, "123", accountFromDb.Domain.LastCommit) + assert.NotEqual(t, "", accountFromDb.Domain.LastDate) tearDown() } diff --git a/src/model/account.go b/src/model/account.go index 0023d8d..df3805c 100644 --- a/src/model/account.go +++ b/src/model/account.go @@ -31,6 +31,7 @@ type DomainDetails struct { Name string `json:"name"` Version string `json:"version"` LastCommit string `json:"lastcommit"` + LastDate string `json:"lastdate"` Status string `json:"status"` Message string `json:"message"` }