[Issue-Id:RICAPP-155]Code changes to support for JJB and Unit testing for AD xApp
[ric-app/ad.git] / tests / test_ad.py
diff --git a/tests/test_ad.py b/tests/test_ad.py
new file mode 100644 (file)
index 0000000..c60c9dc
--- /dev/null
@@ -0,0 +1,51 @@
+# ==================================================================================
+#       Copyright (c) 2020 HCL Technologies Limited.
+#
+#   Licensed under the Apache License, Version 2.0 (the "License");
+#   you may not use this file except in compliance with the License.
+#   You may obtain a copy of the License at
+#
+#          http://www.apache.org/licenses/LICENSE-2.0
+#
+#   Unless required by applicable law or agreed to in writing, software
+#   distributed under the License is distributed on an "AS IS" BASIS,
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#   See the License for the specific language governing permissions and
+#   limitations under the License.
+# ==================================================================================
+from ad import main
+from ricxappframe.xapp_frame import Xapp
+from contextlib import suppress
+import os
+from ad.ad_train import train
+import json
+
+
+def test_RFtrainmodel(monkeypatch):
+    if not os.path.isfile('ad/RF'):
+        train()
+
+
+def test_predict_anomaly(monkeypatch):
+    main.predict_anomaly('test')
+
+
+def test_msg_to_ts(monkeypatch, ad_to_ts):
+
+    def mock_ad_entry(self):
+        val = json.dumps(ad_to_ts).encode()
+        self.rmr_send(val, 30003)
+    global mock_ad_xapp
+    mock_ad_xapp = Xapp(entrypoint=mock_ad_entry, rmr_port=4564, use_fake_sdl=True)
+    mock_ad_xapp.run()  # this will return since mock_ad_entry isn't a loop  # this will return since mock_ad_entry isn't a loop
+
+
+def teardown_module():
+    """
+    this is like a "finally"; the name of this function is pytest magic
+    safer to put down here since certain failures above can lead to pytest never returning
+    for example if an exception gets raised before stop is called in any test function above,
+    pytest will hang forever
+    """
+    with suppress(Exception):
+        mock_ad_xapp.stop()