backplane-server/test/backplane-server-test.lisp

186 lines
4.4 KiB
Common Lisp

;;;; backplane-server-test.lisp
(defpackage #:backplane-server/test
(:use #:cl
#:backplane-server
#:prove)
(:import-from #:arrows
#:->
#:some->)
(:import-from #:cl-json
#:decode-json-from-string
#:encode-json-to-string))
(in-package #:backplane-server/test)
(plan 21)
(ok (error-p (make-error :msg-id 1
:msg "oops")))
(ok (error-p (make-error :msg "oops")))
(ok (success-p (make-success :msg-id 1
:msg "ok")))
(ok (success-p (make-success :msg "ok")))
(ok (not (success-p (make-error :msg-id 1
:msg "oops"))))
(ok (not (error-p (make-success :msg-id 1
:msg "ok"))))
(defun make-request (&key
(service :TEST)
(msgid 1)
(version 1)
(payload '()))
(encode-json-to-string
`((VERSION . ,version)
(SERVICE . ,service)
(MSGID . ,msgid)
(PAYLOAD . ,payload))))
(defun get-key (obj key)
(cdr (assoc key obj)))
(defun handle (body)
(handle-xmpp-message "me" body))
(defun decode (body)
(cl-json:decode-json-from-string body))
(is (-> "}{"
(handle)
(decode)
(get-key :STATUS))
"ERROR")
(is (-> "}{"
(handle)
(decode)
(get-key :MESSAGE))
"invalid json string: }{")
(is (-> (make-request :service :NONEXISTENT)
(handle)
(decode)
(get-key :STATUS))
"ERROR")
(is (-> (make-request :service :NONEXISTENT)
(handle)
(decode)
(get-key :MESSAGE))
"unsupported service: NONEXISTENT")
(is (-> (make-request :service nil)
(handle)
(decode)
(get-key :MESSAGE))
"missing api_version, msgid, or service name in request in message")
(is (-> (make-request :version nil)
(handle)
(decode)
(get-key :MESSAGE))
"missing api_version, msgid, or service name in request in message")
(is (-> (make-request :msgid nil)
(handle)
(decode)
(get-key :MESSAGE))
"missing api_version, msgid, or service name in request in message")
(defmethod parse-message (sender (service (eql :TEST0)) api-version message msg-id)
:TEST-SIMPLE-MESSAGE)
(defmethod handle-message ((message (eql :TEST-SIMPLE-MESSAGE)))
(make-success :msg "successful-test0"))
(is (-> (make-request :service :TEST0)
(handle)
(decode)
(get-key :STATUS))
"OK")
(is (-> (make-request :service :TEST0)
(handle)
(decode)
(get-key :MESSAGE))
"successful-test0")
(defclass my-req-type ()
((msg :initarg :msg)
(msgid :initarg :msgid)))
(defclass my-succeeder (my-req-type) ())
(defclass my-failer (my-req-type) ())
(defmethod parse-message (sender (service (eql :TEST1)) api-version message msg-id)
(if (equalp message "succeed")
(make-instance 'my-succeeder
:msg "successful-test1"
:msgid msg-id)
(make-instance 'my-failer
:msg "failed-test1"
:msgid msg-id)))
(defmethod handle-message ((req my-succeeder))
(with-slots (msg msgid) req
(make-success :msg msg :msg-id msgid)))
(defmethod handle-message ((req my-failer))
(with-slots (msg msgid) req
(make-error :msg msg :msg-id msgid)))
(is (-> (make-request :service :TEST1
:payload "succeed")
(handle)
(decode)
(get-key :STATUS))
"OK")
(is (-> (make-request :service :TEST1
:payload "succeed")
(handle)
(decode)
(get-key :MESSAGE))
"successful-test1")
(is (-> (make-request :service :TEST1
:payload "fail plz")
(handle)
(decode)
(get-key :STATUS))
"ERROR")
(is (-> (make-request :service :TEST1
:payload "fail plz")
(handle)
(decode)
(get-key :MESSAGE))
"failed-test1")
(let ((msg-id "testid0"))
(is (-> (make-request :service :TEST1
:msgid msg-id
:payload "succeed")
(handle)
(decode)
(get-key :MSGID))
msg-id))
(let ((msg-id "testid1"))
(is (-> (make-request :service :TEST1
:msgid msg-id
:payload "fail pl'")
(handle)
(decode)
(get-key :MSGID))
msg-id))
(finalize)