microcks / microcks-testcontainers-java

Java lib for Testcontainers that enables embedding Microcks into your JUnit tests with lightweight, throwaway instance thanks to containers.
https://microcks.io
Apache License 2.0
21 stars 5 forks source link

Add a method to MicrocksContainer class to get invocations count of a service SOAP #91

Open pierrechristinimsa opened 1 week ago

pierrechristinimsa commented 1 week ago

Reason/Context

Hello, We use microcks-testcontainers-java to mock SOAP web services, importing SoapUI projects. It works well, but we lack of methods on MicrocksContainer class that could return the invocations count of a service SOAP. We saw this data is available in admin console, and is accessible via Metrics resource in REST API. So we would like to have this data easily available, we think it could be benefit for many. And why do we need to check this invocation count ?

To be sure the mock as been invoked by the application we are covering with integration tests. Indeed, sometimes we have SOAP WS that just return empty body. For instance : WS that just does an update. Without having this invocations count, we are not able to know whether our application had the correct behaviour or not.

In summary : it would be like what we do when we call the "verify" method of "Mockito".

Description

Add methods to MicrocksContainer class that could return the invocations count of a service SOAP.

Implementation ideas

This is a draft, using project microcks-java-client to have MetricsApi class :

    private static @NotNull BigDecimal getServiceInvocationsCount(String serviceName,
                                                        String serviceVersion,
                                                        @Nullable Date invocationDate) {
        ApiClient apiClient = new ApiClient();
        apiClient.updateBaseUri(getHttpEndpoint() + "/api");

        MetricsApi metricsApi = new MetricsApi(apiClient);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        final String stringifiedDate = (invocationDate != null) ? simpleDateFormat.format(invocationDate) : null;

        BigDecimal count = null;

        try {
            DailyInvocationStatistic dailyInvocationStatistic =
                    metricsApi.getInvocationStatsByService(serviceName, serviceVersion, stringifiedDate);

            count = dailyInvocationStatistic.getDailyCount();
        } catch (ApiException e) {
            throw new RuntimeException(e); // TODO Better exception handling
        }

        return count;
    }

We have already done the code and would like to create branch to push commit but it seems I am not allow to do it. @lbroudoux (or someone else) : could you please, give me the ability to push a commit on a new branch to submit a PR ?

We are opened to any suggestion, and we give thanks to the community for what you've done so far.

github-actions[bot] commented 1 week ago

👋 @pierrechristinimsa

Welcome to the Microcks community! 💖

Thanks and congrats 🎉 for opening your first issue here! Be sure to follow the issue template or please update it accordingly.

📢 If you're using Microcks in your organization, please add your company name to this list. 🙏 It really helps the project to gain momentum and credibility. It's a small contribution back to the project with a big impact.

If you need to know why and how to add yourself to the list, please read the blog post "Join the Microcks Adopters list and Empower the vibrant open source Community 🙌"

Hope you have a great time there!

🌟 ~~~~~ 🌟

📢 If you like Microcks, please ⭐ star ⭐ our repo to support it!

🙏 It really helps the project to gain momentum and credibility. It's a small contribution back to the project with a big impact.

lbroudoux commented 1 week ago

This looks super cool! Thanks for the detailed explanation, I think it makes sense to add this kind of feature.

I'm surprised you're not allowed to create a PR on this one. Some other people have done it in the past. You can directly target the main branch as the base of this PR from your cloned repository branch.

Let me know,

pierrechristinimsa commented 1 week ago

Thank you for your feedback @lbroudoux .

I tried by many ways with many computers and from different networks, it seems I have no write access to projects in microcks group. (I have tried to push a new branch to this project and to 'microcks' project, the same behaviour occurs.) image image

May you help me to have write access to this project, please ?

lbroudoux commented 1 week ago

This looks like normal behavior: you don't need to have write access to the repo for one-shot contribution. Have you forked the repository as advised?

The recommended process is as follows:

  1. You fork the repository to your organization or personal account,
  2. You clone this fork on your machine,
  3. You create a branch starting from main to isolate the changes related to this feature,
  4. You commit your changes and push the branch upstream on GitHub,
  5. You finally create a PR to ask the microcks organization to pull the changes from your branch and integrate them into our main branch.

So that's actually us that reads from your fork and propagates those changes into our repo.

Let me know if it helps,

pierrechristinimsa commented 1 week ago

ok that's clear, thank you and please excuse me for the inconvenience, you should have news from me in a few days.

lbroudoux commented 1 week ago

Awesome!

I was thinking: that integrating the Java client may be inconvenient for 2 different reasons:

  1. It may bring a lot of dependencies (I don't have checked this but it would be worth the pain to do so)
  2. It may bring issues as (if I remember correctly), the Java client is compiled with a Java 17 target and Testcontainers Java is compiled with a Java 8 target (this is a recommendation by the Testscontainers team).

Let us know if you have any clue or hints on those 2 points. Maybe, it would be easier to bypass the client and directly use low-level classes (getting the statistics might be a simple GET request ...)

pierrechristinimsa commented 1 week ago

You must be right, I was wondering if I should propose you two PRs : one using the java client and another one not using it. One issue we encounter for now is that the generated classes of the java client are not part of the generated microcks-testcontainers-java jar. It may not worth to use it, but it would have been interesting because making the code more concise.