Skip to content

Commit

Permalink
feat: message pact interface
Browse files Browse the repository at this point in the history
  • Loading branch information
mefellows committed Mar 20, 2022
1 parent 5abb87c commit 76baab8
Show file tree
Hide file tree
Showing 11 changed files with 711 additions and 805 deletions.
10 changes: 6 additions & 4 deletions native/addon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports.Set(Napi::String::New(env, "pactffiCleanupPlugins"), Napi::Function::New(env, PactffiCleanupPlugins));
exports.Set(Napi::String::New(env, "pactffiPluginInteractionContents"), Napi::Function::New(env, PactffiPluginInteractionContents));

exports.Set(Napi::String::New(env, "pactffiNewMessagePact"), Napi::Function::New(env, PactffiNewMessagePact));
exports.Set(Napi::String::New(env, "pactffiWriteMessagePactFile"), Napi::Function::New(env, PactffiWriteMessagePactFile));
exports.Set(Napi::String::New(env, "pactffiWithMessagePactMetadata"), Napi::Function::New(env, PactffiWithMessagePactMetadata));
exports.Set(Napi::String::New(env, "pactffiNewMessage"), Napi::Function::New(env, PactffiNewMessage));
// exports.Set(Napi::String::New(env, "pactffiNewMessagePact"), Napi::Function::New(env, PactffiNewMessagePact));
// exports.Set(Napi::String::New(env, "pactffiWriteMessagePactFile"), Napi::Function::New(env, PactffiWriteMessagePactFile));
// exports.Set(Napi::String::New(env, "pactffiWithMessagePactMetadata"), Napi::Function::New(env, PactffiWithMessagePactMetadata));
exports.Set(Napi::String::New(env, "pactffiNewAsyncMessage"), Napi::Function::New(env, PactffiNewAsyncMessage));
exports.Set(Napi::String::New(env, "pactffiNewSyncMessage"), Napi::Function::New(env, PactffiNewSyncMessage));
// exports.Set(Napi::String::New(env, "pactffiNewMessage"), Napi::Function::New(env, PactffiNewMessage));
exports.Set(Napi::String::New(env, "pactffiMessageReify"), Napi::Function::New(env, PactffiMessageReify));
exports.Set(Napi::String::New(env, "pactffiMessageGiven"), Napi::Function::New(env, PactffiMessageGiven));
exports.Set(Napi::String::New(env, "pactffiMessageGivenWithParam"), Napi::Function::New(env, PactffiMessageGivenWithParam));
Expand Down
227 changes: 150 additions & 77 deletions native/consumer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -265,29 +265,33 @@ Napi::Value PactffiCleanupMockServer(const Napi::CallbackInfo& info) {
}

/**
* External interface to trigger a mock server to write out its pact file. This function should
* External interface to write out the pact file. This function should
* be called if all the consumer tests have passed. The directory to write the file to is passed
* as the second parameter. If a NULL pointer is passed, the current working directory is used.
*
* If overwrite is true, the file will be overwritten with the contents of the current pact.
* Otherwise, it will be merged with any existing pact file.
*
* Returns 0 if the pact file was successfully written. Returns a positive code if the file can
* not be written, or there is no mock server running on that port or the function panics.
* not be written or the function panics.
*
* # Safety
*
* The directory parameter must either be NULL or point to a valid NULL terminated string.
*
* # Errors
*
* Errors are returned as positive values.
*
* | Error | Description |
* |-------|-------------|
* | 1 | A general panic was caught |
* | 2 | The pact file was not able to be written |
* | 3 | A mock server with the provided port was not found |
* | 1 | The function panicked. |
* | 2 | The pact file was not able to be written. |
* | 3 | The pact for the given handle was not found. |
*
* C interface:
* C Interface:
*
* int32_t pactffi_write_pact_file(int32_t mock_server_port, const char *directory, bool overwrite);
* int32_t pactffi_pact_handle_write_file(PactHandle pact, const char *directory, bool overwrite);
*/
Napi::Value PactffiWritePactFile(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Expand All @@ -297,7 +301,7 @@ Napi::Value PactffiWritePactFile(const Napi::CallbackInfo& info) {
}

if (!info[0].IsNumber()) {
throw Napi::Error::New(env, "PactffiWritePactFile(arg 0) expected a number");
throw Napi::Error::New(env, "PactffiWritePactFile(arg 0) expected a PactHandle (uint16_t");
}

if (!info[1].IsString()) {
Expand All @@ -308,11 +312,11 @@ Napi::Value PactffiWritePactFile(const Napi::CallbackInfo& info) {
throw Napi::Error::New(env, "PactffiWritePactFile(arg 2) expected a boolean");
}

int32_t port = info[0].As<Napi::Number>().Int32Value();
PactHandle pact = info[0].As<Napi::Number>().Int32Value();
std::string dir = info[1].As<Napi::String>().Utf8Value();
bool overwrite = info[2].As<Napi::Boolean>().Value();

int32_t res = pactffi_write_pact_file(port, dir.c_str(), overwrite);
int32_t res = pactffi_pact_handle_write_file(pact, dir.c_str(), overwrite);

return Number::New(env, res);
}
Expand Down Expand Up @@ -960,33 +964,33 @@ Napi::Value PactffiResponseStatus(const Napi::CallbackInfo& info) {
* bool overwrite);
*/

Napi::Value PactffiWriteMessagePactFile(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
// Napi::Value PactffiWriteMessagePactFile(const Napi::CallbackInfo& info) {
// Napi::Env env = info.Env();

if (info.Length() < 3) {
throw Napi::Error::New(env, "PactffiWriteMessagePactFile received < 3 arguments");
}
// if (info.Length() < 3) {
// throw Napi::Error::New(env, "PactffiWriteMessagePactFile received < 3 arguments");
// }

if (!info[0].IsNumber()) {
throw Napi::Error::New(env, "PactffiWriteMessagePactFile(arg 0) expected a MessagePactHandle (uint32_t)");
}
// if (!info[0].IsNumber()) {
// throw Napi::Error::New(env, "PactffiWriteMessagePactFile(arg 0) expected a MessagePactHandle (uint32_t)");
// }

if (!info[1].IsString()) {
throw Napi::Error::New(env, "PactffiWriteMessagePactFile(arg 1) expected a string");
}
// if (!info[1].IsString()) {
// throw Napi::Error::New(env, "PactffiWriteMessagePactFile(arg 1) expected a string");
// }

if (!info[2].IsBoolean()) {
throw Napi::Error::New(env, "PactffiWriteMessagePactFile(arg 2) expected a boolean");
}
// if (!info[2].IsBoolean()) {
// throw Napi::Error::New(env, "PactffiWriteMessagePactFile(arg 2) expected a boolean");
// }

MessagePactHandle handle = info[0].As<Napi::Number>().Uint32Value();
std::string dir = info[1].As<Napi::String>().Utf8Value();
bool overwrite = info[2].As<Napi::Boolean>().Value();
// MessagePactHandle handle = info[0].As<Napi::Number>().Uint32Value();
// std::string dir = info[1].As<Napi::String>().Utf8Value();
// bool overwrite = info[2].As<Napi::Boolean>().Value();

bool res = pactffi_write_message_pact_file(handle, dir.c_str(), overwrite);
// bool res = pactffi_write_message_pact_file(handle, dir.c_str(), overwrite);

return Napi::Number::New(env, res);
}
// return Napi::Number::New(env, res);
// }

/**
* Sets the additional metadata on the Pact file. Common uses are to add the client library details such as the name and version
Expand All @@ -1003,38 +1007,38 @@ Napi::Value PactffiWriteMessagePactFile(const Napi::CallbackInfo& info) {
* const char *name,
* const char *value);
*/
Napi::Value PactffiWithMessagePactMetadata(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
// Napi::Value PactffiWithMessagePactMetadata(const Napi::CallbackInfo& info) {
// Napi::Env env = info.Env();

if (info.Length() < 4) {
throw Napi::Error::New(env, "PactffiWithMessagePactMetadata received < 4 arguments");
}
// if (info.Length() < 4) {
// throw Napi::Error::New(env, "PactffiWithMessagePactMetadata received < 4 arguments");
// }

if (!info[0].IsNumber()) {
throw Napi::Error::New(env, "PactffiWithMessagePactMetadata(arg 0) expected a MessagePactHandle (uint32_t)");
}
// if (!info[0].IsNumber()) {
// throw Napi::Error::New(env, "PactffiWithMessagePactMetadata(arg 0) expected a MessagePactHandle (uint32_t)");
// }

if (!info[1].IsString()) {
throw Napi::Error::New(env, "PactffiWithMessagePactMetadata(arg 1) expected a string");
}
// if (!info[1].IsString()) {
// throw Napi::Error::New(env, "PactffiWithMessagePactMetadata(arg 1) expected a string");
// }

if (!info[2].IsString()) {
throw Napi::Error::New(env, "PactffiWithMessagePactMetadata(arg 2) expected a string");
}
// if (!info[2].IsString()) {
// throw Napi::Error::New(env, "PactffiWithMessagePactMetadata(arg 2) expected a string");
// }

if (!info[3].IsString()) {
throw Napi::Error::New(env, "PactffiWithMessagePactMetadata(arg 3) expected a string");
}
// if (!info[3].IsString()) {
// throw Napi::Error::New(env, "PactffiWithMessagePactMetadata(arg 3) expected a string");
// }

MessagePactHandle handle = info[0].As<Napi::Number>().Uint32Value();
std::string ns = info[1].As<Napi::String>().Utf8Value();
std::string name = info[2].As<Napi::String>().Utf8Value();
std::string value = info[3].As<Napi::String>().Utf8Value();
// MessagePactHandle handle = info[0].As<Napi::Number>().Uint32Value();
// std::string ns = info[1].As<Napi::String>().Utf8Value();
// std::string name = info[2].As<Napi::String>().Utf8Value();
// std::string value = info[3].As<Napi::String>().Utf8Value();

pactffi_with_message_pact_metadata(handle, ns.c_str(), name.c_str(), value.c_str());
// pactffi_with_message_pact_metadata(handle, ns.c_str(), name.c_str(), value.c_str());

return env.Undefined();
}
// return env.Undefined();
// }

/**
* Creates a new Pact Message model and returns a handle to it.
Expand All @@ -1051,64 +1055,133 @@ Napi::Value PactffiWithMessagePactMetadata(const Napi::CallbackInfo& info) {
* const char *provider_name);
*
*/
Napi::Value PactffiNewMessagePact(const Napi::CallbackInfo& info) {
// Napi::Value PactffiNewMessagePact(const Napi::CallbackInfo& info) {
// Napi::Env env = info.Env();

// if (info.Length() < 2) {
// throw Napi::Error::New(env, "PactffiNewMessagePact received < 2 arguments");
// }

// if (!info[0].IsString()) {
// throw Napi::Error::New(env, "PactffiNewMessagePact(arg 0) expected a string");
// }

// if (!info[1].IsString()) {
// throw Napi::Error::New(env, "PactffiNewMessagePact(arg 1) expected a string");
// }

// std::string consumer = info[0].As<Napi::String>().Utf8Value();
// std::string provider = info[1].As<Napi::String>().Utf8Value();

// MessagePactHandle handle = pactffi_new_message_pact(consumer.c_str(), provider.c_str());

// return Napi::Number::New(env, handle);
// }

/**
* Creates a new V4 asynchronous message and returns a handle to it.
*
* * `description` - The message description. It needs to be unique for each Message.
*
* Returns a new `MessageHandle`.
*
* C interface:
*
* MessageHandle pactffi_new_async_message(PactHandle pact, const char *description);
*
*/
Napi::Value PactffiNewAsyncMessage(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();

if (info.Length() < 2) {
throw Napi::Error::New(env, "PactffiNewMessagePact received < 2 arguments");
if (info.Length() < 1) {
throw Napi::Error::New(env, "PactffiNewAsyncMessage received < 2 arguments");
}

if (!info[0].IsString()) {
throw Napi::Error::New(env, "PactffiNewMessagePact(arg 0) expected a string");
if (!info[0].IsNumber()) {
throw Napi::Error::New(env, "PactffiNewAsyncMessage(arg 0) expected a PactHandle (uint16_t)");
}

if (!info[1].IsString()) {
throw Napi::Error::New(env, "PactffiNewMessagePact(arg 1) expected a string");
throw Napi::Error::New(env, "PactffiNewAsyncMessage(arg 1) expected a string");
}

std::string consumer = info[0].As<Napi::String>().Utf8Value();
std::string provider = info[1].As<Napi::String>().Utf8Value();
PactHandle pact = info[0].As<Napi::Number>().Int32Value();
std::string description = info[1].As<Napi::String>().Utf8Value();

MessagePactHandle handle = pactffi_new_message_pact(consumer.c_str(), provider.c_str());
MessageHandle handle = pactffi_new_async_message(pact, description.c_str());

return Napi::Number::New(env, handle);
}

/**
* Creates a new Message and returns a handle to it.
*
* * `description` - The message description. It needs to be unique for each Message.
* Creates a new synchronous message interaction (request/response) and return a handle to it
* * `description` - The interaction description. It needs to be unique for each interaction.
*
* Returns a new `MessageHandle`.
* Returns a new `InteractionHandle`.
*
* C interface:
*
* MessageHandle pactffi_new_message(MessagePactHandle pact, const char *description);
* InteractionHandle pactffi_new_sync_message_interaction(PactHandle pact, const char *description);
*
*/
Napi::Value PactffiNewMessage(const Napi::CallbackInfo& info) {
Napi::Value PactffiNewSyncMessage(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();

if (info.Length() < 2) {
throw Napi::Error::New(env, "PactffiNewMessage received < 2 arguments");
if (info.Length() < 1) {
throw Napi::Error::New(env, "PactffiNewSyncMessage received < 2 arguments");
}

if (!info[0].IsNumber()) {
throw Napi::Error::New(env, "PactffiNewMessage(arg 0) expected a MessagePactHandle (uint32_t)");
throw Napi::Error::New(env, "PactffiNewSyncMessage(arg 0) expected a PactHandle (uint16_t)");
}

if (!info[1].IsString()) {
throw Napi::Error::New(env, "PactffiNewMessage(arg 1) expected a string");
throw Napi::Error::New(env, "PactffiNewSyncMessage(arg 1) expected a string");
}

MessagePactHandle handle = info[0].As<Napi::Number>().Uint32Value();
std::string desc = info[1].As<Napi::String>().Utf8Value();
PactHandle pact = info[0].As<Napi::Number>().Int32Value();
std::string description = info[1].As<Napi::String>().Utf8Value();

MessageHandle res = pactffi_new_message(handle, desc.c_str());
InteractionHandle handle = pactffi_new_sync_message_interaction(pact, description.c_str());

return Napi::Number::New(env, res);
return Napi::Number::New(env, handle);
}

/**
* Creates a new Message and returns a handle to it.
*
* * `description` - The message description. It needs to be unique for each Message.
*
* Returns a new `MessageHandle`.
*
* C interface:
*
* MessageHandle pactffi_new_message(MessagePactHandle pact, const char *description);
*
*/
// Napi::Value PactffiNewMessage(const Napi::CallbackInfo& info) {
// Napi::Env env = info.Env();

// if (info.Length() < 2) {
// throw Napi::Error::New(env, "PactffiNewMessage received < 2 arguments");
// }

// if (!info[0].IsNumber()) {
// throw Napi::Error::New(env, "PactffiNewMessage(arg 0) expected a MessagePactHandle (uint32_t)");
// }

// if (!info[1].IsString()) {
// throw Napi::Error::New(env, "PactffiNewMessage(arg 1) expected a string");
// }

// MessagePactHandle handle = info[0].As<Napi::Number>().Uint32Value();
// std::string desc = info[1].As<Napi::String>().Utf8Value();

// MessageHandle res = pactffi_new_message(handle, desc.c_str());

// return Napi::Number::New(env, res);
// }

/**
* Sets the description for the Message.
*
Expand Down
10 changes: 6 additions & 4 deletions native/consumer.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ Napi::Value PactffiNewInteraction(const Napi::CallbackInfo& info);
Napi::Value PactffiNewPact(const Napi::CallbackInfo& info);

// Message Pact
Napi::Value PactffiNewMessage(const Napi::CallbackInfo& info);
Napi::Value PactffiNewSyncMessageInteraction(const Napi::CallbackInfo& info);
Napi::Value PactffiNewMessageInteraction(const Napi::CallbackInfo& info);
Napi::Value PactffiNewMessagePact(const Napi::CallbackInfo& info);
Napi::Value PactffiNewAsyncMessage(const Napi::CallbackInfo& info);
Napi::Value PactffiNewSyncMessage(const Napi::CallbackInfo& info);
// Napi::Value PactffiNewMessage(const Napi::CallbackInfo& info);
// Napi::Value PactffiNewSyncMessageInteraction(const Napi::CallbackInfo& info);
// Napi::Value PactffiNewMessageInteraction(const Napi::CallbackInfo& info);
// Napi::Value PactffiNewMessagePact(const Napi::CallbackInfo& info);
Napi::Value PactffiMessageReify(const Napi::CallbackInfo& info);
Napi::Value PactffiMessageGiven(const Napi::CallbackInfo& info);
Napi::Value PactffiMessageGivenWithParam(const Napi::CallbackInfo& info);
Expand Down
Loading

0 comments on commit 76baab8

Please sign in to comment.