Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [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 ( |
| 25 | TestBranch_BRANCH *Branch |
| 26 | TestBranch_HASH string |
| 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 | |
| 41 | TestBranch_BRANCH = NewBranch(node, txid, origin, true) |
| 42 | t.Logf("New Branch(txid:%s) created: %+v\n", txid, TestBranch_BRANCH) |
| 43 | |
| 44 | if TestBranch_BRANCH.Latest == nil { |
| 45 | t.Errorf("Branch latest pointer is nil") |
| 46 | } else if TestBranch_BRANCH.Origin == nil { |
| 47 | t.Errorf("Branch origin pointer is nil") |
| 48 | } else if TestBranch_BRANCH.Node == nil { |
| 49 | t.Errorf("Branch node pointer is nil") |
| 50 | } else if TestBranch_BRANCH.Revisions == nil { |
| 51 | t.Errorf("Branch revisions map is nil") |
| 52 | } else if TestBranch_BRANCH.Txid == "" { |
| 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) { |
| 59 | TestBranch_HASH = fmt.Sprintf("%x", md5.Sum([]byte("revision_hash"))) |
| 60 | rev := &NonPersistedRevision{ |
| 61 | Config: &DataRevision{}, |
| 62 | Children: make(map[string][]Revision), |
| 63 | Hash: TestBranch_HASH, |
| 64 | Branch: &Branch{}, |
| 65 | } |
| 66 | |
| 67 | TestBranch_BRANCH.AddRevision(rev) |
| 68 | t.Logf("Added revision: %+v\n", rev) |
| 69 | |
| 70 | if len(TestBranch_BRANCH.Revisions) == 0 { |
| 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) { |
| 77 | if rev := TestBranch_BRANCH.GetRevision(TestBranch_HASH); rev == nil { |
| 78 | t.Errorf("Unable to retrieve revision for hash:%s", TestBranch_HASH) |
| 79 | } else { |
| 80 | t.Logf("Got revision for hash:%s rev:%+v\n", TestBranch_HASH, rev) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // Set the added revision as the latest |
| 85 | func TestBranch_LatestRevision(t *testing.T) { |
| 86 | addedRevision := TestBranch_BRANCH.GetRevision(TestBranch_HASH) |
| 87 | TestBranch_BRANCH.SetLatest(addedRevision) |
| 88 | |
| 89 | rev := TestBranch_BRANCH.GetLatest() |
| 90 | t.Logf("Retrieved latest revision :%+v", rev) |
| 91 | |
| 92 | if rev == nil { |
| 93 | t.Error("Unable to retrieve latest revision") |
| 94 | } else if rev.GetHash() != TestBranch_HASH { |
| 95 | t.Errorf("Latest revision does not match hash: %s", TestBranch_HASH) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // Ensure that the origin revision remains and differs from subsequent revisions |
| 100 | func TestBranch_OriginRevision(t *testing.T) { |
| 101 | rev := TestBranch_BRANCH.Origin |
| 102 | t.Logf("Retrieved origin revision :%+v", rev) |
| 103 | |
| 104 | if rev == nil { |
| 105 | t.Error("Unable to retrieve origin revision") |
| 106 | } else if rev.GetHash() == TestBranch_HASH { |
| 107 | t.Errorf("Origin revision should differ from added revision: %s", TestBranch_HASH) |
| 108 | } |
| 109 | } |