Skip to content

Commit 4a3a80c

Browse files
author
a-pavlov
committed
Add dedicated netlicensing.h/cc with services
1 parent 3775710 commit 4a3a80c

File tree

8 files changed

+124
-59
lines changed

8 files changed

+124
-59
lines changed

client_demo/netlicensing_client_demo.cc

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,53 @@
11
#include <iostream>
2-
#include "netlicensing/context.h"
3-
#include "netlicensing/service.h"
4-
#include "netlicensing/exception.h"
5-
#include "netlicensing/product.h"
6-
#include "json/json.h"
2+
#include <random>
3+
#include <ctime>
4+
#include <sstream>
5+
#include "netlicensing/netlicensing.h"
76

87
int main(int argc, char* argv[]) {
98
using netlicensing::Product;
109
std::string license_number = "I2C3VN7NA-DEMO";
1110
if (argc > 1) {
1211
license_number = argv[1];
1312
}
14-
std::cout << "Hello, this is NetLicensing demo client\n";
1513

16-
std::cout << "Product endpoint " << netlicensing::endpoint<Product>() << std::endl;
14+
std::mt19937 gen;
15+
gen.seed(time(0));
16+
std::stringstream ss;
17+
ss << "P" << gen();
18+
std::string productNumber = ss.str();
1719

18-
// check context direct call
19-
std::list<std::pair<std::string, std::string> > params;
20-
params.push_back(std::make_pair("licenseeNumber", "1"));
21-
params.push_back(std::make_pair("licenseTemplateNumber", "1"));
22-
params.push_back(std::make_pair("active", "true"));
23-
params.push_back(std::make_pair("number", "1"));
24-
params.push_back(std::make_pair("name", "lic"));
20+
std::cout << "Hello, this is NetLicensing demo client\n";
21+
std::cout << "Product endpoint " << netlicensing::endpoint<Product>() << std::endl;
22+
std::cout << "Product test number " << productNumber << std::endl;
2523

2624
using netlicensing::Context;
2725
try {
2826
Context ctx;
2927
ctx.set_base_url("https://go.netlicensing.io/core/v2/rest/");
3028
ctx.set_username("demo");
31-
ctx.set_password("demo");
29+
ctx.set_password("demo");
3230

33-
// product section
31+
// product section
3432
netlicensing::Product p;
35-
p.name_ = "Test name";
36-
p.number_ = "Some number 5";
37-
netlicensing::Product newp = netlicensing::create(ctx, p);
33+
p.setName("Test name");
34+
p.setNumber(productNumber);
35+
netlicensing::Product newp = netlicensing::ProductService::create(ctx, p);
3836

39-
newp.name_ = "Updated name";
40-
netlicensing::Product newp2 = netlicensing::update(ctx, newp.number_, newp);
37+
newp.setName("Updated name");
38+
netlicensing::Product newp2 = netlicensing::ProductService::update(ctx, newp.getNumber(), newp);
4139

42-
std::list<netlicensing::Product> products = netlicensing::list<netlicensing::Product>(ctx, "");
40+
std::list<netlicensing::Product> products = netlicensing::ProductService::list(ctx, "");
4341
std::cout << "before delete products count " << products.size() << std::endl;
4442

45-
netlicensing::del<netlicensing::Product>(ctx, newp2.number_, false);
43+
netlicensing::ProductService::del(ctx, newp2.getNumber(), false);
4644

47-
products = netlicensing::list<netlicensing::Product>(ctx, "");
45+
products = netlicensing::ProductService::list(ctx, "");
4846
std::cout << "after delete products count " << products.size() << std::endl;
4947

5048
if (!license_number.empty()) {
5149
std::cout << "start validation for " << license_number << std::endl;
52-
std::list<netlicensing::ValidationResult> vres = netlicensing::validate(ctx, license_number);
50+
std::list<netlicensing::ValidationResult> vres = netlicensing::ValidationService::validate(ctx, license_number);
5351
std::cout << "got validation results: " << vres.size() << std::endl;
5452
for (auto val_res : vres) {
5553
std::cout << val_res.to_string() << std::endl;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#ifndef __NETLICENSING_H__
2+
#define __NETLICENSING_H__
3+
4+
#include "netlicensing/context.h"
5+
#include "netlicensing/service.h"
6+
#include "netlicensing/product.h"
7+
#include "netlicensing/exception.h"
8+
9+
namespace netlicensing {
10+
11+
class ValidationService {
12+
public:
13+
static std::list<ValidationResult> validate(Context& ctx, const std::string& licenseNumber,
14+
const std::string& productNumber = std::string(),
15+
const std::string& licenseeName = std::string());
16+
};
17+
18+
class ProductService {
19+
public:
20+
static Product create(Context& ctx, const Product&);
21+
static Product update(Context& ctx, const std::string& productNumber, const Product&);
22+
static void del(Context& ctx, const std::string& productNumber, bool forceCascade);
23+
static std::list<Product> list(Context& ctx, const std::string& filter);
24+
};
25+
26+
27+
}
28+
29+
#endif //__NETLICENSING_H__

include/netlicensing/product.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,29 @@ struct Discount : public FlatList<Discount> {
1414
};
1515

1616
struct Product : public Entity {
17+
public:
18+
typedef Discount PropertyType;
19+
private:
1720
std::string number_;
1821
std::string name_;
1922
bool lic_auto_create_;
20-
bool in_use_;
21-
22-
typedef Discount PropertyType;
23+
bool in_use_;
2324

2425
std::list<std::shared_ptr<Discount> > discounts_;
26+
public:
2527

2628
Product() : lic_auto_create_(false), in_use_(false) {}
2729

2830
void add_list(std::shared_ptr<PropertyType> ptr);
2931
void add_property(const std::string& name, const std::string& value);
3032
parameters_type to_parameters_list() const;
33+
34+
std::string getName() const { return name_; }
35+
std::string getNumber() const { return number_; }
36+
37+
void setName(const std::string& name) { name_ = name; }
38+
void setNumber(const std::string& number) { number_ = number; }
39+
std::list<std::shared_ptr<PropertyType> > getDiscounts() const { return discounts_; }
3140
};
3241

3342
}

include/netlicensing/service.h

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -84,27 +84,6 @@ std::list<T> list(Context& ctx, const std::string& filter) {
8484
return mp.items;
8585
}
8686

87-
inline std::list<ValidationResult> validate(Context& ctx,
88-
const std::string& licensee_number,
89-
const std::string& product_number = std::string(),
90-
const std::string& licensee_name = std::string()) {
91-
std::string endpoint = "licensee/" + escape_string(licensee_number) + "/validate";
92-
parameters_type params;
93-
if (!product_number.empty()) params.push_back(std::make_pair("productNumber", escape_string(product_number)));
94-
if (!licensee_name.empty()) params.push_back(std::make_pair("licenseeName", escape_string(licensee_name)));
95-
96-
long http_code;
97-
std::string res = ctx.get(endpoint, params, http_code);
98-
Mapper<ValidationResult> mp;
99-
traverse(mp, res);
100-
101-
if (http_code != 200) {
102-
throw RestException(mp.info_, http_code);
103-
}
104-
105-
return mp.items;
106-
}
107-
10887
};
10988

11089
#endif //__SERVICE_HPP__

include/netlicensing/validation_result.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,23 @@ struct ValidationProperty : public RecursiveList<ValidationProperty> {
2424
* main validation result
2525
*/
2626
struct ValidationResult {
27+
public:
28+
typedef ValidationProperty PropertyType;
29+
private:
2730
std::string product_module_number_;
2831
std::string product_module_name_;
2932
std::string licensing_model_;
3033

31-
typedef ValidationProperty PropertyType;
3234
std::list<std::shared_ptr<ValidationProperty> > properties_;
35+
public:
3336
void add_property(const std::string& name, const std::string& value);
3437
void add_list(std::shared_ptr<PropertyType> ptr);
3538
std::string to_string() const;
39+
40+
std::string getProductModuleNumber() const { return product_module_number_; }
41+
std::string getProductModuleName() const { return product_module_name_; }
42+
std::string getLicensingModel() const { return licensing_model_; }
43+
std::list<std::shared_ptr<ValidationProperty> > getProperties() const { return properties_; }
3644
};
3745

3846
}

src/netlicensing.cc

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include "netlicensing/netlicensing.h"
2+
3+
namespace netlicensing {
4+
5+
std::list<ValidationResult> ValidationService::validate(Context& ctx,
6+
const std::string& licensseNumber,
7+
const std::string& productNumber/* = std::string()*/,
8+
const std::string& licenseeName/* = std::string()*/) {
9+
std::string endpoint = "licensee/" + escape_string(licensseNumber) + "/validate";
10+
parameters_type params;
11+
if (!productNumber.empty()) params.push_back(std::make_pair("productNumber", escape_string(productNumber)));
12+
if (!licenseeName.empty()) params.push_back(std::make_pair("licenseeName", escape_string(licenseeName)));
13+
14+
long http_code;
15+
std::string res = ctx.get(endpoint, params, http_code);
16+
Mapper<ValidationResult> mp;
17+
traverse(mp, res);
18+
19+
if (http_code != 200) {
20+
throw RestException(mp.info_, http_code);
21+
}
22+
23+
return mp.items;
24+
};
25+
26+
Product ProductService::create(Context& ctx, const Product& product) {
27+
return netlicensing::create(ctx, product);
28+
}
29+
30+
Product ProductService::update(Context& ctx, const std::string& productNumber, const Product& product) {
31+
return netlicensing::update(ctx, productNumber, product);
32+
}
33+
34+
void ProductService::del(Context& ctx, const std::string& productNumber, bool forceCascade) {
35+
netlicensing::del<Product>(ctx, productNumber, forceCascade);
36+
}
37+
38+
std::list<Product> ProductService::list(Context& ctx, const std::string& filter) {
39+
return netlicensing::list<Product>(ctx, filter);
40+
}
41+
42+
}

tests/test_product.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ BOOST_AUTO_TEST_CASE(test_product_with_discount) {
1919
Mapper<Product> prod;
2020
traverse(prod, answer);
2121
BOOST_REQUIRE_EQUAL(1u, prod.items.size());
22-
BOOST_REQUIRE_EQUAL(1u, prod.items.back().discounts_.size());
23-
BOOST_CHECK_EQUAL("discount", prod.items.back().discounts_.back()->name_);
22+
BOOST_REQUIRE_EQUAL(1u, prod.items.back().getDiscounts().size());
23+
BOOST_CHECK_EQUAL("discount", prod.items.back().getDiscounts().back()->name_);
2424

25-
BOOST_CHECK_EQUAL("101", prod.items.front().number_);
26-
BOOST_CHECK_EQUAL("QTPro", prod.items.front().name_);
25+
BOOST_CHECK_EQUAL("101", prod.items.front().getNumber());
26+
BOOST_CHECK_EQUAL("QTPro", prod.items.front().getName());
2727
}
2828

2929
BOOST_AUTO_TEST_CASE(test_product_list) {

tests/test_validation.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ BOOST_AUTO_TEST_CASE(test_plain_validation_result) {
1919
Mapper<ValidationResult> vr;
2020
traverse(vr, answer);
2121
BOOST_REQUIRE_EQUAL(1u, vr.items.size());
22-
BOOST_CHECK_EQUAL(2u, vr.items.back().properties_.size());
22+
BOOST_CHECK_EQUAL(2u, vr.items.back().getProperties().size());
2323
BOOST_CHECK_EQUAL(0, vr.level_);
24-
BOOST_CHECK_EQUAL("MAAV03-DEMO", vr.items.back().product_module_number_);
24+
BOOST_CHECK_EQUAL("MAAV03-DEMO", vr.items.back().getProductModuleNumber());
2525
}
2626

2727
BOOST_AUTO_TEST_CASE(test_recursive_validation_result) {
@@ -31,10 +31,10 @@ BOOST_AUTO_TEST_CASE(test_recursive_validation_result) {
3131
Mapper<ValidationResult> vr;
3232
traverse(vr, answer);
3333
BOOST_REQUIRE_EQUAL(1u, vr.items.size());
34-
BOOST_CHECK_EQUAL(2u, vr.items.back().properties_.size());
35-
BOOST_CHECK_EQUAL(2u, vr.items.back().properties_.front()->nested_lists_.size());
34+
BOOST_CHECK_EQUAL(2u, vr.items.back().getProperties().size());
35+
BOOST_CHECK_EQUAL(2u, vr.items.back().getProperties().front()->nested_lists_.size());
3636
BOOST_CHECK_EQUAL(0, vr.level_);
37-
BOOST_CHECK_EQUAL("MAAV03-DEMO", vr.items.back().product_module_number_);
37+
BOOST_CHECK_EQUAL("MAAV03-DEMO", vr.items.back().getProductModuleNumber());
3838
}
3939

4040
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)