0059ab159a67384ff968f0b9c9965c6bf09fa5e7
[metaproxy-moved-to-github.git] / src / test_router_flexml.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2011 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 };
42
43 static mp::filter::Base* filter_creator()
44 {
45     return new TFilter;
46 }
47
48 // Pass well-formed XML and valid configuration to it (implicit NS)
49 BOOST_AUTO_TEST_CASE( test_router_flexml_1 )
50 {
51     try
52     {
53         std::string xmlconf = "<?xml version=\"1.0\"?>\n"
54             "<metaproxy xmlns=\"http://indexdata.com/metaproxy\""
55             " version=\"1.0\">\n"
56             "  <start route=\"start\"/>\n"
57             "  <filters>\n"
58             "    <filter id=\"front_default\" type=\"frontend_net\">\n"
59             "      <port>@:210</port>\n"
60             "    </filter>\n"
61             "    <filter id=\"log_cout1\" type=\"log\">\n"
62             "      <message>my msg</message>\n"
63             "    </filter>\n"
64             "    <filter id=\"tfilter_id\" type=\"tfilter\"/>\n"
65             "    <filter id=\"log_cout2\" type=\"log\">\n"
66             "      <message>other</message>\n"
67             "    </filter>\n"
68             "  </filters>\n"
69             "  <routes>\n"  
70             "    <route id=\"start\">\n"
71             "      <filter refid=\"front_default\"/>\n"
72             "      <filter refid=\"log_cout1\"/>\n"
73             "      <filter type=\"tfilter\">\n"
74             "      </filter>\n"
75             "      <filter type=\"z3950_client\">\n"
76             "      </filter>\n"
77             "    </route>\n"
78             "  </routes>\n"
79             "</metaproxy>\n";
80
81         mp::FactoryStatic factory;
82         factory.add_creator("tfilter", filter_creator);
83         mp::RouterFleXML rflexml(xmlconf, factory, true);
84         BOOST_CHECK_EQUAL(tfilter_ref, 2);
85     }
86     catch ( std::runtime_error &e) {
87         std::cout << "std::runtime error: " << e.what() << "\n";
88         BOOST_CHECK (false);
89     }
90     catch ( ... ) {
91         BOOST_CHECK (false);
92     }
93     BOOST_CHECK_EQUAL(tfilter_ref, 0);
94 }
95
96 // Pass non-wellformed XML
97 BOOST_AUTO_TEST_CASE( test_router_flexml_2 )
98 {
99     bool got_error_as_expected = false;
100     try
101     {
102         std::string xmlconf_invalid = "<?xml version=\"1.0\"?>\n"
103             "<mp:metaproxy xmlns:mp=\"http://indexdata.com/metaproxy\" version=\"1.0\">\n"
104             "  <start route=\"start\"/>\n"
105             "  <filters>\n"
106             "    <filter id=\"front_default\" type=\"frontend_net\">\n"
107             "      <port>@:210</port>\n";
108         
109         mp::FactoryFilter factory;
110         mp::RouterFleXML rflexml(xmlconf_invalid, factory, true);
111     }
112     catch ( mp::XMLError &e) {
113         std::cout << "XMLError: " << e.what() << "\n";
114         got_error_as_expected = true;
115     }
116     catch ( std::runtime_error &e) {
117         std::cout << "std::runtime error: " << e.what() << "\n";
118     }
119     catch ( ... ) {
120         ;
121     }
122     BOOST_CHECK(got_error_as_expected);
123 }
124
125 // Pass well-formed XML with explicit NS
126 BOOST_AUTO_TEST_CASE( test_router_flexml_3 )
127 {
128     try
129     {
130         std::string xmlconf = "<?xml version=\"1.0\"?>\n"
131             "<mp:metaproxy xmlns:mp=\"http://indexdata.com/metaproxy\""
132             "  version=\"1.0\">\n"
133             "  <mp:start route=\"start\"/>\n"
134             "  <mp:filters>\n"
135             "    <mp:filter id=\"front_default\" type=\"frontend_net\">\n"
136             "      <port>@:210</port>\n"
137             "    </mp:filter>\n"
138             "    <mp:filter id=\"log_cout\" type=\"log\">\n"
139             "      <message>my msg</message>\n"
140             "    </mp:filter>\n"
141             "  </mp:filters>\n"
142             "  <mp:routes>\n"  
143             "    <mp:route id=\"start\">\n"
144             "      <mp:filter refid=\"front_default\"/>\n"
145             "      <mp:filter refid=\"log_cout\"/>\n"
146             "    </mp:route>\n"
147             "  </mp:routes>\n"
148             "</mp:metaproxy>\n";
149        
150         mp::FactoryStatic factory;
151         mp::RouterFleXML rflexml(xmlconf, factory, true);
152     }
153     catch ( std::runtime_error &e) {
154         std::cout << "std::runtime error: " << e.what() << "\n";
155         BOOST_CHECK (false);
156     }
157     catch ( ... ) {
158         BOOST_CHECK (false);
159     }
160 }
161
162 // Pass well-formed XML but bad filter type
163 BOOST_AUTO_TEST_CASE( test_router_flexml_4 )
164 {
165     bool got_error_as_expected = false;
166     try
167     {
168         std::string xmlconf = "<?xml version=\"1.0\"?>\n"
169             "<metaproxy xmlns=\"http://indexdata.com/metaproxy\""
170             " version=\"1.0\">\n"
171             "  <start route=\"start\"/>\n" 
172             "  <filters>\n"
173             "    <filter id=\"front_default\" type=\"notknown\">\n"
174             "      <port>@:210</port>\n"
175             "    </filter>\n"
176             "  </filters>\n"
177             "  <routes>\n"  
178             "    <route id=\"start\">\n"
179             "      <filter refid=\"front_default\"/>\n"
180             "    </route>\n"
181             "  </routes>\n"
182             "</metaproxy>\n";
183
184         mp::FactoryStatic factory;
185         factory.add_creator("tfilter", filter_creator);
186         mp::RouterFleXML rflexml(xmlconf, factory, true);
187     }
188     catch ( mp::FactoryFilter::NotFound &e) {
189         std::cout << "mp::FactoryFilter::NotFound: " << e.what() << "\n";
190         got_error_as_expected = true;
191     }
192     catch ( std::runtime_error &e) {
193         std::cout << "std::runtime error: " << e.what() << "\n";
194     }
195     BOOST_CHECK(got_error_as_expected);
196 }
197
198
199 /*
200  * Local variables:
201  * c-basic-offset: 4
202  * c-file-style: "Stroustrup"
203  * indent-tabs-mode: nil
204  * End:
205  * vim: shiftwidth=4 tabstop=8 expandtab
206  */
207