cancel
Showing results for 
Search instead for 
Did you mean: 

Override the port of a mock service using a global variable

Highlighted
Occasional Contributor

Override the port of a mock service using a global variable

Hello everyone,

 

I have created a project with two mock services. In the second mock service I am calling the first mock service using it's URL(I use the second service as a type of proxy). What I need is to "build" this URL using the port of the first mock service, which I want to be overridden by a global variable maybe. 

Is that any way to do this?

Please help me, as I am stuck at this step for over a week and googling din't provide me any solution.

 

Thank you! 

5 REPLIES
Community Leader

Re: Override the port of a mock service using a global variable

Hi,

 

Hope I am not talking trash here... not exactly sure what you want to achieve... but in order to have a dynamic port you could use a project property. For instance I added a project property like:

 

Selection_002.png

 

Afterwards I used it in my subsequent request like:

 

Selection_003.png

 

Let me know if it helps! Smiley Happy

Occasional Contributor

Re: Override the port of a mock service using a global variable

First of all, thank you for the quick reply.

Let me explain what I have and what I want:

 

I have a project like this: tree.png

and the MockProxy is working as a "proxy" and returns the response of the MockService using this script: script.pngThe problem is that the MockService Port is set: 

servicePort.png

So I want to override this Mock Service Port=8089 through command line or a property file, and then, in the script, instead of the http://localhost:8089/budg/inforeuro/api/public/monthly-rates use the overridden port, lets say http://localhost:<port>/budg/inforeuro/api/public/monthly-rates

I don't know if it is possible to override the Port=8089 and then pass it as parameter in the URL.

 

Sorry if I didn't explain it in the first time, and thanks again for the quick reply!

 

Community Leader

Re: Override the port of a mock service using a global variable

Sure, you can just use Groovy Strings to do that:

 

String port = "8089"
def get = new URL("http://localhost:${port}/budg/inforeuro/api/public/monthly-rates").openConnection()

 

As for where to set the value for 'port', why not read it directly from the MockService itself?

 

String port = context.expand('${=project.getRestMockServiceByName("MockService").port}')
def get = new URL("http://localhost:${port}/budg/inforeuro/api/public/monthly-rates").openConnection()
Occasional Contributor

Re: Override the port of a mock service using a global variable

Nice, thank you for the reply! I din't know how to pass this variable from one service to another.

From SoapUI seems to working, when I change from MockService Preferences the Port to "5555" then the MockProxy is running successfully. 

 

But, is there any reason why this is not working from command line? I use this:

./mockservicerunner.sh -m "MockService" -p "5555" "/home/oracle/InfoEuro_WS_Mock.xml"

 

and after that, in a different command line tab, I use this:

./mockservicerunner.sh -m "MockProxy" "/home/oracle/InfoEuro_WS_Mock.xml"

but I am getting a connection refuse exception.

 

If I do not change the port of the service (-p "5555"), both services work fine.

Occasional Contributor

Re: Override the port of a mock service using a global variable

I think that I answer my question when I was writing it. I have the same script, the one that builds the MockService URL and returns the response, in two places. In MockService level and in Get Currencies level (into the MockService). So maybe, the scripts were running one by one, so the first time, the port was taken. So I was receiving a connection refuse exception. 

 

I have retain the script in the Get Currencies level, I hope this is the correct level to have it. And now I am able, using command line to run the service in any port and build dynamically the URL in the MockProxy using the solution you gave me. 

 

Thank you for helping, it was the first time I am writing to such a community, I should have done it earlier. 

Latest News
Top Kudoed Authors