Removed /usr/local from CDPATH
[clearscm.git] / test / testrest.pl
1 #!/usr/brcm/ba/bin/perl
2 use strict;
3 use warnings;
4
5 use FindBin;
6 use Getopt::Long;
7
8 use lib "$FindBin::Bin/../lib";
9
10 use Clearquest::REST;
11 use Display;
12 use Utils;
13
14 my $cq;
15
16 =pod
17
18 Usage $FindBin::Script: [-get] [-add] [-modify] [-change] [-delete]
19
20 =cut
21
22 END {
23   # Always remember to call disconnect for any instanciated Clearquest::REST
24   # object
25   $cq->disconnect if $cq;
26 } # END
27
28 sub displayRecord (%) {
29   my (%record) = @_;
30   
31   display '-' x 79;
32   
33   foreach (keys %record) {
34     display_nolf "$_: ";
35   
36     if (ref $record{$_} eq 'ARRAY') {
37       display join ", ", @{$record{$_}};
38     } elsif ($record{$_}) {
39       display $record{$_};
40     } else {
41       display "<undef>";
42     } # if
43   } # foreach  
44 } # displayRecord
45
46 sub displayResults (@) {
47   my (@records) = @_;
48   
49   if (@records) {
50     displayRecord %$_ foreach (@records);
51   } else {
52     display "Did not find any records";
53   } # if
54 } # displayResults
55
56 sub testGetRecord ($$;@) {
57   my ($table, $key, @fields) = @_;
58   
59   display "Testing get table: $table key: $key";
60   
61   displayRecord $cq->get ($table, $key, @fields);  
62 } # testGetRecord
63
64 sub testFindRecord ($$;@) {
65   my ($table, $condition, @fields) = @_;
66   
67   display "Testing find table: $table condition: $condition";
68   
69   my ($result, $nbrRecs) = $cq->find ($table, $condition, @fields);
70
71   display "$nbrRecs records qualified";
72
73   while (my %record = $cq->getNext ($result)) {
74     displayRecord %record;
75   } # while
76 } # testFindRecord
77
78 sub testModifyRecord ($$;%) {
79   my ($table, $key, %update) = @_;
80   
81   display "Testing modify table: $table key: $key";
82   
83   my $errmsg = $cq->modify ($table, $key, undef, %update);
84   
85   display $errmsg;
86 } # testModifyRecord
87
88 sub testChangeState ($$) {
89   my ($table, $key) = @_;
90   
91   my %record = $cq->get ($table, $key, ('State'));
92
93   my ($action, %update);
94   
95   if ($record{State} eq 'Assigned') {
96     $action                  = 'AdminAssignToSubmit';
97     $update{Stability_Issue} = 'User Fault';
98   } else {
99     $action                  = 'Assign';
100     $update{Stability_Issue} = 'Assert';
101   } # if
102   
103   display "Testing change state table: $table key: $key action: $action";
104   
105   my $errmsg = $cq->modify ($table, $key, $action, %update);
106   
107   display $errmsg;
108 } # testChangeState
109
110 sub testAddRecord ($%) {
111   my ($table, %record) = @_;
112   
113   display "Testing adding table: $table";
114   
115   my $errmsg = $cq->add ($table, %record);
116   
117   display $errmsg;
118 } # testAddRecord
119
120 sub testDeleteRecord ($$) {
121   my ($table, $key) = @_;
122   
123   display "Testing deleting table: $table key: $key";
124   
125   my $errmsg = $cq->delete ($table, $key);
126   
127   display $errmsg;
128 } # testDeleteRecord
129
130 my %opts;
131
132 GetOptions (
133   \%opts,
134   'get',
135   'add',
136   'modify',
137   'change',
138   'delete'
139 ) || Usage;
140
141 # If nothing is set then do everything
142 unless ($opts{get}    or
143         $opts{add}    or
144         $opts{modify} or
145         $opts{change} or
146         $opts{delete}
147   ) {
148   $opts{get} = $opts{add} = $opts{modify} = $opts{change} = 1;
149 } # unless
150
151 # If we are testing add or delete then toggle on the other one
152 $opts{delete} = 1 if $opts{add};
153 $opts{add}    = 1 if $opts{delete};
154
155 $cq = Clearquest::REST->new;
156
157 if ($opts{get}) {
158   # Get record by key
159   testGetRecord 'Project', 'Athena';
160
161   # Get record by condition
162   testFindRecord 'VersionInfo', 'Deprecated = 1';
163
164   # Get record by key with field list
165   testFindRecord 'VersionInfo', 'VersionStr = 1.0', ('VersionStr',   'Deprecated');
166
167   # Get record by condition with field list
168   testFindRecord 'CategorySub', 'Category="Customer-HW"', ('Category', 'CategoryType', 'SubCategory');
169 } # if
170
171 if ($opts{add}) {
172   # Add a record
173   testAddRecord    'VersionInfo', (
174     VersionStr => '2.0',
175     Projects   => {
176       Project  => ['Island', '21331', 'Hera'],
177     },
178     Visibility => 'Nokia Corporation',
179   );
180 } # if
181
182 if ($opts{modify}) {
183   # Modify a record
184   testModifyRecord ('VersionInfo', '1.0', (
185     Deprecated => 1,
186     Projects   => { 
187       Project => ['Island', 'Athena'],
188     },
189   ));
190 } # if
191
192 if ($opts{change}) {
193   # Change State
194   testChangeState 'Defect', 't_sbx00018584';
195 } # if
196
197 if ($opts{add}) {
198   # Delete that record
199   testDeleteRecord 'VersionInfo', '2.0';
200 } # if
201
202 display "done";