/**
     * Test BoolSchemaItem no default value
     *
     * Create SchemaItem without default value. Method setDefaultValue should always return false
     * and leave SmartObject in previous state.
     **/
    TEST(test_no_default_value, test_BoolSchemaItemTest)
    {
        NsSmartDeviceLink::NsSmartObjects::SmartObject obj;

        utils::SharedPtr<NsSmartDeviceLink::NsSmartObjects::CBoolSchemaItem> item =
            NsSmartDeviceLink::NsSmartObjects::CBoolSchemaItem::create(); // No default value

        obj = 5;
        ASSERT_EQ(5, obj.asInt());

        int resultType = item->validate(obj);
        EXPECT_EQ(NsSmartDeviceLink::NsSmartObjects::Errors::INVALID_VALUE, resultType);

        obj = true;
        ASSERT_TRUE(obj.asBool());

        resultType = item->validate(obj);
        EXPECT_EQ(NsSmartDeviceLink::NsSmartObjects::Errors::OK, resultType);
        bool resDefault = item->setDefaultValue(obj);
        EXPECT_FALSE(resDefault);
        EXPECT_TRUE(obj.asBool());

        obj = "Test";
        ASSERT_EQ(std::string("Test"), obj.asString());

        resultType = item->validate(obj);
        EXPECT_EQ(NsSmartDeviceLink::NsSmartObjects::Errors::INVALID_VALUE, resultType);
        resDefault = item->setDefaultValue(obj);
        EXPECT_FALSE(resDefault);
        resultType = item->validate(obj);
        EXPECT_EQ(NsSmartDeviceLink::NsSmartObjects::Errors::INVALID_VALUE, resultType);
    }
    TEST(test_map_validate, test_BoolSchemaItemTest)
    {
        NsSmartDeviceLink::NsSmartObjects::SmartObject obj;
        utils::SharedPtr<NsSmartDeviceLink::NsSmartObjects::CBoolSchemaItem> item =
            NsSmartDeviceLink::NsSmartObjects::CBoolSchemaItem::create(
                NsSmartDeviceLink::NsSmartObjects::TSchemaItemParameter<bool>(false)); // Default value = false

        obj["aa"] = true;
        ASSERT_TRUE(obj["aa"].asBool());

        int resultType = item->validate(obj["aa"]);
        EXPECT_EQ(NsSmartDeviceLink::NsSmartObjects::Errors::OK, resultType);

        resultType = item->validate(obj);
        EXPECT_EQ(NsSmartDeviceLink::NsSmartObjects::Errors::INVALID_VALUE, resultType);

        bool resDefault = item->setDefaultValue(obj["aa"]);
        EXPECT_TRUE(resDefault);
        EXPECT_FALSE(obj["aa"].asBool());

        resDefault = item->setDefaultValue(obj);
        EXPECT_TRUE(resDefault);
        EXPECT_FALSE(obj.asBool());

        resultType = item->validate(obj);
        EXPECT_EQ(NsSmartDeviceLink::NsSmartObjects::Errors::OK, resultType);

        obj["ind"] = true;
        resultType = item->validate(obj);
        EXPECT_EQ(NsSmartDeviceLink::NsSmartObjects::Errors::INVALID_VALUE, resultType);
    }
Exemplo n.º 3
0
Formatters::CFormatterJsonSDLRPCv2::tMetaFormatterErrorCode
  Formatters::CFormatterJsonSDLRPCv2::MetaFormatToString(
            const NsSmartDeviceLink::NsSmartObjects::SmartObject& object,
            const NsSmartDeviceLink::NsSmartObjects::CSmartSchema& schema,
            std::string& outStr) {

  meta_formatter_error_code::tMetaFormatterErrorCode result_code
                                    = meta_formatter_error_code::kErrorOk;

  NsSmartDeviceLink::NsSmartObjects::SmartObject tmp_object;

  if (false == CMetaFormatter::CreateObjectByPattern(object, schema, tmp_object)) {
      result_code |= meta_formatter_error_code::kErrorFailedCreateObjectBySchema;
      return result_code;
  }

  // determine whether smart objects are functions
  // (in terms of SDLRPC communication)
  bool is_root_object_created_by_schema = (
    (tmp_object.getType() == smart_objects_ns::SmartType_Map)
      && tmp_object.keyExists(jsonhandler_ns::strings::S_PARAMS)
      && tmp_object.keyExists(jsonhandler_ns::strings::S_MSG_PARAMS));

  bool is_root_object = (
    (object.getType() == smart_objects_ns::SmartType_Map)
      && object.keyExists(jsonhandler_ns::strings::S_PARAMS)
      && object.keyExists(jsonhandler_ns::strings::S_MSG_PARAMS));

  if (false == is_root_object) {
    result_code |= meta_formatter_error_code::kErrorObjectIsNotFunction;
  }
  if (false == is_root_object_created_by_schema) {
    result_code |= meta_formatter_error_code::kErrorSchemaIsNotFunction;
  }

  CFormatterJsonSDLRPCv2::toString(tmp_object, outStr);

  return result_code;
}
Exemplo n.º 4
0
        bool CSmartFactory<FunctionIdEnum, MessageTypeEnum, StructIdEnum>::attachSchema(NsSmartDeviceLink::NsSmartObjects::SmartObject &object)
        {
            if(false == object.keyExists(strings::S_PARAMS)) return false;
            if(false == object[strings::S_PARAMS].keyExists(strings::S_MESSAGE_TYPE)) return false;
            if(false == object[strings::S_PARAMS].keyExists(strings::S_FUNCTION_ID)) return false;

            MessageTypeEnum msgtype((MessageTypeEnum)(int)object[strings::S_PARAMS][strings::S_MESSAGE_TYPE]);
            FunctionIdEnum fid((FunctionIdEnum)(int)object[strings::S_PARAMS][strings::S_FUNCTION_ID]);

            SmartSchemaKey<FunctionIdEnum, MessageTypeEnum> key(fid, msgtype);

            typename FuncionsSchemesMap::iterator schemaIterator = functions_schemes_.find(key);

            if(schemaIterator == functions_schemes_.end())
            {
                // Schema was not found
                return false;
            }

            object.setSchema(schemaIterator->second);
            schemaIterator->second.applySchema(object);

            return true;
        }
Exemplo n.º 5
0
      bool CSmartFactory<FunctionIdEnum, MessageTypeEnum, StructIdEnum>::
          AttachSchema(
              const StructIdEnum struct_id,
              NsSmartDeviceLink::NsSmartObjects::SmartObject &object) {
        typename StructsSchemesMap::iterator structs_iterator =
            structs_schemes_.find(struct_id);

        if (structs_iterator == structs_schemes_.end()) {
          return false;
        }

        object.setSchema(structs_iterator->second);
        structs_iterator->second.applySchema(object);

        return true;
      }
void NsSmartDeviceLink::NsJSONHandler::Formatters::CFormatterJsonBase::
    objToJsonValue(const NsSmartDeviceLink::NsSmartObjects::SmartObject& obj,
                   utils::json::JsonValueRef item) {
  using namespace utils::json;
  try {
    if (NsSmartDeviceLink::NsSmartObjects::SmartType_Array == obj.getType()) {
      item = ValueType::ARRAY_VALUE;

      for (uint32_t i = 0; i < obj.length(); i++) {
        JsonValue value;

        objToJsonValue(obj.getElement(i), value);

        item.Append(value);
      }
    } else if (NsSmartDeviceLink::NsSmartObjects::SmartType_Map ==
               obj.getType()) {
      item = ValueType::OBJECT_VALUE;
      std::set<std::string> keys = obj.enumerate();

      for (std::set<std::string>::const_iterator key_itr = keys.begin(),
                                                 end = keys.end();
           key_itr != end;
           ++key_itr) {
        JsonValue value;

        objToJsonValue(obj.getElement(*key_itr), value);

        item[*key_itr] = value;
      }
    } else if (NsSmartDeviceLink::NsSmartObjects::SmartType_Boolean ==
               obj.getType()) {
      item = obj.asBool();
    } else if (NsSmartDeviceLink::NsSmartObjects::SmartType_Integer ==
               obj.getType()) {
      item = obj.asInt();
    } else if (NsSmartDeviceLink::NsSmartObjects::SmartType_Double ==
               obj.getType()) {
      item = obj.asDouble();
    } else if (NsSmartDeviceLink::NsSmartObjects::SmartType_Null ==
               obj.getType()) {
      item = ValueType::NULL_VALUE;
    } else {
      item = obj.asString();
    }
  } catch (...) {
  }
}