Rename from yp2 to metaproxy. The namespace for all definitions
[metaproxy-moved-to-github.git] / src / test_filter2.cpp
1 /* $Id: test_filter2.cpp,v 1.18 2006-03-16 10:40:59 adam Exp $
2    Copyright (c) 2005-2006, Index Data.
3
4 %LICENSE%
5  */
6
7 #include <libxml/parser.h>
8 #include <libxml/tree.h>
9
10 #include "config.hpp"
11 #include "filter.hpp"
12 #include "router_chain.hpp"
13 #include "package.hpp"
14
15 #include <iostream>
16
17 #define BOOST_AUTO_TEST_MAIN
18 #include <boost/test/auto_unit_test.hpp>
19
20 using namespace boost::unit_test;
21
22 namespace mp = metaproxy_1;
23
24 class FilterConstant: public mp::filter::Base {
25 public:
26     FilterConstant() : m_constant(1234) { };
27     void process(mp::Package & package) const {
28         package.data() = m_constant;
29         package.move();
30     };
31     void configure(const xmlNode* ptr = 0);
32     int get_constant() const { return m_constant; };
33 private:
34     bool parse_xml_text(const xmlNode *xml_ptr, bool &val);
35     bool parse_xml_text(const xmlNode *xml_ptr, std::string &val);
36 private:
37     const xmlNode *m_ptr;
38     int m_constant;
39 };
40
41
42 void FilterConstant::configure(const xmlNode* ptr)
43 {
44     m_ptr = ptr;
45     
46     BOOST_CHECK_EQUAL (ptr->type, XML_ELEMENT_NODE);
47     BOOST_CHECK_EQUAL(std::string((const char *) ptr->name), "filter");
48     
49     const struct _xmlAttr *attr;
50     
51     for (attr = ptr->properties; attr; attr = attr->next)
52     {
53         BOOST_CHECK_EQUAL( std::string((const char *)attr->name), "type");
54         const xmlNode *val = attr->children;
55         BOOST_CHECK_EQUAL(val->type, XML_TEXT_NODE);
56         BOOST_CHECK_EQUAL(std::string((const char *)val->content), "constant");
57     }
58     const xmlNode *p = ptr->children;
59     for (; p; p = p->next)
60     {
61         if (p->type != XML_ELEMENT_NODE)
62             continue;
63         
64         BOOST_CHECK_EQUAL (p->type, XML_ELEMENT_NODE);
65         BOOST_CHECK_EQUAL(std::string((const char *) p->name), "value");
66         
67         const xmlNode *val = p->children;
68         BOOST_CHECK(val);
69         if (!val)
70             continue;
71         
72         BOOST_CHECK_EQUAL(val->type, XML_TEXT_NODE);
73         BOOST_CHECK_EQUAL(std::string((const char *)val->content), "2");
74
75         m_constant = atoi((const char *) val->content);
76     }
77 }
78
79 bool FilterConstant::parse_xml_text(const xmlNode  *xml_ptr, bool &val)
80 {
81     std::string v;
82     if (!parse_xml_text(xml_ptr, v))
83         return false;
84     if (v.length() == 1 && v[0] == '1')
85         val = true;
86     else
87         val = false;
88     return true;
89 }
90
91 bool FilterConstant::parse_xml_text(const xmlNode *xml_ptr, std::string &val)
92 {
93     xmlNodePtr ptr = (xmlNodePtr) xml_ptr;
94     bool found = false;
95     std::string v;
96     for(ptr = ptr->children; ptr; ptr = ptr->next)
97         if (ptr->type == XML_TEXT_NODE)
98         {
99             xmlChar *t = ptr->content;
100             if (t)
101             {
102                 v += (const char *) t;
103                 found = true;
104             }
105         }
106     if (found)
107         val = v;
108     return found;
109 }
110
111 // This filter dose not have a configure function
112     
113 class FilterDouble: public mp::filter::Base {
114 public:
115     void process(mp::Package & package) const {
116         package.data() = package.data() * 2;
117         package.move();
118     };
119 };
120
121     
122 BOOST_AUTO_UNIT_TEST( testfilter2_1 ) 
123 {
124     try {
125         FilterConstant fc;
126         FilterDouble fd;
127
128         {
129             mp::RouterChain router1;
130             
131             // test filter set/get/exception
132             router1.append(fc);
133             
134             router1.append(fd);
135
136             mp::Session session;
137             mp::Origin origin;
138             mp::Package pack(session, origin);
139             
140             pack.router(router1).move(); 
141             
142             BOOST_CHECK_EQUAL(pack.data(), 2468);
143             
144         }
145         
146         {
147             mp::RouterChain router2;
148             
149             router2.append(fd);
150             router2.append(fc);
151             
152             mp::Session session;
153             mp::Origin origin;
154             mp::Package pack(session, origin);
155          
156             pack.router(router2).move();
157      
158             BOOST_CHECK_EQUAL(pack.data(), 1234);
159             
160         }
161
162     }
163     catch (std::exception &e) {
164         std::cout << e.what() << "\n";
165         BOOST_CHECK (false);
166     }
167     catch ( ...) {
168         BOOST_CHECK (false);
169     }
170
171 }
172
173 BOOST_AUTO_UNIT_TEST( testfilter2_2 ) 
174 {
175     try {
176         FilterConstant fc;
177         BOOST_CHECK_EQUAL(fc.get_constant(), 1234);
178
179         mp::filter::Base *base = &fc;
180
181         std::string some_xml = "<?xml version=\"1.0\"?>\n"
182             "<filter type=\"constant\">\n"
183             " <value>2</value>\n"
184             "</filter>";
185         
186         // std::cout << some_xml  << std::endl;
187
188         xmlDocPtr doc = xmlParseMemory(some_xml.c_str(), some_xml.size());
189
190         BOOST_CHECK(doc);
191
192         if (doc)
193         {
194             xmlNodePtr root_element = xmlDocGetRootElement(doc);
195             
196             base->configure(root_element);
197             
198             xmlFreeDoc(doc);
199         }
200
201         BOOST_CHECK_EQUAL(fc.get_constant(), 2);
202     }
203     catch (std::exception &e) {
204         std::cout << e.what() << "\n";
205         BOOST_CHECK (false);
206     }
207     catch ( ...) {
208         BOOST_CHECK (false);
209     }
210
211 }
212
213 /*
214  * Local variables:
215  * c-basic-offset: 4
216  * indent-tabs-mode: nil
217  * c-file-style: "stroustrup"
218  * End:
219  * vim: shiftwidth=4 tabstop=8 expandtab
220  */