Year 2007.
[metaproxy-moved-to-github.git] / src / test_router_flexml.cpp
1 /* $Id: test_router_flexml.cpp,v 1.19 2007-01-25 14:05:54 adam Exp $
2    Copyright (c) 2005-2007, Index Data.
3
4    See the LICENSE file for details
5  */
6
7 #include "config.hpp"
8 #include <iostream>
9 #include <stdexcept>
10
11 #include "filter.hpp"
12 #include "router_flexml.hpp"
13 #include "factory_static.hpp"
14
15 #define BOOST_AUTO_TEST_MAIN
16 #include <boost/test/auto_unit_test.hpp>
17
18 using namespace boost::unit_test;
19
20 namespace mp = metaproxy_1;
21
22 static int tfilter_ref = 0;
23 class TFilter: public mp::filter::Base {
24 public:
25     void process(mp::Package & package) const {};
26     TFilter() { tfilter_ref++; };
27     ~TFilter() { tfilter_ref--; };
28 };
29
30 static mp::filter::Base* filter_creator()
31 {
32     return new TFilter;
33 }
34
35 // Pass well-formed XML and valid configuration to it (implicit NS)
36 BOOST_AUTO_UNIT_TEST( test_router_flexml_1 )
37 {
38     try
39     {
40         std::string xmlconf = "<?xml version=\"1.0\"?>\n"
41             "<metaproxy xmlns=\"http://indexdata.com/metaproxy\""
42             " version=\"1.0\">\n"
43             "  <start route=\"start\"/>\n"
44             "  <filters>\n"
45             "    <filter id=\"front_default\" type=\"frontend_net\">\n"
46             "      <port>@:210</port>\n"
47             "    </filter>\n"
48             "    <filter id=\"log_cout1\" type=\"log\">\n"
49             "      <message>my msg</message>\n"
50             "    </filter>\n"
51             "    <filter id=\"tfilter_id\" type=\"tfilter\"/>\n"
52             "    <filter id=\"log_cout2\" type=\"log\">\n"
53             "      <message>other</message>\n"
54             "    </filter>\n"
55             "  </filters>\n"
56             "  <routes>\n"  
57             "    <route id=\"start\">\n"
58             "      <filter refid=\"front_default\"/>\n"
59             "      <filter refid=\"log_cout1\"/>\n"
60             "      <filter type=\"tfilter\">\n"
61             "      </filter>\n"
62             "      <filter type=\"z3950_client\">\n"
63             "      </filter>\n"
64             "    </route>\n"
65             "  </routes>\n"
66             "</metaproxy>\n";
67
68         mp::FactoryStatic factory;
69         factory.add_creator("tfilter", filter_creator);
70         mp::RouterFleXML rflexml(xmlconf, factory);
71         BOOST_CHECK_EQUAL(tfilter_ref, 2);
72     }
73     catch ( std::runtime_error &e) {
74         std::cout << "std::runtime error: " << e.what() << "\n";
75         BOOST_CHECK (false);
76     }
77     catch ( ... ) {
78         BOOST_CHECK (false);
79     }
80     BOOST_CHECK_EQUAL(tfilter_ref, 0);
81 }
82
83 // Pass non-wellformed XML
84 BOOST_AUTO_UNIT_TEST( test_router_flexml_2 )
85 {
86     bool got_error_as_expected = false;
87     try
88     {
89         std::string xmlconf_invalid = "<?xml version=\"1.0\"?>\n"
90             "<mp:metaproxy xmlns:mp=\"http://indexdata.com/metaproxy\" version=\"1.0\">\n"
91             "  <start route=\"start\"/>\n"
92             "  <filters>\n"
93             "    <filter id=\"front_default\" type=\"frontend_net\">\n"
94             "      <port>@:210</port>\n";
95         
96         mp::FactoryFilter factory;
97         mp::RouterFleXML rflexml(xmlconf_invalid, factory);
98     }
99     catch ( mp::XMLError &e) {
100         std::cout << "XMLError: " << e.what() << "\n";
101         got_error_as_expected = true;
102     }
103     catch ( std::runtime_error &e) {
104         std::cout << "std::runtime error: " << e.what() << "\n";
105     }
106     catch ( ... ) {
107         ;
108     }
109     BOOST_CHECK(got_error_as_expected);
110 }
111
112 // Pass well-formed XML with explicit NS
113 BOOST_AUTO_UNIT_TEST( test_router_flexml_3 )
114 {
115     try
116     {
117         std::string xmlconf = "<?xml version=\"1.0\"?>\n"
118             "<mp:metaproxy xmlns:mp=\"http://indexdata.com/metaproxy\""
119             "  version=\"1.0\">\n"
120             "  <mp:start route=\"start\"/>\n"
121             "  <mp:filters>\n"
122             "    <mp:filter id=\"front_default\" type=\"frontend_net\">\n"
123             "      <port>@:210</port>\n"
124             "    </mp:filter>\n"
125             "    <mp:filter id=\"log_cout\" type=\"log\">\n"
126             "      <message>my msg</message>\n"
127             "    </mp:filter>\n"
128             "  </mp:filters>\n"
129             "  <mp:routes>\n"  
130             "    <mp:route id=\"start\">\n"
131             "      <mp:filter refid=\"front_default\"/>\n"
132             "      <mp:filter refid=\"log_cout\"/>\n"
133             "    </mp:route>\n"
134             "  </mp:routes>\n"
135             "</mp:metaproxy>\n";
136        
137         mp::FactoryStatic factory;
138         mp::RouterFleXML rflexml(xmlconf, factory);
139     }
140     catch ( std::runtime_error &e) {
141         std::cout << "std::runtime error: " << e.what() << "\n";
142         BOOST_CHECK (false);
143     }
144     catch ( ... ) {
145         BOOST_CHECK (false);
146     }
147 }
148
149 // Pass well-formed XML but bad filter type
150 BOOST_AUTO_UNIT_TEST( test_router_flexml_4 )
151 {
152     bool got_error_as_expected = false;
153     try
154     {
155         std::string xmlconf = "<?xml version=\"1.0\"?>\n"
156             "<metaproxy xmlns=\"http://indexdata.com/metaproxy\""
157             " version=\"1.0\">\n"
158             "  <start route=\"start\"/>\n" 
159             "  <filters>\n"
160             "    <filter id=\"front_default\" type=\"notknown\">\n"
161             "      <port>@:210</port>\n"
162             "    </filter>\n"
163             "  </filters>\n"
164             "  <routes>\n"  
165             "    <route id=\"start\">\n"
166             "      <filter refid=\"front_default\"/>\n"
167             "    </route>\n"
168             "  </routes>\n"
169             "</metaproxy>\n";
170
171         mp::FactoryStatic factory;
172         factory.add_creator("tfilter", filter_creator);
173         mp::RouterFleXML rflexml(xmlconf, factory);
174     }
175     catch ( mp::FactoryFilter::NotFound &e) {
176         std::cout << "mp::FactoryFilter::NotFound: " << e.what() << "\n";
177         got_error_as_expected = true;
178     }
179     catch ( std::runtime_error &e) {
180         std::cout << "std::runtime error: " << e.what() << "\n";
181     }
182     BOOST_CHECK(got_error_as_expected);
183 }
184
185
186 /*
187  * Local variables:
188  * c-basic-offset: 4
189  * indent-tabs-mode: nil
190  * c-file-style: "stroustrup"
191  * End:
192  * vim: shiftwidth=4 tabstop=8 expandtab
193  */