Windows: use Boost 1.59, msvc 14.0
[metaproxy-moved-to-github.git] / src / test_router_flexml.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) Index Data
3
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19 #include "config.hpp"
20 #include <iostream>
21 #include <stdexcept>
22
23 #include <metaproxy/filter.hpp>
24 #include "router_flexml.hpp"
25 #include "factory_static.hpp"
26
27 #define BOOST_AUTO_TEST_MAIN
28 #define BOOST_TEST_DYN_LINK
29 #include <boost/test/auto_unit_test.hpp>
30
31 using namespace boost::unit_test;
32
33 namespace mp = metaproxy_1;
34
35 static int tfilter_ref = 0;
36 class TFilter: public mp::filter::Base {
37 public:
38     void process(mp::Package & package) const {};
39     TFilter() { tfilter_ref++; };
40     ~TFilter() { tfilter_ref--; };
41     void configure(const xmlNode* ptr, bool test_only, const char *path) {};
42 };
43
44 static mp::filter::Base* filter_creator()
45 {
46     return new TFilter;
47 }
48
49 // Pass well-formed XML and valid configuration to it (implicit NS)
50 BOOST_AUTO_TEST_CASE( test_router_flexml_1 )
51 {
52     try
53     {
54         std::string xmlconf = "<?xml version=\"1.0\"?>\n"
55             "<metaproxy xmlns=\"http://indexdata.com/metaproxy\""
56             " version=\"1.0\">\n"
57             "  <start route=\"start\"/>\n"
58             "  <filters>\n"
59             "    <filter id=\"front_default\" type=\"frontend_net\">\n"
60             "      <port>@:210</port>\n"
61             "    </filter>\n"
62             "    <filter id=\"log_cout1\" type=\"log\">\n"
63             "      <message>my msg</message>\n"
64             "    </filter>\n"
65             "    <filter id=\"tfilter_id\" type=\"tfilter\"/>\n"
66             "    <filter id=\"log_cout2\" type=\"log\">\n"
67             "      <message>other</message>\n"
68             "    </filter>\n"
69             "  </filters>\n"
70             "  <routes>\n"
71             "    <route id=\"start\">\n"
72             "      <filter refid=\"front_default\"/>\n"
73             "      <filter refid=\"log_cout1\"/>\n"
74             "      <filter type=\"tfilter\">\n"
75             "      </filter>\n"
76             "      <filter type=\"z3950_client\">\n"
77             "      </filter>\n"
78             "    </route>\n"
79             "  </routes>\n"
80             "</metaproxy>\n";
81
82         mp::FactoryStatic factory;
83         factory.add_creator("tfilter", filter_creator);
84         mp::RouterFleXML rflexml(xmlconf, factory, true);
85         BOOST_CHECK_EQUAL(tfilter_ref, 2);
86     }
87     catch ( std::runtime_error &e) {
88         std::cout << "std::runtime error: " << e.what() << "\n";
89         BOOST_CHECK (false);
90     }
91     catch ( ... ) {
92         BOOST_CHECK (false);
93     }
94     BOOST_CHECK_EQUAL(tfilter_ref, 0);
95 }
96
97 // Pass non-wellformed XML
98 BOOST_AUTO_TEST_CASE( test_router_flexml_2 )
99 {
100     bool got_error_as_expected = false;
101     try
102     {
103         std::string xmlconf_invalid = "<?xml version=\"1.0\"?>\n"
104             "<mp:metaproxy xmlns:mp=\"http://indexdata.com/metaproxy\" version=\"1.0\">\n"
105             "  <start route=\"start\"/>\n"
106             "  <filters>\n"
107             "    <filter id=\"front_default\" type=\"frontend_net\">\n"
108             "      <port>@:210</port>\n";
109
110         mp::FactoryFilter factory;
111         mp::RouterFleXML rflexml(xmlconf_invalid, factory, true);
112     }
113     catch ( mp::XMLError &e) {
114         std::cout << "XMLError: " << e.what() << "\n";
115         got_error_as_expected = true;
116     }
117     catch ( std::runtime_error &e) {
118         std::cout << "std::runtime error: " << e.what() << "\n";
119     }
120     catch ( ... ) {
121         ;
122     }
123     BOOST_CHECK(got_error_as_expected);
124 }
125
126 // Pass well-formed XML with explicit NS
127 BOOST_AUTO_TEST_CASE( test_router_flexml_3 )
128 {
129     try
130     {
131         std::string xmlconf = "<?xml version=\"1.0\"?>\n"
132             "<mp:metaproxy xmlns:mp=\"http://indexdata.com/metaproxy\""
133             "  version=\"1.0\">\n"
134             "  <mp:start route=\"start\"/>\n"
135             "  <mp:filters>\n"
136             "    <mp:filter id=\"front_default\" type=\"frontend_net\">\n"
137             "      <port>@:210</port>\n"
138             "    </mp:filter>\n"
139             "    <mp:filter id=\"log_cout\" type=\"log\">\n"
140             "      <message>my msg</message>\n"
141             "    </mp:filter>\n"
142             "  </mp:filters>\n"
143             "  <mp:routes>\n"
144             "    <mp:route id=\"start\">\n"
145             "      <mp:filter refid=\"front_default\"/>\n"
146             "      <mp:filter refid=\"log_cout\"/>\n"
147             "    </mp:route>\n"
148             "  </mp:routes>\n"
149             "</mp:metaproxy>\n";
150
151         mp::FactoryStatic factory;
152         mp::RouterFleXML rflexml(xmlconf, factory, true);
153     }
154     catch ( std::runtime_error &e) {
155         std::cout << "std::runtime error: " << e.what() << "\n";
156         BOOST_CHECK (false);
157     }
158     catch ( ... ) {
159         BOOST_CHECK (false);
160     }
161 }
162
163 // Pass well-formed XML but bad filter type
164 BOOST_AUTO_TEST_CASE( test_router_flexml_4 )
165 {
166     bool got_error_as_expected = false;
167     try
168     {
169         std::string xmlconf = "<?xml version=\"1.0\"?>\n"
170             "<metaproxy xmlns=\"http://indexdata.com/metaproxy\""
171             " version=\"1.0\">\n"
172             "  <start route=\"start\"/>\n"
173             "  <filters>\n"
174             "    <filter id=\"front_default\" type=\"notknown\">\n"
175             "      <port>@:210</port>\n"
176             "    </filter>\n"
177             "  </filters>\n"
178             "  <routes>\n"
179             "    <route id=\"start\">\n"
180             "      <filter refid=\"front_default\"/>\n"
181             "    </route>\n"
182             "  </routes>\n"
183             "</metaproxy>\n";
184
185         mp::FactoryStatic factory;
186         factory.add_creator("tfilter", filter_creator);
187         mp::RouterFleXML rflexml(xmlconf, factory, true);
188     }
189     catch ( mp::FactoryFilter::NotFound &e) {
190         std::cout << "mp::FactoryFilter::NotFound: " << e.what() << "\n";
191         got_error_as_expected = true;
192     }
193     catch ( std::runtime_error &e) {
194         std::cout << "std::runtime error: " << e.what() << "\n";
195     }
196     BOOST_CHECK(got_error_as_expected);
197 }
198
199
200 /*
201  * Local variables:
202  * c-basic-offset: 4
203  * c-file-style: "Stroustrup"
204  * indent-tabs-mode: nil
205  * End:
206  * vim: shiftwidth=4 tabstop=8 expandtab
207  */
208