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