sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package model |
| 17 | |
| 18 | import ( |
| 19 | "crypto/md5" |
| 20 | "fmt" |
| 21 | "testing" |
| 22 | ) |
| 23 | |
| 24 | var ( |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 25 | TestBranchBranch *Branch |
| 26 | TestBranchHash string |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | // Create a new branch and ensure that fields are populated |
| 30 | func TestBranch_NewBranch(t *testing.T) { |
| 31 | node := &node{} |
| 32 | hash := fmt.Sprintf("%x", md5.Sum([]byte("origin_hash"))) |
| 33 | origin := &NonPersistedRevision{ |
| 34 | Config: &DataRevision{}, |
| 35 | Children: make(map[string][]Revision), |
| 36 | Hash: hash, |
| 37 | Branch: &Branch{}, |
| 38 | } |
| 39 | txid := fmt.Sprintf("%x", md5.Sum([]byte("branch_transaction_id"))) |
| 40 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 41 | TestBranchBranch = NewBranch(node, txid, origin, true) |
| 42 | t.Logf("New Branch(txid:%s) created: %+v\n", txid, TestBranchBranch) |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 43 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 44 | if TestBranchBranch.Latest == nil { |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 45 | t.Errorf("Branch latest pointer is nil") |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 46 | } else if TestBranchBranch.Origin == nil { |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 47 | t.Errorf("Branch origin pointer is nil") |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 48 | } else if TestBranchBranch.Node == nil { |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 49 | t.Errorf("Branch node pointer is nil") |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 50 | } else if TestBranchBranch.Revisions == nil { |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 51 | t.Errorf("Branch revisions map is nil") |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 52 | } else if TestBranchBranch.Txid == "" { |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 53 | t.Errorf("Branch transaction id is empty") |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | // Add a new revision to the branch |
| 58 | func TestBranch_AddRevision(t *testing.T) { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 59 | TestBranchHash = fmt.Sprintf("%x", md5.Sum([]byte("revision_hash"))) |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 60 | rev := &NonPersistedRevision{ |
| 61 | Config: &DataRevision{}, |
| 62 | Children: make(map[string][]Revision), |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 63 | Hash: TestBranchHash, |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 64 | Branch: &Branch{}, |
| 65 | } |
| 66 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 67 | TestBranchBranch.AddRevision(rev) |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 68 | t.Logf("Added revision: %+v\n", rev) |
| 69 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 70 | if len(TestBranchBranch.Revisions) == 0 { |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 71 | t.Errorf("Branch revisions map is empty") |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // Ensure that the added revision can be retrieved |
| 76 | func TestBranch_GetRevision(t *testing.T) { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 77 | if rev := TestBranchBranch.GetRevision(TestBranchHash); rev == nil { |
| 78 | t.Errorf("Unable to retrieve revision for hash:%s", TestBranchHash) |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 79 | } else { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 80 | t.Logf("Got revision for hash:%s rev:%+v\n", TestBranchHash, rev) |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | // Set the added revision as the latest |
| 85 | func TestBranch_LatestRevision(t *testing.T) { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 86 | addedRevision := TestBranchBranch.GetRevision(TestBranchHash) |
| 87 | TestBranchBranch.SetLatest(addedRevision) |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 88 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 89 | rev := TestBranchBranch.GetLatest() |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 90 | t.Logf("Retrieved latest revision :%+v", rev) |
| 91 | |
| 92 | if rev == nil { |
| 93 | t.Error("Unable to retrieve latest revision") |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 94 | } else if rev.GetHash() != TestBranchHash { |
| 95 | t.Errorf("Latest revision does not match hash: %s", TestBranchHash) |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
| 99 | // Ensure that the origin revision remains and differs from subsequent revisions |
| 100 | func TestBranch_OriginRevision(t *testing.T) { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 101 | rev := TestBranchBranch.Origin |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 102 | t.Logf("Retrieved origin revision :%+v", rev) |
| 103 | |
| 104 | if rev == nil { |
| 105 | t.Error("Unable to retrieve origin revision") |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 106 | } else if rev.GetHash() == TestBranchHash { |
| 107 | t.Errorf("Origin revision should differ from added revision: %s", TestBranchHash) |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 108 | } |
| 109 | } |