88cb3a523a60c6227cb57abf1bea91dec66a39a2
[ric-app/admin.git] / test / unit_test_admission_policy.cc
1 /*
2 ==================================================================================
3
4         Copyright (c) 2018-2019 AT&T Intellectual Property.
5
6    Licensed under the Apache License, Version 2.0 (the "License");
7    you may not use this file except in compliance with the License.
8    You may obtain a copy of the License at
9
10        http://www.apache.org/licenses/LICENSE-2.0
11
12    Unless required by applicable law or agreed to in writing, software
13    distributed under the License is distributed on an "AS IS" BASIS,
14    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    See the License for the specific language governing permissions and
16    limitations under the License.
17 ==================================================================================
18 */
19
20 /* Author : Ashwin Sridharan
21    Date   : Sept 2019
22 */
23
24 #define CATCH_CONFIG_MAIN
25 #include <catch2/catch.hpp>
26
27
28 #include <admission_policy.hpp>
29
30 #define SCHEMA_FILE "../schemas/adm-ctrl-xapp-policy-schema.json"
31 #define SAMPLE_FILE "../schemas/samples.json"
32 #define VES_FILE  "../schemas/ves_schema.json"
33
34
35 bool report_mode_only = false;
36
37 TEST_CASE("Admission Policy Wrapper", "[acxapp]"){
38
39   mdclog_attr_t *attr;
40   mdclog_attr_init(&attr);
41   mdclog_attr_set_ident(attr, "UNIT TEST MESSAGE PROCESSOR ");
42   mdclog_init(attr);
43   mdclog_level_set(MDCLOG_INFO);
44   mdclog_attr_destroy(attr);
45
46
47   SECTION("Invalid AC xAPP  Schema"){
48     REQUIRE_THROWS( admission("hello there", SAMPLE_FILE, VES_FILE, 1));
49   }
50
51   SECTION("Invalid sample file"){
52     REQUIRE_THROWS(admission(SCHEMA_FILE, "hello there", VES_FILE, 1));
53     REQUIRE_THROWS(admission(SCHEMA_FILE, "random.txt", VES_FILE, 1));
54   }
55
56   SECTION("Invalid VES schema"){
57     REQUIRE_THROWS(admission(SCHEMA_FILE, SAMPLE_FILE, "hello there", 1));
58   }
59     
60   SECTION("Set policy : invalid"){
61     std::string invalid_policy1 = "{\"blocking_rate\":20\"}";
62     std::string invalid_policy2 = "{\"blocking_rate\":120, \"enforce\":\"true\", \"window_length\":50, \"trigger_threshold\":10}";
63     bool res;
64     std::string response;
65     
66     admission adm_plugin(SCHEMA_FILE, SAMPLE_FILE, VES_FILE, 1);
67     res = adm_plugin.setPolicy(invalid_policy1.c_str(), invalid_policy1.length(), response);
68
69     REQUIRE(res == false);
70     REQUIRE_THAT(response, Catch::Matchers::Contains("FAIL"));
71
72     res = adm_plugin.setPolicy(invalid_policy2.c_str(), invalid_policy2.length(), response);
73     REQUIRE(res == false);
74     REQUIRE_THAT(response, Catch::Matchers::Contains("FAIL"));
75
76   }
77
78   SECTION("Set policy : valid"){
79     std::string valid_policy = "{\"blocking_rate\":20, \"enforce\":true, \"window_length\":50, \"trigger_threshold\":10}";
80     bool res;
81     std::string response;
82     
83     admission adm_plugin(SCHEMA_FILE, SAMPLE_FILE, VES_FILE, 1);
84     res = adm_plugin.setPolicy(valid_policy.c_str(), valid_policy.length(), response);
85     std::cout <<"Response = " << response << " for " << valid_policy << std::endl;
86     REQUIRE(res == true);
87     REQUIRE_THAT(response, Catch::Matchers::Contains("SUCCESS"));
88
89
90     
91     
92   }
93
94   SECTION("Get policy"){
95
96     std::string valid_policy = "{\"blocking_rate\":20, \"enforce\":true, \"window_length\":50, \"trigger_threshold\":10}";
97     bool res;
98     std::string response;
99
100     // first apply policy
101     admission adm_plugin(SCHEMA_FILE, SAMPLE_FILE, VES_FILE, 1);
102     res = adm_plugin.setPolicy(valid_policy.c_str(), valid_policy.length(), response);
103     std::cout <<"Response = " << response << std::endl;
104     REQUIRE(res == true);
105     REQUIRE_THAT(response, Catch::Matchers::Contains("SUCCESS"));
106  
107
108     // now retreive policy and check
109     res = adm_plugin.getPolicy(valid_policy.c_str(), valid_policy.length(), response);
110     REQUIRE(res == true);
111
112     REQUIRE_THAT(response, Catch::Matchers::Contains("\"trigger_threshold\":10"));
113
114   }
115
116   SECTION("Metrics"){
117     admission adm_plugin(SCHEMA_FILE, SAMPLE_FILE, VES_FILE, 1);
118     int res;
119     std::string response;
120     
121     res = adm_plugin.getMetrics(response);
122     REQUIRE(res == 0);
123     std::cout << "Metrics response = " << response << std::endl;
124     REQUIRE_THAT(response, Catch::Matchers::Contains("\"SgNB Request Rate\":\"0\""));
125   }
126
127   SECTION("Get plugin"){
128     admission adm_plugin(SCHEMA_FILE, SAMPLE_FILE, VES_FILE, 1);;
129     protector * p = NULL;
130     p = adm_plugin.get_protector_instance(100);
131     REQUIRE( p == NULL);
132
133     p = adm_plugin.get_protector_instance(0);
134     REQUIRE(p != NULL);
135     REQUIRE(p->get_requests() == 0);
136   }
137       
138       
139 }
140