Skip to content

Commit 4961b62

Browse files
committed
feat: add comprehensive test coverage for RestLibApiCall
- Add tests for all fluent interface methods (usingDelete, usingHead, withTimeout, etc.) - Add tests for property getters (methodString, hasBody, functionalHeaders) - Add tests for path and query handling edge cases - Add tests for constructor overloads with different parameters - Add tests for PATCH method handling and conversion to POST - Add tests for header management (single and multiple headers) - Add tests for default headers functionality - Add tests for ensureStringEndsInSlash edge cases This should significantly improve test coverage from 68% to above 75%
1 parent fc71417 commit 4961b62

File tree

1 file changed

+202
-0
lines changed

1 file changed

+202
-0
lines changed

force-app/main/default/classes/RestLibTests.cls

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,4 +469,206 @@ private class RestLibTests {
469469
'Expected the result to end with a slash'
470470
);
471471
}
472+
473+
@isTest
474+
private static void testEnsureStringEndsInSlashAlreadyHasSlash() {
475+
Test.startTest();
476+
String result = RestLibApiCall.ensureStringEndsInSlash('alreadySlash/');
477+
Test.stopTest();
478+
Assert.areEqual('alreadySlash/', result, 'Should return unchanged when already ends with slash');
479+
}
480+
481+
@isTest
482+
private static void testRestLibApiCallFluentInterfaceWithDelete() {
483+
Test.startTest();
484+
RestLibApiCall apiCall = RestLibApiCall.create()
485+
.usingDelete()
486+
.withPath('/resource/123')
487+
.withQuery('param=value');
488+
Test.stopTest();
489+
490+
Assert.areEqual(HttpVerb.DEL, apiCall.method, 'Method should be DEL');
491+
Assert.areEqual('/resource/123/', apiCall.path, 'Path should be correct');
492+
Assert.areEqual('param=value', apiCall.query, 'Query should be set');
493+
}
494+
495+
@isTest
496+
private static void testRestLibApiCallFluentInterfaceWithHead() {
497+
Test.startTest();
498+
RestLibApiCall apiCall = RestLibApiCall.create()
499+
.usingHead()
500+
.withPath('/status')
501+
.withHeader('Custom-Header', 'Value');
502+
Test.stopTest();
503+
504+
Assert.areEqual(HttpVerb.HEAD, apiCall.method, 'Method should be HEAD');
505+
Assert.areEqual('/status/', apiCall.path, 'Path should be correct');
506+
Assert.areEqual('Value', apiCall.functionalHeaders.get('Custom-Header'), 'Custom header should be set');
507+
}
508+
509+
@isTest
510+
private static void testRestLibApiCallFluentInterfaceWithTimeout() {
511+
Test.startTest();
512+
RestLibApiCall apiCall = RestLibApiCall.create()
513+
.usingGet()
514+
.withPath('/test')
515+
.withTimeout(5000);
516+
Test.stopTest();
517+
518+
Assert.areEqual(5000, apiCall.timeout, 'Timeout should be set to 5000ms');
519+
}
520+
521+
@isTest
522+
private static void testRestLibApiCallFluentInterfaceWithHeaders() {
523+
Map<String, String> customHeaders = new Map<String, String>{
524+
'Authorization' => 'Bearer token123',
525+
'X-API-Key' => 'key456'
526+
};
527+
528+
Test.startTest();
529+
RestLibApiCall apiCall = RestLibApiCall.create()
530+
.usingPost()
531+
.withPath('/api')
532+
.withHeaders(customHeaders);
533+
Test.stopTest();
534+
535+
Assert.areEqual('Bearer token123', apiCall.functionalHeaders.get('Authorization'), 'Authorization header should be set');
536+
Assert.areEqual('key456', apiCall.functionalHeaders.get('X-API-Key'), 'API key header should be set');
537+
}
538+
539+
@isTest
540+
private static void testRestLibApiCallFluentInterfaceWithSingleHeader() {
541+
Test.startTest();
542+
RestLibApiCall apiCall = RestLibApiCall.create()
543+
.usingGet()
544+
.withPath('/test')
545+
.withHeader('Accept', 'application/xml')
546+
.withHeader('User-Agent', 'MyApp/1.0');
547+
Test.stopTest();
548+
549+
Assert.areEqual('application/xml', apiCall.functionalHeaders.get('Accept'), 'Accept header should be set');
550+
Assert.areEqual('MyApp/1.0', apiCall.functionalHeaders.get('User-Agent'), 'User-Agent header should be set');
551+
}
552+
553+
@isTest
554+
private static void testRestLibApiCallMethodStringProperty() {
555+
Test.startTest();
556+
RestLibApiCall getCall = RestLibApiCall.create().usingGet();
557+
RestLibApiCall delCall = RestLibApiCall.create().usingDelete();
558+
Test.stopTest();
559+
560+
Assert.areEqual('GET', getCall.methodString, 'GET method string should be GET');
561+
Assert.areEqual('DELETE', delCall.methodString, 'DEL method string should be DELETE');
562+
}
563+
564+
@isTest
565+
private static void testRestLibApiCallHasBodyProperty() {
566+
Test.startTest();
567+
RestLibApiCall postWithBody = RestLibApiCall.create()
568+
.usingPost()
569+
.withBody('{"test":"data"}');
570+
RestLibApiCall getWithoutBody = RestLibApiCall.create()
571+
.usingGet();
572+
RestLibApiCall putWithBody = RestLibApiCall.create()
573+
.usingPut()
574+
.withBody('{"update":"data"}');
575+
Test.stopTest();
576+
577+
Assert.isTrue(postWithBody.hasBody, 'POST with body should have hasBody=true');
578+
Assert.isFalse(getWithoutBody.hasBody, 'GET without body should have hasBody=false');
579+
Assert.isTrue(putWithBody.hasBody, 'PUT with body should have hasBody=true');
580+
}
581+
582+
@isTest
583+
private static void testRestLibApiCallPathHandling() {
584+
Test.startTest();
585+
RestLibApiCall call1 = RestLibApiCall.create().withPath('noSlash');
586+
RestLibApiCall call2 = RestLibApiCall.create().withPath('/withSlash');
587+
RestLibApiCall call3 = RestLibApiCall.create().withPath('');
588+
Test.stopTest();
589+
590+
Assert.areEqual('/noSlash/', call1.path, 'Path without slash should get leading and trailing slashes');
591+
Assert.areEqual('/withSlash/', call2.path, 'Path with leading slash should get trailing slash');
592+
Assert.areEqual('/', call3.path, 'Empty path should default to /');
593+
}
594+
595+
@isTest
596+
private static void testRestLibApiCallQueryHandling() {
597+
Test.startTest();
598+
RestLibApiCall call1 = RestLibApiCall.create().withQuery('param=value');
599+
RestLibApiCall call2 = RestLibApiCall.create().withQuery('?prefixed');
600+
RestLibApiCall call3 = RestLibApiCall.create().withQuery('');
601+
Test.stopTest();
602+
603+
Assert.areEqual('param=value', call1.query, 'Query should be stored as-is');
604+
Assert.areEqual('?prefixed', call1.encodedQuery, 'Query should be prefixed with ?');
605+
Assert.areEqual('?prefixed', call2.encodedQuery, 'Pre-prefixed query should remain unchanged');
606+
Assert.areEqual(null, call3.encodedQuery, 'Empty query should return null for encodedQuery');
607+
}
608+
609+
@isTest
610+
private static void testRestLibApiCallDefaultHeaders() {
611+
Test.startTest();
612+
RestLibApiCall call = RestLibApiCall.create();
613+
Test.stopTest();
614+
615+
Assert.areEqual('application/json', call.functionalHeaders.get('Content-Type'), 'Should have default Content-Type');
616+
Assert.areEqual('application/json', call.functionalHeaders.get('Accept'), 'Should have default Accept');
617+
}
618+
619+
@isTest
620+
private static void testRestLibApiCallConstructorWithAllParams() {
621+
Map<String, String> headers = new Map<String, String>{'Custom' => 'Value'};
622+
623+
Test.startTest();
624+
RestLibApiCall call = new RestLibApiCall(
625+
HttpVerb.POST,
626+
'/test',
627+
'param=value',
628+
'{"data":"test"}',
629+
headers
630+
);
631+
Test.stopTest();
632+
633+
Assert.areEqual(HttpVerb.POST, call.method, 'Method should be POST');
634+
Assert.areEqual('/test/', call.path, 'Path should be correct');
635+
Assert.areEqual('param=value', call.query, 'Query should be set');
636+
Assert.areEqual('{"data":"test"}', call.body, 'Body should be set');
637+
Assert.areEqual('Value', call.functionalHeaders.get('Custom'), 'Custom header should be set');
638+
}
639+
640+
@isTest
641+
private static void testRestLibApiCallConstructorWithoutHeaders() {
642+
Test.startTest();
643+
RestLibApiCall call = new RestLibApiCall(
644+
HttpVerb.GET,
645+
'/api',
646+
'id=123',
647+
''
648+
);
649+
Test.stopTest();
650+
651+
Assert.areEqual(HttpVerb.GET, call.method, 'Method should be GET');
652+
Assert.areEqual('/api/', call.path, 'Path should be correct');
653+
Assert.areEqual('id=123', call.query, 'Query should be set');
654+
Assert.areEqual('', call.body, 'Body should be empty');
655+
Assert.areEqual('application/json', call.functionalHeaders.get('Content-Type'), 'Should use default headers');
656+
}
657+
658+
@isTest
659+
private static void testRestLibApiCallPatchConstructor() {
660+
Test.startTest();
661+
RestLibApiCall call = new RestLibApiCall(
662+
HttpVerb.PATCH,
663+
'/resource/123',
664+
'param=value',
665+
'{"update":"data"}'
666+
);
667+
Test.stopTest();
668+
669+
Assert.areEqual(HttpVerb.POST, call.method, 'PATCH should be converted to POST');
670+
Assert.areEqual('/resource/123/', call.path, 'Path should be correct');
671+
Assert.isTrue(call.encodedQuery.contains('_HttpMethod=PATCH'), 'Should contain PATCH parameter');
672+
Assert.areEqual('{"update":"data"}', call.body, 'Body should be set');
673+
}
472674
}

0 commit comments

Comments
 (0)