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