khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [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 | */ |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 16 | package model |
| 17 | |
| 18 | import ( |
| 19 | "crypto/md5" |
| 20 | "fmt" |
| 21 | "testing" |
| 22 | ) |
| 23 | |
| 24 | var ( |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 25 | TestBranch_BRANCH *Branch |
| 26 | TestBranch_HASH string |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 27 | ) |
| 28 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 29 | // Create a new branch and ensure that fields are populated |
| 30 | func TestBranch_NewBranch(t *testing.T) { |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 31 | node := &node{} |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 32 | hash := fmt.Sprintf("%x", md5.Sum([]byte("origin_hash"))) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 33 | origin := &NonPersistedRevision{ |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 34 | Config: &DataRevision{}, |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 35 | Children: make(map[string][]Revision), |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 36 | Hash: hash, |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 37 | Branch: &Branch{}, |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 38 | } |
| 39 | txid := fmt.Sprintf("%x", md5.Sum([]byte("branch_transaction_id"))) |
| 40 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 41 | TestBranch_BRANCH = NewBranch(node, txid, origin, true) |
| 42 | t.Logf("New Branch(txid:%s) created: %+v\n", txid, TestBranch_BRANCH) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 43 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 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 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 55 | } |
| 56 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 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"))) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 60 | rev := &NonPersistedRevision{ |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 61 | Config: &DataRevision{}, |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 62 | Children: make(map[string][]Revision), |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 63 | Hash: TestBranch_HASH, |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 64 | Branch: &Branch{}, |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 65 | } |
| 66 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 67 | TestBranch_BRANCH.AddRevision(rev) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 68 | t.Logf("Added revision: %+v\n", rev) |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 69 | |
| 70 | if len(TestBranch_BRANCH.Revisions) == 0 { |
| 71 | t.Errorf("Branch revisions map is empty") |
| 72 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 73 | } |
| 74 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 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 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 82 | } |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 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 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 97 | } |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 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 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 109 | } |