diff options
author | iAndrew5 <abyzhynar@luxoft.com> | 2015-10-20 18:18:38 +0300 |
---|---|---|
committer | iAndrew5 <abyzhynar@luxoft.com> | 2015-10-20 18:18:38 +0300 |
commit | 790208669af77d4da040922283c2644dfab4f457 (patch) | |
tree | c671d2100f4c4d3358da76e79df5b2780dd7c5cd /src/components/formatters | |
parent | 13fd07953c463a8d035df130bf1713ee6508d7d8 (diff) | |
download | sdl_core-790208669af77d4da040922283c2644dfab4f457.tar.gz |
Moved, Enabled and partially refactored Unit tests
Diffstat (limited to 'src/components/formatters')
13 files changed, 3643 insertions, 7 deletions
diff --git a/src/components/formatters/test/CFormatterJsonBase_test.cc b/src/components/formatters/test/CFormatterJsonBase_test.cc new file mode 100644 index 0000000000..9efbfdcf77 --- /dev/null +++ b/src/components/formatters/test/CFormatterJsonBase_test.cc @@ -0,0 +1,337 @@ +/* + * Copyright (c) 2015, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <string> +#include <algorithm> +#include "json/value.h" +#include "gtest/gtest.h" +#include "json/reader.h" +#include "formatters/CFormatterJsonBase.hpp" +#include "formatters/generic_json_formatter.h" + +namespace test { +namespace components { +namespace formatters { + +using namespace NsSmartDeviceLink::NsSmartObjects; +using namespace NsSmartDeviceLink::NsJSONHandler::Formatters; + +TEST(CFormatterJsonBaseTest, JSonStringValueToSmartObj_ExpectSuccessful) { + // Arrange value + std::string string_val("test_string"); + Json::Value json_value(string_val); // Json value from string + SmartObject object; + // Convert json to smart object + CFormatterJsonBase::jsonValueToObj(json_value, object); + // Check conversion was successful + EXPECT_EQ(string_val, object.asString()); +} + +TEST(CFormatterJsonBaseTest, JSonDoubleValueToSmartObj_ExpectSuccessful) { + // Arrange value + double dval = 3.512; + Json::Value json_value(dval); // Json value from double + SmartObject object; + // Convert json to smart object + CFormatterJsonBase::jsonValueToObj(json_value, object); + // Check conversion was successful + EXPECT_DOUBLE_EQ(dval, object.asDouble()); +} + +TEST(CFormatterJsonBaseTest, JSonMinIntValueToSmartObj_ExpectSuccessful) { + // Arrange value + Json::Int ival = Json::Value::minInt; + Json::Value json_value(ival); // Json value from possible minimum signed int + SmartObject object; + // Convert json to smart object + CFormatterJsonBase::jsonValueToObj(json_value, object); + // Check conversion was successful + EXPECT_EQ(ival, object.asInt()); +} + +TEST(CFormatterJsonBaseTest, JSonNullIntValueToSmartObj_ExpectSuccessful) { + // Arrange value + Json::Int ival = Json::nullValue; + Json::Value json_value(ival); // Json value from null int value + SmartObject object; + // Convert json to smart object + CFormatterJsonBase::jsonValueToObj(json_value, object); + // Check conversion was successful + EXPECT_EQ(ival, object.asInt()); +} + +TEST(CFormatterJsonBaseTest, JSonSignedMaxIntValueToSmartObj_ExpectSuccessful) { + // Arrange value + Json::Int ival = Json::Value::maxInt; + Json::Value json_value(ival); // Json value from maximum possible signed int + SmartObject object; + // Convert json to smart object + CFormatterJsonBase::jsonValueToObj(json_value, object); + // Check conversion was successful + EXPECT_EQ(ival, object.asInt()); +} + +TEST(CFormatterJsonBaseTest, DISABLED_JSonUnsignedMaxIntValueToSmartObj_ExpectSuccessful) { + // Arrange value + Json::UInt ui_val = Json::Value::maxUInt; + Json::Value json_value(ui_val); // Json value from maximum possible unsigned int + SmartObject object; + // Convert json to smart object + CFormatterJsonBase::jsonValueToObj(json_value, object); + // Check conversion was successful + EXPECT_EQ(ui_val, object.asUInt()); +} + +TEST(CFormatterJsonBaseTest, JSonSignedMaxInt64ValueToSmartObj_ExpectFailed) { + // Arrange value + Json::Int64 ival = Json::Value::maxInt64; + Json::Value json_value(ival); // Json value from maximum possible signed int + SmartObject object; + // Convert json to smart object + CFormatterJsonBase::jsonValueToObj(json_value, object); + // Check conversion was not successful as there is no such conversion + EXPECT_EQ(invalid_int64_value, object.asInt64()); +} + +TEST(CFormatterJsonBaseTest, JSonBoolValueToSmartObj_ExpectSuccessful) { + // Arrange value + bool bval1 = true; + bool bval2 = false; + Json::Value json_value1(bval1); // Json value from bool + Json::Value json_value2(bval2); // Json value from bool + SmartObject object1; + SmartObject object2; + // Convert json to smart object + CFormatterJsonBase::jsonValueToObj(json_value1, object1); + CFormatterJsonBase::jsonValueToObj(json_value2, object2); + // Check conversion was successful + EXPECT_TRUE(object1.asBool()); + EXPECT_FALSE(object2.asBool()); +} + +TEST(CFormatterJsonBaseTest, JSonCStringValueToSmartObj_ExpectSuccessful) { + // Arrange value + const char* cstr_val = "cstring_test"; + Json::Value json_value(cstr_val); // Json value from const char* + SmartObject object; + // Convert json to smart object + CFormatterJsonBase::jsonValueToObj(json_value, object); + // Check conversion was successful + EXPECT_STREQ(cstr_val, object.asCharArray()); +} + +TEST(CFormatterJsonBaseTest, JSonArrayValueToSmartObj_ExpectSuccessful) { + // Arrange value + const char* json_array = "[\"test1\", \"test2\", \"test3\"]"; // Array in json format + Json::Value json_value; // Json value from array. Will be initialized later + SmartObject object; + Json::Reader reader; // Json reader - Needed for correct parsing + // Parse array to json value + ASSERT_TRUE(reader.parse(json_array, json_value)); + // Convert json array to SmartObject + CFormatterJsonBase::jsonValueToObj(json_value, object); + // Check conversion was successful + EXPECT_TRUE(json_value.isArray()); + EXPECT_EQ(3u, object.asArray()->size()); + SmartArray *ptr = NULL; // Smart Array pointer; + EXPECT_NE(ptr, object.asArray()); +} + +TEST(CFormatterJsonBaseTest, JSonObjectValueToSmartObj_ExpectSuccessful) { + // Arrange value + const char* json_object = + "{ \"json_test_object\": [\"test1\", \"test2\", \"test3\"], \"json_test_object2\": [\"test11\", \"test12\", \"test13\" ]}"; // Json object + Json::Value json_value; // Json value from object. Will be initialized later + SmartObject object; + Json::Reader reader; // Json reader - Needed for correct parsing + ASSERT_TRUE(reader.parse(json_object, json_value)); // If parsing not successful - no sense to continue + CFormatterJsonBase::jsonValueToObj(json_value, object); + // Check conversion was successful + EXPECT_TRUE(json_value.isObject()); + EXPECT_TRUE(json_value.type() == Json::objectValue); + // Get keys collection from Smart Object + std::set<std::string> keys = object.enumerate(); + std::set<std::string>::iterator it1 = keys.begin(); + // Get members names(keys) from Json object + Json::Value::Members mems = json_value.getMemberNames(); + std::vector<std::string>::iterator it; + // Compare sizes + EXPECT_EQ(mems.size(), keys.size()); + // Sort mems + std::sort(mems.begin(), mems.end()); + // Full data compare + for (it = mems.begin(); it != mems.end(); ++it) { + EXPECT_EQ(*it, *it1); + ++it1; + } + ASSERT(it == mems.end() && it1 == keys.end()); +} + +TEST(CFormatterJsonBaseTest, StringSmartObjectToJSon_ExpectSuccessful) { + // Arrange value + std::string string_val("test_string"); + SmartObject object(string_val); + Json::Value json_value; // Json value from string + // Convert smart object to json + CFormatterJsonBase::objToJsonValue(object, json_value); + // Check conversion was successful + EXPECT_EQ(string_val, json_value.asString()); +} + +TEST(CFormatterJsonBaseTest, DoubleSmartObjectToJSon_ExpectSuccessful) { + // Arrange value + double dval = 3.512; + Json::Value json_value; // Json value from double + SmartObject object(dval); + // Convert json to smart object + CFormatterJsonBase::objToJsonValue(object, json_value); + // Check conversion was successful + EXPECT_DOUBLE_EQ(dval, json_value.asDouble()); +} + +TEST(CFormatterJsonBaseTest, ZeroIntSmartObjectToJSon_ExpectSuccessful) { + // Arrange value + Json::Int ival = Json::nullValue; + Json::Value json_value; // Json value from zero int + SmartObject object(ival); + // Convert json to smart object + CFormatterJsonBase::objToJsonValue(object, json_value); + // Check conversion was successful + EXPECT_EQ(ival, json_value.asInt()); +} + +TEST(CFormatterJsonBaseTest, MinIntSmartObjectToJSon_ExpectSuccessful) { + // Arrange value + Json::Int ival = Json::Value::minInt; + Json::Value json_value; // Json value from mimimum possible signed int + SmartObject object(ival); + // Convert json to smart object + CFormatterJsonBase::objToJsonValue(object, json_value); + // Check conversion was successful + EXPECT_EQ(ival, json_value.asInt()); +} + +TEST(CFormatterJsonBaseTest, DISABLED_UnsignedMaxIntSmartObjectToJSon_ExpectSuccessful) { + // Arrange value + Json::UInt ui_val = Json::Value::maxUInt; + Json::Value json_value; // Json value from maximum unsigned int + SmartObject object(ui_val); + // Convert json to smart object + CFormatterJsonBase::objToJsonValue(object, json_value); + // Check conversion was successful + EXPECT_EQ(ui_val, json_value.asUInt()); +} + +TEST(CFormatterJsonBaseTest, BoolSmartObjectToJSon_ExpectSuccessful) { + // Arrange value + bool bval1 = true; + bool bval2 = false; + Json::Value json_value1; // Json value from bool + Json::Value json_value2; // Json value from bool + SmartObject object1(bval1); + SmartObject object2(bval2); + // Convert json to smart object + CFormatterJsonBase::objToJsonValue(object1, json_value1); + CFormatterJsonBase::objToJsonValue(object2, json_value2); + // Check conversion was successful + EXPECT_TRUE(json_value1.asBool()); + EXPECT_FALSE(json_value2.asBool()); +} + +TEST(CFormatterJsonBaseTest, CStringSmartObjectToJSon_ExpectSuccessful) { + // Arrange value + const char* cstr_val = "cstring_test"; + Json::Value json_value; // Json value from const char* + SmartObject object(cstr_val); + // Convert json to smart object + CFormatterJsonBase::objToJsonValue(object, json_value); + // Check conversion was successful + EXPECT_STREQ(cstr_val, json_value.asCString()); +} + +TEST(CFormatterJsonBaseTest, ArraySmartObjectToJSon_ExpectSuccessful) { + // Arrange value + const char* json_array = "[\"test1\", \"test2\", \"test3\"]"; // Array in json format + Json::Value json_value; // Json value from array. Will be initialized later + Json::Value result; // Json value from array. Will be initialized later + SmartObject object; + Json::Reader reader; // Json reader - Needed for correct parsing + // Parse array to json value + ASSERT_TRUE(reader.parse(json_array, json_value)); // Convert json array to SmartObject + // Convert json array to SmartObject + CFormatterJsonBase::jsonValueToObj(json_value, object); + // Convert SmartObject to JSon + CFormatterJsonBase::objToJsonValue(object, result); + // Check conversion was successful + EXPECT_TRUE(result.isArray()); + EXPECT_EQ(3u, result.size()); +} + +TEST(CFormatterJsonBaseTest, JSonObjectValueToObj_ExpectSuccessful) { + // Arrange value + const char* json_object = + "{ \"json_test_object\": [\"test1\", \"test2\", \"test3\"], \"json_test_object2\": [\"test11\", \"test12\", \"test13\" ]}"; // Json object + Json::Value json_value; // Json value from json object. Will be initialized later + Json::Value result; // Json value from Smart object. Will keep conversion result + SmartObject object; + Json::Reader reader; // Json reader - Needed for correct parsing + // Parse json object to correct json value + ASSERT_TRUE(reader.parse(json_object, json_value)); // If parsing not successful - no sense to continue + // Convert json array to SmartObject + CFormatterJsonBase::jsonValueToObj(json_value, object); + // Convert SmartObject to JSon + CFormatterJsonBase::objToJsonValue(object, result); + // Check conversion was successful + EXPECT_TRUE(result.isObject()); + EXPECT_TRUE(result.type() == Json::objectValue); + EXPECT_TRUE(result == json_value); + // Get keys collection from Smart Object + std::set<std::string> keys = object.enumerate(); + std::set<std::string>::iterator it1 = keys.begin(); + // Get members names(keys) from Json object + Json::Value::Members mems = result.getMemberNames(); + std::vector<std::string>::iterator it; + // Compare sizes + EXPECT_EQ(mems.size(), keys.size()); + // Sort mems + std::sort(mems.begin(), mems.end()); + // Full data compare + for (it = mems.begin(); it != mems.end(); ++it) { + EXPECT_EQ(*it, *it1); + ++it1; + } + ASSERT(it == mems.end() && it1 == keys.end()); +} + +} // namespace formatters +} // namespace components +} // namespace test diff --git a/src/components/formatters/test/CMakeLists.txt b/src/components/formatters/test/CMakeLists.txt index cbe9a2190e..b9966c8670 100644 --- a/src/components/formatters/test/CMakeLists.txt +++ b/src/components/formatters/test/CMakeLists.txt @@ -1,4 +1,4 @@ -# Copyright (c) 2014, Ford Motor Company +# Copyright (c) 2015, Ford Motor Company # All rights reserved. # # Redistribution and use in source and binary forms, with or without @@ -35,19 +35,33 @@ include_directories( ${GMOCK_INCLUDE_DIRECTORY} ${COMPONENTS_DIR}/smart_objects/include ${COMPONENTS_DIR}/formatters/include + ${COMPONENTS_DIR}/formatters/test/include + ${CMAKE_BINARY_DIR}/src/components/interfaces + ${CMAKE_SOURCE_DIR}/src/3rd_party-static/jsoncpp/include ) set(LIBRARIES - gmock - SmartObjects - formatters - jsoncpp + gmock + HMI_API + MOBILE_API + SmartObjects + formatters + jsoncpp ) set(SOURCES -${COMPONENTS_DIR}/formatters/test/generic_json_formatter_test.cc + ${COMPONENTS_DIR}/formatters/test/src/SmartFactoryTestHelper.cc + ${COMPONENTS_DIR}/formatters/test/CSmartFactory_test.cc + ${COMPONENTS_DIR}/formatters/test/CFormatterJsonBase_test.cc + ${COMPONENTS_DIR}/formatters/test/generic_json_formatter_test.cc + ${COMPONENTS_DIR}/formatters/test/formatter_json_rpc_test.cc + ${COMPONENTS_DIR}/formatters/test/src/create_smartSchema.cc + ${COMPONENTS_DIR}/formatters/test/cFormatterJsonSDLRPCv1_test.cc + ${COMPONENTS_DIR}/formatters/test/cFormatterJsonSDLRPCv2_test.cc + ${COMPONENTS_DIR}/formatters/test/src/meta_formatter_test_helper.cc + ${COMPONENTS_DIR}/formatters/test/meta_formatter_test.cc ) -create_test("generic_json_formatter_test" "${SOURCES}" "${LIBRARIES}") +create_test("formatters_test" "${SOURCES}" "${LIBRARIES}") endif() diff --git a/src/components/formatters/test/CSmartFactory_test.cc b/src/components/formatters/test/CSmartFactory_test.cc new file mode 100644 index 0000000000..39cf67b3fd --- /dev/null +++ b/src/components/formatters/test/CSmartFactory_test.cc @@ -0,0 +1,397 @@ +/* + * Copyright (c) 2015, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "SmartFactoryTestHelper.h" +#include "formatters/CSmartFactory.hpp" +#include "gtest/gtest.h" + +namespace test { +namespace components { +namespace formatters { + +TEST(CSmartFactoryTest, CreateSmartSchemaKey_ExpectCreated) { + SmartSchemaKey<FunctionIdTest::eType, MessageTypeTest::eType> test_key( + FunctionIdTest::Function1, MessageTypeTest::notification); + EXPECT_EQ(test_key.functionId, FunctionIdTest::Function1); + EXPECT_EQ(test_key.messageType, MessageTypeTest::notification); +} + +TEST(CSmartFactoryTest, CreateSmartFactory_ExpectCreated) { + CSmartFactoryTest test_factory; + EXPECT_EQ(9u, test_factory.function_schemes().size()); + EXPECT_EQ(2u, test_factory.structs_schemes().size()); +} + +TEST(CSmartFactoryTest, CreateSmartObjWithSchema1_ExpectCreatedObjectToCorrespondSmSchema1) { + CSmartFactoryTest test_factory; + // Create SmartObject with attached SmartChmema + SmartObject obj = test_factory.CreateSmartObject(FunctionIdTest::Function1, + MessageTypeTest::request); + EXPECT_TRUE(SmartType::SmartType_Map == obj.getType()); + // Adding necessary fields + obj[S_PARAMS][S_FUNCTION_ID] = FunctionIdTest::Function1; + obj[S_PARAMS][S_MESSAGE_TYPE] = MessageTypeTest::request; + obj[S_PARAMS][S_CORRELATION_ID] = 444; + obj[S_PARAMS][S_PROTOCOL_VERSION] = 1; + obj[S_PARAMS][S_PROTOCOL_TYPE] = 0; + obj[S_MSG_PARAMS] = SmartObject(SmartType::SmartType_Map); + std::set<std::string> keys = obj.enumerate(); + EXPECT_EQ(2u, keys.size()); + EXPECT_EQ(Errors::eType::OK, obj.validate()); + EXPECT_TRUE(obj.isValid()); +} + +TEST(CSmartFactoryTest, CreateSmartObjWithNotExistedSchema_ExpectCreatedObjectNotValid) { + CSmartFactoryTest test_factory; + // Create SmartObject with attached SmartChmema + SmartObject obj = test_factory.CreateSmartObject( + FunctionIdTest::Function1, MessageTypeTest::INVALID_ENUM); + EXPECT_FALSE(SmartType::SmartType_Map == obj.getType()); + EXPECT_TRUE(SmartType::SmartType_Null == obj.getType()); + EXPECT_EQ(Errors::eType::OK, obj.validate()); + EXPECT_TRUE(obj.isValid()); +} + +TEST(CSmartFactoryTest, CreateSmartObjectWithSchema1_MissedOneField_ExpectCreatedNotCorrespondSmartSchema) { + CSmartFactoryTest test_factory; + // Create SmartObject with attached SmartChmema + SmartObject obj = test_factory.CreateSmartObject(FunctionIdTest::Function1, + MessageTypeTest::request); + EXPECT_TRUE(SmartType::SmartType_Map == obj.getType()); + // Adding necessary fields but one field is missed + obj[S_PARAMS][S_FUNCTION_ID] = FunctionIdTest::Function1; + obj[S_PARAMS][S_MESSAGE_TYPE] = MessageTypeTest::request; + obj[S_PARAMS][S_PROTOCOL_VERSION] = 1; + obj[S_PARAMS][S_PROTOCOL_TYPE] = 0; + obj[S_MSG_PARAMS] = SmartObject(SmartType::SmartType_Map); + std::set<std::string> keys = obj.enumerate(); + EXPECT_EQ(2u, keys.size()); + EXPECT_EQ(Errors::eType::MISSING_MANDATORY_PARAMETER, obj.validate()); + EXPECT_FALSE(obj.isValid()); +} + +TEST(CSmartFactoryTest, CreateSmartObjectWithSchema1_AddOutOfRangeValue_ExpectCreatedNotCorrespondSmartSchema) { + CSmartFactoryTest test_factory; + // Create SmartObject with attached SmartChmema + SmartObject obj = test_factory.CreateSmartObject(FunctionIdTest::Function1, + MessageTypeTest::request); + EXPECT_TRUE(SmartType::SmartType_Map == obj.getType()); + // Adding necessary fields but one field is missed + obj[S_PARAMS][S_FUNCTION_ID] = FunctionIdTest::Function1; + obj[S_PARAMS][S_MESSAGE_TYPE] = 5; + obj[S_PARAMS][S_PROTOCOL_VERSION] = 1; + obj[S_PARAMS][S_PROTOCOL_TYPE] = 0; + obj[S_PARAMS][S_CORRELATION_ID] = 444; + obj[S_MSG_PARAMS] = SmartObject(SmartType::SmartType_Map); + std::set<std::string> keys = obj.enumerate(); + EXPECT_EQ(2u, keys.size()); + EXPECT_EQ(Errors::eType::OUT_OF_RANGE, obj.validate()); + EXPECT_FALSE(obj.isValid()); +} + +TEST(CSmartFactoryTest, CreateSmartObjectWithSchema1_AddInvalidValue_ExpectCreatedNotCorrespondSmartSchema) { + CSmartFactoryTest test_factory; + // Create SmartObject with attached SmartChmema + SmartObject obj = test_factory.CreateSmartObject(FunctionIdTest::Function1, + MessageTypeTest::request); + EXPECT_TRUE(SmartType::SmartType_Map == obj.getType()); + // Adding necessary fields but one field is missed + obj[S_PARAMS][S_FUNCTION_ID] = FunctionIdTest::Function1; + obj[S_PARAMS][S_MESSAGE_TYPE] = "return"; + obj[S_PARAMS][S_PROTOCOL_VERSION] = 1; + obj[S_PARAMS][S_PROTOCOL_TYPE] = 0; + obj[S_PARAMS][S_CORRELATION_ID] = 444; + obj[S_MSG_PARAMS] = SmartObject(SmartType::SmartType_Map); + std::set<std::string> keys = obj.enumerate(); + EXPECT_EQ(2u, keys.size()); + EXPECT_EQ(Errors::eType::INVALID_VALUE, obj.validate()); + EXPECT_FALSE(obj.isValid()); +} + +TEST(CSmartFactoryTest, CreateSmartObj_AttachSchema1_ExpectCreatedObjectCorrespondsSmSchema1) { + CSmartFactoryTest test_factory; + // Create empty SmartObject without any schema + SmartObject obj; + // Adding fields necessary for schema to attach + obj[S_PARAMS][S_FUNCTION_ID] = FunctionIdTest::Function1; + obj[S_PARAMS][S_MESSAGE_TYPE] = MessageTypeTest::request; + // Attach schema to object + EXPECT_TRUE(test_factory.attachSchema(obj)); + EXPECT_TRUE(SmartType::SmartType_Map == obj.getType()); + // Adding necessary fileds to correspond schema + obj[S_PARAMS][S_CORRELATION_ID] = 444; + obj[S_PARAMS][S_PROTOCOL_VERSION] = 1; + obj[S_PARAMS][S_PROTOCOL_TYPE] = 0; + obj[S_MSG_PARAMS] = SmartObject(SmartType::SmartType_Map); + std::set<std::string> keys = obj.enumerate(); + EXPECT_EQ(2u, keys.size()); + EXPECT_EQ(Errors::eType::OK, obj.validate()); + EXPECT_TRUE(obj.isValid()); +} + +TEST(CSmartFactoryTest, CreateSmartObj_AttachSchema1_MissOneField_ExpectCreatedObjectNotCorrespondsSmSchema1) { + CSmartFactoryTest test_factory; + // Create empty SmartObject without any schema + SmartObject obj; + // Adding fields necessary for schema to attach + obj[S_PARAMS][S_FUNCTION_ID] = FunctionIdTest::Function1; + obj[S_PARAMS][S_MESSAGE_TYPE] = MessageTypeTest::request; + // Attach schema to object + EXPECT_TRUE(test_factory.attachSchema(obj)); + EXPECT_TRUE(SmartType::SmartType_Map == obj.getType()); + // Adding necessary fileds to correspond schema but 1 field is missing + obj[S_PARAMS][S_PROTOCOL_VERSION] = 1; + obj[S_PARAMS][S_PROTOCOL_TYPE] = 0; + obj[S_MSG_PARAMS] = SmartObject(SmartType::SmartType_Map); + std::set<std::string> keys = obj.enumerate(); + EXPECT_EQ(2u, keys.size()); + EXPECT_EQ(Errors::eType::MISSING_MANDATORY_PARAMETER, obj.validate()); + EXPECT_FALSE(obj.isValid()); +} + +TEST(CSmartFactoryTest, CreateSmartObj_AttachNotExistedSchema_ExpectSmSchemaNotAttached) { + CSmartFactoryTest test_factory; + // Create empty SmartObject without any schema + SmartObject obj; + // Adding fields necessary for schema to attach but one value is invalid + obj[S_PARAMS][S_FUNCTION_ID] = FunctionIdTest::Function1; + obj[S_PARAMS][S_MESSAGE_TYPE] = 10; + // Attach schema to object + EXPECT_FALSE(test_factory.attachSchema(obj)); + EXPECT_TRUE(SmartType::SmartType_Map == obj.getType()); +} + +TEST(CSmartFactoryTest, CreateSmartObj_AttachSchema1_AddInvalidValue_ExpectCreatedObjectNotCorrespondsSmSchema1) { + CSmartFactoryTest test_factory; + // Create empty SmartObject without any schema + SmartObject obj; + // Adding fields necessary for schema to attach + obj[S_PARAMS][S_FUNCTION_ID] = FunctionIdTest::Function1; + obj[S_PARAMS][S_MESSAGE_TYPE] = MessageTypeTest::request; + // Attach schema to object + EXPECT_TRUE(test_factory.attachSchema(obj)); + EXPECT_TRUE(SmartType::SmartType_Map == obj.getType()); + // Adding necessary fileds to correspond schema but 1 field is missing + obj[S_PARAMS][S_PROTOCOL_VERSION] = "string"; + obj[S_PARAMS][S_PROTOCOL_TYPE] = 0; + obj[S_PARAMS][S_CORRELATION_ID] = 444; + obj[S_MSG_PARAMS] = SmartObject(SmartType::SmartType_Map); + std::set<std::string> keys = obj.enumerate(); + EXPECT_EQ(2u, keys.size()); + EXPECT_EQ(Errors::eType::INVALID_VALUE, obj.validate()); + EXPECT_FALSE(obj.isValid()); +} + +TEST(CSmartFactoryTest, CreateSmartObj_AttachSchema1_ExpectCreatedObjectCorrespondsSmSchema) { + CSmartFactoryTest test_factory; + // Create empty SmartObject without any schema + SmartObject obj; + // Attach schema to object + EXPECT_TRUE(test_factory.AttachSchema(StructIdentifiersTest::Common_1, obj)); + obj["text"] = "test"; + obj["position"] = 200; + std::set<std::string> keys = obj.enumerate(); + EXPECT_EQ(2u, keys.size()); + EXPECT_EQ(Errors::eType::OK, obj.validate()); + EXPECT_TRUE(obj.isValid()); +} + +TEST(CSmartFactoryTest, CreateSmartObj_AttachSchema1_OneMandatoryFieldMissed_ExpectCreatedObjectNotCorrespondsSmSchema) { + CSmartFactoryTest test_factory; + // Create empty SmartObject without any schema + SmartObject obj; + // Attach schema to object + EXPECT_TRUE(test_factory.AttachSchema(StructIdentifiersTest::Common_1, obj)); + obj["text"] = "test"; + std::set<std::string> keys = obj.enumerate(); + EXPECT_EQ(1u, keys.size()); + EXPECT_EQ(Errors::eType::MISSING_MANDATORY_PARAMETER, obj.validate()); + EXPECT_FALSE(obj.isValid()); +} + +TEST(CSmartFactoryTest, CreateSmartObj_AttachSchema2_ExpectCreatedObjectCorrespondsSmSchema) { + CSmartFactoryTest test_factory; + // Create empty SmartObject without any schema + SmartObject obj; + // Attach schema to object + EXPECT_TRUE(test_factory.AttachSchema(StructIdentifiersTest::Common_2, obj)); + obj["text"] = "test1"; + obj["position"] = 200; + std::set<std::string> keys = obj.enumerate(); + EXPECT_EQ(2u, keys.size()); + EXPECT_EQ(Errors::eType::OK, obj.validate()); + EXPECT_TRUE(obj.isValid()); +} + +TEST(CSmartFactoryTest, CreateSmartObj_AttachSchema_ExpectCreatedObjectCorrespondsSmSchema) { + CSmartFactoryTest test_factory; + // Create empty SmartObject without any schema + SmartObject obj; + // Attach schema to object + EXPECT_TRUE(test_factory.AttachSchema(StructIdentifiersTest::Common_1, obj)); + obj["text"] = "test"; + obj["position"] = 200; + obj["image"]["text"] = "test2"; + obj["image"]["position"] = 100; + EXPECT_EQ(Errors::eType::OK, obj["image"].validate()); + std::set<std::string> keys = obj.enumerate(); + EXPECT_EQ(3u, keys.size()); + EXPECT_EQ(Errors::eType::OK, obj.validate()); + EXPECT_TRUE(obj.isValid()); +} + +TEST(CSmartFactoryTest, CreateSmartObj_WithSchemaFromStructId_ExpectCreatedObjectCorrespondsSmSchema) { + CSmartFactoryTest test_factory; + // Create empty SmartObject with schema correspopnding StructId + SmartObject obj = test_factory.CreateSmartObject( + StructIdentifiersTest::Common_1); + // Add fields + obj["text"] = "test"; + obj["position"] = 200; + obj["image"]["text"] = "test2"; + obj["image"]["position"] = 100; + // Check object "image" + EXPECT_EQ(Errors::eType::OK, obj["image"].validate()); + std::set<std::string> keys = obj.enumerate(); + EXPECT_EQ(3u, keys.size()); + // Check global object + EXPECT_EQ(Errors::eType::OK, obj.validate()); + EXPECT_TRUE(obj.isValid()); +} + +TEST(CSmartFactoryTest, CreateSmartObj_WithSchemaFromStructId_MissedOneField_ExpectCreatedObjectNotCorrespondsSmSchema) { + CSmartFactoryTest test_factory; + // Create empty SmartObject with schema correspopnding StructId + SmartObject obj = test_factory.CreateSmartObject( + StructIdentifiersTest::Common_1); + // Add fields. One missed. + obj["text"] = "test"; + obj["image"]["text"] = "test2"; + obj["image"]["position"] = 100; + // Check object "image" + EXPECT_EQ(Errors::eType::OK, obj["image"].validate()); + std::set<std::string> keys = obj.enumerate(); + EXPECT_EQ(2u, keys.size()); + // Check global object + EXPECT_EQ(Errors::eType::MISSING_MANDATORY_PARAMETER, obj.validate()); + EXPECT_FALSE(obj.isValid()); +} + +TEST(CSmartFactoryTest, CreateSmartObj_WithSchemaFromStructId2_ExpectCreatedObjectCorrespondsSmSchema) { + CSmartFactoryTest test_factory; + // Create empty SmartObject with schema correspopnding StructId + SmartObject obj = test_factory.CreateSmartObject( + StructIdentifiersTest::Common_2); + // Add fields + obj["text"] = "test"; + obj["position"] = 200; + obj["image"]["text"] = "test2"; + obj["image"]["position"] = 100; + // Check object "image" + EXPECT_EQ(Errors::eType::OK, obj["image"].validate()); + std::set<std::string> keys = obj.enumerate(); + EXPECT_EQ(3u, keys.size()); + // Check global object + EXPECT_EQ(Errors::eType::OK, obj.validate()); + EXPECT_TRUE(obj.isValid()); +} + +TEST(CSmartFactoryTest, CreateSmartObj_WithSchemaFromStructId2_MissedOneField_ExpectCreatedObjectNotCorrespondsSmSchema) { + CSmartFactoryTest test_factory; + // Create empty SmartObject with schema correspopnding StructId + SmartObject obj = test_factory.CreateSmartObject( + StructIdentifiersTest::Common_2); + // Add fields. One missed. + obj["text"] = "test"; + obj["image"]["text"] = "test2"; + std::set<std::string> keys = obj.enumerate(); + EXPECT_EQ(2u, keys.size()); + // Check global object + EXPECT_EQ(Errors::eType::MISSING_MANDATORY_PARAMETER, obj.validate()); + EXPECT_FALSE(obj.isValid()); +} + +TEST(CSmartFactoryTest, CreateSmartObj_WithSchemaFromStructId2_InvalidValueAdded_ExpectCreatedObjectNotCorrespondsSmSchema) { + CSmartFactoryTest test_factory; + // Create empty SmartObject with schema correspopnding StructId + SmartObject obj = test_factory.CreateSmartObject( + StructIdentifiersTest::Common_2); + // Add fields. One missed. + obj["text"] = 111; + obj["position"] = 200; + obj["image"]["text"] = "test2"; + std::set<std::string> keys = obj.enumerate(); + EXPECT_EQ(3u, keys.size()); + // Check global object + EXPECT_EQ(Errors::eType::INVALID_VALUE, obj.validate()); + EXPECT_FALSE(obj.isValid()); +} + +TEST(CSmartFactoryTest, GetSchemaWithSmartFactory_ExpectReceivedSchema) { + CSmartFactoryTest test_factory; + CSmartSchema schema; + EXPECT_TRUE( + test_factory.GetSchema(FunctionIdTest::Function1, + MessageTypeTest::request, schema)); +} + +TEST(CSmartFactoryTest, GetNotExistedSchemaWithSmartFactory_ExpectNotReceivedSchema) { + CSmartFactoryTest test_factory; + CSmartSchema schema; + EXPECT_FALSE( + test_factory.GetSchema(FunctionIdTest::Function1, + MessageTypeTest::INVALID_ENUM, schema)); +} + +TEST(CSmartFactoryTest, GetSchemaWithSmartFactoryWithStructId1_ExpectReceivedSchema) { + CSmartFactoryTest test_factory; + CSmartSchema schema; + EXPECT_TRUE(test_factory.GetSchema(StructIdentifiersTest::Common_1, schema)); +} + +TEST(CSmartFactoryTest, GetSchemaWithSmartFactoryWithStructId2_ExpectReceivedSchema) { + CSmartFactoryTest test_factory; + CSmartSchema schema; + EXPECT_TRUE(test_factory.GetSchema(StructIdentifiersTest::Common_2, schema)); +} + +TEST(CSmartFactoryTest, GetNotExistedSchemaWithSmartFactoryWithStructId_ExpectNotReceivedSchema) { + CSmartFactoryTest test_factory; + CSmartSchema schema; + EXPECT_FALSE( + test_factory.GetSchema(StructIdentifiersTest::INVALID_ENUM, schema)); +} + +} // namespace formatters +} // namespace components +} // namespace test + diff --git a/src/components/formatters/test/cFormatterJsonSDLRPCv1_test.cc b/src/components/formatters/test/cFormatterJsonSDLRPCv1_test.cc new file mode 100644 index 0000000000..55b7f886fd --- /dev/null +++ b/src/components/formatters/test/cFormatterJsonSDLRPCv1_test.cc @@ -0,0 +1,502 @@ +/* + * Copyright (c) 2015, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "gtest/gtest.h" + +#include "formatters/CFormatterJsonSDLRPCv1.hpp" +#include "create_smartSchema.h" + +namespace test { +namespace components { +namespace formatters { + +TEST(CFormatterJsonSDLRPCv1Test, EmptySmartObjectToString) { + SmartObject srcObj; + + EXPECT_EQ(Errors::eType::OK, srcObj.validate()); + + std::string jsonString; + bool result = CFormatterJsonSDLRPCv1::toString(srcObj, jsonString); + + EXPECT_TRUE(result); + + std::string expectOutputJsonString = + "{\n \ + \"\" : {\n\ + \"name\" : \"\",\n\ + \"parameters\" : \"\"\n\ + }\n\ +}\n"; + + EXPECT_EQ(expectOutputJsonString, jsonString); +} + +TEST(CFormatterJsonSDLRPCv1Test, SmObjWithRequestWithoutMsgNotValid_ToString) { + SmartObject srcObj; + CSmartSchema schema = initObjectSchema(); + srcObj.setSchema(schema); + + srcObj[S_PARAMS][S_MESSAGE_TYPE] = MessageTypeTest::request; + srcObj[S_PARAMS][S_FUNCTION_ID] = FunctionIDTest::RegisterAppInterface; + srcObj[S_PARAMS][S_CORRELATION_ID] = 13; + srcObj[S_PARAMS][S_PROTOCOL_TYPE] = 0; + srcObj[S_PARAMS][S_PROTOCOL_VERSION] = 1; + + EXPECT_EQ(Errors::eType::MISSING_MANDATORY_PARAMETER, srcObj.validate()); + + std::string jsonString; + bool result = CFormatterJsonSDLRPCv1::toString(srcObj, jsonString); + EXPECT_TRUE(result); + + std::string expectOutputJsonString = + "{\n \ + \"request\" : {\n\ + \"correlationID\" : 13,\n\ + \"name\" : \"RegisterAppInterface\",\n\ + \"parameters\" : \"\"\n\ + }\n\ +}\n"; + + EXPECT_EQ(expectOutputJsonString, jsonString); +} + +TEST(CFormatterJsonSDLRPCv1Test, SmObjWithRequestWithEmptyMsgWithTestSchemaToString) { + SmartObject srcObj; + CSmartSchema schema = initObjectSchema(); + srcObj.setSchema(schema); + + srcObj[S_PARAMS][S_MESSAGE_TYPE] = MessageTypeTest::request; + srcObj[S_PARAMS][S_FUNCTION_ID] = FunctionIDTest::RegisterAppInterface; + srcObj[S_PARAMS][S_CORRELATION_ID] = 13; + srcObj[S_PARAMS][S_PROTOCOL_TYPE] = 0; + srcObj[S_PARAMS][S_PROTOCOL_VERSION] = 1; + srcObj[S_MSG_PARAMS][""] = ""; + + EXPECT_EQ(Errors::eType::OK, srcObj.validate()); + + std::string jsonString; + + bool result = CFormatterJsonSDLRPCv1::toString(srcObj, jsonString); + + EXPECT_TRUE(result); + + std::string expectOutputJsonString = + "{\n \ + \"request\" : {\n\ + \"correlationID\" : 13,\n\ + \"name\" : \"RegisterAppInterface\",\n\ + \"parameters\" : {}\n\ + }\n\ +}\n"; + + EXPECT_EQ(expectOutputJsonString, jsonString); +} + +TEST(CFormatterJsonSDLRPCv1Test, SmObjWithRequestWithNonemptyMsgWithTestSchemaToString) { + SmartObject srcObj; + CSmartSchema schema = initObjectSchema(); + srcObj.setSchema(schema); + + srcObj[S_PARAMS][S_MESSAGE_TYPE] = MessageTypeTest::request; + srcObj[S_PARAMS][S_FUNCTION_ID] = FunctionIDTest::RegisterAppInterface; + srcObj[S_PARAMS][S_CORRELATION_ID] = 13; + srcObj[S_PARAMS][S_PROTOCOL_TYPE] = 0; + srcObj[S_PARAMS][S_PROTOCOL_VERSION] = 1; + srcObj[S_MSG_PARAMS]["info"] = "value"; + + std::string jsonString; + + bool result = CFormatterJsonSDLRPCv1::toString(srcObj, jsonString); + + EXPECT_TRUE(result); + + std::string expectOutputJsonString = + "{\n \ + \"request\" : {\n\ + \"correlationID\" : 13,\n\ + \"name\" : \"RegisterAppInterface\",\n\ + \"parameters\" : {\n\ + \"info\" : \"value\"\n\ + }\n\ + }\n\ +}\n"; + + EXPECT_EQ(expectOutputJsonString, jsonString); +} + +TEST(CFormatterJsonSDLRPCv1Test, SmObjWithRequestWithNonemptyMsgToString) { + SmartObject srcObj; + + srcObj[S_PARAMS][S_MESSAGE_TYPE] = MessageTypeTest::request; + srcObj[S_PARAMS][S_FUNCTION_ID] = 5; + srcObj[S_PARAMS][S_CORRELATION_ID] = 13; + srcObj[S_PARAMS][S_PROTOCOL_TYPE] = 0; + srcObj[S_PARAMS][S_PROTOCOL_VERSION] = 1; + srcObj[S_MSG_PARAMS]["vrSynonyms"][0] = "Synonym 1"; + + std::string jsonString; + + bool result = CFormatterJsonSDLRPCv1::toString(srcObj, jsonString); + + EXPECT_TRUE(result); + + std::string expectOutputJsonString = + "{\n \ + \"0\" : {\n\ + \"correlationID\" : 13,\n\ + \"name\" : \"5\",\n\ + \"parameters\" : {\n\ + \"vrSynonyms\" : [ \"Synonym 1\" ]\n\ + }\n\ + }\n\ +}\n"; + EXPECT_EQ(expectOutputJsonString, jsonString); +} + +TEST(CFormatterJsonSDLRPCv1Test, SmObjWithResponseWithoutSchemaToString) { + SmartObject srcObj; + + srcObj[S_PARAMS][S_MESSAGE_TYPE] = MessageTypeTest::response; + srcObj[S_PARAMS][S_FUNCTION_ID] = 5; + srcObj[S_PARAMS][S_CORRELATION_ID] = 13; + srcObj[S_PARAMS][S_PROTOCOL_TYPE] = 0; + srcObj[S_PARAMS][S_PROTOCOL_VERSION] = 1; + srcObj[S_MSG_PARAMS]["success"] = true; + srcObj[S_MSG_PARAMS]["resultCode"] = 0; + + std::string jsonString; + + bool result = CFormatterJsonSDLRPCv1::toString(srcObj, jsonString); + + EXPECT_TRUE(result); + + std::string expectOutputJsonString = + "{\n \ + \"1\" : {\n\ + \"correlationID\" : 13,\n\ + \"name\" : \"5\",\n\ + \"parameters\" : {\n\ + \"resultCode\" : 0,\n\ + \"success\" : true\n\ + }\n\ + }\n\ +}\n"; + EXPECT_EQ(expectOutputJsonString, jsonString); +} + +TEST(CFormatterJsonSDLRPCv1Test, SmObjWithNotificationToString) { + SmartObject srcObj; + CSmartSchema schema = initObjectSchema(); + srcObj.setSchema(schema); + + srcObj[S_PARAMS][S_MESSAGE_TYPE] = MessageTypeTest::notification; + srcObj[S_PARAMS][S_FUNCTION_ID] = FunctionIDTest::SetGlobalProperties; + srcObj[S_PARAMS][S_CORRELATION_ID] = 13; + srcObj[S_PARAMS][S_PROTOCOL_TYPE] = 0; + srcObj[S_PARAMS][S_PROTOCOL_VERSION] = 1; + srcObj[S_MSG_PARAMS][""] = ""; + std::string jsonString; + + bool result = CFormatterJsonSDLRPCv1::toString(srcObj, jsonString); + + EXPECT_TRUE(result); + + std::string expectOutputJsonString = + "{\n \ + \"notification\" : {\n\ + \"correlationID\" : 13,\n\ + \"name\" : \"SetGlobalProperties\",\n\ + \"parameters\" : {}\n\ + }\n\ +}\n"; + + EXPECT_EQ(expectOutputJsonString, jsonString); +} + +TEST(CFormatterJsonSDLRPCv1Test, SmObjWithResponseToString) { + SmartObject srcObj; + + CSmartSchema schema = initObjectSchema(); + srcObj.setSchema(schema); + + srcObj[S_PARAMS][S_MESSAGE_TYPE] = MessageTypeTest::response; + srcObj[S_PARAMS][S_FUNCTION_ID] = FunctionIDTest::RegisterAppInterface; + srcObj[S_PARAMS][S_CORRELATION_ID] = 13; + srcObj[S_PARAMS][S_PROTOCOL_TYPE] = 0; + srcObj[S_PARAMS][S_PROTOCOL_VERSION] = 1; + + srcObj[S_MSG_PARAMS]["success"] = true; + srcObj[S_MSG_PARAMS]["resultCode"] = TestType::SUCCESS; + + std::string jsonString; + + bool result = CFormatterJsonSDLRPCv1::toString(srcObj, jsonString); + + EXPECT_TRUE(result); + + std::string expectOutputJsonString = + "{\n \ + \"response\" : {\n\ + \"correlationID\" : 13,\n\ + \"name\" : \"RegisterAppInterface\",\n\ + \"parameters\" : {\n\ + \"resultCode\" : \"SUCCESS\",\n\ + \"success\" : true\n\ + }\n\ + }\n\ +}\n"; + + EXPECT_EQ(expectOutputJsonString, jsonString); +} + +TEST(CFormatterJsonSDLRPCv1Test, SmObjWithResponseWithoutSchemaWithoutParamsToString) { + SmartObject srcObj; + srcObj[S_PARAMS][S_MESSAGE_TYPE] = MessageTypeTest::response; + std::string jsonString; + + bool result = CFormatterJsonSDLRPCv1::toString(srcObj, jsonString); + + EXPECT_TRUE(result); + + std::string expectOutputJsonString = + "{\n \ + \"1\" : {\n\ + \"name\" : \"\",\n\ + \"parameters\" : \"\"\n\ + }\n\ +}\n"; + + EXPECT_EQ(expectOutputJsonString, jsonString); +} + +TEST(CFormatterJsonSDLRPCv1Test, StringRequestToSmObj) { + std::string inputJsonString = + "\ + {\ + \"request\": {\ + \"correlationID\": 5,\ + \"name\" : \"RegisterAppInterface\",\n\ + \"parameters\": {\ + \"syncMsgVersion\" : {\ + \"majorVersion\" : 2,\ + \"minorVersion\" : 10\ + },\ + \"appName\": \"some app name\",\ + \"ttsName\": [{\ + \"text\": \"ABC\",\ + \"type\": \"TEXT\"\ + }],\ + \"vrSynonyms\": [\"Synonym 1\", \"Synonym 2\"]\ + }\ + }\ + }"; + + SmartObject obj; + + CSmartSchema schema = initObjectSchema(); + obj.setSchema(schema); + + bool result = CFormatterJsonSDLRPCv1::fromString<FunctionIDTest::eType, + MessageTypeTest::eType>(inputJsonString, obj); + + EXPECT_EQ(CFormatterJsonSDLRPCv1::kSuccess, result); + EXPECT_EQ(Errors::eType::OK, obj.validate()); + EXPECT_EQ(obj[S_PARAMS][S_MESSAGE_TYPE], MessageTypeTest::request); + EXPECT_EQ(obj[S_PARAMS][S_FUNCTION_ID], FunctionIDTest::RegisterAppInterface); + EXPECT_EQ(obj[S_PARAMS][S_CORRELATION_ID], 5); + EXPECT_EQ(obj[S_PARAMS][S_PROTOCOL_TYPE], 0); + EXPECT_EQ(obj[S_PARAMS][S_PROTOCOL_VERSION], 1); + EXPECT_EQ(obj[S_MSG_PARAMS]["appName"], "some app name"); + + EXPECT_EQ(obj[S_MSG_PARAMS]["syncMsgVersion"]["majorVersion"], 2); + EXPECT_EQ(obj[S_MSG_PARAMS]["syncMsgVersion"]["minorVersion"], 10); + EXPECT_EQ(obj[S_MSG_PARAMS]["ttsName"][0]["text"], "ABC"); + EXPECT_EQ(obj[S_MSG_PARAMS]["ttsName"][0]["type"], "TEXT"); + EXPECT_EQ(obj[S_MSG_PARAMS]["vrSynonyms"][0], "Synonym 1"); + EXPECT_EQ(obj[S_MSG_PARAMS]["vrSynonyms"][1], "Synonym 2"); +} + +TEST(CFormatterJsonSDLRPCv1Test, StringRequestWithoutNameToSmartObject) { + std::string inputJsonString = + "\ + {\ + \"request\": {\ + \"correlationID\": 5,\ + \"parameters\": {\ + \"syncMsgVersion\" : {\ + \"majorVersion\" : 2,\ + \"minorVersion\" : 10\ + },\ + \"appName\": \"some app name\",\ + \"ttsName\": [{\ + \"text\": \"ABC\",\ + \"type\": \"TEXT\"\ + }],\ + \"vrSynonyms\": [\"Synonym 1\", \"Synonym 2\"]\ + }\ + }\ + }"; + + SmartObject obj; + + bool result = CFormatterJsonSDLRPCv1::fromString<FunctionIDTest::eType, + MessageTypeTest::eType>(inputJsonString, obj); + + EXPECT_EQ(CFormatterJsonSDLRPCv1::kParsingError, result); + + EXPECT_EQ(obj[S_PARAMS][S_MESSAGE_TYPE], MessageTypeTest::request); + EXPECT_EQ(obj[S_PARAMS][S_FUNCTION_ID], "-1"); + EXPECT_EQ(obj[S_PARAMS][S_CORRELATION_ID], 5); + EXPECT_EQ(obj[S_MSG_PARAMS]["appName"], "some app name"); + + EXPECT_EQ(obj[S_MSG_PARAMS]["syncMsgVersion"]["majorVersion"], 2); + EXPECT_EQ(obj[S_MSG_PARAMS]["syncMsgVersion"]["minorVersion"], 10); + EXPECT_EQ(obj[S_MSG_PARAMS]["ttsName"][0]["text"], "ABC"); + EXPECT_EQ(obj[S_MSG_PARAMS]["ttsName"][0]["type"], "TEXT"); + EXPECT_EQ(obj[S_MSG_PARAMS]["vrSynonyms"][0], "Synonym 1"); + EXPECT_EQ(obj[S_MSG_PARAMS]["vrSynonyms"][1], "Synonym 2"); +} + +TEST(CFormatterJsonSDLRPCv1Test, StringRequestWithIncorrectCorIDToSmartObject) { + std::string inputJsonString = + "\ + {\ + \"request\": {\ + \"correlationID\": \"5\",\ + \"parameters\": {\ + \"syncMsgVersion\" : {\ + \"majorVersion\" : 2,\ + \"minorVersion\" : 10\ + },\ + \"appName\": \"some app name\",\ + \"ttsName\": [{\ + \"text\": \"ABC\",\ + \"type\": \"TEXT\"\ + }],\ + \"vrSynonyms\": [\"Synonym 1\", \"Synonym 2\"]\ + }\ + }\ + }"; + + SmartObject obj; + + bool result = CFormatterJsonSDLRPCv1::fromString<FunctionIDTest::eType, + MessageTypeTest::eType>(inputJsonString, obj); + EXPECT_EQ(CFormatterJsonSDLRPCv1::kParsingError, result); + + EXPECT_EQ(obj[S_PARAMS][S_MESSAGE_TYPE], MessageTypeTest::request); + EXPECT_EQ(obj[S_PARAMS][S_FUNCTION_ID], "-1"); + EXPECT_EQ(obj[S_MSG_PARAMS]["appName"], "some app name"); + EXPECT_EQ(obj[S_MSG_PARAMS]["syncMsgVersion"]["majorVersion"], 2); + EXPECT_EQ(obj[S_MSG_PARAMS]["ttsName"][0]["text"], "ABC"); + EXPECT_EQ(obj[S_MSG_PARAMS]["vrSynonyms"][0], "Synonym 1"); +} + +TEST(CFormatterJsonSDLRPCv1Test, StringResponceToSmartObject) { + std::string inputJsonString = + "{\n \ + \"response\" : {\n\ + \"correlationID\" : 13,\n\ + \"name\" : \"RegisterAppInterface\",\n\ + \"parameters\" : {\n\ + \"resultCode\" : \"SUCCESS\",\n\ + \"success\" : true\n\ + }\n\ + }\n\ +}\n"; + + SmartObject obj; + + CSmartSchema schema = initObjectSchema(); + obj.setSchema(schema); + + bool result = CFormatterJsonSDLRPCv1::fromString<FunctionIDTest::eType, + MessageTypeTest::eType>(inputJsonString, obj); + EXPECT_EQ(CFormatterJsonSDLRPCv1::kSuccess, result); + EXPECT_EQ(obj[S_PARAMS][S_MESSAGE_TYPE], MessageTypeTest::response); + EXPECT_EQ(obj[S_PARAMS][S_FUNCTION_ID], 0); + EXPECT_EQ(obj[S_PARAMS][S_CORRELATION_ID], 13); + EXPECT_EQ(obj[S_PARAMS][S_PROTOCOL_TYPE], 0); + EXPECT_EQ(obj[S_PARAMS][S_PROTOCOL_VERSION], 1); + EXPECT_EQ(obj[S_MSG_PARAMS]["resultCode"], "SUCCESS"); + EXPECT_EQ(obj[S_MSG_PARAMS]["success"], true); +} + +TEST(CFormatterJsonSDLRPCv1Test, StringNotificationToSmartObject) { + std::string inputJsonString = + "{\n \ + \"notification\" : {\n\ + \"correlationID\" : 13,\n\ + \"name\" : \"SetGlobalProperties\",\n\ + \"parameters\" : {}\n\ + }\n\ +}\n"; + + SmartObject obj; + + CSmartSchema schema = initObjectSchema(); + obj.setSchema(schema); + + bool result = CFormatterJsonSDLRPCv1::fromString<FunctionIDTest::eType, + MessageTypeTest::eType>(inputJsonString, obj); + EXPECT_EQ(CFormatterJsonSDLRPCv1::kSuccess, result); + EXPECT_EQ(Errors::eType::OK, obj.validate()); + EXPECT_EQ(obj[S_PARAMS][S_MESSAGE_TYPE], MessageTypeTest::notification); + EXPECT_EQ(obj[S_PARAMS][S_FUNCTION_ID], FunctionIDTest::SetGlobalProperties); + EXPECT_EQ(obj[S_PARAMS][S_CORRELATION_ID], 13); + EXPECT_EQ(obj[S_PARAMS][S_PROTOCOL_TYPE], 0); + EXPECT_EQ(obj[S_PARAMS][S_PROTOCOL_VERSION], 1); +} + +TEST(CFormatterJsonSDLRPCv1Test, MetaFormatToString) { + SmartObject srcObj; + + srcObj[S_PARAMS][S_MESSAGE_TYPE] = MessageTypeTest::request; + srcObj[S_PARAMS][S_FUNCTION_ID] = FunctionIDTest::RegisterAppInterface; + srcObj[S_PARAMS][S_CORRELATION_ID] = 13; + srcObj[S_PARAMS][S_PROTOCOL_TYPE] = 0; + srcObj[S_PARAMS][S_PROTOCOL_VERSION] = 1; + srcObj[S_MSG_PARAMS]["info"] = "value"; + + std::string jsonString; + + CSmartSchema schema = initObjectSchema(); + srcObj.setSchema(schema); + + meta_formatter_error_code::tMetaFormatterErrorCode result = + CFormatterJsonSDLRPCv1::MetaFormatToString(srcObj, schema, jsonString); + EXPECT_EQ(meta_formatter_error_code::kErrorOk, result); +} + +} // namespace formatters +} // namespace components +} // namespace test diff --git a/src/components/formatters/test/cFormatterJsonSDLRPCv2_test.cc b/src/components/formatters/test/cFormatterJsonSDLRPCv2_test.cc new file mode 100644 index 0000000000..814cff4ab7 --- /dev/null +++ b/src/components/formatters/test/cFormatterJsonSDLRPCv2_test.cc @@ -0,0 +1,392 @@ +/* + * Copyright (c) 2015, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "gtest/gtest.h" +#include "create_smartSchema.h" +#include "formatters/CFormatterJsonSDLRPCv2.hpp" + +namespace test { +namespace components { +namespace formatters { + +TEST(CFormatterJsonSDLRPCv2Test, EmptySmartObjectToString) { + SmartObject srcObj; + + EXPECT_EQ(Errors::eType::OK, srcObj.validate()); + + std::string jsonString; + bool result = CFormatterJsonSDLRPCv2::toString(srcObj, jsonString); + + EXPECT_TRUE(result); + + std::string expectOutputJsonString = "\"\"\n"; + + EXPECT_EQ(expectOutputJsonString, jsonString); +} + +TEST(CFormatterJsonSDLRPCv2Test, SmObjWithRequestWithoutMsgNotValid_ToString) { + SmartObject srcObj; + CSmartSchema schema = initObjectSchema(); + srcObj.setSchema(schema); + + srcObj[S_PARAMS][S_MESSAGE_TYPE] = MessageTypeTest::request; + srcObj[S_PARAMS][S_FUNCTION_ID] = FunctionIDTest::RegisterAppInterface; + srcObj[S_PARAMS][S_CORRELATION_ID] = 13; + srcObj[S_PARAMS][S_PROTOCOL_TYPE] = 0; + srcObj[S_PARAMS][S_PROTOCOL_VERSION] = 2; + + EXPECT_EQ(Errors::eType::MISSING_MANDATORY_PARAMETER, srcObj.validate()); + + std::string jsonString; + bool result = CFormatterJsonSDLRPCv2::toString(srcObj, jsonString); + EXPECT_TRUE(result); + + std::string expectOutputJsonString = "\"\"\n"; + + EXPECT_EQ(expectOutputJsonString, jsonString); +} + +TEST(CFormatterJsonSDLRPCv2Test, SmObjWithRequestWithEmptyMsgWithTestSchemaToString) { + SmartObject srcObj; + CSmartSchema schema = initObjectSchema(); + srcObj.setSchema(schema); + + srcObj[S_PARAMS][S_MESSAGE_TYPE] = MessageTypeTest::request; + srcObj[S_PARAMS][S_FUNCTION_ID] = FunctionIDTest::RegisterAppInterface; + srcObj[S_PARAMS][S_CORRELATION_ID] = 13; + srcObj[S_PARAMS][S_PROTOCOL_TYPE] = 0; + srcObj[S_PARAMS][S_PROTOCOL_VERSION] = 2; + srcObj[S_MSG_PARAMS][""] = ""; + + EXPECT_EQ(Errors::eType::OK, srcObj.validate()); + + std::string jsonString; + + bool result = CFormatterJsonSDLRPCv2::toString(srcObj, jsonString); + + EXPECT_TRUE(result); + + std::string expectOutputJsonString = "{}\n"; + + EXPECT_EQ(expectOutputJsonString, jsonString); +} + +TEST(CFormatterJsonSDLRPCv2Test, SmObjWithRequestWithNonemptyMsgWithTestSchemaToString) { + SmartObject srcObj; + CSmartSchema schema = initObjectSchema(); + srcObj.setSchema(schema); + + srcObj[S_PARAMS][S_MESSAGE_TYPE] = MessageTypeTest::request; + srcObj[S_PARAMS][S_FUNCTION_ID] = FunctionIDTest::RegisterAppInterface; + srcObj[S_PARAMS][S_CORRELATION_ID] = 13; + srcObj[S_PARAMS][S_PROTOCOL_TYPE] = 0; + srcObj[S_PARAMS][S_PROTOCOL_VERSION] = 2; + srcObj[S_MSG_PARAMS]["info"] = "value"; + + std::string jsonString; + + bool result = CFormatterJsonSDLRPCv2::toString(srcObj, jsonString); + + EXPECT_TRUE(result); + + std::string expectOutputJsonString = "{\n \"info\" : \"value\"\n}\n"; + + EXPECT_EQ(expectOutputJsonString, jsonString); +} + +TEST(CFormatterJsonSDLRPCv2Test, SmObjWithRequestWithNonemptyMsgToString) { + SmartObject srcObj; + + srcObj[S_PARAMS][S_MESSAGE_TYPE] = MessageTypeTest::request; + srcObj[S_PARAMS][S_FUNCTION_ID] = 5; + srcObj[S_PARAMS][S_CORRELATION_ID] = 13; + srcObj[S_PARAMS][S_PROTOCOL_TYPE] = 0; + srcObj[S_PARAMS][S_PROTOCOL_VERSION] = 2; + srcObj[S_MSG_PARAMS]["vrSynonyms"][0] = "Synonym 1"; + + std::string jsonString; + + bool result = CFormatterJsonSDLRPCv2::toString(srcObj, jsonString); + + EXPECT_TRUE(result); + + std::string expectOutputJsonString = + "{\n \"vrSynonyms\" : [ \"Synonym 1\" ]\n}\n"; + EXPECT_EQ(expectOutputJsonString, jsonString); +} + +TEST(CFormatterJsonSDLRPCv2Test, SmObjWithResponseWithoutSchemaToString) { + SmartObject srcObj; + + srcObj[S_PARAMS][S_MESSAGE_TYPE] = MessageTypeTest::response; + srcObj[S_PARAMS][S_FUNCTION_ID] = 5; + srcObj[S_PARAMS][S_CORRELATION_ID] = 13; + srcObj[S_PARAMS][S_PROTOCOL_TYPE] = 0; + srcObj[S_PARAMS][S_PROTOCOL_VERSION] = 2; + srcObj[S_MSG_PARAMS]["success"] = true; + srcObj[S_MSG_PARAMS]["resultCode"] = 0; + + std::string jsonString; + + bool result = CFormatterJsonSDLRPCv2::toString(srcObj, jsonString); + + EXPECT_TRUE(result); + + std::string expectOutputJsonString = + "{\n \"resultCode\" : 0,\n \"success\" : true\n}\n"; + EXPECT_EQ(expectOutputJsonString, jsonString); +} + +TEST(CFormatterJsonSDLRPCv2Test, SmObjWithNotificationToString) { + SmartObject srcObj; + CSmartSchema schema = initObjectSchema(); + srcObj.setSchema(schema); + + srcObj[S_PARAMS][S_MESSAGE_TYPE] = MessageTypeTest::notification; + srcObj[S_PARAMS][S_FUNCTION_ID] = FunctionIDTest::SetGlobalProperties; + srcObj[S_PARAMS][S_CORRELATION_ID] = 13; + srcObj[S_PARAMS][S_PROTOCOL_TYPE] = 0; + srcObj[S_PARAMS][S_PROTOCOL_VERSION] = 2; + srcObj[S_MSG_PARAMS]["info"] = "info_notification"; + std::string jsonString; + + bool result = CFormatterJsonSDLRPCv2::toString(srcObj, jsonString); + + EXPECT_TRUE(result); + + std::string expectOutputJsonString = + "{\n \"info\" : \"info_notification\"\n}\n"; + + EXPECT_EQ(expectOutputJsonString, jsonString); +} + +TEST(CFormatterJsonSDLRPCv2Test, SmObjWithResponseToString) { + SmartObject srcObj; + + CSmartSchema schema = initObjectSchema(); + srcObj.setSchema(schema); + + srcObj[S_PARAMS][S_MESSAGE_TYPE] = MessageTypeTest::response; + srcObj[S_PARAMS][S_FUNCTION_ID] = FunctionIDTest::RegisterAppInterface; + srcObj[S_PARAMS][S_CORRELATION_ID] = 13; + srcObj[S_PARAMS][S_PROTOCOL_TYPE] = 0; + srcObj[S_PARAMS][S_PROTOCOL_VERSION] = 2; + + srcObj[S_MSG_PARAMS]["success"] = true; + srcObj[S_MSG_PARAMS]["resultCode"] = TestType::SUCCESS; + + std::string jsonString; + + bool result = CFormatterJsonSDLRPCv2::toString(srcObj, jsonString); + + EXPECT_TRUE(result); + + std::string expectOutputJsonString = + "{\n \"resultCode\" : \"SUCCESS\",\n \"success\" : true\n}\n"; + + EXPECT_EQ(expectOutputJsonString, jsonString); +} + +TEST(CFormatterJsonSDLRPCv2Test, SmObjWithResponseWithoutSchemaWithoutParamsToString) { + SmartObject srcObj; + srcObj[S_PARAMS][S_MESSAGE_TYPE] = MessageTypeTest::response; + std::string jsonString; + + bool result = CFormatterJsonSDLRPCv2::toString(srcObj, jsonString); + + EXPECT_TRUE(result); + + std::string expectOutputJsonString = "\"\"\n"; + + EXPECT_EQ(expectOutputJsonString, jsonString); +} + +TEST(CFormatterJsonSDLRPCv2Test, StringRequestWithoutCorIdToSmObj) { + std::string inputJsonString = + "\ + {\ + \"syncMsgVersion\" : {\ + \"majorVersion\" : 2,\ + \"minorVersion\" : 10\ + },\ + \"appName\": \"some app name\",\ + \"ttsName\": [{\ + \"text\": \"ABC\",\ + \"type\": \"TEXT\"\ + }],\ + \"vrSynonyms\": [\"Synonym 1\", \"Synonym 2\"]\ + }\n"; + + SmartObject obj; + + CSmartSchema schema = initObjectSchema(); + obj.setSchema(schema); + + bool result = CFormatterJsonSDLRPCv2::fromString<FunctionIDTest::eType, + MessageTypeTest::eType>(inputJsonString, obj, + FunctionIDTest::RegisterAppInterface, + MessageTypeTest::request); + + EXPECT_EQ(true, result); + EXPECT_EQ(Errors::eType::MISSING_MANDATORY_PARAMETER, obj.validate()); + EXPECT_EQ(obj[S_PARAMS][S_MESSAGE_TYPE], MessageTypeTest::request); + EXPECT_EQ(obj[S_PARAMS][S_FUNCTION_ID], FunctionIDTest::RegisterAppInterface); + EXPECT_EQ(obj[S_PARAMS][S_PROTOCOL_TYPE], 0); + EXPECT_EQ(obj[S_PARAMS][S_PROTOCOL_VERSION], 2); + EXPECT_EQ(obj[S_MSG_PARAMS]["appName"], "some app name"); + + EXPECT_EQ(obj[S_MSG_PARAMS]["syncMsgVersion"]["majorVersion"], 2); + EXPECT_EQ(obj[S_MSG_PARAMS]["syncMsgVersion"]["minorVersion"], 10); + EXPECT_EQ(obj[S_MSG_PARAMS]["ttsName"][0]["text"], "ABC"); + EXPECT_EQ(obj[S_MSG_PARAMS]["ttsName"][0]["type"], "TEXT"); + EXPECT_EQ(obj[S_MSG_PARAMS]["vrSynonyms"][0], "Synonym 1"); + EXPECT_EQ(obj[S_MSG_PARAMS]["vrSynonyms"][1], "Synonym 2"); +} + +TEST(CFormatterJsonSDLRPCv2Test, StringRequestWithCorIdToSmObj) { + std::string inputJsonString = + "\ + {\ + \"syncMsgVersion\" : {\ + \"majorVersion\" : 2,\ + \"minorVersion\" : 10\ + },\ + \"appName\": \"some app name\",\ + \"ttsName\": [{\ + \"text\": \"ABC\",\ + \"type\": \"TEXT\"\ + }],\ + \"vrSynonyms\": [\"Synonym 1\", \"Synonym 2\"]\ + }\n"; + + SmartObject obj; + + CSmartSchema schema = initObjectSchema(); + obj.setSchema(schema); + int32_t corId = 10; + bool result = CFormatterJsonSDLRPCv2::fromString<FunctionIDTest::eType, + MessageTypeTest::eType>(inputJsonString, obj, + FunctionIDTest::RegisterAppInterface, + MessageTypeTest::request, corId); + + EXPECT_EQ(true, result); + EXPECT_EQ(Errors::eType::OK, obj.validate()); + EXPECT_EQ(obj[S_PARAMS][S_MESSAGE_TYPE], MessageTypeTest::request); + EXPECT_EQ(obj[S_PARAMS][S_FUNCTION_ID], FunctionIDTest::RegisterAppInterface); + EXPECT_EQ(obj[S_PARAMS][S_CORRELATION_ID], corId); + EXPECT_EQ(obj[S_PARAMS][S_PROTOCOL_TYPE], 0); + EXPECT_EQ(obj[S_PARAMS][S_PROTOCOL_VERSION], 2); + EXPECT_EQ(obj[S_MSG_PARAMS]["appName"], "some app name"); + + EXPECT_EQ(obj[S_MSG_PARAMS]["syncMsgVersion"]["majorVersion"], 2); + EXPECT_EQ(obj[S_MSG_PARAMS]["syncMsgVersion"]["minorVersion"], 10); + EXPECT_EQ(obj[S_MSG_PARAMS]["ttsName"][0]["text"], "ABC"); + EXPECT_EQ(obj[S_MSG_PARAMS]["ttsName"][0]["type"], "TEXT"); + EXPECT_EQ(obj[S_MSG_PARAMS]["vrSynonyms"][0], "Synonym 1"); + EXPECT_EQ(obj[S_MSG_PARAMS]["vrSynonyms"][1], "Synonym 2"); +} + +TEST(CFormatterJsonSDLRPCv2Test, StringResponceWithCorIdToSmartObject) { + std::string inputJsonString = + "{\n \ + \"resultCode\" : \"SUCCESS\",\n\ + \"success\" : true\n\ + }\n"; + + SmartObject obj; + + CSmartSchema schema = initObjectSchema(); + obj.setSchema(schema); + int32_t corId = 10; + bool result = CFormatterJsonSDLRPCv2::fromString<FunctionIDTest::eType, + MessageTypeTest::eType>(inputJsonString, obj, + FunctionIDTest::RegisterAppInterface, + MessageTypeTest::response, corId); + EXPECT_EQ(true, result); + + EXPECT_EQ(obj[S_PARAMS][S_MESSAGE_TYPE], MessageTypeTest::response); + EXPECT_EQ(obj[S_PARAMS][S_FUNCTION_ID], 0); + EXPECT_EQ(obj[S_PARAMS][S_CORRELATION_ID], corId); + EXPECT_EQ(obj[S_PARAMS][S_PROTOCOL_TYPE], 0); + EXPECT_EQ(obj[S_PARAMS][S_PROTOCOL_VERSION], 2); + EXPECT_EQ(obj[S_MSG_PARAMS]["resultCode"], "SUCCESS"); + EXPECT_EQ(obj[S_MSG_PARAMS]["success"], true); +} + +TEST(CFormatterJsonSDLRPCv2Test, StringNotificationToSmartObject) { + std::string inputJsonString = + "{\ + \"info\" : \"info_notification\"\ + }\n"; + + SmartObject obj; + + CSmartSchema schema = initObjectSchema(); + obj.setSchema(schema); + int32_t corId = 10; + bool result = CFormatterJsonSDLRPCv2::fromString<FunctionIDTest::eType, + MessageTypeTest::eType>(inputJsonString, obj, + FunctionIDTest::SetGlobalProperties, + MessageTypeTest::notification, corId); + EXPECT_EQ(true, result); + EXPECT_EQ(Errors::eType::OK, obj.validate()); + EXPECT_EQ(obj[S_PARAMS][S_MESSAGE_TYPE], MessageTypeTest::notification); + EXPECT_EQ(obj[S_PARAMS][S_FUNCTION_ID], FunctionIDTest::SetGlobalProperties); + EXPECT_EQ(obj[S_PARAMS][S_CORRELATION_ID], corId); + EXPECT_EQ(obj[S_PARAMS][S_PROTOCOL_TYPE], 0); + EXPECT_EQ(obj[S_PARAMS][S_PROTOCOL_VERSION], 2); + EXPECT_EQ(obj[S_MSG_PARAMS]["info"], "info_notification"); +} + +TEST(CFormatterJsonSDLRPCv2Test, MetaFormatToString) { + SmartObject srcObj; + + srcObj[S_PARAMS][S_MESSAGE_TYPE] = MessageTypeTest::request; + srcObj[S_PARAMS][S_FUNCTION_ID] = FunctionIDTest::RegisterAppInterface; + srcObj[S_PARAMS][S_CORRELATION_ID] = 13; + srcObj[S_PARAMS][S_PROTOCOL_TYPE] = 0; + srcObj[S_PARAMS][S_PROTOCOL_VERSION] = 2; + srcObj[S_MSG_PARAMS]["info"] = "value"; + + std::string jsonString; + + CSmartSchema schema = initObjectSchema(); + srcObj.setSchema(schema); + + meta_formatter_error_code::tMetaFormatterErrorCode result = + CFormatterJsonSDLRPCv2::MetaFormatToString(srcObj, schema, jsonString); + EXPECT_EQ(meta_formatter_error_code::kErrorOk, result); +} + +} // namespace formatters +} // namespace components +} // namespace test diff --git a/src/components/formatters/test/formatter_json_rpc_test.cc b/src/components/formatters/test/formatter_json_rpc_test.cc new file mode 100644 index 0000000000..24bdc2fa3f --- /dev/null +++ b/src/components/formatters/test/formatter_json_rpc_test.cc @@ -0,0 +1,199 @@ +/* + * Copyright (c) 2015, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <algorithm> +#include "gtest/gtest.h" +#include "formatters/formatter_json_rpc.h" +#include "formatters/CSmartFactory.hpp" +#include "HMI_API_schema.h" +#include "MOBILE_API_schema.h" + +namespace test { +namespace components { +namespace formatters { + +using namespace NsSmartDeviceLink::NsSmartObjects; +using namespace NsSmartDeviceLink::NsJSONHandler::Formatters; +using namespace NsSmartDeviceLink::NsJSONHandler::strings; + +TEST(FormatterJsonRPCTest, CorrectRPCv1_request_SmartObjectToString_EXPECT_SUCCESS) { + // Create SmartObject + SmartObject obj; + obj[S_PARAMS][S_FUNCTION_ID] = hmi_apis::FunctionID::VR_IsReady; + obj[S_PARAMS][S_MESSAGE_TYPE] = hmi_apis::messageType::request; + obj[S_PARAMS][S_PROTOCOL_VERSION] = 2; + obj[S_PARAMS][S_PROTOCOL_TYPE] = 1; + obj[S_PARAMS][S_CORRELATION_ID] = 4444; + obj[S_MSG_PARAMS] = SmartObject(SmartType::SmartType_Map); + // Attach Schema + hmi_apis::HMI_API factory; + EXPECT_TRUE(factory.attachSchema(obj)); + + std::string result; + // Convert SmrtObject to Json string + EXPECT_TRUE(FormatterJsonRpc::ToString(obj, result)); + EXPECT_EQ( + std::string( + "{\n \"id\" : 4444,\n \"jsonrpc\" : \"2.0\",\n \"method\" : \"VR.IsReady\"\n}\n"), + result); +} + +TEST(FormatterJsonRPCTest, CorrectRPCv2_request_SmartObjectToString_EXPECT_SUCCESS) { + // Create SmartObject + SmartObject obj; + obj[S_PARAMS][S_FUNCTION_ID] = mobile_apis::FunctionID::AddCommandID; + obj[S_PARAMS][S_MESSAGE_TYPE] = mobile_apis::messageType::request; + obj[S_PARAMS][S_CORRELATION_ID] = 4444; + obj[S_MSG_PARAMS] = SmartObject(SmartType::SmartType_Map); + // Attach Schema + mobile_apis::MOBILE_API factory; + EXPECT_TRUE(factory.attachSchema(obj)); + + std::string result; + // Convert SmrtObject to Json string + EXPECT_TRUE(FormatterJsonRpc::ToString(obj, result)); + EXPECT_EQ( + std::string( + "{\n \"id\" : 4444,\n \"jsonrpc\" : \"2.0\",\n \"method\" : \"AddCommandID\"\n}\n"), + result); +} + +TEST(FormatterJsonRPCTest, CorrectRPCv1_notification_SmartObjectToString_EXPECT_SUCCESS) { + // Create SmartObject + SmartObject obj; + std::string result; + obj[S_PARAMS][S_FUNCTION_ID] = hmi_apis::FunctionID::Buttons_OnButtonPress; + obj[S_PARAMS][S_MESSAGE_TYPE] = hmi_apis::messageType::notification; + obj[S_PARAMS][S_PROTOCOL_VERSION] = 2; + obj[S_PARAMS][S_PROTOCOL_TYPE] = 1; + obj[S_PARAMS][S_CORRELATION_ID] = 4222; + obj[S_MSG_PARAMS] = SmartObject(SmartType::SmartType_Map); + // Attach Schema + hmi_apis::HMI_API factory; + EXPECT_TRUE(factory.attachSchema(obj)); + // Convert SmrtObject to Json string + EXPECT_TRUE(FormatterJsonRpc::ToString(obj, result)); + EXPECT_EQ( + std::string( + "{\n \"jsonrpc\" : \"2.0\",\n \"method\" : \"Buttons.OnButtonPress\",\n \"params\" : {}\n}\n"), + result); +} + +TEST(FormatterJsonRPCTest, InvalidRPC_SmartObjectToString_EXPECT_FALSE) { + // Create SmartObject + SmartObject obj; + std::string result; + obj[S_PARAMS][S_FUNCTION_ID] = + hmi_apis::FunctionID::BasicCommunication_OnReady; + obj[S_PARAMS][S_MESSAGE_TYPE] = hmi_apis::messageType::response; + obj[S_PARAMS][S_PROTOCOL_VERSION] = 2; + obj[S_PARAMS][S_PROTOCOL_TYPE] = 1; + obj[S_PARAMS][S_CORRELATION_ID] = 4222; + obj[S_MSG_PARAMS] = SmartObject(SmartType::SmartType_Map); + // Attach Schema + hmi_apis::HMI_API factory; + EXPECT_FALSE(factory.attachSchema(obj)); + // Convert SmrtObject to Json string + EXPECT_FALSE(FormatterJsonRpc::ToString(obj, result)); + // Expect result with default value. No correct conversion was done + EXPECT_EQ(std::string("{\n \"jsonrpc\" : \"2.0\"\n}\n"), result); +} + +TEST(FormatterJsonRPCTest, FromStringNotificationToSmartObj_ExpectSuccess) { + // Source Json string + const std::string json_string( + "{\n \"jsonrpc\" : \"2.0\",\n \"method\" : \"BasicCommunication.OnReady\",\n \"params\" : {}\n}\n"); + // Smart Object to keep result + SmartObject obj; + // Convert json string to smart object + EXPECT_EQ( + 0, + (FormatterJsonRpc::FromString<hmi_apis::FunctionID::eType, + hmi_apis::messageType::eType>(json_string, obj))); + // Get keys collection from Smart Object + std::set<std::string> keys = obj["params"].enumerate(); + EXPECT_EQ(4u, keys.size()); +} + +TEST(FormatterJsonRPCTest, FromStringToSmartObjInvalidFormat_ExpectFalse) { + // Source Json string + const std::string json_string( + "{\n \"method\" : \"BasicCommunication.OnReady\",\n \"params\" : {}\n}\n"); + // Smart Object to keep result + SmartObject obj; + // Convert json string to smart object + EXPECT_EQ( + 2, + (FormatterJsonRpc::FromString<hmi_apis::FunctionID::eType, + hmi_apis::messageType::eType>(json_string, obj))); + // Get keys collection from Smart Object + std::set<std::string> keys = obj["params"].enumerate(); + EXPECT_EQ(4u, keys.size()); +} + +TEST(FormatterJsonRPCTest, FromStringRequestToSmartObj_ExpectSuccess) { + // Source Json string + const std::string json_string( + "{\n \"id\" : 4444,\n \"jsonrpc\" : \"2.0\",\n \"method\" : \"VR.IsReady\"\n}\n"); + // Smart Object to keep result + SmartObject obj; + // Convert json string to smart object + EXPECT_EQ( + 0, + (FormatterJsonRpc::FromString<hmi_apis::FunctionID::eType, + hmi_apis::messageType::eType>(json_string, obj))); + // Get keys collection from Smart Object + std::set<std::string> keys = obj["params"].enumerate(); + std::set<std::string>::iterator it1 = keys.begin(); + EXPECT_EQ(5u, keys.size()); +} + +TEST(FormatterJsonRPCTest, FromStringResponseToSmartObj_ExpectSuccess) { + // Source Json string + const std::string json_string( + "{\n \"id\" : 4444,\n \"jsonrpc\" : \"2.0\",\n \"method\" : \"VR.IsReady\"\n}\n"); + // Smart Object to keep result + SmartObject obj; + // Convert json string to smart object + EXPECT_EQ( + 0, + (FormatterJsonRpc::FromString<hmi_apis::FunctionID::eType, + hmi_apis::messageType::eType>(json_string, obj))); + // Get keys collection from Smart Object + std::set<std::string> keys = obj["params"].enumerate(); + std::set<std::string>::iterator it1 = keys.begin(); + EXPECT_EQ(5u, keys.size()); +} + +} // namespace formatters +} // namespace components +} // namespace test diff --git a/src/components/formatters/test/include/SmartFactoryTestHelper.h b/src/components/formatters/test/include/SmartFactoryTestHelper.h new file mode 100644 index 0000000000..ca7021b844 --- /dev/null +++ b/src/components/formatters/test/include/SmartFactoryTestHelper.h @@ -0,0 +1,167 @@ +/* + * Copyright (c) 2015, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef SRC_COMPONENTS_FORMATTERS_TEST_INCLUDE_SMARTFACTORYTESTHELPER_H_ +#define SRC_COMPONENTS_FORMATTERS_TEST_INCLUDE_SMARTFACTORYTESTHELPER_H_ + +#include <map> +#include <set> + +#include "formatters/CSmartFactory.hpp" +#include "HMI_API_schema.h" +#include "smart_objects/always_true_schema_item.h" +#include "smart_objects/always_false_schema_item.h" +#include "smart_objects/array_schema_item.h" +#include "smart_objects/bool_schema_item.h" +#include "smart_objects/object_schema_item.h" +#include "smart_objects/string_schema_item.h" +#include "smart_objects/enum_schema_item.h" +#include "smart_objects/number_schema_item.h" +#include "smart_objects/schema_item_parameter.h" + +namespace test { +namespace components { +namespace formatters { + +using namespace NsSmartDeviceLink::NsSmartObjects; +using namespace NsSmartDeviceLink::NsJSONHandler; +using namespace NsSmartDeviceLink::NsJSONHandler::strings; +using namespace hmi_apis; + +namespace TestType { +enum eType { + INVALID_ENUM = -1, + APPLICATION_NOT_REGISTERED = 0, + SUCCESS, + TOO_MANY_PENDING_REQUESTS, + REJECTED, + INVALID_DATA, + OUT_OF_MEMORY, + ABORTED, + USER_DISALLOWED, + GENERIC_ERROR, + DISALLOWED +}; +} // namespace TestType + +namespace FunctionIdTest { +enum eType { + INVALID_ENUM = -1, + Function1, + Function2, + Function3 +}; +} // namespace FunctionIdTest + +namespace MessageTypeTest { +enum eType { + INVALID_ENUM = -1, + request, + response, + notification, + error_response +}; +} // namespace MessageTypeTest + +namespace StructIdentifiersTest { +enum eType { + INVALID_ENUM = -1, + Common_1, + Common_2, + Common_3 +}; +} // namespace StructIdentifiersTest + +class CSmartFactoryTest : public CSmartFactory<FunctionIdTest::eType, + MessageTypeTest::eType, StructIdentifiersTest::eType> { + public: + CSmartFactoryTest(); + std::map<SmartSchemaKey<FunctionIdTest::eType, MessageTypeTest::eType>, + CSmartSchema> function_schemes() { + return functions_schemes_; + } + std::map<StructIdentifiersTest::eType, CSmartSchema> structs_schemes() { + return structs_schemes_; + } + protected: + typedef std::map<const StructIdentifiersTest::eType, + utils::SharedPtr<ISchemaItem> > TStructsSchemaItems; + + static utils::SharedPtr<ISchemaItem> ProvideObjectSchemaItemForStruct( + TStructsSchemaItems &struct_schema_items, + const StructIdentifiersTest::eType struct_id); + + void InitStructSchemes(TStructsSchemaItems &struct_schema_items); + + void InitFunctionSchemes( + const TStructsSchemaItems &struct_schema_items, + const std::set<FunctionIdTest::eType> &function_id_items, + const std::set<MessageTypeTest::eType> &message_type_items); + + static CSmartSchema InitFunction_Function1_request( + const std::set<FunctionIdTest::eType> &function_id_items, + const std::set<MessageTypeTest::eType> &message_type_items); + + static CSmartSchema InitFunction_Function1_response( + const TStructsSchemaItems &struct_schema_items, + const std::set<FunctionIdTest::eType> &function_id_items, + const std::set<MessageTypeTest::eType> &message_type_items); + + static CSmartSchema InitFunction_Function2_request( + const std::set<FunctionIdTest::eType> &function_id_items, + const std::set<MessageTypeTest::eType> &message_type_items); + + static CSmartSchema InitFunction_Function2_response( + const TStructsSchemaItems &struct_schema_items, + const std::set<FunctionIdTest::eType> &function_id_items, + const std::set<MessageTypeTest::eType> &message_type_items); + + static CSmartSchema InitFunction_Function3_request( + const std::set<FunctionIdTest::eType> &function_id_items, + const std::set<MessageTypeTest::eType> &message_type_items); + + static CSmartSchema InitFunction_Function3_response( + const TStructsSchemaItems &struct_schema_items, + const std::set<FunctionIdTest::eType> &function_id_items, + const std::set<MessageTypeTest::eType> &message_type_items); + + static utils::SharedPtr<ISchemaItem> InitStructSchemaItem_Common_1( + TStructsSchemaItems &struct_schema_items); + + static utils::SharedPtr<ISchemaItem> InitStructSchemaItem_Common_2(); +}; + +} // namespace formatters +} // namespace components +} // namespace test + +#endif // SRC_COMPONENTS_FORMATTERS_TEST_INCLUDE_SMARTFACTORYTESTHELPER_H_ diff --git a/src/components/formatters/test/include/create_smartSchema.h b/src/components/formatters/test/include/create_smartSchema.h new file mode 100644 index 0000000000..c3bc2651e5 --- /dev/null +++ b/src/components/formatters/test/include/create_smartSchema.h @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2015, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef SRC_COMPONENTS_FORMATTERS_TEST_INCLUDE_CREATESMARTSCHEMA_H_ +#define SRC_COMPONENTS_FORMATTERS_TEST_INCLUDE_CREATESMARTSCHEMA_H_ + +#include "formatters/CFormatterJsonSDLRPCv1.hpp" +#include "SmartFactoryTestHelper.h" + +namespace test { +namespace components { +namespace formatters { + +using namespace NsSmartDeviceLink::NsJSONHandler::strings; +using namespace NsSmartDeviceLink::NsJSONHandler::Formatters; +using namespace NsSmartDeviceLink::NsSmartObjects; + +namespace FunctionIDTest { +enum eType { + INVALID_ENUM = -1, + RegisterAppInterface, + UnregisterAppInterface, + SetGlobalProperties, +}; +} + +namespace Language { +enum eType { + INVALID_ENUM = -1, + EN_EU, + RU_RU +}; +} +namespace AppTypeTest { +enum eType { + INVALID_ENUM = -1, + SYSTEM, + MEDIA +}; +} +namespace SpeechCapabilities { +enum eType { + INVALID_ENUM = -1, + SC_TEXT, +}; +} + +namespace StructIdentifiers { +enum eType { + INVALID_ENUM = -1, + Struct1, + Struct2 +}; +} + +CSmartSchema initObjectSchema(); +CSmartSchema initSchemaForMetaFormatter(); + +} // namespace formatters +} // namespace components +} // namespace test + +#endif // SRC_COMPONENTS_FORMATTERS_TEST_INCLUDE_CREATESMARTSCHEMA_H_ diff --git a/src/components/formatters/test/include/meta_formatter_test_helper.h b/src/components/formatters/test/include/meta_formatter_test_helper.h new file mode 100644 index 0000000000..e2be3beb64 --- /dev/null +++ b/src/components/formatters/test/include/meta_formatter_test_helper.h @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2015, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef SRC_COMPONENTS_FORMATTERS_TEST_INCLUDE_METAFORMATTERTESTHELPER_H_ +#define SRC_COMPONENTS_FORMATTERS_TEST_INCLUDE_METAFORMATTERTESTHELPER_H_ + +#include "gtest/gtest.h" + +#include "smart_objects/smart_object.h" +#include "formatters/CFormatterJsonSDLRPCv1.hpp" +#include "formatters/CSmartFactory.hpp" +#include "create_smartSchema.h" + +namespace test { +namespace components { +namespace formatters { + +class CMetaFormatterTestHelper : public ::testing::Test { + protected: + + virtual void SetUp(); + + virtual void TearDown(); + + void AnyObjectToJsonString( + const NsSmartDeviceLink::NsSmartObjects::SmartObject& obj, + std::string& result_string); + + void FillObjectIdenticalToSchema( + NsSmartDeviceLink::NsSmartObjects::SmartObject& obj); + + void FillObjectIdenticalToSchemaWithoutNoMandatoriesParams( + NsSmartDeviceLink::NsSmartObjects::SmartObject& obj); + + void CompareObjects( + const NsSmartDeviceLink::NsSmartObjects::SmartObject& first, + const NsSmartDeviceLink::NsSmartObjects::SmartObject& second); + + void FillObjectWithDefaultValues( + NsSmartDeviceLink::NsSmartObjects::SmartObject& obj); + + void FillObjectWithoutSomeMandatoryFields( + NsSmartDeviceLink::NsSmartObjects::SmartObject& obj); + + // Members + std::set<FunctionIDTest::eType> function_id_items_; + std::set<MessageTypeTest::eType> message_type_items_; +}; + +} // namespace formatters +} // namespace components +} // namespace test + +#endif // SRC_COMPONENTS_FORMATTERS_TEST_INCLUDE_METAFORMATTERTESTHELPER_H_ diff --git a/src/components/formatters/test/meta_formatter_test.cc b/src/components/formatters/test/meta_formatter_test.cc new file mode 100644 index 0000000000..17c2506ac6 --- /dev/null +++ b/src/components/formatters/test/meta_formatter_test.cc @@ -0,0 +1,351 @@ +/* + * Copyright (c) 2015, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "gtest/gtest.h" +#include "formatters/meta_formatter.h" +#include "meta_formatter_test_helper.h" + +namespace test { +namespace components { +namespace formatters { + +TEST_F(CMetaFormatterTestHelper, inputObjectIdenticalToSchemaWithAndWithoutMandatoryParams) { + Json::Value value; + Json::Reader reader; + CSmartFactory<FunctionIDTest::eType, MessageTypeTest::eType, + StructIdentifiers::eType> factory_; + + SmartObject object1 = factory_.CreateSmartObject( + FunctionIDTest::RegisterAppInterface, MessageTypeTest::request); + + SmartObject object2 = factory_.CreateSmartObject( + FunctionIDTest::RegisterAppInterface, MessageTypeTest::request); + + SmartObject result_object1; + SmartObject result_object2; + // Get schema + CSmartSchema schema; + schema = initSchemaForMetaFormatter(); + + FillObjectIdenticalToSchema(object1); + FillObjectIdenticalToSchemaWithoutNoMandatoriesParams(object2); + bool creationresult; + creationresult = CMetaFormatter::CreateObjectByPattern(object1, schema, + result_object1); + EXPECT_TRUE(creationresult); + creationresult = CMetaFormatter::CreateObjectByPattern(object2, schema, + result_object2); + EXPECT_TRUE(creationresult); + + // Uncomment code to print objects in console +// std::string formatted_string; +// CFormatterJsonSDLRPCv1::toString(object1, formatted_string); +// printf("object1 %s\n", formatted_string.c_str()); +// +// CFormatterJsonSDLRPCv1::toString(result_object1, formatted_string); +// printf("result_object1 %s\n", formatted_string.c_str()); +// +// CFormatterJsonSDLRPCv1::toString(object2, formatted_string); +// printf("object2 %s\n", formatted_string.c_str()); +// +// CFormatterJsonSDLRPCv1::toString(result_object2, formatted_string); +// printf("result_object2 %s\n", formatted_string.c_str()); + + CompareObjects(object1, result_object1); + CompareObjects(object2, result_object2); + + // Enums must be unapplied (converted to string) in order to be compared against strings + result_object1.getSchema().unapplySchema(result_object1); + EXPECT_EQ("request", result_object1[S_PARAMS][S_MESSAGE_TYPE].asString()); + EXPECT_EQ("RegisterAppInterface", + result_object1[S_PARAMS][S_FUNCTION_ID].asString()); + + result_object2.getSchema().unapplySchema(result_object2); + EXPECT_EQ("request", result_object2[S_PARAMS][S_MESSAGE_TYPE].asString()); + EXPECT_EQ("RegisterAppInterface", + result_object2[S_PARAMS][S_FUNCTION_ID].asString()); +} + +TEST_F(CMetaFormatterTestHelper, NormalSchemaWithEmptyObject) { + SmartObject object; + SmartObject result_object; + SmartObject expected_object; + + // Get schema + CSmartSchema schema = initSchemaForMetaFormatter(); + bool create_object_result = CMetaFormatter::CreateObjectByPattern( + object, schema, result_object); + EXPECT_TRUE(create_object_result); + + FillObjectWithDefaultValues(expected_object); + + CompareObjects(expected_object, result_object); + +// Uncomment code to print objects in console +// std::string str; +// AnyObjectToJsonString(result_object, str); +// printf("result_object(default) %s", str.c_str()); +// AnyObjectToJsonString(expected_object, str); +// printf("expected_object %s", str.c_str()); + + +} + +TEST_F(CMetaFormatterTestHelper, NormalSchemaWithObjectWithoutSomeMandatoryFields) { + SmartObject object; + SmartObject result_object; + + // Get schema + CSmartSchema schema = initSchemaForMetaFormatter(); + FillObjectWithoutSomeMandatoryFields(object); + + CMetaFormatter::CreateObjectByPattern(object, schema, result_object); + + CompareObjects(object, result_object); + EXPECT_EQ(0, result_object[S_PARAMS][S_CORRELATION_ID].asInt()); + EXPECT_EQ( + 0, result_object[S_MSG_PARAMS]["syncMsgVersion"]["majorVersion"].asInt()); + EXPECT_EQ( + 0, result_object[S_MSG_PARAMS]["syncMsgVersion"]["minorVersion"].asInt()); + +// Uncomment code to print object in console +// std::string str; +// AnyObjectToJsonString(result_object, str); +// printf("result_object %s", str.c_str()); + +} + +TEST_F(CMetaFormatterTestHelper, ObjectWithEmptyMap) { + std::map<std::string, CObjectSchemaItem::SMember> schemaMembersMap; + CSmartSchema map_schema = CSmartSchema( + CObjectSchemaItem::create(schemaMembersMap)); + + SmartObject object; + SmartObject result_object_empty_map; + + SmartObject object_empty_map = SmartObject(SmartType_Map); + CMetaFormatter::CreateObjectByPattern(object_empty_map, map_schema, + result_object_empty_map); + EXPECT_EQ(SmartType_Map, result_object_empty_map.getType())<< "smartObject is not map type"; + EXPECT_EQ(0u, result_object_empty_map.length())<< "non empty map"; + + CMetaFormatter::CreateObjectByPattern(object, map_schema, + result_object_empty_map); + EXPECT_EQ(SmartType_Map, result_object_empty_map.getType())<< "smartObject is not map type"; + EXPECT_EQ(0u, result_object_empty_map.length())<< "non empty map"; + + object["field1"] = 0; + object["field2"] = SmartObject(); + CMetaFormatter::CreateObjectByPattern(object, map_schema, + result_object_empty_map); + EXPECT_EQ(SmartType_Map, result_object_empty_map.getType())<< "smartObject is not map type"; + EXPECT_EQ(0u, result_object_empty_map.length())<< "non empty map"; + + // Fill object with any values. Result must be the same + FillObjectIdenticalToSchema(object); + CMetaFormatter::CreateObjectByPattern(object, map_schema, + result_object_empty_map); + EXPECT_EQ(SmartType_Map, result_object_empty_map.getType())<< "smartObject is not map type"; + EXPECT_EQ(0u, result_object_empty_map.length())<< "non empty map"; + + // Fill object with any values. Result must be the same + FillObjectIdenticalToSchemaWithoutNoMandatoriesParams(object); + CMetaFormatter::CreateObjectByPattern(object, map_schema, + result_object_empty_map); + EXPECT_EQ(SmartType_Map, result_object_empty_map.getType())<< "smartObject is not map type"; + EXPECT_EQ(0u, result_object_empty_map.length())<< "non empty map"; + +// Uncomment code to print object in console +// std::string str; +// AnyObjectToJsonString(result_object_empty_map, str); +// printf("result_object(empty map) %s", str.c_str()); + +} + +TEST_F(CMetaFormatterTestHelper, ObjectWithEmptyArray) { + SmartObject object; + + SmartObject result_object_empty_array; + CSmartSchema array_schema = CSmartSchema(CArraySchemaItem::create()); + + SmartObject object_empty_aray = SmartObject(SmartType_Array); + + CMetaFormatter::CreateObjectByPattern(object_empty_aray, array_schema, + result_object_empty_array); + EXPECT_EQ(SmartType_Array, result_object_empty_array.getType())<< "smartObject is not array type"; + EXPECT_EQ(0u, result_object_empty_array.length())<< "non empty array"; + + CMetaFormatter::CreateObjectByPattern(object, array_schema, + result_object_empty_array); + EXPECT_EQ(SmartType_Array, result_object_empty_array.getType())<< "smartObject is not array type"; + EXPECT_EQ(0u, result_object_empty_array.length())<< "non empty array"; + + // Fill object with any values. Result must be the same + FillObjectIdenticalToSchema(object); + CMetaFormatter::CreateObjectByPattern(object, array_schema, + result_object_empty_array); + EXPECT_EQ(SmartType_Array, result_object_empty_array.getType())<< "smartObject is not array type"; + EXPECT_EQ(0u, result_object_empty_array.length())<< "non empty array"; + + // Fill object with any values. Result must be the same + FillObjectWithoutSomeMandatoryFields(object); + CMetaFormatter::CreateObjectByPattern(object, array_schema, + result_object_empty_array); + EXPECT_EQ(SmartType_Array, result_object_empty_array.getType())<< "smartObject is not array type"; + EXPECT_EQ(0u, result_object_empty_array.length())<< "non empty array"; + +// Uncomment code to print object in console +// std::string str; +// AnyObjectToJsonString(result_object_empty_array, str); +// printf("result_object(empty array) %s", str.c_str()); + +} + +TEST_F(CMetaFormatterTestHelper, ObjectWithEmptyArrayAndEmptyMapWithOtherParameters) { + // Arrange + SmartObject result_object; + SmartObject object; + + std::map<std::string, CObjectSchemaItem::SMember> paramsMembersMap; + + paramsMembersMap[S_FUNCTION_ID] = CObjectSchemaItem::SMember( + TEnumSchemaItem<FunctionIDTest::eType>::create(function_id_items_), true); + + paramsMembersMap[S_MESSAGE_TYPE] = CObjectSchemaItem::SMember( + TEnumSchemaItem<MessageTypeTest::eType>::create(message_type_items_), + true); + + paramsMembersMap[S_CORRELATION_ID] = CObjectSchemaItem::SMember( + TNumberSchemaItem<int>::create(TSchemaItemParameter<int>(0), + TSchemaItemParameter<int>(100), + TSchemaItemParameter<int>(55)), + true); + + paramsMembersMap[S_PROTOCOL_VERSION] = CObjectSchemaItem::SMember( + TNumberSchemaItem<int>::create(TSchemaItemParameter<int>(1), + TSchemaItemParameter<int>(2)), + false); + paramsMembersMap[S_PROTOCOL_TYPE] = CObjectSchemaItem::SMember( + TNumberSchemaItem<int>::create(), false); + + std::map<std::string, CObjectSchemaItem::SMember> schemaMembersMap; + + schemaMembersMap["mandatory_emptyMap1"] = CObjectSchemaItem::SMember( + CObjectSchemaItem::create( + std::map<std::string, CObjectSchemaItem::SMember>()), + true); + + schemaMembersMap["mandatory_emptyMap2"] = CObjectSchemaItem::SMember( + CObjectSchemaItem::create( + std::map<std::string, CObjectSchemaItem::SMember>()), + true); + + schemaMembersMap["mandatory_emptyAray"] = CObjectSchemaItem::SMember( + CArraySchemaItem::create(TNumberSchemaItem<int>::create()), true); + + schemaMembersMap["non_mandatory_Array"] = CObjectSchemaItem::SMember( + CArraySchemaItem::create(TNumberSchemaItem<int>::create(), + TSchemaItemParameter<size_t>(1), + TSchemaItemParameter<size_t>(2)), + false); + + schemaMembersMap["mandatory_string"] = CObjectSchemaItem::SMember( + CStringSchemaItem::create(TSchemaItemParameter<size_t>(0), + TSchemaItemParameter<size_t>(500), + TSchemaItemParameter<std::string>("defValue")), + true); + + schemaMembersMap["non_mandatory_string"] = CObjectSchemaItem::SMember( + CStringSchemaItem::create( + TSchemaItemParameter<size_t>(0), TSchemaItemParameter<size_t>(500), + TSchemaItemParameter<std::string>("ignoredDefValue")), + false); + + std::map<std::string, CObjectSchemaItem::SMember> rootMembersMap; + rootMembersMap[S_MSG_PARAMS] = CObjectSchemaItem::SMember( + CObjectSchemaItem::create(schemaMembersMap), true); + rootMembersMap[S_PARAMS] = CObjectSchemaItem::SMember( + CObjectSchemaItem::create(paramsMembersMap), true); + + CSmartSchema schema = CSmartSchema(CObjectSchemaItem::create(rootMembersMap)); + + // Set object value + object[S_PARAMS][S_FUNCTION_ID] = 500; + object[S_PARAMS][S_PROTOCOL_VERSION] = 11; + object[S_PARAMS]["new_field"] = "100500 string"; + + object[S_MSG_PARAMS]["mandatory_emptyMap1"]["field1"] = 123; + object[S_MSG_PARAMS]["mandatory_emptyMap1"]["field2"][0] = 100; + object[S_MSG_PARAMS]["mandatory_emptyMap1"]["field2"][1] = 200; + object[S_MSG_PARAMS]["non_mandatory_Array"][0] = 100; + object[S_MSG_PARAMS]["non_mandatory_Array"][1] = 200; + object[S_MSG_PARAMS]["non_mandatory_Array"][2] = 300; + object[S_MSG_PARAMS]["non_mandatory_string"] = "some string"; + + CMetaFormatter::CreateObjectByPattern(object, schema, result_object); + +// Uncomment code to print object in console +// std::string str; +// AnyObjectToJsonString(object, str); +// printf("object %s", str.c_str()); +// AnyObjectToJsonString(result_object, str); +// printf("result_object %s", str.c_str()); + + + // Assert + EXPECT_EQ(500, result_object[S_PARAMS][S_FUNCTION_ID].asInt()); + EXPECT_EQ(-1, result_object[S_PARAMS][S_MESSAGE_TYPE].asInt()); + EXPECT_EQ(55, result_object[S_PARAMS][S_CORRELATION_ID].asInt()); + EXPECT_EQ(11u, result_object[S_PARAMS][S_PROTOCOL_VERSION].asUInt()); + + EXPECT_EQ(SmartType_Map, + result_object[S_MSG_PARAMS]["mandatory_emptyMap1"].getType()); + EXPECT_EQ(0u, result_object[S_MSG_PARAMS]["mandatory_emptyMap1"].length()); + EXPECT_EQ(SmartType_Map, + result_object[S_MSG_PARAMS]["mandatory_emptyMap2"].getType()); + EXPECT_EQ(0u, result_object[S_MSG_PARAMS]["mandatory_emptyMap2"].length()); + EXPECT_EQ(SmartType_Array, + result_object[S_MSG_PARAMS]["mandatory_emptyAray"].getType()); + EXPECT_EQ(0u, result_object[S_MSG_PARAMS]["mandatory_emptyAray"].length()); + EXPECT_EQ(100, result_object[S_MSG_PARAMS]["non_mandatory_Array"][0].asInt()); + EXPECT_EQ(200, result_object[S_MSG_PARAMS]["non_mandatory_Array"][1].asInt()); + EXPECT_EQ(300u, + result_object[S_MSG_PARAMS]["non_mandatory_Array"][2].asUInt()); + EXPECT_EQ(std::string("defValue"), + result_object[S_MSG_PARAMS]["mandatory_string"].asString()); + EXPECT_EQ(std::string("some string"), + result_object[S_MSG_PARAMS]["non_mandatory_string"].asString()); +} + +} // namespace formatters +} // namespace components +} // namespace test diff --git a/src/components/formatters/test/src/SmartFactoryTestHelper.cc b/src/components/formatters/test/src/SmartFactoryTestHelper.cc new file mode 100644 index 0000000000..8f601afc29 --- /dev/null +++ b/src/components/formatters/test/src/SmartFactoryTestHelper.cc @@ -0,0 +1,501 @@ +/* + * Copyright (c) 2015, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "SmartFactoryTestHelper.h" + +using namespace test::components::formatters; + +template<> +const EnumConversionHelper<TestType::eType>::EnumToCStringMap EnumConversionHelper< + test::components::formatters::TestType::eType>::enum_to_cstring_map_ = + EnumConversionHelper<test::components::formatters::TestType::eType>::InitEnumToCStringMap(); + +template<> +const EnumConversionHelper<TestType::eType>::CStringToEnumMap EnumConversionHelper< + test::components::formatters::TestType::eType>::cstring_to_enum_map_ = + EnumConversionHelper<test::components::formatters::TestType::eType>::InitCStringToEnumMap(); + +template<> +const char* const EnumConversionHelper<TestType::eType>::cstring_values_[] = { + "APPLICATION_NOT_REGISTERED", "SUCCESS", "TOO_MANY_PENDING_REQUESTS", + "REJECTED", "INVALID_DATA", "OUT_OF_MEMORY", "ABORTED", "USER_DISALLOWED", + "GENERIC_ERROR", "DISALLOWED" }; + +template<> +const TestType::eType EnumConversionHelper<TestType::eType>::enum_values_[] = { + test::components::formatters::TestType::APPLICATION_NOT_REGISTERED, + test::components::formatters::TestType::SUCCESS, + test::components::formatters::TestType::TOO_MANY_PENDING_REQUESTS, + test::components::formatters::TestType::REJECTED, + test::components::formatters::TestType::INVALID_DATA, + test::components::formatters::TestType::OUT_OF_MEMORY, + test::components::formatters::TestType::ABORTED, + test::components::formatters::TestType::USER_DISALLOWED, + test::components::formatters::TestType::GENERIC_ERROR, + test::components::formatters::TestType::DISALLOWED }; + +template<> +const EnumConversionHelper<FunctionIdTest::eType>::EnumToCStringMap EnumConversionHelper< + test::components::formatters::FunctionIdTest::eType>::enum_to_cstring_map_ = + EnumConversionHelper<test::components::formatters::FunctionIdTest::eType>::InitEnumToCStringMap(); + +template<> +const EnumConversionHelper<FunctionIdTest::eType>::CStringToEnumMap EnumConversionHelper< + test::components::formatters::FunctionIdTest::eType>::cstring_to_enum_map_ = + EnumConversionHelper<test::components::formatters::FunctionIdTest::eType>::InitCStringToEnumMap(); + +template<> +const char* const EnumConversionHelper<FunctionIdTest::eType>::cstring_values_[] = + { "Function1", "Function2", "Function3" }; + +template<> +const FunctionIdTest::eType EnumConversionHelper<FunctionIdTest::eType>::enum_values_[] = + { test::components::formatters::FunctionIdTest::Function1, + test::components::formatters::FunctionIdTest::Function2, + test::components::formatters::FunctionIdTest::Function3 }; + +template<> +const EnumConversionHelper<MessageTypeTest::eType>::EnumToCStringMap EnumConversionHelper< + test::components::formatters::MessageTypeTest::eType>::enum_to_cstring_map_ = + EnumConversionHelper<test::components::formatters::MessageTypeTest::eType>::InitEnumToCStringMap(); + +template<> +const EnumConversionHelper<MessageTypeTest::eType>::CStringToEnumMap EnumConversionHelper< + test::components::formatters::MessageTypeTest::eType>::cstring_to_enum_map_ = + EnumConversionHelper<test::components::formatters::MessageTypeTest::eType>::InitCStringToEnumMap(); + +template<> +const char* const EnumConversionHelper<MessageTypeTest::eType>::cstring_values_[] = + { "request", "response", "notification" }; + +template<> +const MessageTypeTest::eType EnumConversionHelper<MessageTypeTest::eType>::enum_values_[] = + { test::components::formatters::MessageTypeTest::request, + test::components::formatters::MessageTypeTest::response, + test::components::formatters::MessageTypeTest::notification }; + +CSmartFactoryTest::CSmartFactoryTest() + : CSmartFactory<FunctionIdTest::eType, MessageTypeTest::eType, + StructIdentifiersTest::eType>() { + TStructsSchemaItems struct_schema_items; + InitStructSchemes(struct_schema_items); + std::set<FunctionIdTest::eType> function_id_items; + function_id_items.insert(FunctionIdTest::Function1); + function_id_items.insert(FunctionIdTest::Function2); + function_id_items.insert(FunctionIdTest::Function3); + + std::set<MessageTypeTest::eType> message_type_items; + message_type_items.insert(MessageTypeTest::request); + message_type_items.insert(MessageTypeTest::response); + message_type_items.insert(MessageTypeTest::notification); + message_type_items.insert(MessageTypeTest::error_response); + InitFunctionSchemes(struct_schema_items, function_id_items, + message_type_items); +} + +void CSmartFactoryTest::InitStructSchemes( + TStructsSchemaItems &struct_schema_items) { + utils::SharedPtr<ISchemaItem> struct_schema_item_Common_1 = + InitStructSchemaItem_Common_1(struct_schema_items); + struct_schema_items.insert( + std::make_pair(StructIdentifiersTest::Common_1, + struct_schema_item_Common_1)); + structs_schemes_.insert( + std::make_pair(StructIdentifiersTest::Common_1, + CSmartSchema(struct_schema_item_Common_1))); + + utils::SharedPtr<ISchemaItem> struct_schema_item_Common_2 = + InitStructSchemaItem_Common_2(); + struct_schema_items.insert( + std::make_pair(StructIdentifiersTest::Common_2, + struct_schema_item_Common_2)); + structs_schemes_.insert( + std::make_pair(StructIdentifiersTest::Common_2, + CSmartSchema(struct_schema_item_Common_2))); +} + +void CSmartFactoryTest::InitFunctionSchemes( + const TStructsSchemaItems &struct_schema_items, + const std::set<FunctionIdTest::eType> &function_id_items, + const std::set<MessageTypeTest::eType> &message_type_items) { + CObjectSchemaItem::Members params_members; + params_members[S_FUNCTION_ID] = CObjectSchemaItem::SMember( + TEnumSchemaItem<FunctionIdTest::eType>::create(function_id_items), true); + params_members[S_MESSAGE_TYPE] = CObjectSchemaItem::SMember( + TEnumSchemaItem<MessageTypeTest::eType>::create(message_type_items), + true); + params_members[S_PROTOCOL_VERSION] = CObjectSchemaItem::SMember( + TNumberSchemaItem<int>::create(), true); + params_members[S_PROTOCOL_TYPE] = CObjectSchemaItem::SMember( + TNumberSchemaItem<int>::create(), true); + params_members[S_CORRELATION_ID] = CObjectSchemaItem::SMember( + TNumberSchemaItem<int>::create(), true); + params_members[kCode] = CObjectSchemaItem::SMember( + TNumberSchemaItem<int>::create(), true); + params_members[kMessage] = CObjectSchemaItem::SMember( + CStringSchemaItem::create(), true); + + CObjectSchemaItem::Members root_members_map; + root_members_map[NsSmartDeviceLink::NsJSONHandler::strings::S_PARAMS] = + CObjectSchemaItem::SMember(CObjectSchemaItem::create(params_members), + true); + + CSmartSchema error_response_schema( + CObjectSchemaItem::create(root_members_map)); + + functions_schemes_.insert( + std::make_pair( + SmartSchemaKey<FunctionIdTest::eType, MessageTypeTest::eType>( + FunctionIdTest::Function1, MessageTypeTest::error_response), + error_response_schema)); + functions_schemes_.insert( + std::make_pair( + SmartSchemaKey<FunctionIdTest::eType, MessageTypeTest::eType>( + FunctionIdTest::Function1, MessageTypeTest::request), + InitFunction_Function1_request(function_id_items, + message_type_items))); + functions_schemes_.insert( + std::make_pair( + SmartSchemaKey<FunctionIdTest::eType, MessageTypeTest::eType>( + FunctionIdTest::Function1, MessageTypeTest::response), + InitFunction_Function1_response(struct_schema_items, + function_id_items, + message_type_items))); + + functions_schemes_.insert( + std::make_pair( + SmartSchemaKey<FunctionIdTest::eType, MessageTypeTest::eType>( + FunctionIdTest::Function2, MessageTypeTest::error_response), + error_response_schema)); + functions_schemes_.insert( + std::make_pair( + SmartSchemaKey<FunctionIdTest::eType, MessageTypeTest::eType>( + FunctionIdTest::Function2, MessageTypeTest::request), + InitFunction_Function2_request(function_id_items, + message_type_items))); + functions_schemes_.insert( + std::make_pair( + SmartSchemaKey<FunctionIdTest::eType, MessageTypeTest::eType>( + FunctionIdTest::Function2, MessageTypeTest::response), + InitFunction_Function2_response(struct_schema_items, + function_id_items, + message_type_items))); + + functions_schemes_.insert( + std::make_pair( + SmartSchemaKey<FunctionIdTest::eType, MessageTypeTest::eType>( + FunctionIdTest::Function3, MessageTypeTest::error_response), + error_response_schema)); + functions_schemes_.insert( + std::make_pair( + SmartSchemaKey<FunctionIdTest::eType, MessageTypeTest::eType>( + FunctionIdTest::Function3, MessageTypeTest::request), + InitFunction_Function3_request(function_id_items, + message_type_items))); + functions_schemes_.insert( + std::make_pair( + SmartSchemaKey<FunctionIdTest::eType, MessageTypeTest::eType>( + FunctionIdTest::Function3, MessageTypeTest::response), + InitFunction_Function3_response(struct_schema_items, + function_id_items, + message_type_items))); +} + +CSmartSchema CSmartFactoryTest::InitFunction_Function1_request( + const std::set<FunctionIdTest::eType> &function_id_items, + const std::set<MessageTypeTest::eType> &message_type_items) { + + CObjectSchemaItem::Members schema_members; + CObjectSchemaItem::Members params_members; + + params_members[S_FUNCTION_ID] = CObjectSchemaItem::SMember( + TEnumSchemaItem<FunctionIdTest::eType>::create(function_id_items), true); + params_members[S_MESSAGE_TYPE] = CObjectSchemaItem::SMember( + TEnumSchemaItem<MessageTypeTest::eType>::create(message_type_items), + true); + params_members[S_PROTOCOL_VERSION] = CObjectSchemaItem::SMember( + TNumberSchemaItem<int>::create(), true); + params_members[S_PROTOCOL_TYPE] = CObjectSchemaItem::SMember( + TNumberSchemaItem<int>::create(), true); + params_members[S_CORRELATION_ID] = CObjectSchemaItem::SMember( + TNumberSchemaItem<int>::create(), true); + + CObjectSchemaItem::Members root_members_map; + root_members_map[S_MSG_PARAMS] = CObjectSchemaItem::SMember( + CObjectSchemaItem::create(schema_members), true); + root_members_map[S_PARAMS] = CObjectSchemaItem::SMember( + CObjectSchemaItem::create(params_members), true); + + return CSmartSchema(CObjectSchemaItem::create(root_members_map)); +} + +CSmartSchema CSmartFactoryTest::InitFunction_Function1_response( + const TStructsSchemaItems &struct_schema_items, + const std::set<FunctionIdTest::eType> &function_id_items, + const std::set<MessageTypeTest::eType> &message_type_items) { + // Function parameter available. + utils::SharedPtr<ISchemaItem> available_SchemaItem = CBoolSchemaItem::create( + TSchemaItemParameter<bool>()); + + CObjectSchemaItem::Members schema_members; + + schema_members["available"] = CObjectSchemaItem::SMember(available_SchemaItem, + true); + + CObjectSchemaItem::Members params_members; + params_members[S_FUNCTION_ID] = CObjectSchemaItem::SMember( + TEnumSchemaItem<FunctionIdTest::eType>::create(function_id_items), true); + params_members[S_MESSAGE_TYPE] = CObjectSchemaItem::SMember( + TEnumSchemaItem<MessageTypeTest::eType>::create(message_type_items), + true); + params_members[S_PROTOCOL_VERSION] = CObjectSchemaItem::SMember( + TNumberSchemaItem<int>::create(), true); + params_members[S_PROTOCOL_TYPE] = CObjectSchemaItem::SMember( + TNumberSchemaItem<int>::create(), true); + params_members[S_CORRELATION_ID] = CObjectSchemaItem::SMember( + TNumberSchemaItem<int>::create(), true); + params_members[kCode] = CObjectSchemaItem::SMember( + TNumberSchemaItem<int>::create(), true); + + CObjectSchemaItem::Members root_members_map; + root_members_map[S_MSG_PARAMS] = CObjectSchemaItem::SMember( + CObjectSchemaItem::create(schema_members), true); + root_members_map[S_PARAMS] = CObjectSchemaItem::SMember( + CObjectSchemaItem::create(params_members), true); + + return CSmartSchema(CObjectSchemaItem::create(root_members_map)); +} + +CSmartSchema CSmartFactoryTest::InitFunction_Function2_request( + const std::set<FunctionIdTest::eType> &function_id_items, + const std::set<MessageTypeTest::eType> &message_type_items) { + CObjectSchemaItem::Members schema_members; + + CObjectSchemaItem::Members params_members; + params_members[S_FUNCTION_ID] = CObjectSchemaItem::SMember( + TEnumSchemaItem<FunctionIdTest::eType>::create(function_id_items), true); + params_members[S_MESSAGE_TYPE] = CObjectSchemaItem::SMember( + TEnumSchemaItem<MessageTypeTest::eType>::create(message_type_items), + true); + params_members[S_PROTOCOL_VERSION] = CObjectSchemaItem::SMember( + TNumberSchemaItem<int>::create(), true); + params_members[S_PROTOCOL_TYPE] = CObjectSchemaItem::SMember( + TNumberSchemaItem<int>::create(), true); + params_members[S_CORRELATION_ID] = CObjectSchemaItem::SMember( + TNumberSchemaItem<int>::create(), true); + + CObjectSchemaItem::Members root_members_map; + root_members_map[S_MSG_PARAMS] = CObjectSchemaItem::SMember( + CObjectSchemaItem::create(schema_members), true); + root_members_map[S_PARAMS] = CObjectSchemaItem::SMember( + CObjectSchemaItem::create(params_members), true); + + return CSmartSchema(CObjectSchemaItem::create(root_members_map)); +} + +CSmartSchema CSmartFactoryTest::InitFunction_Function2_response( + const TStructsSchemaItems &struct_schema_items, + const std::set<FunctionIdTest::eType> &function_id_items, + const std::set<MessageTypeTest::eType> &message_type_items) { + // Function parameter available. + utils::SharedPtr<ISchemaItem> available_SchemaItem = CBoolSchemaItem::create( + TSchemaItemParameter<bool>()); + + CObjectSchemaItem::Members schema_members; + + schema_members["available"] = CObjectSchemaItem::SMember(available_SchemaItem, + true); + + CObjectSchemaItem::Members params_members; + params_members[S_FUNCTION_ID] = CObjectSchemaItem::SMember( + TEnumSchemaItem<FunctionIdTest::eType>::create(function_id_items), true); + params_members[S_MESSAGE_TYPE] = CObjectSchemaItem::SMember( + TEnumSchemaItem<MessageTypeTest::eType>::create(message_type_items), + true); + params_members[S_PROTOCOL_VERSION] = CObjectSchemaItem::SMember( + TNumberSchemaItem<int>::create(), true); + params_members[S_PROTOCOL_TYPE] = CObjectSchemaItem::SMember( + TNumberSchemaItem<int>::create(), true); + params_members[S_CORRELATION_ID] = CObjectSchemaItem::SMember( + TNumberSchemaItem<int>::create(), true); + params_members[kCode] = CObjectSchemaItem::SMember( + TNumberSchemaItem<int>::create(), true); + + CObjectSchemaItem::Members root_members_map; + root_members_map[S_MSG_PARAMS] = CObjectSchemaItem::SMember( + CObjectSchemaItem::create(schema_members), true); + root_members_map[S_PARAMS] = CObjectSchemaItem::SMember( + CObjectSchemaItem::create(params_members), true); + + return CSmartSchema(CObjectSchemaItem::create(root_members_map)); +} + +CSmartSchema CSmartFactoryTest::InitFunction_Function3_request( + const std::set<FunctionIdTest::eType> &function_id_items, + const std::set<MessageTypeTest::eType> &message_type_items) { + CObjectSchemaItem::Members schema_members; + + CObjectSchemaItem::Members params_members; + params_members[S_FUNCTION_ID] = CObjectSchemaItem::SMember( + TEnumSchemaItem<FunctionIdTest::eType>::create(function_id_items), true); + params_members[S_MESSAGE_TYPE] = CObjectSchemaItem::SMember( + TEnumSchemaItem<MessageTypeTest::eType>::create(message_type_items), + true); + params_members[S_PROTOCOL_VERSION] = CObjectSchemaItem::SMember( + TNumberSchemaItem<int>::create(), true); + params_members[S_PROTOCOL_TYPE] = CObjectSchemaItem::SMember( + TNumberSchemaItem<int>::create(), true); + params_members[S_CORRELATION_ID] = CObjectSchemaItem::SMember( + TNumberSchemaItem<int>::create(), true); + + CObjectSchemaItem::Members root_members_map; + root_members_map[S_MSG_PARAMS] = CObjectSchemaItem::SMember( + CObjectSchemaItem::create(schema_members), true); + root_members_map[S_PARAMS] = CObjectSchemaItem::SMember( + CObjectSchemaItem::create(params_members), true); + + return CSmartSchema(CObjectSchemaItem::create(root_members_map)); +} + +CSmartSchema CSmartFactoryTest::InitFunction_Function3_response( + const TStructsSchemaItems &struct_schema_items, + const std::set<FunctionIdTest::eType> &function_id_items, + const std::set<MessageTypeTest::eType> &message_type_items) { + // Function parameter available. + // + // Must be true if VR is present and ready to communicate with SDL. + utils::SharedPtr<ISchemaItem> available_SchemaItem = CBoolSchemaItem::create( + TSchemaItemParameter<bool>()); + + CObjectSchemaItem::Members schema_members; + + schema_members["available"] = CObjectSchemaItem::SMember(available_SchemaItem, + true); + + CObjectSchemaItem::Members params_members; + params_members[S_FUNCTION_ID] = CObjectSchemaItem::SMember( + TEnumSchemaItem<FunctionIdTest::eType>::create(function_id_items), true); + params_members[S_MESSAGE_TYPE] = CObjectSchemaItem::SMember( + TEnumSchemaItem<MessageTypeTest::eType>::create(message_type_items), + true); + params_members[S_PROTOCOL_VERSION] = CObjectSchemaItem::SMember( + TNumberSchemaItem<int>::create(), true); + params_members[S_PROTOCOL_TYPE] = CObjectSchemaItem::SMember( + TNumberSchemaItem<int>::create(), true); + params_members[S_CORRELATION_ID] = CObjectSchemaItem::SMember( + TNumberSchemaItem<int>::create(), true); + params_members[kCode] = CObjectSchemaItem::SMember( + TNumberSchemaItem<int>::create(), true); + + CObjectSchemaItem::Members root_members_map; + root_members_map[S_MSG_PARAMS] = CObjectSchemaItem::SMember( + CObjectSchemaItem::create(schema_members), true); + root_members_map[S_PARAMS] = CObjectSchemaItem::SMember( + CObjectSchemaItem::create(params_members), true); + + return CSmartSchema(CObjectSchemaItem::create(root_members_map)); +} + +utils::SharedPtr<ISchemaItem> CSmartFactoryTest::InitStructSchemaItem_Common_1( + TStructsSchemaItems &struct_schema_items) { + // Struct member text. + // + // Text to display + utils::SharedPtr<ISchemaItem> text_SchemaItem = CStringSchemaItem::create( + TSchemaItemParameter<size_t>(1), TSchemaItemParameter<size_t>(500), + TSchemaItemParameter<std::string>()); + + // Struct member image. + // + // Image struct + utils::SharedPtr<ISchemaItem> image_SchemaItem = + ProvideObjectSchemaItemForStruct(struct_schema_items, + StructIdentifiersTest::Common_2); + + // Struct member position. + // + // Position to display item + utils::SharedPtr<ISchemaItem> position_SchemaItem = + TNumberSchemaItem<int32_t>::create(TSchemaItemParameter<int32_t>(1), + TSchemaItemParameter<int32_t>(500), + TSchemaItemParameter<int32_t>()); + CObjectSchemaItem::Members struct_members; + struct_members["image"] = CObjectSchemaItem::SMember(image_SchemaItem, false); + + CObjectSchemaItem::Members schema_members; + + schema_members["text"] = CObjectSchemaItem::SMember(text_SchemaItem, true); + schema_members["position"] = CObjectSchemaItem::SMember(position_SchemaItem, + true); + + CObjectSchemaItem::Members root_members_map; + root_members_map[""] = CObjectSchemaItem::SMember( + CObjectSchemaItem::create(struct_members), true); + root_members_map[""] = CObjectSchemaItem::SMember( + CObjectSchemaItem::create(schema_members), true); + return CObjectSchemaItem::create(schema_members); +} + +utils::SharedPtr<ISchemaItem> CSmartFactoryTest::InitStructSchemaItem_Common_2() { + // Struct member text. + // + // Text to display + utils::SharedPtr<ISchemaItem> text_SchemaItem = CStringSchemaItem::create( + TSchemaItemParameter<size_t>(1), TSchemaItemParameter<size_t>(500), + TSchemaItemParameter<std::string>()); + // Struct member position. + // + // Position to display item + utils::SharedPtr<ISchemaItem> position_SchemaItem = + TNumberSchemaItem<int32_t>::create(TSchemaItemParameter<int32_t>(1), + TSchemaItemParameter<int32_t>(500), + TSchemaItemParameter<int32_t>()); + + CObjectSchemaItem::Members schema_members; + schema_members["text"] = CObjectSchemaItem::SMember(text_SchemaItem, true); + schema_members["position"] = CObjectSchemaItem::SMember(position_SchemaItem, + true); + + return CObjectSchemaItem::create(schema_members); +} + +utils::SharedPtr<ISchemaItem> CSmartFactoryTest::ProvideObjectSchemaItemForStruct( + TStructsSchemaItems &struct_schema_items, + const StructIdentifiersTest::eType struct_id) { + const TStructsSchemaItems::const_iterator it = struct_schema_items.find( + struct_id); + if (it != struct_schema_items.end()) { + return it->second; + } + return NsSmartDeviceLink::NsSmartObjects::CAlwaysFalseSchemaItem::create(); +} + diff --git a/src/components/formatters/test/src/create_smartSchema.cc b/src/components/formatters/test/src/create_smartSchema.cc new file mode 100644 index 0000000000..9d44567dcd --- /dev/null +++ b/src/components/formatters/test/src/create_smartSchema.cc @@ -0,0 +1,379 @@ +/* + * Copyright (c) 2015, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "create_smartSchema.h" +namespace test { +namespace components { +namespace formatters { + +using namespace NsSmartDeviceLink::NsJSONHandler::strings; +using namespace NsSmartDeviceLink::NsJSONHandler::Formatters; +using namespace NsSmartDeviceLink::NsSmartObjects; + +template<> +const EnumConversionHelper<FunctionIDTest::eType>::EnumToCStringMap EnumConversionHelper< + test::components::formatters::FunctionIDTest::eType>::enum_to_cstring_map_ = + EnumConversionHelper<test::components::formatters::FunctionIDTest::eType>::InitEnumToCStringMap(); + +template<> +const EnumConversionHelper<FunctionIDTest::eType>::CStringToEnumMap EnumConversionHelper< + test::components::formatters::FunctionIDTest::eType>::cstring_to_enum_map_ = + EnumConversionHelper<test::components::formatters::FunctionIDTest::eType>::InitCStringToEnumMap(); + +template<> +const char* const EnumConversionHelper<FunctionIDTest::eType>::cstring_values_[] = + { "RegisterAppInterface", "UnregisterAppInterface", "SetGlobalProperties" }; + +template<> +const FunctionIDTest::eType EnumConversionHelper<FunctionIDTest::eType>::enum_values_[] = + { test::components::formatters::FunctionIDTest::RegisterAppInterface, + test::components::formatters::FunctionIDTest::UnregisterAppInterface, + test::components::formatters::FunctionIDTest::SetGlobalProperties }; + +template<> +const EnumConversionHelper<Language::eType>::EnumToCStringMap EnumConversionHelper< + test::components::formatters::Language::eType>::enum_to_cstring_map_ = + EnumConversionHelper<test::components::formatters::Language::eType>::InitEnumToCStringMap(); + +template<> +const EnumConversionHelper<Language::eType>::CStringToEnumMap EnumConversionHelper< + test::components::formatters::Language::eType>::cstring_to_enum_map_ = + EnumConversionHelper<test::components::formatters::Language::eType>::InitCStringToEnumMap(); + +template<> +const char* const EnumConversionHelper<Language::eType>::cstring_values_[] = + { "EN_EU", "RU_RU"}; + +template<> +const Language::eType EnumConversionHelper<Language::eType>::enum_values_[] = + { test::components::formatters::Language::EN_EU, + test::components::formatters::Language::RU_RU}; + +template<> +const EnumConversionHelper<SpeechCapabilities::eType>::EnumToCStringMap EnumConversionHelper< + test::components::formatters::SpeechCapabilities::eType>::enum_to_cstring_map_ = + EnumConversionHelper<test::components::formatters::SpeechCapabilities::eType>::InitEnumToCStringMap(); + +template<> +const EnumConversionHelper<SpeechCapabilities::eType>::CStringToEnumMap EnumConversionHelper< + test::components::formatters::SpeechCapabilities::eType>::cstring_to_enum_map_ = + EnumConversionHelper<test::components::formatters::SpeechCapabilities::eType>::InitCStringToEnumMap(); + +template<> +const char* const EnumConversionHelper<SpeechCapabilities::eType>::cstring_values_[] = + { "SC_TEXT"}; + +template<> +const SpeechCapabilities::eType EnumConversionHelper<SpeechCapabilities::eType>::enum_values_[] = + { test::components::formatters::SpeechCapabilities::SC_TEXT}; + +template<> +const EnumConversionHelper<AppTypeTest::eType>::EnumToCStringMap EnumConversionHelper< + test::components::formatters::AppTypeTest::eType>::enum_to_cstring_map_ = + EnumConversionHelper<test::components::formatters::AppTypeTest::eType>::InitEnumToCStringMap(); + +template<> +const EnumConversionHelper<AppTypeTest::eType>::CStringToEnumMap EnumConversionHelper< + test::components::formatters::AppTypeTest::eType>::cstring_to_enum_map_ = + EnumConversionHelper<test::components::formatters::AppTypeTest::eType>::InitCStringToEnumMap(); + +template<> +const char* const EnumConversionHelper<AppTypeTest::eType>::cstring_values_[] = + { "SYSTEM", "MEDIA"}; + +template<> +const AppTypeTest::eType EnumConversionHelper<AppTypeTest::eType>::enum_values_[] = + { test::components::formatters::AppTypeTest::SYSTEM, + test::components::formatters::AppTypeTest::MEDIA, + }; + +CSmartSchema initObjectSchema() { + std::set<TestType::eType> resultCode_allowedEnumSubsetValues; + resultCode_allowedEnumSubsetValues.insert( + TestType::APPLICATION_NOT_REGISTERED); + resultCode_allowedEnumSubsetValues.insert(TestType::SUCCESS); + resultCode_allowedEnumSubsetValues.insert( + TestType::TOO_MANY_PENDING_REQUESTS); + resultCode_allowedEnumSubsetValues.insert(TestType::REJECTED); + resultCode_allowedEnumSubsetValues.insert(TestType::INVALID_DATA); + resultCode_allowedEnumSubsetValues.insert(TestType::OUT_OF_MEMORY); + resultCode_allowedEnumSubsetValues.insert(TestType::ABORTED); + resultCode_allowedEnumSubsetValues.insert(TestType::USER_DISALLOWED); + resultCode_allowedEnumSubsetValues.insert(TestType::GENERIC_ERROR); + resultCode_allowedEnumSubsetValues.insert(TestType::DISALLOWED); + + // Possible functions in this test scheme + std::set<FunctionIDTest::eType> functionId_allowedEnumSubsetValues; + functionId_allowedEnumSubsetValues.insert( + FunctionIDTest::RegisterAppInterface); + functionId_allowedEnumSubsetValues.insert( + FunctionIDTest::UnregisterAppInterface); + functionId_allowedEnumSubsetValues.insert( + FunctionIDTest::SetGlobalProperties); + + // Possible message types + std::set<MessageTypeTest::eType> messageType_allowedEnumSubsetValues; + messageType_allowedEnumSubsetValues.insert(MessageTypeTest::request); + messageType_allowedEnumSubsetValues.insert(MessageTypeTest::response); + messageType_allowedEnumSubsetValues.insert(MessageTypeTest::notification); + + // Create result item + ISchemaItemPtr success_SchemaItem = CBoolSchemaItem::create( + TSchemaItemParameter<bool>()); + ISchemaItemPtr resultCode_SchemaItem = + TEnumSchemaItem<TestType::eType>::create( + resultCode_allowedEnumSubsetValues, + TSchemaItemParameter<TestType::eType>()); + + // Create info value with min 0 length and max 1000 + ISchemaItemPtr info_SchemaItem = CStringSchemaItem::create( + TSchemaItemParameter<size_t>(0), TSchemaItemParameter<size_t>(1000), + TSchemaItemParameter<std::string>()); + + ISchemaItemPtr tryAgainTime_SchemaItem = TNumberSchemaItem<int>::create( + TSchemaItemParameter<int>(0), TSchemaItemParameter<int>(2000000000), + TSchemaItemParameter<int>()); + + // Map of parameters + std::map<std::string, CObjectSchemaItem::SMember> schemaMembersMap; + + schemaMembersMap["success"] = CObjectSchemaItem::SMember(success_SchemaItem, + false); + schemaMembersMap["resultCode"] = CObjectSchemaItem::SMember( + resultCode_SchemaItem, false); + schemaMembersMap["info"] = CObjectSchemaItem::SMember(info_SchemaItem, false); + schemaMembersMap["tryAgainTime"] = CObjectSchemaItem::SMember( + tryAgainTime_SchemaItem, false); + + std::map<std::string, CObjectSchemaItem::SMember> paramsMembersMap; + paramsMembersMap[S_FUNCTION_ID] = CObjectSchemaItem::SMember( + TEnumSchemaItem<FunctionIDTest::eType>::create( + functionId_allowedEnumSubsetValues), + true); + paramsMembersMap[S_MESSAGE_TYPE] = CObjectSchemaItem::SMember( + TEnumSchemaItem<MessageTypeTest::eType>::create( + messageType_allowedEnumSubsetValues), + true); + paramsMembersMap[S_CORRELATION_ID] = CObjectSchemaItem::SMember( + TNumberSchemaItem<int>::create(), true); + paramsMembersMap[S_PROTOCOL_VERSION] = CObjectSchemaItem::SMember( + TNumberSchemaItem<int>::create(TSchemaItemParameter<int>(1), + TSchemaItemParameter<int>(2)), + true); + paramsMembersMap[S_PROTOCOL_TYPE] = CObjectSchemaItem::SMember( + TNumberSchemaItem<int>::create(), true); + + std::map<std::string, CObjectSchemaItem::SMember> rootMembersMap; + rootMembersMap[S_MSG_PARAMS] = CObjectSchemaItem::SMember( + CObjectSchemaItem::create(schemaMembersMap), true); + rootMembersMap[S_PARAMS] = CObjectSchemaItem::SMember( + CObjectSchemaItem::create(paramsMembersMap), true); + return CSmartSchema(CObjectSchemaItem::create(rootMembersMap)); +}; + + +CSmartSchema initSchemaForMetaFormatter() { + std::set<TestType::eType> resultCode_allowedEnumSubsetValues; + resultCode_allowedEnumSubsetValues.insert( + TestType::APPLICATION_NOT_REGISTERED); + resultCode_allowedEnumSubsetValues.insert(TestType::SUCCESS); + resultCode_allowedEnumSubsetValues.insert( + TestType::TOO_MANY_PENDING_REQUESTS); + resultCode_allowedEnumSubsetValues.insert(TestType::REJECTED); + resultCode_allowedEnumSubsetValues.insert(TestType::INVALID_DATA); + resultCode_allowedEnumSubsetValues.insert(TestType::OUT_OF_MEMORY); + resultCode_allowedEnumSubsetValues.insert(TestType::ABORTED); + resultCode_allowedEnumSubsetValues.insert(TestType::USER_DISALLOWED); + resultCode_allowedEnumSubsetValues.insert(TestType::GENERIC_ERROR); + resultCode_allowedEnumSubsetValues.insert(TestType::DISALLOWED); + + // Possible functions in this test scheme + std::set<FunctionIDTest::eType> functionId_allowedEnumSubsetValues; + functionId_allowedEnumSubsetValues.insert( + FunctionIDTest::RegisterAppInterface); + functionId_allowedEnumSubsetValues.insert( + FunctionIDTest::UnregisterAppInterface); + functionId_allowedEnumSubsetValues.insert( + FunctionIDTest::SetGlobalProperties); + + std::set<Language::eType> languageDesired_allowedEnumSubsetValues; + languageDesired_allowedEnumSubsetValues.insert(Language::RU_RU); + languageDesired_allowedEnumSubsetValues.insert(Language::EN_EU); + + + std::set<AppTypeTest::eType> appType_allowedEnumSubsetValues; + appType_allowedEnumSubsetValues.insert(AppTypeTest::SYSTEM); + appType_allowedEnumSubsetValues.insert(AppTypeTest::MEDIA); + + std::set<SpeechCapabilities::eType> speechCapabilities_allowedEnumSubsetValues; + speechCapabilities_allowedEnumSubsetValues.insert(SpeechCapabilities::SC_TEXT); + + // Possible message types + std::set<MessageTypeTest::eType> messageType_allowedEnumSubsetValues; + messageType_allowedEnumSubsetValues.insert(MessageTypeTest::request); + messageType_allowedEnumSubsetValues.insert(MessageTypeTest::response); + messageType_allowedEnumSubsetValues.insert(MessageTypeTest::notification); + + // Create param items + ISchemaItemPtr appID_SchemaItem = CStringSchemaItem::create( + TSchemaItemParameter<size_t>(0), TSchemaItemParameter<size_t>(1000), + TSchemaItemParameter<std::string>()); + ISchemaItemPtr appName_SchemaItem = CStringSchemaItem::create( + TSchemaItemParameter<size_t>(0), TSchemaItemParameter<size_t>(1000), + TSchemaItemParameter<std::string>()); + ISchemaItemPtr isMediaApplication_SchemaItem = CBoolSchemaItem::create( + TSchemaItemParameter<bool>()); + ISchemaItemPtr ngnMediaScreenAppName_SchemaItem = CStringSchemaItem::create( + TSchemaItemParameter<size_t>(0), TSchemaItemParameter<size_t>(1000), + TSchemaItemParameter<std::string>()); + + ISchemaItemPtr ttsNameItem_SchemaItem = CStringSchemaItem::create( + TSchemaItemParameter<size_t>(0), TSchemaItemParameter<size_t>(1000), + TSchemaItemParameter<std::string>()); + + ISchemaItemPtr ttstype_SchemaItem = + TEnumSchemaItem<SpeechCapabilities::eType>::create( + speechCapabilities_allowedEnumSubsetValues, + TSchemaItemParameter<SpeechCapabilities::eType>()); + + std::map<std::string, CObjectSchemaItem::SMember> ttsMap; + ttsMap["text"]=CObjectSchemaItem::SMember(ttsNameItem_SchemaItem, + false); + ttsMap["type"]=CObjectSchemaItem::SMember(ttstype_SchemaItem, + false);; + + ISchemaItemPtr hmiDisplayLanguageDesired_SchemaItem = + TEnumSchemaItem<Language::eType>::create( + languageDesired_allowedEnumSubsetValues, + TSchemaItemParameter<Language::eType>()); + + ISchemaItemPtr languageDesired_SchemaItem = + TEnumSchemaItem<Language::eType>::create( + languageDesired_allowedEnumSubsetValues, + TSchemaItemParameter<Language::eType>()); + + ISchemaItemPtr vrElementSchemaItem = CStringSchemaItem::create( + TSchemaItemParameter<size_t>(0), TSchemaItemParameter<size_t>(1000), + TSchemaItemParameter<std::string>()); + + ISchemaItemPtr appTypeElementSchemaItem = + TEnumSchemaItem<AppTypeTest::eType>::create( + appType_allowedEnumSubsetValues, + TSchemaItemParameter<AppTypeTest::eType>()); + + ISchemaItemPtr ttsElementSchemaItem = CObjectSchemaItem::create(ttsMap); + + ISchemaItemPtr ttsName_SchemaItem = + CArraySchemaItem::create(ttsElementSchemaItem, + TSchemaItemParameter<size_t>(0), TSchemaItemParameter<size_t>(1000)); + + ISchemaItemPtr vrSynonyms_SchemaItem = + CArraySchemaItem::create(vrElementSchemaItem, + TSchemaItemParameter<size_t>(0), TSchemaItemParameter<size_t>(1000)); + + ISchemaItemPtr appType_SchemaItem = + CArraySchemaItem::create(appTypeElementSchemaItem, + TSchemaItemParameter<size_t>(0), TSchemaItemParameter<size_t>(1000)); + + + ISchemaItemPtr majorVersion_SchemaItem = TNumberSchemaItem<int>::create(); + ISchemaItemPtr minorVersion_SchemaItem = TNumberSchemaItem<int>::create(); + + ISchemaItemPtr syncMsg_SchemaItem =CStringSchemaItem::create( + TSchemaItemParameter<size_t>(0), TSchemaItemParameter<size_t>(1000), + TSchemaItemParameter<std::string>()); + + ISchemaItemPtr syncMsgVersion_SchemaItem = + CArraySchemaItem::create(syncMsg_SchemaItem, + TSchemaItemParameter<size_t>(0), TSchemaItemParameter<size_t>(1000)); + + // Creation map for syncMsgVersion + std::map<std::string, CObjectSchemaItem::SMember> schemaSyncMsgVersionMap; + schemaSyncMsgVersionMap["majorVersion"]=CObjectSchemaItem::SMember(majorVersion_SchemaItem, + false); + schemaSyncMsgVersionMap["minorVersion"]=CObjectSchemaItem::SMember(minorVersion_SchemaItem, + false);; + + // Map of parameters + std::map<std::string, CObjectSchemaItem::SMember> schemaMembersMap; + + schemaMembersMap["appID"] = CObjectSchemaItem::SMember(appID_SchemaItem, + false); + schemaMembersMap["appName"] = CObjectSchemaItem::SMember(appName_SchemaItem, + false); + schemaMembersMap["appType"] = CObjectSchemaItem::SMember(appType_SchemaItem, + false); + schemaMembersMap["hmiDisplayLanguageDesired"] = CObjectSchemaItem::SMember(hmiDisplayLanguageDesired_SchemaItem, + false); + schemaMembersMap["isMediaApplication"] = CObjectSchemaItem::SMember(isMediaApplication_SchemaItem, + false); + schemaMembersMap["languageDesired"] = CObjectSchemaItem::SMember(languageDesired_SchemaItem, + false); + schemaMembersMap["ngnMediaScreenAppName"] = CObjectSchemaItem::SMember(ngnMediaScreenAppName_SchemaItem, + false); + schemaMembersMap["syncMsgVersion"] = CObjectSchemaItem::SMember(CObjectSchemaItem::create(schemaSyncMsgVersionMap), + false); + schemaMembersMap["ttsName"] = CObjectSchemaItem::SMember(ttsName_SchemaItem, + false); + schemaMembersMap["vrSynonyms"] = CObjectSchemaItem::SMember(vrSynonyms_SchemaItem, false); + + std::map<std::string, CObjectSchemaItem::SMember> paramsMembersMap; + paramsMembersMap[S_FUNCTION_ID] = CObjectSchemaItem::SMember( + TEnumSchemaItem<FunctionIDTest::eType>::create( + functionId_allowedEnumSubsetValues), + true); + paramsMembersMap[S_MESSAGE_TYPE] = CObjectSchemaItem::SMember( + TEnumSchemaItem<MessageTypeTest::eType>::create( + messageType_allowedEnumSubsetValues), + true); + paramsMembersMap[S_CORRELATION_ID] = CObjectSchemaItem::SMember( + TNumberSchemaItem<int>::create(), true); + paramsMembersMap[S_PROTOCOL_VERSION] = CObjectSchemaItem::SMember( + TNumberSchemaItem<int>::create(TSchemaItemParameter<int>(1), + TSchemaItemParameter<int>(2)), + true); + paramsMembersMap[S_PROTOCOL_TYPE] = CObjectSchemaItem::SMember( + TNumberSchemaItem<int>::create(), true); + + std::map<std::string, CObjectSchemaItem::SMember> rootMembersMap; + rootMembersMap[S_MSG_PARAMS] = CObjectSchemaItem::SMember( + CObjectSchemaItem::create(schemaMembersMap), true); + rootMembersMap[S_PARAMS] = CObjectSchemaItem::SMember( + CObjectSchemaItem::create(paramsMembersMap), true); + return CSmartSchema(CObjectSchemaItem::create(rootMembersMap)); +}; + + + +} // namespace formatters +} // namespace components +} // namespace test diff --git a/src/components/formatters/test/src/meta_formatter_test_helper.cc b/src/components/formatters/test/src/meta_formatter_test_helper.cc new file mode 100644 index 0000000000..3445d948bb --- /dev/null +++ b/src/components/formatters/test/src/meta_formatter_test_helper.cc @@ -0,0 +1,222 @@ +/* + * Copyright (c) 2015, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +#include "gtest/gtest.h" +#include "meta_formatter_test_helper.h" + +namespace test { +namespace components { +namespace formatters { + +using namespace NsSmartDeviceLink::NsJSONHandler::strings; +using namespace NsSmartDeviceLink::NsJSONHandler::Formatters; + +void CMetaFormatterTestHelper::SetUp() { + function_id_items_.insert(FunctionIDTest::RegisterAppInterface); + function_id_items_.insert(FunctionIDTest::UnregisterAppInterface); + function_id_items_.insert(FunctionIDTest::SetGlobalProperties); + + message_type_items_.insert(MessageTypeTest::request); + message_type_items_.insert(MessageTypeTest::response); + message_type_items_.insert(MessageTypeTest::notification); +} + +void CMetaFormatterTestHelper::TearDown() { + function_id_items_.clear(); + message_type_items_.clear(); +} + +//----------------------------------------------------------- + +void CMetaFormatterTestHelper::AnyObjectToJsonString( + const SmartObject& obj, std::string& result_string) { + + Json::Value params(Json::objectValue); + + SmartObject formattedObj(obj); + + CFormatterJsonBase::objToJsonValue(formattedObj, params); + + result_string = params.toStyledString(); +} + +//----------------------------------------------------------- + +void CMetaFormatterTestHelper::FillObjectIdenticalToSchema(SmartObject& obj) { + + obj[S_PARAMS][S_MESSAGE_TYPE] = MessageTypeTest::request; + obj[S_PARAMS][S_FUNCTION_ID] = FunctionIDTest::RegisterAppInterface; + obj[S_PARAMS][S_CORRELATION_ID] = 12; + obj[S_PARAMS][S_PROTOCOL_VERSION] = 1; + obj[S_PARAMS][S_PROTOCOL_TYPE] = 0; + + obj[S_MSG_PARAMS]["syncMsgVersion"]["majorVersion"] = 2; + obj[S_MSG_PARAMS]["syncMsgVersion"]["minorVersion"] = 10; + obj[S_MSG_PARAMS]["appName"] = "APP NAME"; + obj[S_MSG_PARAMS]["ttsName"][0]["text"] = "ABC"; + obj[S_MSG_PARAMS]["ttsName"][0]["type"] = SpeechCapabilities::SC_TEXT; + obj[S_MSG_PARAMS]["ngnMediaScreenAppName"] = "SCREEN NAME"; + obj[S_MSG_PARAMS]["vrSynonyms"][0] = "Synonym1"; + obj[S_MSG_PARAMS]["vrSynonyms"][1] = "Synonym2"; + obj[S_MSG_PARAMS]["isMediaApplication"] = true; + obj[S_MSG_PARAMS]["languageDesired"] = Language::EN_EU; + obj[S_MSG_PARAMS]["hmiDisplayLanguageDesired"] = Language::RU_RU; + obj[S_MSG_PARAMS]["appType"][0] = AppTypeTest::SYSTEM; + obj[S_MSG_PARAMS]["appType"][1] = AppTypeTest::MEDIA; + obj[S_MSG_PARAMS]["appID"] = "APP ID"; +} + +//----------------------------------------------------------- +void CMetaFormatterTestHelper::FillObjectIdenticalToSchemaWithoutNoMandatoriesParams( + SmartObject& obj) { + obj[S_PARAMS][S_MESSAGE_TYPE] = MessageTypeTest::request; + obj[S_PARAMS][S_FUNCTION_ID] = FunctionIDTest::RegisterAppInterface; + obj[S_PARAMS][S_CORRELATION_ID] = 12; + obj[S_PARAMS][S_PROTOCOL_VERSION] = 1; + obj[S_PARAMS][S_PROTOCOL_TYPE] = 0; + + obj[S_MSG_PARAMS]["syncMsgVersion"]["majorVersion"] = 2; + obj[S_MSG_PARAMS]["syncMsgVersion"]["minorVersion"] = 10; + obj[S_MSG_PARAMS]["appName"] = "APP NAME"; + obj[S_MSG_PARAMS]["ngnMediaScreenAppName"] = "SCREEN NAME"; + obj[S_MSG_PARAMS]["isMediaApplication"] = true; + obj[S_MSG_PARAMS]["languageDesired"] = Language::EN_EU; + obj[S_MSG_PARAMS]["hmiDisplayLanguageDesired"] = Language::RU_RU; + obj[S_MSG_PARAMS]["appID"] = "APP ID"; + + // Commented not mandatory params for check creation object without them +// obj[S_MSG_PARAMS]["ttsName"][0]["text"] = "ABC"; +// obj[S_MSG_PARAMS]["ttsName"][0]["type"] = +// SpeechCapabilities::SC_TEXT; + +// obj[S_MSG_PARAMS]["vrSynonyms"][0] = "Synonym1"; +// obj[S_MSG_PARAMS]["vrSynonyms"][1] = "Synonym2"; + +// obj[S_MSG_PARAMS]["appType"][0] = AppTypeTest::SYSTEM; // not mandatory +// obj[S_MSG_PARAMS]["appType"][1] = AppTypeTest::MEDIA; + +} + +void CMetaFormatterTestHelper::FillObjectWithoutSomeMandatoryFields( + SmartObject& obj) { + obj[S_PARAMS][S_MESSAGE_TYPE] = MessageTypeTest::request; + obj[S_PARAMS][S_FUNCTION_ID] = FunctionIDTest::RegisterAppInterface; + + obj[S_PARAMS][S_PROTOCOL_VERSION] = 1; + obj[S_PARAMS][S_PROTOCOL_TYPE] = 0; + +// Commented mandatory params for check creation object without them +// obj[S_PARAMS][S_CORRELATION_ID] = 12; +// obj[S_MSG_PARAMS]["syncMsgVersion"]["majorVersion"] = 2; +// obj[S_MSG_PARAMS]["syncMsgVersion"]["minorVersion"] = 10; + + obj[S_MSG_PARAMS]["appName"] = "APP NAME"; + obj[S_MSG_PARAMS]["ttsName"][0]["text"] = "ABC"; + obj[S_MSG_PARAMS]["ttsName"][0]["type"] = SpeechCapabilities::SC_TEXT; + obj[S_MSG_PARAMS]["ngnMediaScreenAppName"] = "SCREEN NAME"; + obj[S_MSG_PARAMS]["vrSynonyms"][0] = "Synonym1"; + obj[S_MSG_PARAMS]["vrSynonyms"][1] = "Synonym2"; + obj[S_MSG_PARAMS]["isMediaApplication"] = true; + obj[S_MSG_PARAMS]["languageDesired"] = Language::EN_EU; + obj[S_MSG_PARAMS]["hmiDisplayLanguageDesired"] = Language::RU_RU; + obj[S_MSG_PARAMS]["appType"][0] = AppTypeTest::SYSTEM; + obj[S_MSG_PARAMS]["appType"][1] = AppTypeTest::MEDIA; + obj[S_MSG_PARAMS]["appID"] = "APP ID"; +} + +//----------------------------------------------------------- + +void CMetaFormatterTestHelper::CompareObjects(const SmartObject& first, + const SmartObject& second) { + + if (SmartType_Array == first.getType()) { + ASSERT_EQ(SmartType_Array, second.getType()); + for (size_t i = 0; i < first.length(); i++) { + CompareObjects(first.getElement(i), second.getElement(i)); + } + } else if (SmartType_Map == first.getType()) { + ASSERT_EQ(SmartType_Map, second.getType()); + std::set < std::string > keys = first.enumerate(); + + for (std::set<std::string>::const_iterator key = keys.begin(); + key != keys.end(); key++) { + CompareObjects(first.getElement(*key), second.getElement(*key)); + } + } else if (SmartType_Boolean == first.getType()) { + ASSERT_EQ(first.asBool(), second.asBool()); + } else if (SmartType_Integer == first.getType()) { + ASSERT_EQ(first.asInt(), second.asInt()); + } else if (SmartType_Double == first.getType()) { + ASSERT_EQ(first.asDouble(), second.asDouble()); + } else if (SmartType_String == first.getType()) { + ASSERT_EQ(first.asString(), second.asString()); + } else if (SmartType_Null == first.getType()) { + ASSERT_EQ(SmartType_Null, second.getType()); + } else { + FAIL()<< "Unknown SmartObject type: " << first.getType(); + } +} + +//----------------------------------------------------------- + +void CMetaFormatterTestHelper::FillObjectWithDefaultValues(SmartObject& obj) { + + obj[S_PARAMS][S_MESSAGE_TYPE] = -1; + obj[S_PARAMS][S_FUNCTION_ID] = -1; + obj[S_PARAMS][S_CORRELATION_ID] = 0; + obj[S_PARAMS][S_PROTOCOL_VERSION] = 0; + obj[S_PARAMS][S_PROTOCOL_TYPE] = 0; + + obj[S_MSG_PARAMS]["syncMsgVersion"]["majorVersion"] = 0; + obj[S_MSG_PARAMS]["syncMsgVersion"]["minorVersion"] = 0; + obj[S_MSG_PARAMS]["appName"] = ""; + obj[S_MSG_PARAMS]["ngnMediaScreenAppName"] = ""; + obj[S_MSG_PARAMS]["isMediaApplication"] = false; + obj[S_MSG_PARAMS]["languageDesired"] = -1; + obj[S_MSG_PARAMS]["hmiDisplayLanguageDesired"] = -1; + obj[S_MSG_PARAMS]["appID"] = ""; + +// Commented params for check creation object with only default values +// obj[S_MSG_PARAMS]["ttsName"][0]["text"] = "ABC"; +// obj[S_MSG_PARAMS]["ttsName"][0]["type"] = +// SpeechCapabilities::SC_TEXT; + +// obj[S_MSG_PARAMS]["vrSynonyms"][0] = "Synonym1"; +// obj[S_MSG_PARAMS]["vrSynonyms"][1] = "Synonym2"; + +// obj[S_MSG_PARAMS]["appType"][0] = AppTypeTest::SYSTEM; +// obj[S_MSG_PARAMS]["appType"][1] = AppTypeTest::MEDIA; + +} + +} // namespace formatters +} // namespace components +} // namespace test |