start fixing test for multi cat phenotype upload.
[sgn.git] / t / unit_fixture / CXGN / Uploading / Storebrapiobservations.t
blob07e89cbfcdb641db501a4453360c6fc23fdf6e54
1 use strict;
2 use warnings;
4 use lib 't/lib';
5 use SGN::Test::Fixture;
6 use Test::More;
7 use Test::WWW::Mechanize;
8 use SGN::Model::Cvterm;
9 use Data::Dumper;
10 use JSON;
11 use HTTP::Request;
12 use LWP::UserAgent;
14 my $f = SGN::Test::Fixture->new();
15 my $schema = $f->bcs_schema;
17 my $mech = Test::WWW::Mechanize->new(timeout=>30000);
18 my $response;
20 my $plot_id1 = $schema->resultset('Stock::Stock')->find({uniquename=>'test_trial210'})->stock_id;
21 my $plot_id2 = $schema->resultset('Stock::Stock')->find({uniquename=>'test_trial214'})->stock_id;
23 $mech->post_ok('http://localhost:3010/brapi/v1/token', [ "username"=> "janedoe", "password"=> "secretpw", "grant_type"=> "password" ]);
24 $response = decode_json $mech->content;
25 print STDERR Dumper $response;
26 is($response->{'metadata'}->{'status'}->[2]->{'message'}, 'Login Successfull');
27 my $sgn_session_id = $response->{access_token};
30 my $data = {
31     observations => [
32         {
33             observationDbId => '',
34             observationUnitDbId => $plot_id1,
35             observationVariableDbId => '70741',
36             collector => 'collector1',
37             observationTimeStamp => '2015-06-16T00:53:26Z',
38             value => '11'
39         },
40         {
41             observationDbId => '',
42             observationUnitDbId => $plot_id2,
43             observationVariableDbId => '70773',
44             collector => 'collector1',
45             observationTimeStamp => '2015-06-16T00:53:26Z',
46             value => '110'
47         },
48     ]
50 my $j = encode_json $data;
52 my $req = HTTP::Request->new( "PUT" => "http://localhost:3010/brapi/v1/observations" );
53 $req->content_type( 'application/json' );
54 $req->content_length(
55     do { use bytes; length( $j ) }
57 $req->content( $j );
59 my $ua = LWP::UserAgent->new();
60 my $res = $ua->request($req);
61 $response = decode_json $res->content;
62 print STDERR Dumper $response;
63 is_deeply($response, {
64           'result' => undef,
65           'metadata' => {
66                           'status' => [
67                                         {
68                                           'messageType' => 'ERROR',
69                                           'message' => 'You must login and have permission to access this BrAPI call.'
70                                         }
71                                       ],
72                           'datafiles' => [],
73                           'pagination' => {
74                                             'pageSize' => 1,
75                                             'totalCount' => 0,
76                                             'totalPages' => 0,
77                                             'currentPage' => 0
78                                           }
79                         }
80         });
82 my $trait_id1 = SGN::Model::Cvterm->get_cvterm_row_from_trait_name($schema, 'CO_334:0000092')->cvterm_id();
83 my $trait_id2 = SGN::Model::Cvterm->get_cvterm_row_from_trait_name($schema, 'CO_334:0000016')->cvterm_id();
85 my $data = {
86     access_token => $sgn_session_id,
87     observations => [
88         {
89             observationDbId => '',
90             observationUnitDbId => $plot_id1,
91             observationVariableDbId => $trait_id1,
92             collector => 'collector1',
93             observationTimeStamp => '2015-06-16T00:53:26Z',
94             value => '11'
95         },
96         {
97             observationDbId => '',
98             observationUnitDbId => $plot_id2,
99             observationVariableDbId => $trait_id2,
100             collector => 'collector1',
101             observationTimeStamp => '2015-06-16T00:53:26Z',
102             value => '110'
103         },
104     ]
106 $j = encode_json $data;
107 $req = HTTP::Request->new( "PUT" => "http://localhost:3010/brapi/v1/observations" );
108 $req->content_type( 'application/json' );
109 $req->content_length(
110     do { use bytes; length( $j ) }
112 $req->content( $j );
114 $ua = LWP::UserAgent->new();
115 $res = $ua->request($req);
116 $response = decode_json $res->content;
117 print STDERR Dumper $response;
119 #Remove observationdbid from result because it is variable
120 foreach (@{$response->{result}->{observations}}){
121     delete $_->{observationDbId};
124 is_deeply($response, {
125           'result' => {
126                         'observations' => [
127                                             {
128                                               'observationLevel' => 'plot',
129                                               'observationTimeStamp' => '2015-06-16T00:53:26Z',
130                                               'germplasmName' => 'test_accession3',
131                                               'observationUnitName' => 'test_trial210',
132                                               'uploadedBy' => 'collector1',
133                                               'collector' => 'collector1',
134                                               'germplasmDbId' => 38842,
135                                               'observationUnitDbId' => 38866,
136                                               'value' => '11',
137                                               'observationVariableName' => 'dry matter content percentage',
138                                               'observationVariableDbId' => '70741',
139                                               'studyDbId' => 137,
140                                               'externalReferences' => undef,
141                                               'additionalInfo' => undef,
142                                             },
143                                             {
144                                               'observationLevel' => 'plot',
145                                               'observationTimeStamp' => '2015-06-16T00:53:26Z',
146                                               'germplasmName' => 'test_accession4',
147                                               'observationUnitName' => 'test_trial214',
148                                               'uploadedBy' => 'collector1',
149                                               'collector' => 'collector1',
150                                               'germplasmDbId' => 38843,
151                                               'observationUnitDbId' => 38870,
152                                               'value' => '110',
153                                               'observationVariableDbId' => '70773',
154                                               'observationVariableName' => 'fresh shoot weight measurement in kg',
155                                               'studyDbId' => 137,
156                                               'externalReferences' => undef,
157                                               'additionalInfo' => undef,
158                                             }
159                                           ]
160                       },
161           'metadata' => {
162                           'datafiles' => [],
163                           'status' => [
164                                         {
165                                           'message' => 'BrAPI base call found with page=0, pageSize=10',
166                                           'messageType' => 'INFO'
167                                         },
168                                         {
169                                           'messageType' => 'INFO',
170                                           'message' => 'Loading CXGN::BrAPI::v1::Observations'
171                                         },
172                                         {
173                                           'message' => 'Request structure is valid',
174                                           'messageType' => 'info'
175                                         },
176                                         {
177                                           'messageType' => 'info',
178                                           'message' => 'Request data is valid'
179                                         },
180                                         {
181                                           'message' => 'File for incoming brapi obserations saved in archive.',
182                                           'messageType' => 'info'
183                                         },
184                                         {
185                                           'messageType' => 'INFO',
186                                           'message' => 'All values in your file have been successfully processed!<br><br>2 new values stored<br>0 previously stored values skipped<br>0 previously stored values overwritten<br>0 previously stored values removed<br><br>'
187                                         }
188                                       ],
189                           'pagination' => {
190                                             'totalPages' => 1,
191                                             'totalCount' => 2,
192                                             'pageSize' => 10,
193                                             'currentPage' => 0
194                                           }
195                         }
196         });
198 done_testing;