components/global/core/io.cellery.observability.api/src/test/java/io/cellery/observability/api/auth/OIDCOauthManagerTestCase.java
private HttpResponse generateHttpResponse(JSONObject jsonObject, int statusCode) throws Exception {
        char[] resultantCharArray = jsonObject.toString().toCharArray();
        HttpEntity httpEntity = Mockito.mock(HttpEntity.class);
        InputStream inputStream = Mockito.mock(InputStream.class);
        Mockito.when(httpEntity.getContent()).thenReturn(inputStream);
        Mockito.when(httpEntity.getContentLength()).thenReturn((long) resultantCharArray.length);

        HttpResponse response = Mockito.mock(HttpResponse.class);
        Mockito.when(response.getEntity()).thenReturn(httpEntity);
        StatusLine statusLine = Mockito.mock(StatusLine.class);
        Mockito.when(statusLine.getStatusCode()).thenReturn(statusCode);
        Mockito.when(response.getStatusLine()).thenReturn(statusLine);

        NameValuePair nameValuePair = Mockito.mock(NameValuePair.class);
        Mockito.when(nameValuePair.getName()).thenReturn("charset");
        Mockito.when(nameValuePair.getValue()).thenReturn(StandardCharsets.UTF_8.toString());
        HeaderElement headerElement = Mockito.mock(HeaderElement.class);
        Mockito.when(headerElement.getParameters()).thenReturn(new NameValuePair[]{nameValuePair});
        Header header = Mockito.mock(Header.class);
        Mockito.when(header.getElements()).thenReturn(new HeaderElement[]{headerElement});
        Mockito.when(httpEntity.getContentType()).thenReturn(header);

        InputStreamReader inputStreamReader = Mockito.mock(InputStreamReader.class);
        PowerMockito.whenNew(InputStreamReader.class)
                .withParameterTypes(InputStream.class, Charset.class)
                .withArguments(Mockito.eq(inputStream), Mockito.any(Charset.class))
                .thenReturn(inputStreamReader);

        Mockito.when(inputStreamReader.read(Mockito.any(char[].class)))
                .thenAnswer(invocationOnMock -> {
                    char[] charArray = invocationOnMock.getArgumentAt(0, char[].class);
                    System.arraycopy(resultantCharArray, 0, charArray, 0, resultantCharArray.length);
                    return resultantCharArray.length;
                })
                .thenReturn(-1);

        return response;
    }
/**
     * Generate a mock Http Response object that would return the string of the JSON object provided.
     *
     * @param jsonObject The JSON object to be returned from the HTTP response
     * @param statusCode The status code of the response
     * @return The HTTP response
     * @throws Exception if mocking fails
     */
Generate a mock Http Response object that would return the string of the JSON object provided.
@param jsonObject The JSON object to be returned from the HTTP response
@param statusCode The status code of the response
@return The HTTP response
@throws Exception if mocking fails
Generate a mock Http Response object that would return the string of the JSON object provided . @param jsonObject The JSON object to be returned from the HTTP response @param statusCode The status code of the response @return The HTTP response @throws Exception if mocking fails
private HttpResponse generateHttpResponse(JSONObject jsonObject, int statusCode) throws Exception {
        char[] resultantCharArray = jsonObject.toString().toCharArray();
        HttpEntity httpEntity = Mockito.mock(HttpEntity.class);
        InputStream inputStream = Mockito.mock(InputStream.class);
        Mockito.when(httpEntity.getContent()).thenReturn(inputStream);
        Mockito.when(httpEntity.getContentLength()).thenReturn((long) resultantCharArray.length);
        HttpResponse response = Mockito.mock(HttpResponse.class);
        Mockito.when(response.getEntity()).thenReturn(httpEntity);
        StatusLine statusLine = Mockito.mock(StatusLine.class);
        Mockito.when(statusLine.getStatusCode()).thenReturn(statusCode);
        Mockito.when(response.getStatusLine()).thenReturn(statusLine);
        NameValuePair nameValuePair = Mockito.mock(NameValuePair.class);
        Mockito.when(nameValuePair.getName()).thenReturn("charset");
        Mockito.when(nameValuePair.getValue()).thenReturn(StandardCharsets.UTF_8.toString());
        HeaderElement headerElement = Mockito.mock(HeaderElement.class);
        Mockito.when(headerElement.getParameters()).thenReturn(new NameValuePair[]{nameValuePair});
        Header header = Mockito.mock(Header.class);
        Mockito.when(header.getElements()).thenReturn(new HeaderElement[]{headerElement});
        Mockito.when(httpEntity.getContentType()).thenReturn(header);
        InputStreamReader inputStreamReader = Mockito.mock(InputStreamReader.class);
        PowerMockito.whenNew(InputStreamReader.class)
                .withParameterTypes(InputStream.class, Charset.class)
                .withArguments(Mockito.eq(inputStream), Mockito.any(Charset.class))
                .thenReturn(inputStreamReader);
        Mockito.when(inputStreamReader.read(Mockito.any(char[].class)))
                .thenAnswer(invocationOnMock -> {
                    char[] charArray = invocationOnMock.getArgumentAt(0, char[].class);
                    System.arraycopy(resultantCharArray, 0, charArray, 0, resultantCharArray.length);
                    return resultantCharArray.length;
                })
                .thenReturn(-1);
        return response;
    }
private HttpResponse generateHttpResponse ( JSONObject jsonObject , int statusCode ) throws Exception { char [ ] resultantCharArray = jsonObject . toString ( ) . toCharArray ( ) ; HttpEntity httpEntity = Mockito . mock ( HttpEntity . class ) ; InputStream inputStream = Mockito . mock ( InputStream . class ) ; Mockito . when ( httpEntity . getContent ( ) ) . thenReturn ( inputStream ) ; Mockito . when ( httpEntity . getContentLength ( ) ) . thenReturn ( ( long ) resultantCharArray . length ) ; HttpResponse response = Mockito . mock ( HttpResponse . class ) ; Mockito . when ( response . getEntity ( ) ) . thenReturn ( httpEntity ) ; StatusLine statusLine = Mockito . mock ( StatusLine . class ) ; Mockito . when ( statusLine . getStatusCode ( ) ) . thenReturn ( statusCode ) ; Mockito . when ( response . getStatusLine ( ) ) . thenReturn ( statusLine ) ; NameValuePair nameValuePair = Mockito . mock ( NameValuePair . class ) ; Mockito . when ( nameValuePair . getName ( ) ) . thenReturn ( "charset" ) ; Mockito . when ( nameValuePair . getValue ( ) ) . thenReturn ( StandardCharsets . UTF_8 . toString ( ) ) ; HeaderElement headerElement = Mockito . mock ( HeaderElement . class ) ; Mockito . when ( headerElement . getParameters ( ) ) . thenReturn ( new NameValuePair [ ] { nameValuePair } ) ; Header header = Mockito . mock ( Header . class ) ; Mockito . when ( header . getElements ( ) ) . thenReturn ( new HeaderElement [ ] { headerElement } ) ; Mockito . when ( httpEntity . getContentType ( ) ) . thenReturn ( header ) ; InputStreamReader inputStreamReader = Mockito . mock ( InputStreamReader . class ) ; PowerMockito . whenNew ( InputStreamReader . class ) . withParameterTypes ( InputStream . class , Charset . class ) . withArguments ( Mockito . eq ( inputStream ) , Mockito . any ( Charset . class ) ) . thenReturn ( inputStreamReader ) ; Mockito . when ( inputStreamReader . read ( Mockito . any ( char [ ] . class ) ) ) . thenAnswer ( invocationOnMock -> { char [ ] charArray = invocationOnMock . getArgumentAt ( 0 , char [ ] . class ) ; System . arraycopy ( resultantCharArray , 0 , charArray , 0 , resultantCharArray . length ) ; return resultantCharArray . length ; } ) . thenReturn ( - 1 ) ; return response ; }
Generate a mock Http Response object that would return the string of the JSON object provided.
Generate a mock Http Response object that would return the string of the JSON object provided .