Add test for Jobs response parsing
@@ -90,6 +90,7 @@ TEST_GROUP_RUNNER( Jobs_Unit_Serialize )
RUN_TEST_CASE( Jobs_Unit_Serialize, SerializeUpdateStatus );
RUN_TEST_CASE( Jobs_Unit_Serialize, SerializeUpdateNumbers );
RUN_TEST_CASE( Jobs_Unit_Serialize, SerializeUpdateFlags );
+ RUN_TEST_CASE( Jobs_Unit_Serialize, ParseErrorResponse );
}
/*-----------------------------------------------------------*/
@@ -727,3 +728,78 @@ TEST( Jobs_Unit_Serialize, SerializeUpdateFlags )
}
/*-----------------------------------------------------------*/
+
+/**
+ * @brief Tests parsing of Jobs error responses.
+ */
+TEST( Jobs_Unit_Serialize, ParseErrorResponse )
+{
+ size_t i = 0;
+ _jobsOperation_t operation = { .link = { 0 } };
+
+ /* Test for failure to allocate memory for response. */
+ operation.status = AWS_IOT_JOBS_STATUS_PENDING;
+ operation.flags = AWS_IOT_JOBS_FLAG_WAITABLE;
+ operation.mallocResponse = IotTest_Malloc;
+
+ UnityMalloc_MakeMallocFailAfterCount( 0 );
+
+ _AwsIotJobs_ParseResponse( AWS_IOT_ACCEPTED, "{}", 2, &operation );
+ TEST_ASSERT_EQUAL( AWS_IOT_JOBS_NO_MEMORY, operation.status );
+ UnityMalloc_MakeMallocFailAfterCount( -1 );
+ operation.flags = 0;
+
+ /* Test parsing of error responses. */
+ const char * pErrorResponses[] =
+ {
+ "{\"code\": \"InvalidTopic\"}",
+ "{\"code\": \"InvalidJson\"}",
+ "{\"code\": \"InvalidRequest\"}",
+ "{\"code\": \"InvalidStateTransition\"}",
+ "{\"code\": \"ResourceNotFound\"}",
+ "{\"code\": \"VersionMismatch\"}",
+ "{\"code\": \"InternalError\"}",
+ "{\"code\": \"RequestThrottled\"}",
+ "{\"code\": \"TerminalStateReached\"}"
+ };
+ const AwsIotJobsError_t pExpectedResponse[] =
+ {
+ AWS_IOT_JOBS_INVALID_TOPIC,
+ AWS_IOT_JOBS_INVALID_JSON,
+ AWS_IOT_JOBS_INVALID_REQUEST,
+ AWS_IOT_JOBS_INVALID_STATE,
+ AWS_IOT_JOBS_NOT_FOUND,
+ AWS_IOT_JOBS_VERSION_MISMATCH,
+ AWS_IOT_JOBS_INTERNAL_ERROR,
+ AWS_IOT_JOBS_THROTTLED,
+ AWS_IOT_JOBS_TERMINAL_STATE
+ };
+
+ for( i = 0; i < ( sizeof( pErrorResponses ) / sizeof( pErrorResponses[ 0 ] ) ); i++ )
+ {
+ operation.status = AWS_IOT_JOBS_STATUS_PENDING;
+
+ _AwsIotJobs_ParseResponse( AWS_IOT_REJECTED,
+ pErrorResponses[ i ],
+ strlen( pErrorResponses[ i ] ),
+ &operation );
+ TEST_ASSERT_EQUAL( pExpectedResponse[ i ], operation.status );
+ }
+
+ /* Test parsing of invalid code. */
+ operation.status = AWS_IOT_JOBS_STATUS_PENDING;
+ _AwsIotJobs_ParseResponse( AWS_IOT_REJECTED,
+ "{\"code\": \"NotAnErrorCodeNotAnErrorCode\"}",
+ 40,
+ &operation );
+ TEST_ASSERT_EQUAL( AWS_IOT_JOBS_BAD_RESPONSE, operation.status );
+
+ operation.status = AWS_IOT_JOBS_STATUS_PENDING;
+ _AwsIotJobs_ParseResponse( AWS_IOT_REJECTED,
+ "{}",
+ 2,
+ &operation );
+ TEST_ASSERT_EQUAL( AWS_IOT_JOBS_BAD_RESPONSE, operation.status );
+}
+
+/*-----------------------------------------------------------*/
