View | Details | Raw Unified | Return to bug 19542
Collapse All | Expand All

(-)a/Koha/Exceptions/Config.pm (+16 lines)
Line 0 Link Here
1
package Koha::Exceptions::Config;
2
3
use Modern::Perl;
4
5
use Exception::Class (
6
7
    'Koha::Exceptions::Config' => {
8
        description => 'Something went wrong!',
9
    },
10
    'Koha::Exceptions::Config::MissingEntry' => {
11
        isa => 'Koha::Exceptions::Config',
12
        description => 'The required entry is missing in the configuration file'
13
    }
14
);
15
16
1;
(-)a/Koha/SearchEngine/Elasticsearch.pm (+62 lines)
Lines 22-27 use base qw(Class::Accessor); Link Here
22
use C4::Context;
22
use C4::Context;
23
23
24
use Koha::Database;
24
use Koha::Database;
25
use Koha::Exceptions::Config;
25
use Koha::SearchFields;
26
use Koha::SearchFields;
26
use Koha::SearchMarcMaps;
27
use Koha::SearchMarcMaps;
27
28
Lines 29-34 use Carp; Link Here
29
use JSON;
30
use JSON;
30
use Modern::Perl;
31
use Modern::Perl;
31
use Readonly;
32
use Readonly;
33
use Search::Elasticsearch;
34
use Try::Tiny;
32
use YAML::Syck;
35
use YAML::Syck;
33
36
34
use Data::Dumper;    # TODO remove
37
use Data::Dumper;    # TODO remove
Lines 479-484 sub process_error { Link Here
479
    return "Unable to perform your search. Please try again.\n";
482
    return "Unable to perform your search. Please try again.\n";
480
}
483
}
481
484
485
=head2 _read_configuration
486
487
    my $conf = _read_configuration();
488
489
Reads the I<configuration file> and returns a hash structure with the
490
configuration information. It raises an exception if mandatory entries
491
are missing.
492
493
The hashref structure has the following form:
494
495
    {
496
        'nodes' => ['127.0.0.1:9200', 'anotherserver:9200'],
497
        'index_name' => 'koha_instance',
498
    }
499
500
This is configured by the following in the C<config> block in koha-conf.xml:
501
502
    <elasticsearch>
503
        <server>127.0.0.1:9200</server>
504
        <server>anotherserver:9200</server>
505
        <index_name>koha_instance</index_name>
506
    </elasticsearch>
507
508
=cut
509
510
sub _read_configuration {
511
512
    my $configuration;
513
514
    my $conf = C4::Context->config('elasticsearch');
515
    Koha::Exceptions::Config::MissingEntry->throw(
516
        "Missing 'elasticsearch' block in config file")
517
      unless defined $conf;
518
519
    if ( $conf && $conf->{server} ) {
520
        my $nodes = $conf->{server};
521
        if ( ref($nodes) eq 'ARRAY' ) {
522
            $configuration->{nodes} = $nodes;
523
        }
524
        else {
525
            $configuration->{nodes} = [$nodes];
526
        }
527
    }
528
    else {
529
        Koha::Exceptions::Config::MissingEntry->throw(
530
            "Missing 'server' entry in config file for elasticsearch");
531
    }
532
533
    if ( defined $conf->{index_name} ) {
534
        $configuration->{index_name} = $conf->{index_name};
535
    }
536
    else {
537
        Koha::Exceptions::Config::MissingEntry->throw(
538
            "Missing 'index_name' entry in config file for elasticsearch");
539
    }
540
541
    return $configuration;
542
}
543
482
1;
544
1;
483
545
484
__END__
546
__END__
(-)a/t/Koha/SearchEngine/Elasticsearch.t (-1 / +86 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
#
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Test::More tests => 1;
21
use Test::Exception;
22
23
use t::lib::Mocks;
24
25
use Koha::SearchEngine::Elasticsearch;
26
27
subtest '_read_configuration() tests' => sub {
28
29
    plan tests => 10;
30
31
    my $configuration;
32
    t::lib::Mocks::mock_config( 'elasticsearch', undef );
33
34
    # 'elasticsearch' missing in configuration
35
    throws_ok {
36
        $configuration = Koha::SearchEngine::Elasticsearch::_read_configuration;
37
    }
38
    'Koha::Exceptions::Config::MissingEntry',
39
      'Configuration problem, exception thrown';
40
    is(
41
        $@->message,
42
        "Missing 'elasticsearch' block in config file",
43
        'Exception message is correct'
44
    );
45
46
    # 'elasticsearch' present but no 'server' entry
47
    t::lib::Mocks::mock_config( 'elasticsearch', {} );
48
    throws_ok {
49
        $configuration = Koha::SearchEngine::Elasticsearch::_read_configuration;
50
    }
51
    'Koha::Exceptions::Config::MissingEntry',
52
      'Configuration problem, exception thrown';
53
    is(
54
        $@->message,
55
        "Missing 'server' entry in config file for elasticsearch",
56
        'Exception message is correct'
57
    );
58
59
    # 'elasticsearch' and 'server' entries present, but no 'index_name'
60
    t::lib::Mocks::mock_config( 'elasticsearch', { server => 'a_server' } );
61
    throws_ok {
62
        $configuration = Koha::SearchEngine::Elasticsearch::_read_configuration;
63
    }
64
    'Koha::Exceptions::Config::MissingEntry',
65
      'Configuration problem, exception thrown';
66
    is(
67
        $@->message,
68
        "Missing 'index_name' entry in config file for elasticsearch",
69
        'Exception message is correct'
70
    );
71
72
    # Correct configuration, only one server
73
    t::lib::Mocks::mock_config( 'elasticsearch',  { server => 'a_server', index_name => 'index' } );
74
75
    $configuration = Koha::SearchEngine::Elasticsearch::_read_configuration;
76
    is( $configuration->{index_name}, 'index', 'Index configuration parsed correctly' );
77
    is_deeply( $configuration->{nodes}, ['a_server'], 'Server configuration parsed correctly' );
78
79
    # Correct configuration, two servers
80
    my @servers = ('a_server', 'another_server');
81
    t::lib::Mocks::mock_config( 'elasticsearch', { server => \@servers, index_name => 'index' } );
82
83
    $configuration = Koha::SearchEngine::Elasticsearch::_read_configuration;
84
    is( $configuration->{index_name}, 'index', 'Index configuration parsed correctly' );
85
    is_deeply( $configuration->{nodes}, \@servers , 'Server configuration parsed correctly' );
86
};

Return to bug 19542