Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
OTA-4174: Move the campaign's functions to its class scope to make do…
Browse files Browse the repository at this point in the history
…xygen happy

Signed-off-by: Mike Sul <[email protected]>
  • Loading branch information
Mike Sul committed Jan 16, 2020
1 parent d4007b6 commit 11681c2
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 17 deletions.
6 changes: 3 additions & 3 deletions src/libaktualizr/campaign/campaign.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void Campaign::getJson(Json::Value &out) const {
out["metadata"][2]["value"] = std::to_string(estPreparationDuration);
}

std::vector<Campaign> campaignsFromJson(const Json::Value &json) {
std::vector<Campaign> Campaign::campaignsFromJson(const Json::Value &json) {
std::vector<Campaign> campaigns;

Json::Value campaigns_array;
Expand All @@ -95,7 +95,7 @@ std::vector<Campaign> campaignsFromJson(const Json::Value &json) {
return campaigns;
}

void JsonFromCampaigns(const std::vector<Campaign> &in, Json::Value &out) {
void Campaign::JsonFromCampaigns(const std::vector<Campaign> &in, Json::Value &out) {
out.clear();
auto i = 0;
Json::Value json;
Expand All @@ -106,7 +106,7 @@ void JsonFromCampaigns(const std::vector<Campaign> &in, Json::Value &out) {
}
}

std::vector<Campaign> fetchAvailableCampaigns(HttpInterface &http_client, const std::string &tls_server) {
std::vector<Campaign> Campaign::fetchAvailableCampaigns(HttpInterface &http_client, const std::string &tls_server) {
HttpResponse response = http_client.get(tls_server + "/campaigner/campaigns", kMaxCampaignsMetaSize);
if (!response.isOk()) {
LOG_ERROR << "Failed to fetch list of available campaigns";
Expand Down
8 changes: 5 additions & 3 deletions src/libaktualizr/campaign/campaign.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ static inline Cmd cmdFromName(const std::string &name) {

// Out of uptane concept: update campaign for a device
class Campaign {
public:
static std::vector<Campaign> campaignsFromJson(const Json::Value &json);
static void JsonFromCampaigns(const std::vector<Campaign> &in, Json::Value &out);
static std::vector<Campaign> fetchAvailableCampaigns(HttpInterface &http_client, const std::string &tls_server);

public:
Campaign() = default;
Campaign(const Json::Value &json);
Expand All @@ -43,9 +48,6 @@ class Campaign {
int estPreparationDuration{0};
};

std::vector<Campaign> campaignsFromJson(const Json::Value &json);
void JsonFromCampaigns(const std::vector<Campaign> &in, Json::Value &out);
std::vector<Campaign> fetchAvailableCampaigns(HttpInterface &http_client, const std::string &tls_server);
} // namespace campaign

#endif
18 changes: 9 additions & 9 deletions src/libaktualizr/campaign/campaign_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ boost::filesystem::path test_data_dir;
TEST(campaign, Campaigns_from_json) {
auto json = Utils::parseJSONFile(test_data_dir / "campaigns_sample.json");

auto campaigns = campaign::campaignsFromJson(json);
auto campaigns = campaign::Campaign::campaignsFromJson(json);
EXPECT_EQ(campaigns.size(), 1);

EXPECT_EQ(campaigns.at(0).name, "campaign1");
Expand All @@ -27,17 +27,17 @@ TEST(campaign, Campaigns_from_json) {
bad4["campaigns"][0] = Json::Value();
bad4["campaigns"][0]["name"] = "a";
bad4["campaigns"][0]["id"] = "a";
auto campaignsNoAutoAccept = campaign::campaignsFromJson(bad4);
auto campaignsNoAutoAccept = campaign::Campaign::campaignsFromJson(bad4);
EXPECT_FALSE(campaignsNoAutoAccept.at(0).autoAccept);
}

/* Get JSON from campaign. */
TEST(campaign, Campaigns_to_json) {
auto json = Utils::parseJSONFile(test_data_dir / "campaigns_sample.json");

auto campaigns = campaign::campaignsFromJson(json);
auto campaigns = campaign::Campaign::campaignsFromJson(json);
Json::Value res;
JsonFromCampaigns(campaigns, res);
campaign::Campaign::JsonFromCampaigns(campaigns, res);

EXPECT_EQ(res["campaigns"][0]["name"], "campaign1");
EXPECT_EQ(res["campaigns"][0]["id"], "c2eb7e8d-8aa0-429d-883f-5ed8fdb2a493");
Expand All @@ -53,27 +53,27 @@ TEST(campaign, Campaigns_to_json) {

TEST(campaign, Campaigns_from_invalid_json) {
// empty object
EXPECT_EQ(campaign::campaignsFromJson(Json::Value()).size(), 0);
EXPECT_EQ(campaign::Campaign::campaignsFromJson(Json::Value()).size(), 0);

// naked array
EXPECT_EQ(campaign::campaignsFromJson(Json::Value(Json::arrayValue)).size(), 0);
EXPECT_EQ(campaign::Campaign::campaignsFromJson(Json::Value(Json::arrayValue)).size(), 0);

// object in object
Json::Value bad1;
bad1["campaigns"] = Json::Value();
EXPECT_EQ(campaign::campaignsFromJson(bad1).size(), 0);
EXPECT_EQ(campaign::Campaign::campaignsFromJson(bad1).size(), 0);

// array in array in object
Json::Value bad2;
bad2["campaigns"] = Json::Value(Json::arrayValue);
bad2["campaigns"][0] = Json::Value(Json::arrayValue);
EXPECT_EQ(campaign::campaignsFromJson(bad2).size(), 0);
EXPECT_EQ(campaign::Campaign::campaignsFromJson(bad2).size(), 0);

// no name
Json::Value bad3;
bad3["campaigns"] = Json::Value(Json::arrayValue);
bad3["campaigns"][0] = Json::Value();
EXPECT_EQ(campaign::campaignsFromJson(bad3).size(), 0);
EXPECT_EQ(campaign::Campaign::campaignsFromJson(bad3).size(), 0);
}

#ifndef __NO_MAIN__
Expand Down
2 changes: 1 addition & 1 deletion src/libaktualizr/primary/sotauptaneclient.cc
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,7 @@ result::Install SotaUptaneClient::uptaneInstall(const std::vector<Uptane::Target
}

result::CampaignCheck SotaUptaneClient::campaignCheck() {
auto campaigns = campaign::fetchAvailableCampaigns(*http, config.tls.server);
auto campaigns = campaign::Campaign::fetchAvailableCampaigns(*http, config.tls.server);
for (const auto &c : campaigns) {
LOG_INFO << "Campaign: " << c.name;
LOG_INFO << "Campaign id: " << c.id;
Expand Down
2 changes: 1 addition & 1 deletion src/uptane_generator/repo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ void Repo::generateCampaigns() const {
c.estPreparationDuration = 20;

Json::Value json;
campaign::JsonFromCampaigns(campaigns, json);
campaign::Campaign::JsonFromCampaigns(campaigns, json);

Utils::writeFile(path_ / "campaigns.json", Utils::jsonToCanonicalStr(json));
}
Expand Down

0 comments on commit 11681c2

Please sign in to comment.