Skip to content
Snippets Groups Projects
Commit 607635b2 authored by Aline Gondim Santos's avatar Aline Gondim Santos Committed by Sébastien Blin
Browse files

AutoAnswer: add process test

GitLab: jami-daemon#745

Change-Id: I54e5df2ffa44e779f52e5b404898c91d280ac1f6
parent fd30291e
No related branches found
No related tags found
No related merge requests found
/AutoAnswer
AutoAnswer*
...@@ -72,7 +72,8 @@ BotChatHandler::detach(chatSubjectPtr subject) ...@@ -72,7 +72,8 @@ BotChatHandler::detach(chatSubjectPtr subject)
BotChatHandler::~BotChatHandler() BotChatHandler::~BotChatHandler()
{ {
for (const auto& subject : subjects) { const auto copy = subjects;
for (const auto& subject : copy) {
detach(subject); detach(subject);
} }
} }
......
...@@ -60,6 +60,10 @@ BotPeerChatSubscriber::update(Observable<pluginMessagePtr>*, const pluginMessage ...@@ -60,6 +60,10 @@ BotPeerChatSubscriber::update(Observable<pluginMessagePtr>*, const pluginMessage
else if (message->data.at("type") == "text/plain" && message->data.at("body") == input) { else if (message->data.at("type") == "text/plain" && message->data.at("body") == input) {
sendMsg["type"] = "text/plain"; sendMsg["type"] = "text/plain";
sendMsg["body"] = answer; sendMsg["body"] = answer;
#ifdef __DEBUG__
Plog::log(Plog::LogPriority::INFO, TAG, "input " + message->data.at("body"));
Plog::log(Plog::LogPriority::INFO, TAG, "ouput " + answer);
#endif
} }
if (!sendMsg.empty()) { if (!sendMsg.empty()) {
sendText(message->accountId, message->peerId, sendMsg, message->isSwarm); sendText(message->accountId, message->peerId, sendMsg, message->isSwarm);
...@@ -106,6 +110,8 @@ BotPeerChatSubscriber::sendText(std::string& accountId, ...@@ -106,6 +110,8 @@ BotPeerChatSubscriber::sendText(std::string& accountId,
sendMsg, sendMsg,
true); true);
botAnswer->isSwarm = swarm; botAnswer->isSwarm = swarm;
#ifndef __DEBUG__
api_->invokeService(api_, "sendTextMessage", botAnswer.get()); api_->invokeService(api_, "sendTextMessage", botAnswer.get());
#endif
} }
} // namespace jami } // namespace jami
...@@ -33,24 +33,39 @@ message(JPL\ path:\ ${JPL_DIRECTORY}/../../../build/${ProjectName}/${JPL_FILE_NA ...@@ -33,24 +33,39 @@ message(JPL\ path:\ ${JPL_DIRECTORY}/../../../build/${ProjectName}/${JPL_FILE_NA
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True) set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd") if(TESTPROCESS)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /D__DEBUG__ /MD")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /D__DEBUG__ /MD")
else()
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MD")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MD")
endif()
set(plugin_SRC BotChatHandler.cpp set(plugin_SRC BotChatHandler.cpp
BotPeerChatSubscriber.cpp BotPeerChatSubscriber.cpp
PluginPreferenceHandler.cpp PluginPreferenceHandler.cpp
main.cpp main.cpp
./../lib/common.cpp
) )
set(plugin_HDR BotChatHandler.h set(plugin_HDR BotChatHandler.h
BotPeerChatSubscriber.h BotPeerChatSubscriber.h
PluginPreferenceHandler.h PluginPreferenceHandler.h
./../lib/pluglog.h ./../lib/pluglog.h
./../lib/common.h
) )
if(TESTPROCESS)
add_executable(${ProjectName} ${plugin_SRC}
${plugin_HDR}
)
else()
add_library(${ProjectName} SHARED ${plugin_SRC} add_library(${ProjectName} SHARED ${plugin_SRC}
${plugin_HDR} ${plugin_HDR}
) )
endif()
target_include_directories(${ProjectName} PUBLIC ${PROJECT_BINARY_DIR} target_include_directories(${ProjectName} PUBLIC ${PROJECT_BINARY_DIR}
${PROJECT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}
...@@ -58,11 +73,22 @@ target_include_directories(${ProjectName} PUBLIC ${PROJECT_BINARY_DIR} ...@@ -58,11 +73,22 @@ target_include_directories(${ProjectName} PUBLIC ${PROJECT_BINARY_DIR}
${DAEMON_SRC} ${DAEMON_SRC}
${CONTRIB_PATH} ${CONTRIB_PATH}
${CONTRIB_PATH}/build/fmt/include ${CONTRIB_PATH}/build/fmt/include
${CONTRIB_PATH}/build/yaml-cpp/include
) )
target_link_directories(${ProjectName} PUBLIC ${CONTRIB_PATH} target_link_directories(${ProjectName} PUBLIC ${CONTRIB_PATH}
${CONTRIB_PATH}/build/fmt/msvc/Release ${CONTRIB_PATH}/build/fmt/msvc/Release
${CONTRIB_PATH}/build/yaml-cpp/msvc/Release
) )
target_link_libraries(${ProjectName} PUBLIC libyaml-cppmd)
if(TESTPROCESS)
add_custom_command(
TARGET ${ProjectName}
PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/testPreferences.yml ${PROJECT_BINARY_DIR}/
)
else()
add_custom_command( add_custom_command(
TARGET ${ProjectName} TARGET ${ProjectName}
PRE_BUILD PRE_BUILD
...@@ -78,3 +104,4 @@ add_custom_command( ...@@ -78,3 +104,4 @@ add_custom_command(
COMMAND python ${PROJECT_SOURCE_DIR}/../SDK/jplManipulation.py --assemble --plugin=${ProjectName} COMMAND python ${PROJECT_SOURCE_DIR}/../SDK/jplManipulation.py --assemble --plugin=${ProjectName}
COMMENT "Generating JPL archive" COMMENT "Generating JPL archive"
) )
endif()
...@@ -6,10 +6,10 @@ ARCH=$(uname -m) ...@@ -6,10 +6,10 @@ ARCH=$(uname -m)
EXTRAPATH='' EXTRAPATH=''
# Flags: # Flags:
# -p: number of processors to use # -p: number of processors to use.
# -c: Runtime plugin cpu/gpu setting. # -c: Runtime plugin cpu/gpu setting.
# -t: target platform. # -t: target platform.
# -d: debug program.
if [ -z "${DAEMON}" ]; then if [ -z "${DAEMON}" ]; then
DAEMON="./../../daemon" DAEMON="./../../daemon"
...@@ -36,8 +36,11 @@ elif [ "${PLATFORM}" = "Darwin" ]; then ...@@ -36,8 +36,11 @@ elif [ "${PLATFORM}" = "Darwin" ]; then
echo "Building with ${PLATFORM}" echo "Building with ${PLATFORM}"
fi fi
while getopts t:c:p OPT; do while getopts t:c:p:d OPT; do
case "$OPT" in case "$OPT" in
d)
DEBUG=true
;;
t) t)
PLATFORM="${OPTARG}" PLATFORM="${OPTARG}"
;; ;;
...@@ -54,34 +57,48 @@ done ...@@ -54,34 +57,48 @@ done
if [ "${PLATFORM}" = "linux-gnu" ] || [ "${PLATFORM}" = "redhat-linux" ] || [ "${PLATFORM}" = "Darwin" ] if [ "${PLATFORM}" = "linux-gnu" ] || [ "${PLATFORM}" = "redhat-linux" ] || [ "${PLATFORM}" = "Darwin" ]
then then
python3 ./../SDK/jplManipulation.py --preassemble --plugin=${PLUGIN_NAME}
CONTRIB_PLATFORM=${CONTRIB_PLATFORM_CURT}-${PLATFORM} CONTRIB_PLATFORM=${CONTRIB_PLATFORM_CURT}-${PLATFORM}
OUTPUTFOLDER=${CONTRIB_PLATFORM} OUTPUTFOLDER=${CONTRIB_PLATFORM}
if [ "${PLATFORM}" = "Darwin" ]; then if [ "${PLATFORM}" = "Darwin" ]; then
CONTRIB_PLATFORM=${CONTRIB_PLATFORM}${CONTRIB_PLATFORM_EXTRA} CONTRIB_PLATFORM=${CONTRIB_PLATFORM}${CONTRIB_PLATFORM_EXTRA}
fi fi
if [ ${DEBUG} ]; then
OUTPUT="${PLUGIN_NAME}"
CLANG_OPTS="-g -fsanitize=address"
EXTRA_DEBUG_LIBRARIES="-lyaml-cpp"
EXTRA_DEFINES="-D__DEBUG__"
else
python3 ./../SDK/jplManipulation.py --preassemble --plugin=${PLUGIN_NAME}
CLANG_OPTS="-O3 -shared"
OUTPUT="build-local/jpl/lib/${OUTPUTFOLDER}/${SO_FILE_NAME}"
fi
# Compile # Compile
clang++ -std=c++17 -shared -fPIC \ clang++ -std=c++17 -fPIC ${CLANG_OPTS} \
-Wl,-rpath,"\${ORIGIN}" \ -Wl,-rpath,"\${ORIGIN}" \
-Wall -Wextra \ -Wall -Wextra \
-Wno-unused-variable \
-Wno-unused-function \
-Wno-unused-parameter \ -Wno-unused-parameter \
${EXTRA_DEFINES} \
-I"." \ -I"." \
-I"${DAEMON_SRC}" \ -I"${DAEMON_SRC}" \
-I"${CONTRIB_PATH}/${CONTRIB_PLATFORM}/include" \ -I"${CONTRIB_PATH}/${CONTRIB_PLATFORM}/include" \
-I"${PLUGINS_LIB}" \ -I"${PLUGINS_LIB}" \
./../lib/common.cpp \
PluginPreferenceHandler.cpp \ PluginPreferenceHandler.cpp \
BotChatHandler.cpp \ BotChatHandler.cpp \
BotPeerChatSubscriber.cpp \ BotPeerChatSubscriber.cpp \
main.cpp \ main.cpp \
-L"${CONTRIB_PATH}/${CONTRIB_PLATFORM}/lib/" \ -L"${CONTRIB_PATH}/${CONTRIB_PLATFORM}/lib/" \
-o "build-local/jpl/lib/${OUTPUTFOLDER}/${SO_FILE_NAME}" ${EXTRA_DEBUG_LIBRARIES} \
-o "${OUTPUT}"
if [ "${PLATFORM}" = "Darwin" ]; then if [ "${PLATFORM}" = "Darwin" ]; then
install_name_tool -id "@loader_path/${SO_FILE_NAME}" "build-local/jpl/lib/${OUTPUTFOLDER}/${SO_FILE_NAME}" if [ ! ${DEBUG} ]; then
install_name_tool -id "@loader_path/${PLUGIN_NAME}" "${OUTPUT}"
else
install_name_tool -id "@loader_path/${SO_FILE_NAME}" "${OUTPUT}"
fi
fi fi
if [ -n "${APPLE_SIGN_CERTIFICATE}" ]; then if [ -n "${APPLE_SIGN_CERTIFICATE}" ]; then
...@@ -199,8 +216,6 @@ then ...@@ -199,8 +216,6 @@ then
-Wl,-Bsymbolic,-rpath,"\${ORIGIN}" \ -Wl,-Bsymbolic,-rpath,"\${ORIGIN}" \
-shared \ -shared \
-Wall -Wextra \ -Wall -Wextra \
-Wno-unused-variable \
-Wno-unused-function \
-Wno-unused-parameter \ -Wno-unused-parameter \
-I"." \ -I"." \
-I"${DAEMON_SRC}" \ -I"${DAEMON_SRC}" \
...@@ -223,4 +238,6 @@ then ...@@ -223,4 +238,6 @@ then
done done
fi fi
if [ ! ${DEBUG} ]; then
python3 ./../SDK/jplManipulation.py --assemble --plugin=${PLUGIN_NAME} --distribution=${PLATFORM} --extraPath=${EXTRAPATH} python3 ./../SDK/jplManipulation.py --assemble --plugin=${PLUGIN_NAME} --distribution=${PLATFORM} --extraPath=${EXTRAPATH}
fi
...@@ -26,6 +26,13 @@ ...@@ -26,6 +26,13 @@
#include "plugin/jamiplugin.h" #include "plugin/jamiplugin.h"
#include "BotChatHandler.h" #include "BotChatHandler.h"
#ifdef __DEBUG__
#include <common.h>
#include <assert.h>
#include <yaml-cpp/yaml.h>
#include <fstream>
#endif
#ifdef WIN32 #ifdef WIN32
#define EXPORT_PLUGIN __declspec(dllexport) #define EXPORT_PLUGIN __declspec(dllexport)
#else #else
...@@ -80,3 +87,85 @@ JAMI_dynPluginInit(const JAMI_PluginAPI* api) ...@@ -80,3 +87,85 @@ JAMI_dynPluginInit(const JAMI_PluginAPI* api)
return pluginExit; return pluginExit;
} }
} }
#ifdef __DEBUG__
int
main ()
{
std::cout << "********************************" << std::endl;
std::cout << "** AutoAnswer Debug Version **" << std::endl;
std::cout << "********************************" << std::endl;
std::cout << "Version " << AutoAnswer_VERSION_MAJOR << "." << AutoAnswer_VERSION_MINOR << "."
<< AutoAnswer_VERSION_PATCH << std::endl << std::endl;
#ifdef _WIN32
std::ifstream file = std::ifstream(string_utils::to_wstring("testPreferences.yml"));
#else
std::ifstream file = std::ifstream("testPreferences.yml", std::ios_base::in);
#endif
assert(file.is_open());
YAML::Node node = YAML::Load(file);
assert(node.IsMap());
std::map<std::string, std::map<std::string, std::string>> preferences;
preferences["default"] = {};
for (const auto& kv : node) {
preferences["default"][kv.first.as<std::string>()] = kv.second.as<std::string>();
std::cout << "Key: " << kv.first.as<std::string>() << "; Value: " << kv.second.as<std::string>() << std::endl;
}
std::string dataPath = "tester";
auto fmpPluginPreferenceHandler
= std::make_unique<jami::PluginPreferenceHandler>(nullptr, std::move(preferences), dataPath);
auto fmpBotChatHandler
= std::make_unique<jami::BotChatHandler>(nullptr,
std::move(dataPath),
fmpPluginPreferenceHandler.get());
auto subject = std::make_shared<jami::PublishObservable<jami::pluginMessagePtr>>();
std::pair<std::string, std::string> subjectConnection("origin", "destiny");
fmpBotChatHandler->notifyChatSubject(subjectConnection, subject);
// Only test for swarm
// Valid Sender, Receiver, direction and message
std::cout << "Test 1" << std::endl << "Should print input/output" << std::endl;
std::map<std::string, std::string> sendMsg = {{"type", "text/plain"}, {"body", preferences["default"]["inText"]}};
jami::pluginMessagePtr jamiMsg = std::make_shared<JamiMessage>("origin",
"destiny",
true,
sendMsg,
false);
jamiMsg->isSwarm = true;
subject->publish(jamiMsg);
// Valid Sender, Receiver and message but not direction
std::cout << "Test 2" << std::endl << "Should NOT print input/output" << std::endl;
jamiMsg.reset(new JamiMessage("origin",
"destiny",
false,
sendMsg,
false));
jamiMsg->isSwarm = true;
subject->publish(jamiMsg);
// Invalid Sender, Receiver, direction and message
std::cout << "Test 3" << std::endl << "Should NOT print input/output" << std::endl;
sendMsg["body"] = preferences["default"]["invalid"];
jamiMsg.reset(new JamiMessage("destiny",
"origin",
true,
sendMsg,
false));
jamiMsg->isSwarm = true;
subject->publish(jamiMsg);
return 0;
}
#endif
...@@ -3,9 +3,12 @@ ...@@ -3,9 +3,12 @@
"version": "2.0.0", "version": "2.0.0",
"extractLibs": false, "extractLibs": false,
"deps": [ "deps": [
"fmt" "fmt",
"yaml-cpp"
],
"defines": [
"TESTPROCESS=False"
], ],
"defines": [],
"custom_scripts": { "custom_scripts": {
"pre_build": [ "pre_build": [
"mkdir msvc" "mkdir msvc"
......
answer: "Occupied"
inText: "Trigger"
invalid: "invalid message"
...@@ -29,24 +29,7 @@ extern "C" { ...@@ -29,24 +29,7 @@ extern "C" {
#include <pluglog.h> #include <pluglog.h>
#ifdef WIN32 #ifdef WIN32
#include <WTypes.h> #include <common.h>
namespace string_utils {
std::wstring
to_wstring(const std::string& str) {
int codePage = CP_UTF8;
int srcLength = (int) str.length();
int requiredSize = MultiByteToWideChar(codePage, 0, str.c_str(), srcLength, nullptr, 0);
if (!requiredSize) {
throw std::runtime_error("Can't convert string to wstring");
}
std::wstring result((size_t) requiredSize, 0);
if (!MultiByteToWideChar(codePage, 0, str.c_str(), srcLength, &(*result.begin()), requiredSize)) {
throw std::runtime_error("Can't convert string to wstring");
}
return result;
}
} // namespace string_utils
#endif #endif
const char sep = separator(); const char sep = separator();
......
/**
* Copyright (C) 2022 Savoir-faire Linux Inc.
*
* Author: Aline Gondim Santos <aline.gondimsantos@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "common.h"
#ifdef WIN32
#include <WTypes.h>
#include <stdexcept>
namespace string_utils {
std::wstring
to_wstring(const std::string& str) {
int codePage = CP_UTF8;
int srcLength = (int) str.length();
int requiredSize = MultiByteToWideChar(codePage, 0, str.c_str(), srcLength, nullptr, 0);
if (!requiredSize) {
throw std::runtime_error("Can't convert string to wstring");
}
std::wstring result((size_t) requiredSize, 0);
if (!MultiByteToWideChar(codePage, 0, str.c_str(), srcLength, &(*result.begin()), requiredSize)) {
throw std::runtime_error("Can't convert string to wstring");
}
return result;
}
} // namespace string_utils
#endif // WIN32
/**
* Copyright (C) 2022 Savoir-faire Linux Inc.
*
* Author: Aline Gondim Santos <aline.gondimsantos@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef COMMON_H
#define COMMON_H
#ifdef WIN32
#include <string>
namespace string_utils {
std::wstring to_wstring(const std::string& str);
} // namespace string_utils
#endif // WIN32
#endif // COMMON_H
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment