Skip to content

Commit f151cec

Browse files
committed
update test cases after merge
1 parent cd97b09 commit f151cec

17 files changed

Lines changed: 156 additions & 179 deletions

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ target_compile_definitions(mo_unit_tests PUBLIC
202202
MO_OVERRIDE_ALLOCATION=1
203203
MO_ENABLE_HEAP_PROFILER=1
204204
MO_HEAP_PROFILER_EXTERNAL_CONTROL=1
205+
MO_ENABLE_MOCK_SERVER=1
205206
CATCH_CONFIG_EXTERNAL_INTERFACES
206207
)
207208

src/MicroOcpp/Core/Connection.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ MO_Connection *mo_loopback_make() {
153153
MO_DBG_ERR("OOM");
154154
goto fail;
155155
}
156+
memset(connection, 0, sizeof(*connection));
156157

157158
connection->userData = reinterpret_cast<void*>(data);
158159
connection->sendTXT = MicroOcpp::LoopbackConnection::sendTXT;

src/MicroOcpp/Model/Metering/MeterValue.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ int MeterValue::getJsonCapacity(int ocppVersion, bool internalFormat) {
352352
if (internalFormat || ocppVersion == MO_OCPP_V201) {
353353
valueLen = 0;
354354
}
355-
valueLen = snprintf(nullptr, 0, PRId32, sv->valueInt);
355+
valueLen = snprintf(nullptr, 0, "%" PRId32, sv->valueInt);
356356
break;
357357
case SampledValue::Type::Float:
358358
if (internalFormat || ocppVersion == MO_OCPP_V201) {
@@ -485,7 +485,7 @@ bool MeterValue::toJson(Clock& clock, int ocppVersion, bool internalFormat, Json
485485
if (internalFormat || ocppVersion == MO_OCPP_V201) {
486486
svJson["value"] = sv->valueInt;
487487
} else {
488-
int ret = snprintf(valueBuf, sizeof(valueBuf), PRId32, sv->valueInt);
488+
int ret = snprintf(valueBuf, sizeof(valueBuf), "%" PRId32, sv->valueInt);
489489
if (ret < 0 || (size_t)ret >= sizeof(valueBuf)) {
490490
MO_DBG_ERR("serialization error");
491491
return false;

src/MicroOcpp/Model/SmartCharging/SmartChargingModel.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,20 @@ bool ChargingProfile::toJson(Clock& clock, int ocppVersion, JsonObject out) {
699699
out["validTo"] = validToStr;
700700
}
701701

702-
JsonObject chargingScheduleJson = out.createNestedObject("chargingSchedule");
702+
JsonObject chargingScheduleJson;
703+
704+
#if MO_ENABLE_V16
705+
if (ocppVersion == MO_OCPP_V16) {
706+
chargingScheduleJson = out.createNestedObject("chargingSchedule");
707+
}
708+
#endif //MO_ENABLE_V16
709+
#if MO_ENABLE_V201
710+
if (ocppVersion == MO_OCPP_V201) {
711+
JsonArray chargingScheduleArray = out.createNestedArray("chargingSchedule");
712+
chargingScheduleJson = chargingScheduleArray.createNestedObject();
713+
}
714+
#endif //MO_ENABLE_V201
715+
703716
if (!chargingSchedule.toJson(clock, ocppVersion, /*compositeSchedule*/ false, chargingScheduleJson)) {
704717
return false;
705718
}

src/MicroOcpp/Model/SmartCharging/SmartChargingService.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,9 +789,9 @@ bool SmartChargingService::loadProfiles() {
789789
}
790790

791791
if (!valid) {
792-
success = false;
793792
MO_DBG_ERR("profile corrupt: %s, remove", fname);
794793
filesystem->remove(path);
794+
// restored valid state - continue
795795
}
796796
}
797797
}

tests/Boot.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ TEST_CASE( "Boot Behavior" ) {
9797

9898
REQUIRE( isOperative() ); //normal BN succeeded
9999

100-
loopback.setConnected( false );
100+
mo_loopback_setConnected(mo_getContext()->getConnection(), false);
101101

102102
beginTransaction_authorized("mIdTag");
103103

@@ -107,7 +107,7 @@ TEST_CASE( "Boot Behavior" ) {
107107

108108
mocpp_deinitialize();
109109

110-
loopback.setConnected( true );
110+
mo_loopback_setConnected(mo_getContext()->getConnection(), true);
111111

112112
MO_DBG_INFO("Start charger again with queued transaction messages, also init non-tx-related msg, but now delay BN procedure");
113113

@@ -240,7 +240,7 @@ TEST_CASE( "Boot Behavior" ) {
240240

241241
//start one transaction in full offline mode
242242

243-
loopback.setConnected( false );
243+
mo_loopback_setConnected(mo_getContext()->getConnection(), false);
244244
loop();
245245
REQUIRE( getChargePointStatus() == ChargePointStatus_Available );
246246

@@ -270,7 +270,7 @@ TEST_CASE( "Boot Behavior" ) {
270270
});
271271
});
272272

273-
loopback.setConnected( true );
273+
mo_loopback_setConnected(mo_getContext()->getConnection(), true);
274274
loop();
275275
REQUIRE( startTxCount == 0 );
276276

tests/Certificates.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ TEST_CASE( "M - Certificates" ) {
6969

7070
//initialize Context with dummy socket
7171
mo_initialize();
72-
73-
LoopbackConnection loopback;
74-
mo_getContext()->setConnection(&loopback);
72+
mo_useMockServer();
7573

7674
auto ocppVersion = GENERATE(MO_OCPP_V16, MO_OCPP_V201);
7775
mo_setOcppVersion(ocppVersion);

tests/ChargingSessions.cpp

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,11 @@ TEST_CASE( "Charging sessions" ) {
5757

5858
sentOperations.clear();
5959

60-
//initialize Context without any configs
60+
// Initialize Context without any configs and enable mock server
6161
mo_initialize();
62+
mo_useMockServer();
6263

63-
mo_getContext()->setTicksCb(custom_timer_cb);
64-
65-
LoopbackConnection loopback;
66-
mo_getContext()->setConnection(&loopback);
64+
mo_setTicksCb(custom_timer_cb);
6765

6866
auto ocppVersion = GENERATE(MO_OCPP_V16, MO_OCPP_V201);
6967
mo_setOcppVersion(ocppVersion);
@@ -306,7 +304,7 @@ TEST_CASE( "Charging sessions" ) {
306304

307305
SECTION("Preboot transactions - tx before BootNotification") {
308306

309-
loopback.setOnline(false);
307+
mo_loopback_setOnline(mo_getContext()->getConnection(), false);
310308

311309
mo_setVarConfigBool(mo_getApiContext(), "CustomizationCtrlr", "PreBootTransactions", MO_CONFIG_EXT_PREFIX "PreBootTransactions", true);
312310

@@ -330,7 +328,7 @@ TEST_CASE( "Charging sessions" ) {
330328

331329
mo_setUnixTime(BASE_TIME_UNIX);
332330

333-
loopback.setOnline(true);
331+
mo_loopback_setOnline(mo_getContext()->getConnection(), true);
334332
loop();
335333

336334
const char *startTimeStr = nullptr;
@@ -375,7 +373,7 @@ TEST_CASE( "Charging sessions" ) {
375373

376374
mocpp_deinitialize();
377375

378-
loopback.setOnline(false);
376+
mo_loopback_setOnline(mo_getContext()->getConnection(), false);
379377
mocpp_initialize(loopback, ChargerCredentials("test-runner1234"));
380378

381379
declareConfiguration<bool>(MO_CONFIG_EXT_PREFIX "PreBootTransactions", true, CONFIGURATION_FN)->setBool(true);
@@ -405,7 +403,7 @@ TEST_CASE( "Charging sessions" ) {
405403
checkProcessed = true;
406404
});
407405

408-
loopback.setOnline(true);
406+
mo_loopback_setOnline(mo_getContext()->getConnection(), true);
409407

410408
loop();
411409

@@ -426,7 +424,7 @@ TEST_CASE( "Charging sessions" ) {
426424

427425
mocpp_deinitialize();
428426

429-
loopback.setOnline(false);
427+
mo_loopback_setOnline(mo_getContext()->getConnection(), false);
430428
mocpp_initialize(loopback, ChargerCredentials("test-runner1234"));
431429

432430
declareConfiguration<bool>(MO_CONFIG_EXT_PREFIX "PreBootTransactions", true, CONFIGURATION_FN)->setBool(true);
@@ -464,7 +462,7 @@ TEST_CASE( "Charging sessions" ) {
464462
REQUIRE(adjustmentDelay == 1);
465463
});
466464

467-
loopback.setOnline(true);
465+
mo_loopback_setOnline(mo_getContext()->getConnection(), true);
468466

469467
loop();
470468

@@ -474,7 +472,7 @@ TEST_CASE( "Charging sessions" ) {
474472
SECTION("Preboot transactions - reject tx if limit exceeded") {
475473
mocpp_deinitialize();
476474

477-
loopback.setConnected(false);
475+
mo_loopback_setConnected(mo_getContext()->getConnection(), false);
478476
mocpp_initialize(loopback, ChargerCredentials("test-runner1234"));
479477

480478
declareConfiguration<bool>(MO_CONFIG_EXT_PREFIX "PreBootTransactions", true, CONFIGURATION_FN)->setBool(true);
@@ -541,7 +539,7 @@ TEST_CASE( "Charging sessions" ) {
541539
return createEmptyDocument();
542540
});});
543541

544-
loopback.setConnected(true);
542+
mo_loopback_setConnected(mo_getContext()->getConnection(), true);
545543
loop();
546544

547545
REQUIRE( txId_confirm == txId_base + MO_TXRECORD_SIZE );
@@ -550,7 +548,7 @@ TEST_CASE( "Charging sessions" ) {
550548
SECTION("Preboot transactions - charge without tx if limit exceeded") {
551549
mocpp_deinitialize();
552550

553-
loopback.setConnected(false);
551+
mo_loopback_setConnected(mo_getContext()->getConnection(), false);
554552
mocpp_initialize(loopback, ChargerCredentials("test-runner1234"));
555553

556554
declareConfiguration<bool>(MO_CONFIG_EXT_PREFIX "PreBootTransactions", true, CONFIGURATION_FN)->setBool(true);
@@ -621,7 +619,7 @@ TEST_CASE( "Charging sessions" ) {
621619
return createEmptyDocument();
622620
});});
623621

624-
loopback.setConnected(true);
622+
mo_loopback_setConnected(mo_getContext()->getConnection(), true);
625623
loop();
626624

627625
REQUIRE( txId_confirm == txId_base + MO_TXRECORD_SIZE );
@@ -652,7 +650,7 @@ TEST_CASE( "Charging sessions" ) {
652650
loop();
653651

654652
// start Tx #1 (offline tx)
655-
loopback.setConnected(false);
653+
mo_loopback_setConnected(mo_getContext()->getConnection(), false);
656654

657655
MO_DBG_DEBUG("begin tx (%s)", tx1_idTag);
658656
beginTransaction(tx1_idTag);
@@ -726,7 +724,7 @@ TEST_CASE( "Charging sessions" ) {
726724
});});
727725

728726
// get online
729-
loopback.setConnected(true);
727+
mo_loopback_setConnected(mo_getContext()->getConnection(), true);
730728
loop();
731729

732730
// start Tx #4
@@ -980,7 +978,7 @@ TEST_CASE( "Charging sessions" ) {
980978
return createEmptyDocument();
981979
});});
982980

983-
loopback.setOnline(false);
981+
mo_loopback_setOnline(mo_getContext()->getConnection(), false);
984982

985983
REQUIRE( !ocppPermitsCharge() );
986984

@@ -994,7 +992,7 @@ TEST_CASE( "Charging sessions" ) {
994992

995993
mtime += 10 * 60 * 1000; //jump 10 minutes into future
996994

997-
loopback.setOnline(true);
995+
mo_loopback_setOnline(mo_getContext()->getConnection(), true);
998996
loop();
999997

1000998
REQUIRE( !checkProcessedStartTx );
@@ -1024,7 +1022,7 @@ TEST_CASE( "Charging sessions" ) {
10241022
checkProcessedStartTx = false;
10251023
checkProcessedStopTx = false;
10261024

1027-
loopback.setOnline(false);
1025+
mo_loopback_setOnline(mo_getContext()->getConnection(), false);
10281026

10291027
REQUIRE( !ocppPermitsCharge() );
10301028

@@ -1036,7 +1034,7 @@ TEST_CASE( "Charging sessions" ) {
10361034
loop();
10371035
REQUIRE( !ocppPermitsCharge() );
10381036

1039-
loopback.setOnline(true);
1037+
mo_loopback_setOnline(mo_getContext()->getConnection(), true);
10401038
loop();
10411039

10421040
REQUIRE( !checkProcessedStartTx );
@@ -1054,13 +1052,13 @@ TEST_CASE( "Charging sessions" ) {
10541052
REQUIRE( ocppPermitsCharge() );
10551053
REQUIRE( checkProcessedStartTx );
10561054

1057-
loopback.setOnline(false);
1055+
mo_loopback_setOnline(mo_getContext()->getConnection(), false);
10581056

10591057
endTransaction();
10601058
loop();
10611059
mtime += 10 * 60 * 1000; //jump 10 minutes into future
10621060

1063-
loopback.setOnline(true);
1061+
mo_loopback_setOnline(mo_getContext()->getConnection(), true);
10641062
loop();
10651063
REQUIRE( !checkProcessedStopTx );
10661064

tests/ConfigurationBehavior.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ TEST_CASE( "Configuration Behavior" ) {
150150
auto authorizationTimeoutInt = declareConfiguration<int>(MO_CONFIG_EXT_PREFIX "AuthorizationTimeout", 1);
151151
authorizationTimeoutInt->setInt(1); //try normal Authorize for 1s, then enter offline mode
152152

153-
loopback.setOnline(false); //connection loss
153+
mo_loopback_setOnline(mo_getContext()->getConnection(), false); //connection loss
154154

155155
SECTION("set true") {
156156
configBool->setBool(true);
@@ -178,7 +178,7 @@ TEST_CASE( "Configuration Behavior" ) {
178178
}
179179

180180
endTransaction();
181-
loopback.setOnline(true);
181+
mo_loopback_setOnline(mo_getContext()->getConnection(), true);
182182
}
183183

184184
#if MO_ENABLE_LOCAL_AUTH
@@ -195,7 +195,7 @@ TEST_CASE( "Configuration Behavior" ) {
195195
loopback.sendTXT(localListMsg, strlen(localListMsg));
196196
loop();
197197

198-
loopback.setOnline(false); //connection loss
198+
mo_loopback_setOnline(mo_getContext()->getConnection(), false); //connection loss
199199

200200
SECTION("set true - accepted idtag") {
201201
configBool->setBool(true);
@@ -214,15 +214,15 @@ TEST_CASE( "Configuration Behavior" ) {
214214

215215
REQUIRE(connector->getStatus() == ChargePointStatus_Preparing);
216216

217-
loopback.setOnline(true);
217+
mo_loopback_setOnline(mo_getContext()->getConnection(), true);
218218
mtime += 20000; //Authorize will be retried after a few seconds
219219
loop();
220220

221221
REQUIRE(connector->getStatus() == ChargePointStatus_Charging);
222222
}
223223

224224
endTransaction();
225-
loopback.setOnline(true);
225+
mo_loopback_setOnline(mo_getContext()->getConnection(), true);
226226
}
227227
#endif //MO_ENABLE_LOCAL_AUTH
228228

tests/Core.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,11 @@ TEST_CASE( "Time" ) {
1414

1515
//initialize Context without any configs
1616
mo_initialize();
17+
mo_useMockServer();
1718

1819
mtime = 0;
1920
mo_getContext()->setTicksCb(custom_timer_cb);
2021

21-
LoopbackConnection loopback;
22-
mo_getContext()->setConnection(&loopback);
23-
2422
mo_setup();
2523

2624
auto& clock = mo_getContext()->getClock();

0 commit comments

Comments
 (0)