1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
21 #include "response_model.h"
23 ResponseModel::ResponseModel(int code) : m_code(code) {}
25 void ResponseModel::setRepSchema(SimulatorResourceModelSP &repSchema)
27 m_repSchema = repSchema;
30 SimulatorResult ResponseModel::verifyResponse(const OC::OCRepresentation &rep)
32 for (auto & ocAttribute : rep)
34 SimulatorResourceModel::Attribute attribute;
35 if (false == m_repSchema->getAttribute(ocAttribute.attrname(), attribute))
37 return SIMULATOR_UKNOWN_PROPERTY;
40 switch (attribute.getValueType())
42 case SimulatorResourceModel::Attribute::ValueType::INTEGER : // Integer
44 SimulatorResult result = validateAttributeInteger(attribute, ocAttribute);
45 if (SIMULATOR_OK != result)
52 case SimulatorResourceModel::Attribute::ValueType::DOUBLE : // Double
54 SimulatorResult result = validateAttributeDouble(attribute, ocAttribute);
55 if (SIMULATOR_OK != result)
62 case SimulatorResourceModel::Attribute::ValueType::STRING : // String
64 SimulatorResult result = validateAttributeString(attribute, ocAttribute);
65 if (SIMULATOR_OK != result)
77 SimulatorResult ResponseModel::validateAttributeInteger(
78 SimulatorResourceModel::Attribute &attrSchema,
79 const OC::OCRepresentation::AttributeItem &ocAttribute)
81 // Check the value type
82 if (OC::AttributeType::Integer != ocAttribute.type())
84 return SIMULATOR_TYPE_MISMATCH;
87 // Check value if it is in range
89 attrSchema.getRange(min, max);
90 value = ocAttribute.getValue<int>();
91 if (value < min || value > max)
93 return SIMULATOR_BAD_VALUE;
99 SimulatorResult ResponseModel::validateAttributeDouble(
100 SimulatorResourceModel::Attribute &attrSchema,
101 const OC::OCRepresentation::AttributeItem &ocAttribute)
103 // Check the value type
104 if (OC::AttributeType::Double != ocAttribute.type())
106 return SIMULATOR_TYPE_MISMATCH;
112 SimulatorResult ResponseModel::validateAttributeString(
113 SimulatorResourceModel::Attribute &attrSchema,
114 const OC::OCRepresentation::AttributeItem &ocAttribute)
116 // Check the value type
117 if (OC::AttributeType::String != ocAttribute.type())
119 return SIMULATOR_TYPE_MISMATCH;
122 // TODO: Check the allowed values