blob: d91d15ebdad1732e8eecfc9f0b9b0eb4c67f40c1 [file] [log] [blame]
sbarbari17d7e222019-11-05 10:02:29 -05001/*
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 */
16package model
17
18import (
19 "crypto/md5"
20 "fmt"
21 "testing"
22)
23
24var (
npujar9a30c702019-11-14 17:06:39 +053025 TestBranchBranch *Branch
26 TestBranchHash string
sbarbari17d7e222019-11-05 10:02:29 -050027)
28
29// Create a new branch and ensure that fields are populated
30func 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
npujar9a30c702019-11-14 17:06:39 +053041 TestBranchBranch = NewBranch(node, txid, origin, true)
42 t.Logf("New Branch(txid:%s) created: %+v\n", txid, TestBranchBranch)
sbarbari17d7e222019-11-05 10:02:29 -050043
npujar9a30c702019-11-14 17:06:39 +053044 if TestBranchBranch.Latest == nil {
sbarbari17d7e222019-11-05 10:02:29 -050045 t.Errorf("Branch latest pointer is nil")
npujar9a30c702019-11-14 17:06:39 +053046 } else if TestBranchBranch.Origin == nil {
sbarbari17d7e222019-11-05 10:02:29 -050047 t.Errorf("Branch origin pointer is nil")
npujar9a30c702019-11-14 17:06:39 +053048 } else if TestBranchBranch.Node == nil {
sbarbari17d7e222019-11-05 10:02:29 -050049 t.Errorf("Branch node pointer is nil")
npujar9a30c702019-11-14 17:06:39 +053050 } else if TestBranchBranch.Revisions == nil {
sbarbari17d7e222019-11-05 10:02:29 -050051 t.Errorf("Branch revisions map is nil")
npujar9a30c702019-11-14 17:06:39 +053052 } else if TestBranchBranch.Txid == "" {
sbarbari17d7e222019-11-05 10:02:29 -050053 t.Errorf("Branch transaction id is empty")
54 }
55}
56
57// Add a new revision to the branch
58func TestBranch_AddRevision(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +053059 TestBranchHash = fmt.Sprintf("%x", md5.Sum([]byte("revision_hash")))
sbarbari17d7e222019-11-05 10:02:29 -050060 rev := &NonPersistedRevision{
61 Config: &DataRevision{},
62 Children: make(map[string][]Revision),
npujar9a30c702019-11-14 17:06:39 +053063 Hash: TestBranchHash,
sbarbari17d7e222019-11-05 10:02:29 -050064 Branch: &Branch{},
65 }
66
npujar9a30c702019-11-14 17:06:39 +053067 TestBranchBranch.AddRevision(rev)
sbarbari17d7e222019-11-05 10:02:29 -050068 t.Logf("Added revision: %+v\n", rev)
69
npujar9a30c702019-11-14 17:06:39 +053070 if len(TestBranchBranch.Revisions) == 0 {
sbarbari17d7e222019-11-05 10:02:29 -050071 t.Errorf("Branch revisions map is empty")
72 }
73}
74
75// Ensure that the added revision can be retrieved
76func TestBranch_GetRevision(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +053077 if rev := TestBranchBranch.GetRevision(TestBranchHash); rev == nil {
78 t.Errorf("Unable to retrieve revision for hash:%s", TestBranchHash)
sbarbari17d7e222019-11-05 10:02:29 -050079 } else {
npujar9a30c702019-11-14 17:06:39 +053080 t.Logf("Got revision for hash:%s rev:%+v\n", TestBranchHash, rev)
sbarbari17d7e222019-11-05 10:02:29 -050081 }
82}
83
84// Set the added revision as the latest
85func TestBranch_LatestRevision(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +053086 addedRevision := TestBranchBranch.GetRevision(TestBranchHash)
87 TestBranchBranch.SetLatest(addedRevision)
sbarbari17d7e222019-11-05 10:02:29 -050088
npujar9a30c702019-11-14 17:06:39 +053089 rev := TestBranchBranch.GetLatest()
sbarbari17d7e222019-11-05 10:02:29 -050090 t.Logf("Retrieved latest revision :%+v", rev)
91
92 if rev == nil {
93 t.Error("Unable to retrieve latest revision")
npujar9a30c702019-11-14 17:06:39 +053094 } else if rev.GetHash() != TestBranchHash {
95 t.Errorf("Latest revision does not match hash: %s", TestBranchHash)
sbarbari17d7e222019-11-05 10:02:29 -050096 }
97}
98
99// Ensure that the origin revision remains and differs from subsequent revisions
100func TestBranch_OriginRevision(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +0530101 rev := TestBranchBranch.Origin
sbarbari17d7e222019-11-05 10:02:29 -0500102 t.Logf("Retrieved origin revision :%+v", rev)
103
104 if rev == nil {
105 t.Error("Unable to retrieve origin revision")
npujar9a30c702019-11-14 17:06:39 +0530106 } else if rev.GetHash() == TestBranchHash {
107 t.Errorf("Origin revision should differ from added revision: %s", TestBranchHash)
sbarbari17d7e222019-11-05 10:02:29 -0500108 }
109}