Pact for MessageQueue's : Sample Provider test in case of MessageQueues - messagebroker

I have written sample Pact test for MessageProvider by referring to sample example given in pact repo. Following is the consumer test which is generating the PACT json file for the message expected from Provider.
In case of API, to verify the PACT, I was able to do this using "pact-jvm-provider-maven" plugin. In this case the PACT is verified against the actual hosted API service of Provider.
My Question is, how in case of MessageQueue, PACT will be verified? Will a mock Queue gets created? or I need to publish a message to actual Queue and need to verify the PACT message against this message published to Queue.
Can someone explain how exactly it works?
Also please point me to sample code (example test) to be written at provider end to verify the message of MessageQueue.
Sample message (Consumer) test :
public class Inbound_Receiving_OpenMessageTest {
private byte[] receivingOpenLoadDetailsMessage;
#Rule
public MessagePactProviderRule mockProvider = new MessagePactProviderRule(this);
#Pact(provider = Configuration.ReceivingProviderOpen, consumer = Configuration.InboundConsumer)
public MessagePact createPact(MessagePactBuilder builder) {
PactDslJsonBody body = (PactDslJsonBody) new PactDslJsonBody()
.stringType("_id")
.object("delivery")
.stringType("deliveryNumber")
.closeObject()
.array("state")
.object()
.stringType("changeTime")
.stringValue("status", "OPEN")
.stringType("changeUser")
.closeObject()
.closeArray();
Map<String, String> metadata = new HashMap<String, String>();
metadata.put("contentType", "application/json");
return builder
.given("Receiving(Open) App State")
.expectsToReceive("Receiving Open Load details Test")
.withMetadata(metadata)
.withContent(body)
.toPact();
}
#Test
#PactVerification({Configuration.ReceivingProviderOpen, "Receiving(Open) App State"})
public void test() throws Exception {
Assert.assertNotNull(new String(receivingOpenLoadDetailsMessage));
LoadDetails openLoadDetails = null;
Gson gson = new GsonBuilder().create();
String entity = new String(receivingOpenLoadDetailsMessage);
openLoadDetails = gson.fromJson(entity, LoadDetails.class);
if(openLoadDetails.getDelivery().getDeliveryNumber() == null ||
openLoadDetails.getState().get(0).getChangeUser() == null ||
openLoadDetails.getState().get(0).getChangeTime() == null ||
openLoadDetails.getState().get(0).getStatus() == null){
Assert.fail("Either one of the field 'deliveryNumber' or 'changeTime' or 'status' or 'changeUser' is NULL");
}
}
public void setMessage(byte[] messageContents) {
receivingOpenLoadDetailsMessage = messageContents;
}
}

This blog post explains it in more detail.
Essentially, the idea is that if you can verify that the code that puts the message onto the queue conforms to the contract (the provider), and the code that handles the message from the queue also conforms to the contract (the consumer), you don't actually need a message queue to verify the contract.

Related

Mass Printing on Generic Inquiry

I have created a generic inquiry in Acumatica that returns work orders that are open. We know that there is a print action that can be applied however, we want to print out multiple instances of the work orders. So we do not have to print them individually. Is there currently a solution for mass printing or can someone point me in the right direction of maybe a work around? I know there is a mass action print all function that can be found however, it does not do anything.
Just like in any other action redirecting users to the generated report(s), the Service Orders' Print Service Order button throws a PXReportRequiredException to open generated report in new window:
public class ServiceOrderEntry : PXGraph<ServiceOrderEntry, FSServiceOrder>
{
...
public PXAction<FSServiceOrder> printServiceOrder;
[PXUIField]
[PXButton]
public virtual void PrintServiceOrder()
{
if (this.IsDirty) Actions.PressSave();
if (ServiceOrderRecords.Current == null) return;
Dictionary<string, string> serviceOrderParameters =
GetServiceOrderParameters(ServiceOrderRecords.Current, false);
if (serviceOrderParameters.Count != 0)
{
throw new PXReportRequiredException(serviceOrderParameters,
"SD641000", PXBaseRedirectException.WindowMode.NewWindow, string.Empty);
}
}
...
}
If you add Service Orders' Print Service Order action as a mass action to Generic Inquiry (as shown on the screenshots below), the GI mass action will generate and open Service Order report form only for the first selected Service Order. Processing of any consequent Service Order will not be possible due to the PXReportRequiredException thrown to show the report form generated for the first Service Order.
In order to merge Service Order report forms generated for several Service Orders into a single report, you should create a custom processing screen and use the AddSibling method on a PXReportRequiredException instance. The AddSibling method will append the reports generated for 2nd+ Service Order to the PXReportRequiredException instance initially created for the first processed Service Order. After all selected Service Orders has been processed, a single PXReportRequiredException will be thrown to redirect the user to Report Viewer displaying all generated reports at once.
public class PrintServiceOrderProcess : PXGraph<PrintServiceOrderProcess>
{
public PXCancel<FSServiceOrder> Cancel;
public PXProcessing<FSServiceOrder> ServiceOrderRecords;
public PrintServiceOrderProcess()
{
ServiceOrderRecords.SetProcessDelegate(list => PrintServiceOrders(list));
}
public static void PrintServiceOrders(IEnumerable<FSServiceOrder> list)
{
PXReportRequiredException ex = null;
foreach (var order in list)
{
Dictionary<string, string> parameters = new Dictionary<string, string>();
string srvOrdType = SharedFunctions
.GetFieldName<FSServiceOrder.srvOrdType>(true);
string refNbr = SharedFunctions
.GetFieldName<FSServiceOrder.refNbr>(true);
parameters[srvOrdType] = order.SrvOrdType;
parameters[refNbr] = order.RefNbr;
if (ex == null)
{
ex = new PXReportRequiredException(parameters, "SD641000", "SD641000");
}
else
{
ex.AddSibling("SD641000", parameters, false);
}
}
if (ex != null) throw ex;
}
}

Spock No thread-bound request found

I have created a simple util class UtilService.groovy and when I tried to run write test case for it.
class UtilService {
static transactional = true
def messageSource
HttpServletRequest getCurrentRequest() {
GrailsWebRequest webUtils = WebUtils.retrieveGrailsWebRequest()
def request = webUtils.getCurrentRequest()
return request
}
String getMessage(String code, Object[] args = null, String defaultMessage = null) {
HttpServletRequest request = currentRequest
Locale locale = request.locale
if (defaultMessage) {
return messageSource.getMessage(code, args, defaultMessage, locale)
} else {
return messageSource.getMessage(code, args, locale)
}
}
Test case
#TestMixin(GrailsUnitTestMixin)
class UtilServiceSpec extends Specification {
void "Test get message"() {
setup:
def utilService = new UtilService()
when:
String data = utilService.getMessage("payCode.label")
then:
data == "Pay Code"
}
}
Error:
No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread?
I have searched and try some links from google but they did not work for me.
Obviously, you are doing it outside of web request, WebUtils.retrieveGrailsWebRequest() will fail because there's no web request going on. If your service is called from a controller during tests it should work.
Instead of you service depending on WebRequest, why don't you pass the Locale as an argument to getMessage, controllers can call getMessage and pass the request Locale, or else the service will choose the default locale. Its generally not good idea to have services be aware of web apis (eg requests, response, session etc). And you will be able to test both service and controller.
I have written Integration test for this requirement rather than Unit test and able to run my test cases successfully.

How to propagate spring security context to JMS?

I have a web application which sets a spring security context through a spring filter. Services are protected with spring annotations based on users roles. This works.
Asynchronous tasks are executed in JMS listeners (extend javax.jms.MessageListener). The setup of this listeners is done with Spring.
Messages are sent from the web application, at this time a user is authenticated. I need the same authentication in the JMS thread (user and roles) during message processing.
Today this is done by putting the spring authentication in the JMS ObjectMessage:
SecurityContext context = SecurityContextHolder.getContext();
Authentication auth = context.getAuthentication();
... put the auth object in jms message object
Then inside the JMS listener the authentication object is extracted and set in the context:
SecurityContext context = new SecurityContextImpl();
context.setAuthentication(auth);
SecurityContextHolder.setContext(context);
This works most of the time. But when there is a delay before the processing of a message, message will never be processed. I couldn't determine yet the cause of these messages loss, but I'm not sure the way we propagate authentication is good, even if it works in custer when the message is processed in another server.
Is this the right way to propagate a spring authentication ?
Regards,
Mickaƫl
I did not find better solution, but this one works for me just fine.
By sending of JMS Message I'am storing Authentication as Header and respectively by receiving recreating Security Context. In order to store Authentication as Header you have to serialise it as Base64:
class AuthenticationSerializer {
static String serialize(Authentication authentication) {
byte[] bytes = SerializationUtils.serialize(authentication);
return DatatypeConverter.printBase64Binary(bytes);
}
static Authentication deserialize(String authentication) {
byte[] decoded = DatatypeConverter.parseBase64Binary(authentication);
Authentication auth = (Authentication) SerializationUtils.deserialize(decoded);
return auth;
}
}
By sending just set Message header - you can create Decorator for Message Template, so that it will happen automatically. In you decorator just call such method:
private void attachAuthenticationContext(Message message){
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
String serialized = AuthenticationSerializer.serialize(auth);
message.setStringProperty("authcontext", serialized);
}
Receiving gets more complicated, but it can be also done automatically. Instead of applying #EnableJMS use following Configuration:
#Configuration
class JmsBootstrapConfiguration {
#Bean(name = JmsListenerConfigUtils.JMS_LISTENER_ANNOTATION_PROCESSOR_BEAN_NAME)
#Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public JmsListenerAnnotationBeanPostProcessor jmsListenerAnnotationProcessor() {
return new JmsListenerPostProcessor();
}
#Bean(name = JmsListenerConfigUtils.JMS_LISTENER_ENDPOINT_REGISTRY_BEAN_NAME)
public JmsListenerEndpointRegistry defaultJmsListenerEndpointRegistry() {
return new JmsListenerEndpointRegistry();
}
}
class JmsListenerPostProcessor extends JmsListenerAnnotationBeanPostProcessor {
#Override
protected MethodJmsListenerEndpoint createMethodJmsListenerEndpoint() {
return new ListenerEndpoint();
}
}
class ListenerEndpoint extends MethodJmsListenerEndpoint {
#Override
protected MessagingMessageListenerAdapter createMessageListenerInstance() {
return new ListenerAdapter();
}
}
class ListenerAdapter extends MessagingMessageListenerAdapter {
#Override
public void onMessage(Message jmsMessage, Session session) throws JMSException {
propagateSecurityContext(jmsMessage);
super.onMessage(jmsMessage, session);
}
private void propagateSecurityContext(Message jmsMessage) throws JMSException {
String authStr = jmsMessage.getStringProperty("authcontext");
Authentication auth = AuthenticationSerializer.deserialize(authStr);
SecurityContextHolder.getContext().setAuthentication(auth);
}
}
I have implemented for myself a different solution, which seems easier for me.
Already I have a message converter, the standard JSON Jackson message converter, which I need to configure on the JMSTemplate and the listeners.
So I created a MessageConverter implementation which wraps around another message converter, and propagates the security context via the JMS message properties.
(In my case, the propagated context is a JWT token which I can extract from the current context and apply to the security context of the listening thread).
This way the entire responsibility for propagation of security context is elegantly implemented in a single class, and requires only a little bit of configuration.
Thanks great but I am handling this in easy way . put one util file and solved .
public class AuthenticationSerializerUtil {
public static final String AUTH_CONTEXT = "authContext";
public static String serialize(Authentication authentication) {
byte[] bytes = SerializationUtils.serialize(authentication);
return DatatypeConverter.printBase64Binary(bytes);
}
public static Authentication deserialize(String authentication) {
byte[] decoded = DatatypeConverter.parseBase64Binary(authentication);
Authentication auth = (Authentication) SerializationUtils.deserialize(decoded);
return auth;
}
/**
* taking message and return string json from message & set current context
* #param message
* #return
*/
public static String jsonAndSetContext(Message message){
LongString authContext = (LongString)message.getMessageProperties().getHeaders().get(AUTH_CONTEXT);
Authentication auth = deserialize(authContext.toString());
SecurityContextHolder.getContext().setAuthentication(auth);
byte json[] = message.getBody();
return new String(json);
}
}

ServiceStack authentication request fails

I am trying to set up authentication with my ServiceStack service by following this tutorial.
My service is decorated with the [Authenticate] attribute.
My AppHost looks like this:
public class TestAppHost : AppHostHttpListenerBase
{
public TestAppHost() : base("TestService", typeof(TestService).Assembly) { }
public static void ConfigureAppHost(IAppHost host, Container container)
{
try
{
// Set JSON web services to return idiomatic JSON camelCase properties.
ServiceStack.Text.JsConfig.EmitCamelCaseNames = true;
// Configure the IOC container
IoC.Configure(container);
// Configure ServiceStack authentication to use our custom authentication providers.
var appSettings = new AppSettings();
host.Plugins.Add(new AuthFeature(() =>
new AuthUserSession(), // use ServiceStack's session class but fill it with our own data using our own auth service provider
new IAuthProvider[] {
new UserCredentialsAuthProvider(appSettings)
}));
}
}
where UserCredentialsAuthProvider is my custom credentials provider:
public class UserCredentialsAuthProvider : CredentialsAuthProvider
{
public override bool TryAuthenticate(IServiceBase authService, string userName, string password)
{
try
{
// Authenticate the user.
var userRepo = authService.TryResolve<IUserRepository>();
var user = userRepo.Authenticate(userName, password);
// Populate session properties.
var session = authService.GetSession();
session.IsAuthenticated = true;
session.CreatedAt = DateTime.UtcNow;
session.DisplayName = user.FullName;
session.UserAuthName = session.UserName = user.Username;
session.UserAuthId = user.ID.ToString();
}
catch (Exception ex)
{
// ... Log exception ...
return false;
}
return true;
}
}
In my user tests I initialize and start my TestAppHost on http://127.0.0.1:8888, then use JsonServiceClient to authenticate itself to the service like so:
var client = new JsonServiceClient("http://127.0.0.1:8888/")
var response = client.Send<AuthResponse>(new Auth
{
provider = UserCredentialsAuthProvider.Name,
UserName = username,
Password = password,
RememberMe = true
});
But getting the following exception:
The remote server returned an error: (400) Bad Request.
at System.Net.HttpWebRequest.GetResponse()
at ServiceStack.ServiceClient.Web.ServiceClientBase.Send[TResponse](Object request)...
The ServiceStack.ServiceInterface.Auth.Auth request contains the correct username and passsword, and the request is being posted to:
http://127.0.0.1:8888/json/syncreply/Auth
I am not sure why the URL is not /json/auth/credentials or what I might be doing wrong. Any suggestions?
UPDATE
Tracing the chain of events up the stack I found the following:
JsonDataContractSerializer.SerializeToStream correctly serializes the Auth request into Json. However, the System.Net.HttpRequestStream passed to JsonDataContractDeserializer by EndpointHandlerBase has a stream of the correct length that is filled with nulls (zero bytes). As a result, the request object passed to CredentialsAuthProvider.Authenticate has nulls in all its properties.
How can the HTTP stream get stripped of its data?
Got it!!!
The problem was the following pre-request filter that I added for logging purposes in TestAppHost.Configure:
PreRequestFilters.Add((httpReq, httpRes) =>
{
LastRequestBody = httpReq.GetRawBody();
});
as seen here.
When the GetRawBody() method reads the request InputStream it leaves it in the EOS state, and all subsequent read attempts return nothing.
So obviously GetRawBody() can only be safely used with buffered streams, but unfortunately it quietly causes a very nasty bug instead of throwing an exception when used with a non-buffered stream.

In OpenRasta, how should you handle codec errors or exceptions?

My scenario is this:
A client application executes a HTTP POST against an endpoint exposed by OpenRasta.
The body of the request contains an error that causes a problem in the codec - which is a custom implementation of OpenRasta.Codecs.IMediaTypeReader. This converts a JSON payload to the POCO expected by the handler.
The codec throws an exception that describes the error in a useful way. For example: Newtonsoft.Json.JsonReaderException: After parsing a value an unexpected character was encountered: ". Line 4, position 5.
The client application receives a HTTP 405 - MethodNotAllowed. The client doesn't see any of the exception details.
If the codec is modified to catch a JsonReaderException and return Missing.Value, similar to the Implementing a codec wiki, then the client receives a HTTP 500 - Internal Server Error. The body of the response also describes the following exception:
System.InvalidOperationException: The operation is not ready for invocation.
at OpenRasta.OperationModel.MethodBased.MethodBasedOperation.Invoke()
at OpenRasta.OperationModel.Interceptors.OperationWithInterceptors.<Invoke>b__0()
at OpenRasta.OperationModel.Interceptors.OperationWithInterceptors.Invoke()
at OpenRasta.OperationModel.OperationExecutor.Execute(IEnumerable`1 operations)
at OpenRasta.Pipeline.Contributors.OperationInvokerContributor.ExecuteOperations(ICommunicationContext context)
at OpenRasta.Pipeline.PipelineRunner.ExecuteContributor(ICommunicationContext context, ContributorCall call)
How should I modify my application so that:
The client receives a HTTP 400 Bad Request.
The client receives a string containing the details of the exception encountered in the codec.
Here is a minor variation of the answer above - this time with codec selection based on the operation result data.
Within IConfigurationSource:
using (OpenRastaConfiguration.Manual)
{
ResourceSpace.Uses.PipelineContributor<ErrorCheckingContributor>();
ResourceSpace.Has.ResourcesOfType<ApplicationError>()
.WithoutUri
.TranscodedBy<ApplicationErrorCodec>();
// Or use a generic JSON serializer like this:
// .AsJsonDataContract();
// Other configuration here
}
Now ErrorCheckingContributor looks like this:
public class ErrorCheckingContributor : IPipelineContributor
{
public void Initialize(IPipeline pipelineRunner)
{
pipelineRunner
.Notify(CheckRequestDecoding)
.After<KnownStages.IOperationResultInvocation>()
.And.Before<KnownStages.ICodecResponseSelection>();
}
private static PipelineContinuation CheckRequestDecoding(ICommunicationContext context)
{
if (context.ServerErrors.Count == 0)
{
return PipelineContinuation.Continue;
}
Error err = context.ServerErrors[0];
// Get a suitable message (err.Message contains stack traces, so try to avoid that)
string msg = err.Title;
if (msg == null && err.Exception != null)
msg = err.Exception.Message;
if (msg == null)
msg = err.Message;
// Create instance of an error information resource which is specific for the application
// - This one is rather simple and only contains a copy of the message
ApplicationError error = new ApplicationError(msg);
// Set operation result to be "400 Bad Request" and remove errors
context.OperationResult = new OperationResult.BadRequest { ResponseResource = error };
context.ServerErrors.Clear();
// Render immediately without starting any handlers
return PipelineContinuation.RenderNow;
}
}
The class ApplicationError is:
public class ApplicationError
{
public string Message { get; set; }
public ApplicationError(string message)
{
Message = message;
}
}
At last we need a codec ApplicationErrorCodec for ApplicationError. This is not different from any other IMediaTypeWriter codec but depends a lot on your expected response media type. See https://github.com/openrasta/openrasta/wiki/Implementing-a-Codec for one example.
Having found this thread on Google Groups which contains all the answers, my current implementation looks something like this.
Within my implementation of IConfigurationSource:
using (OpenRastaConfiguration.Manual)
{
ResourceSpace.Uses.PipelineContributor<ErrorCheckingContributor>();
// Other configuration here
}
Then ErrorCheckingContributor looks something like this:
public class ErrorCheckingContributor : IPipelineContributor
{
public void Initialize(IPipeline pipelineRunner)
{
pipelineRunner
.Notify(CheckRequestDecoding)
.After<KnownStages.IOperationResultInvocation>()
.And.Before<KnownStages.ICodecResponseSelection>();
}
private static PipelineContinuation CheckRequestDecoding(ICommunicationContext context)
{
if (context.ServerErrors.Count == 0)
{
return PipelineContinuation.Continue;
}
var first = context.ServerErrors[0];
if (first.Exception is Newtonsoft.Json.JsonReaderException)
{
context.Response.Entity.ContentType = MediaType.TextPlain;
context.Response.Entity.ContentLength = first.Exception.Message.Length;
using (var sw = new StreamWriter(context.Response.Entity.Stream))
{
sw.Write(first.Exception.Message);
}
}
return PipelineContinuation.Continue;
}
}
There's some things to be aware of with the above:
If a handler were to throw a JsonReaderException, it would also be processed here.
It doesn't check what media types the client accepts. This is different from exceptions thrown by Handlers that do go through codec selection.
Tried setting context.OperationResult to context.ServerErrors - but it doesn't go through the codec.

Resources