public static String buildHttpErrorMessage(final HttpURLConnection connection) throws IOException {
        val messageBuilder = new StringBuilder("(").append(connection.getResponseCode()).append(")");
        if (connection.getResponseMessage() != null) {
            messageBuilder.append(" ");
            messageBuilder.append(connection.getResponseMessage());
        }
        try (var isr = new InputStreamReader(connection.getErrorStream(), StandardCharsets.UTF_8);
             var br = new BufferedReader(isr)) {
            String output;
            messageBuilder.append("[");
            while ((output = br.readLine()) != null) {
                messageBuilder.append(output);
            }
            messageBuilder.append("]");
        } finally {
            connection.disconnect();
        }
        return messageBuilder.toString();
    }
@Test
    public void testBuildHttpErrorMessage() throws IOException {
        // creating mock htp connection
        HttpURLConnection connectionMock = null ;

        // expected test data for mock connection
        var testResponseBody = "{\"error_description\":\"MSIS9612: The authorization code received in [code] parameter is invalid. \"}";
        var testConnectionResponseCode =400;
        var testConnResponseMessage = "Bad Request.";

        // mocking expected test data
        try(InputStream in = new ByteArrayInputStream(testResponseBody.getBytes(StandardCharsets.UTF_8))) {
            connectionMock = Mockito.mock(HttpURLConnection.class);
            Mockito.when(connectionMock.getResponseCode()).thenReturn(testConnectionResponseCode);
            Mockito.when(connectionMock.getResponseMessage()).thenReturn(testConnResponseMessage);
            Mockito.when(connectionMock.getErrorStream()).thenReturn(in);

            //evaluating test
            var actual = HttpUtils.buildHttpErrorMessage(connectionMock);
            var expected = String.format("(%d) %s[%s]", testConnectionResponseCode, testConnResponseMessage, testResponseBody);
            Assert.assertTrue(expected.equals(actual));
        }

    }