My test Case is giving URISyntaxException | Community
Skip to main content
October 29, 2024
Solved

My test Case is giving URISyntaxException

  • October 29, 2024
  • 2 replies
  • 680 views

Dear All,

 

I am calling one Backend API in my service impl class like below. Previously to get the username I was using [backend API]/content-author/v2/privateCommunityManager/subnaik

and value is coming fine in postman.

 

Now we have updated like below and the value are coming fine in POSTMAN.

[backend API]/content-author/v2/privateCommunityManager/[subnaik]

 

But in JAVA code I have changed like below and java code is fine. But in the test cases the username [subnaik] is not taking and getting error, as shown below.

@9944223 public String getPrivateCommunities(String userId, boolean isEditor) throws IOException, URISyntaxException { String responseStr = ""; //String url = config.getUrl().concat(isEditor ? Constants.CONTENT_AUTHORING_API_ADD_MEMBERS_TO_COMMUNITY : Constants.CONTENT_AUTHORING_API_PRIVATE_COMMUNITY_MANAGER.concat(userId)); // previousely [domain]/content-author/v2/privateCommunityManager/subnaik String url = config.getUrl().concat(isEditor ? Constants.CONTENT_AUTHORING_API_ADD_MEMBERS_TO_COMMUNITY : Constants.CONTENT_AUTHORING_API_PRIVATE_COMMUNITY_MANAGER.concat("["+userId+"]")); // After changing [domain]/content-author/v2/privateCommunityManager/[subnaik] HttpGet httpGet = new HttpGet(new URI(url)); httpGet.setHeaders(getHeaders()); HttpClient httpClient = HttpClientBuilder.create().build(); HttpResponse response = httpClient.execute(httpGet); String entityResponse = EntityUtils.toString(response.getEntity()); if(!entityResponse.isEmpty()) responseStr = isEditor ? filterPrivateCommunities(entityResponse) : processResponseForPrivateCommunityManager(entityResponse); return responseStr; }

My unit test class is below and getting URISyntaxException error while calling the bracket [ ]

 

@BeforeEach void setUp() throws LoginException { MockitoAnnotations.initMocks(this); contentAuthoringAPIService.activate(config); contentAuthoringAPIService.update(config); wireMockServer.start(); lenient().when(config.getAuthenticationServiceUrl()).thenReturn("http://localhost:"+ wireMockServer.port() + "/search"); lenient().when(config.getUrl()).thenReturn("http://localhost:"+ wireMockServer.port() + "/search"); when(config.marcelServiceApiAccessKey()).thenReturn("=ABC"); lenient().when(config.ocpApimSubscriptionApiKey()).thenReturn("=123"); when(config.serviceName()).thenReturn("AEM"); lenient().when(config.getAnalyticsUrl()).thenReturn("http://localhost:"+ wireMockServer.port() + "/search"); lenient().when(config.muIdServiceName()).thenReturn("2F30E117-3EC3-4645-B3D5-30A1BE176AW2"); } void shoulGetPrivateCommunitiesWhenIsEditorIsFalse() throws IOException, URISyntaxException, LoginException, RepositoryException { boolean isEditor = false; wireMockServer.stubFor(get( "/search".concat(String.format(Constants.CONTENT_AUTHORING_API_PRIVATE_COMMUNITY_MANAGER.concat("MOCK_USER")))) .willReturn(aResponse().withBody("{\n" + " \"data\": {\n" + " \"people\": [\n" + " {\n" + " \"name\": \"MOCK USER\",\n" + " \"isACommunityManagerFors\": [\n" + " {\n" + " \"name\": \"MOCK NAME\",\n" + " \"communityName\": \"MOCK NAME\",\n" + " \"communityType\": \"MOCK TYPE\",\n" + " \"isPrivate\": \"true\",\n" + " \"id\" : 123,\n" + " \"hasThePeopleMembers\": [\n" + " {\n" + " \"name\": \"MOCK USER NAME\",\n" + " \"id\": 12345\n" + " }\n" + " ],\n" + " \"hasTheCommunityManagers\": [\n" + " {\n" + " \"id\": 1234,\n" + " \"name\": \"MOCK USER NAME\",\n" + " \"lionLoginId\": \"MOCK_USER\"\n" + " }\n" + " ]\n" + " }\n" + " ]\n" + " }\n" + " ]\n" + " }\n" + "}"))); String expected = "[{\"id\":123,\"name\":\"MOCK NAME\",\"communityType\":\"MOCK TYPE\",\"members\":[{\"id\":12345,\"name\":\"MOCK USER NAME\"}],\"displayList\":[{\"id\":1234,\"name\":\"MOCK USER NAME\",\"lionLoginId\":\"MOCK_USER\"},{\"id\":12345,\"name\":\"MOCK USER NAME\"}]}]"; String actual = contentAuthoringAPIService.getPrivateCommunities("MOCK_USER",isEditor); Assert.assertEquals("Response should match", expected, actual); wireMockServer.stop(); }

 

Can anyone please let me know if anything I need to do here to fix the error.

 

Thanks in advance

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Tethich

Hi@subnaik 

 

You said you changed your API from [backend API]/content-author/v2/privateCommunityManager/subnaik to [backend API]/content-author/v2/privateCommunityManager/[subnaik]. And I see in the second the username is now in brackets. Not sure if you only showed it here like this to let us know that is a placeholder or something or if you actually use brackets in the API endpoint. But if you did, please try to remove the brackets. Those are reserved characters for URI.

2 replies

Tethich
Community Advisor
TethichCommunity AdvisorAccepted solution
Community Advisor
October 29, 2024

Hi@subnaik 

 

You said you changed your API from [backend API]/content-author/v2/privateCommunityManager/subnaik to [backend API]/content-author/v2/privateCommunityManager/[subnaik]. And I see in the second the username is now in brackets. Not sure if you only showed it here like this to let us know that is a placeholder or something or if you actually use brackets in the API endpoint. But if you did, please try to remove the brackets. Those are reserved characters for URI.

arunpatidar
Community Advisor
Community Advisor
October 29, 2024

Hi @subnaik 
You may need to print the actual error

 

System.err.println("URI Syntax Exception: " + e.getMessage());

But it seems issue is due to [], Use URLEncoder.encode() for path segments if they contain special characters.

Arun Patidar