blob: cf8406cc422d3d3aa7f48920a113b66c5acfb5ff [file] [log] [blame]
khenaidoobf6e7bb2018-08-14 22:27:29 -04001/*
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 Barbarie4a2564d2018-07-26 11:02:58 -040016package model
17
18import (
19 "crypto/md5"
20 "fmt"
21 "testing"
22)
23
24var (
Stephane Barbarie1039ec42019-02-04 10:43:16 -050025 TestBranch_BRANCH *Branch
26 TestBranch_HASH string
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040027)
28
Stephane Barbarie1039ec42019-02-04 10:43:16 -050029// Create a new branch and ensure that fields are populated
30func TestBranch_NewBranch(t *testing.T) {
Stephane Barbarie06c4a742018-10-01 11:09:32 -040031 node := &node{}
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040032 hash := fmt.Sprintf("%x", md5.Sum([]byte("origin_hash")))
Stephane Barbarieec0919b2018-09-05 14:14:29 -040033 origin := &NonPersistedRevision{
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040034 Config: &DataRevision{},
Stephane Barbarieec0919b2018-09-05 14:14:29 -040035 Children: make(map[string][]Revision),
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040036 Hash: hash,
Stephane Barbarieec0919b2018-09-05 14:14:29 -040037 Branch: &Branch{},
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040038 }
39 txid := fmt.Sprintf("%x", md5.Sum([]byte("branch_transaction_id")))
40
Stephane Barbarie1039ec42019-02-04 10:43:16 -050041 TestBranch_BRANCH = NewBranch(node, txid, origin, true)
42 t.Logf("New Branch(txid:%s) created: %+v\n", txid, TestBranch_BRANCH)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040043
Stephane Barbarie1039ec42019-02-04 10:43:16 -050044 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 Barbarie4a2564d2018-07-26 11:02:58 -040055}
56
Stephane Barbarie1039ec42019-02-04 10:43:16 -050057// Add a new revision to the branch
58func TestBranch_AddRevision(t *testing.T) {
59 TestBranch_HASH = fmt.Sprintf("%x", md5.Sum([]byte("revision_hash")))
Stephane Barbarieec0919b2018-09-05 14:14:29 -040060 rev := &NonPersistedRevision{
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040061 Config: &DataRevision{},
Stephane Barbarieec0919b2018-09-05 14:14:29 -040062 Children: make(map[string][]Revision),
Stephane Barbarie1039ec42019-02-04 10:43:16 -050063 Hash: TestBranch_HASH,
Stephane Barbarieec0919b2018-09-05 14:14:29 -040064 Branch: &Branch{},
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040065 }
66
Stephane Barbarie1039ec42019-02-04 10:43:16 -050067 TestBranch_BRANCH.AddRevision(rev)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040068 t.Logf("Added revision: %+v\n", rev)
Stephane Barbarie1039ec42019-02-04 10:43:16 -050069
70 if len(TestBranch_BRANCH.Revisions) == 0 {
71 t.Errorf("Branch revisions map is empty")
72 }
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040073}
74
Stephane Barbarie1039ec42019-02-04 10:43:16 -050075// Ensure that the added revision can be retrieved
76func 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 Barbarie4a2564d2018-07-26 11:02:58 -040082}
Stephane Barbarie1039ec42019-02-04 10:43:16 -050083
84// Set the added revision as the latest
85func 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 Barbarie4a2564d2018-07-26 11:02:58 -040097}
Stephane Barbarie1039ec42019-02-04 10:43:16 -050098
99// Ensure that the origin revision remains and differs from subsequent revisions
100func 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 Barbarie4a2564d2018-07-26 11:02:58 -0400109}