Skip to content

Commit

Permalink
Merge pull request eclipse#185 from boschresearch/feature/iso-timestamps
Browse files Browse the repository at this point in the history
Feature/iso timestamps
  • Loading branch information
wenwenchenbosch authored May 6, 2021
2 parents ed2f18a + b08ab16 commit 2f3afd9
Show file tree
Hide file tree
Showing 9 changed files with 244 additions and 275 deletions.
4 changes: 3 additions & 1 deletion include/JsonResponses.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ namespace JsonResponses {
const std::string message,
jsoncons::json& jsonResponse);

int64_t getTimeStamp();
std::string getTimeStamp();

std::string getTimeStampZero();
}

#endif
37 changes: 32 additions & 5 deletions src/JsonResponses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
* *****************************************************************************
*/
#include "JsonResponses.hpp"
#include <chrono>

#include <time.h>


namespace JsonResponses {
void malFormedRequest(std::string request_id,
Expand Down Expand Up @@ -130,9 +132,34 @@ namespace JsonResponses {
return ss.str();
}

int64_t getTimeStamp(){
return std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch()
).count();
/** Return an extended ISO8601 UTC timestamp according to W3C guidelines https://www.w3.org/TR/NOTE-datetime
* Complete date plus hours, minutes, seconds and a decimal fraction of a second
YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)
where:
YYYY = four-digit year
MM = two-digit month (01=January, etc.)
DD = two-digit day of month (01 through 31)
hh = two digits of hour (00 through 23) (am/pm NOT allowed)
mm = two digits of minute (00 through 59)
ss = two digits of second (00 through 59)
s = one or more digits representing a decimal fraction of a second
TZD = time zone designator (Z or +hh:mm or -hh:mm)
*/
std::string getTimeStamp(){
auto itt=std::time(nullptr);
std::ostringstream ss;
ss << std::put_time(gmtime(&itt), "%FT%T.%sZ");
return ss.str();
}

/** This extended ISO8601 UTC timestamp according to W3C guidelines https://www.w3.org/TR/NOTE-datetime
* for unix timestamp zero. This will be used when values that have never been set are queried
* This makes sure to habe a syntactically compliant timestamp
*/
std::string getTimeStampZero() {
return "1981-01-01T00:00:00.0000000000Z";
}
}

2 changes: 1 addition & 1 deletion src/VssDatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ jsoncons::json VssDatabase::getSignal(const VSSPath& path) {
if (result.contains("timestamp")) {
answer["timestamp"] = result["timestamp"].as<string>();
} else {
answer["timestamp"] = "0";
answer["timestamp"] = JsonResponses::getTimeStampZero();
}
return answer;

Expand Down
42 changes: 15 additions & 27 deletions test/unit-test/Gen2GetTests.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* ******************************************************************************
* Copyright (c) 2020 Robert Bosch GmbH.
* Copyright (c) 2020-2021 Robert Bosch GmbH.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
Expand All @@ -19,7 +19,8 @@
#include <turtle/mock.hpp>
#undef BOOST_BIND_GLOBAL_PLACEHOLDERS

#include <chrono>
#include "UnitTestHelpers.hpp"

#include <thread>

#include <memory>
Expand Down Expand Up @@ -83,6 +84,8 @@ struct TestSuiteFixture {
};
} // namespace



// Define name of test suite and define test suite fixture for pre and post test
// handling
BOOST_FIXTURE_TEST_SUITE(Gen2GetTests, TestSuiteFixture);
Expand Down Expand Up @@ -168,12 +171,8 @@ BOOST_AUTO_TEST_CASE(Gen2_Get_Invalid_JSON) {
processor->processQuery(jsonSetRequestForSignal.as_string(), channel);
auto res = json::parse(resStr);

// Does result have a timestamp?
BOOST_TEST(res["timestamp"].as<int64_t>() > 0);

// Remove timestamp for comparision purposes
expectedJson["timestamp"] = res["timestamp"].as<int64_t>();

verify_timestamp(expectedJson,res);

BOOST_TEST(res == expectedJson);
}

Expand Down Expand Up @@ -208,11 +207,8 @@ BOOST_AUTO_TEST_CASE(Gen2_Get_Invalid_JSON_NoRequestID) {
auto res = json::parse(resStr);

// Does result have a timestamp?
BOOST_TEST(res["timestamp"].as<int64_t>() > 0);

// Remove timestamp for comparision purposes
expectedJson["timestamp"] = res["timestamp"].as<int64_t>();

verify_timestamp(expectedJson,res);

BOOST_TEST(res == expectedJson);
}

Expand Down Expand Up @@ -242,10 +238,8 @@ BOOST_AUTO_TEST_CASE(Gen2_Get_NonExistingPath) {
auto res = json::parse(resStr);

// verify
BOOST_TEST(res["timestamp"].as<int64_t>() > 0);
res["timestamp"] =
jsonPathNotFound["timestamp"]
.as<int64_t>(); // ignoring timestamp difference for response
verify_timestamp(jsonPathNotFound,res);

BOOST_TEST(res == jsonPathNotFound);
}

Expand Down Expand Up @@ -371,10 +365,8 @@ BOOST_AUTO_TEST_CASE(Gen2_Get_Wildcard_NonExisting) {
auto res = json::parse(resStr);

// verify
BOOST_TEST(res["timestamp"].as<int64_t>() > 0);
res["timestamp"] =
jsonPathNotFound["timestamp"]
.as<int64_t>(); // ignoring timestamp difference for response
verify_timestamp(jsonPathNotFound,res);

BOOST_TEST(res == jsonPathNotFound);
}

Expand Down Expand Up @@ -409,8 +401,7 @@ BOOST_AUTO_TEST_CASE(Gen2_Get_noPermissionException) {
// verify

// timestamp must not be zero
BOOST_TEST(res["timestamp"].as<int64_t>() > 0);
jsonNoAccess["timestamp"] = res["timestamp"].as<int64_t>(); // ignoring timestamp difference for response
verify_timestamp(jsonNoAccess,res);
BOOST_TEST(res == jsonNoAccess);

}
Expand Down Expand Up @@ -471,11 +462,8 @@ BOOST_AUTO_TEST_CASE(Gen2_Get_StableTimestamp) {
processor->processQuery(jsonGetRequestForSignal.as_string(), channel);
auto res = json::parse(resStr);

// Does result have a timestamp?
BOOST_TEST(res["timestamp"].as<int64_t>() > 0);
verify_timestamp(expectedJson,res);

// Remove timestamp for comparision purposes
expectedJson.insert_or_assign("timestamp",res["timestamp"]);
BOOST_TEST(res == expectedJson);

//wait 20ms (timestamps should be 1 ms resolution, but 20 ms should
Expand Down
44 changes: 10 additions & 34 deletions test/unit-test/Gen2SetTests.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* ******************************************************************************
* Copyright (c) 2020 Robert Bosch GmbH.
* Copyright (c) 2020-2021 Robert Bosch GmbH.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
Expand All @@ -19,6 +19,8 @@
#include <turtle/mock.hpp>
#undef BOOST_BIND_GLOBAL_PLACEHOLDERS

#include "UnitTestHelpers.hpp"

#include <memory>
#include <string>

Expand Down Expand Up @@ -130,11 +132,7 @@ BOOST_AUTO_TEST_CASE(Gen2_Set_Sensor_Simple) {
processor->processQuery(jsonSetRequestForSignal.as_string(), channel);
auto res = json::parse(resStr);

// Does result have a timestamp?
BOOST_TEST(res["timestamp"].as<int64_t>() > 0);

// Remove timestamp for comparision purposes
expectedJson["timestamp"] = res["timestamp"].as<int64_t>();
verify_timestamp(expectedJson,res);

BOOST_TEST(res == expectedJson);
}
Expand Down Expand Up @@ -174,11 +172,7 @@ BOOST_AUTO_TEST_CASE(Gen2_Set_Invalid_JSON) {
processor->processQuery(jsonSetRequestForSignal.as_string(), channel);
auto res = json::parse(resStr);

// Does result have a timestamp?
BOOST_TEST(res["timestamp"].as<int64_t>() > 0);

// Remove timestamp for comparision purposes
expectedJson["timestamp"] = res["timestamp"].as<int64_t>();
verify_timestamp(expectedJson,res);

BOOST_TEST(res == expectedJson);
}
Expand Down Expand Up @@ -214,11 +208,7 @@ BOOST_AUTO_TEST_CASE(Gen2_Set_Invalid_JSON_NoRequestID) {
processor->processQuery(jsonSetRequestForSignal.as_string(), channel);
auto res = json::parse(resStr);

// Does result have a timestamp?
BOOST_TEST(res["timestamp"].as<int64_t>() > 0);

// Remove timestamp for comparision purposes
expectedJson["timestamp"] = res["timestamp"].as<int64_t>();
verify_timestamp(expectedJson,res);

BOOST_TEST(res == expectedJson);
}
Expand Down Expand Up @@ -267,11 +257,7 @@ BOOST_AUTO_TEST_CASE(Gen2_Set_Non_Existing_Path) {

auto res = json::parse(resStr);

// Does result have a timestamp?
BOOST_TEST(res["timestamp"].as<int64_t>() > 0);

// Remove timestamp for comparision purposes
expectedJson["timestamp"] = res["timestamp"].as<int64_t>();
verify_timestamp(expectedJson,res);

BOOST_TEST(res == expectedJson);
}
Expand Down Expand Up @@ -321,11 +307,7 @@ BOOST_AUTO_TEST_CASE(Gen2_Set_Branch) {

auto res = json::parse(resStr);

// Does result have a timestamp?
BOOST_TEST(res["timestamp"].as<int64_t>() > 0);

// Remove timestamp for comparision purposes
expectedJson["timestamp"] = res["timestamp"].as<int64_t>();
verify_timestamp(expectedJson,res);

BOOST_TEST(res == expectedJson);
}
Expand Down Expand Up @@ -375,11 +357,7 @@ BOOST_AUTO_TEST_CASE(Gen2_Set_Attribute) {

auto res = json::parse(resStr);

// Does result have a timestamp?
BOOST_TEST(res["timestamp"].as<int64_t>() > 0);

// Remove timestamp for comparision purposes
expectedJson["timestamp"] = res["timestamp"].as<int64_t>();
verify_timestamp(expectedJson,res);

BOOST_TEST(res == expectedJson);
}
Expand Down Expand Up @@ -414,10 +392,8 @@ BOOST_AUTO_TEST_CASE(Gen2_Set_noPermissionException) {
auto res = json::parse(resStr);

// verify
verify_timestamp(jsonNoAccess,res);

// timestamp must not be zero
BOOST_TEST(res["timestamp"].as<int64_t>() > 0);
jsonNoAccess["timestamp"] = res["timestamp"].as<int64_t>(); // ignoring timestamp difference for response
BOOST_TEST(res == jsonNoAccess);
}

Expand Down
Loading

0 comments on commit 2f3afd9

Please sign in to comment.