aboutsummaryrefslogtreecommitdiffstats
path: root/crates/configuration/src/server/http_svc/routes/rule.rs
blob: 597693f4698962a6ff193cc80651a716b50ef415 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
pub mod create;
pub mod delete;
pub mod get;
pub mod update;

#[cfg(test)]
mod tests {
    use axum::{
        body::{self, Body},
        http::{Request, StatusCode},
    };
    use sqlx::PgPool;
    use tower::ServiceExt;
    use warden_core::configuration::rule::RuleConfiguration;
    use warden_stack::cache::RedisManager;

    use crate::{
        server::http_svc::{build_router, routes::test_config},
        state::{AppState, Services},
    };

    #[sqlx::test]
    async fn all_operations(pool: PgPool) {
        let config = test_config();

        let cache = RedisManager::new(&config.cache).await.unwrap();
        let client = async_nats::connect(&config.nats.hosts[0]).await.unwrap();
        let jetstream = async_nats::jetstream::new(client);

        let state = AppState::create(
            Services {
                postgres: pool,
                cache,
                jetstream,
            },
            &test_config(),
        )
        .await
        .unwrap();

        let app = build_router(state);

        let rule = serde_json::json!({
              "id": "901",
              "version": "1.0.0",
              "description": "Number of outgoing transactions - debtor",
              "configuration": {
                "parameters": {
                  "max_query_range": 86400000
                },
                "exit_conditions": [
                  {
                    "sub_rule_ref": ".x00",
                    "reason": "Incoming transaction is unsuccessful"
                  }
                ],
                "bands": [
                  {
                    "sub_rule_ref": ".01",
                    "upper_limit": 2,
                    "reason": "The debtor has performed one transaction to date"
                  },
                  {
                    "sub_rule_ref": ".02",
                    "lower_limit": 2,
                    "upper_limit": 3,
                    "reason": "The debtor has performed two transactions to date"
                  },
                  {
                    "sub_rule_ref": ".03",
                    "lower_limit": 3,
                    "reason": "The debtor has performed three or more transactions to date"
                  }
                ]
              }
        });

        let body = serde_json::to_vec(&rule).unwrap();

        let response = app
            .clone()
            .oneshot(
                Request::builder()
                    .method("POST")
                    .header("Content-Type", "application/json")
                    .uri("/api/v0/rule")
                    .body(Body::from(body))
                    .unwrap(),
            )
            .await
            .unwrap();

        assert_eq!(response.status(), StatusCode::CREATED);

        let response = app
            .clone()
            .oneshot(
                Request::builder()
                    .method("GET")
                    .header("Content-Type", "application/json")
                    .uri("/api/v0/rule?id=901&version=1.0.0")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();

        let body = body::to_bytes(response.into_body(), usize::MAX)
            .await
            .unwrap();

        let config: RuleConfiguration = serde_json::from_slice(&body).unwrap();

        assert_eq!(&config.id, "901");
        assert_eq!(&config.version, "1.0.0");

        let rule = serde_json::json!({
              "id": "902",
              "version": "1.0.0",
              "description": "Number of outgoing transactions - debtor",
              "configuration": {
                "parameters": {
                  "max_query_range": 86400000
                },
                "exit_conditions": [
                  {
                    "sub_rule_ref": ".x00",
                    "reason": "Incoming transaction is unsuccessful"
                  }
                ],
                "bands": []
              }
        });

        let body = serde_json::to_vec(&rule).unwrap();

        app.clone()
            .oneshot(
                Request::builder()
                    .method("PUT")
                    .header("Content-Type", "application/json")
                    .uri("/api/v0/rule?id=901&version=1.0.0")
                    .body(Body::from(body))
                    .unwrap(),
            )
            .await
            .unwrap();

        let response = app
            .clone()
            .oneshot(
                Request::builder()
                    .method("GET")
                    .header("Content-Type", "application/json")
                    .uri("/api/v0/rule?id=902&version=1.0.0")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();

        let body = body::to_bytes(response.into_body(), usize::MAX)
            .await
            .unwrap();

        let config: RuleConfiguration = serde_json::from_slice(&body).unwrap();

        assert_eq!(&config.id, "902");
        assert!(&config.configuration.unwrap().bands.is_empty());

        let response = app
            .clone()
            .oneshot(
                Request::builder()
                    .method("DELETE")
                    .header("Content-Type", "application/json")
                    .uri("/api/v0/rule?id=902&version=1.0.0")
                    .body(Body::from(body))
                    .unwrap(),
            )
            .await
            .unwrap();

        assert_eq!(response.status(), StatusCode::OK);
    }
}