/**
* Copyright (c) 2018 LEAP Encryption Access Project and contributers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package se.leap.bitmaskclient.testutils;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Parcelable;
import android.os.ResultReceiver;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateEncodingException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import okhttp3.OkHttpClient;
import se.leap.bitmaskclient.ConfigHelper;
import se.leap.bitmaskclient.OkHttpClientGenerator;
import se.leap.bitmaskclient.R;
import se.leap.bitmaskclient.testutils.BackendMockResponses.BackendMockProvider;
import se.leap.bitmaskclient.testutils.matchers.BundleMatcher;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
/**
* Created by cyberta on 08.10.17.
*/
public class TestSetupHelper {
public static String getInputAsString(InputStream fileAsInputStream) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(fileAsInputStream));
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
line = br.readLine();
}
return sb.toString();
}
@NonNull
public static Bundle mockBundle() {
final Map fakeBooleanBundle = new HashMap<>();
final Map fakeStringBundle = new HashMap<>();
final Map fakeIntBundle = new HashMap<>();
final Map fakeParcelableBundle = new HashMap<>();
Bundle bundle = mock(Bundle.class);
//mock String values in Bundle
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Object[] arguments = invocation.getArguments();
String key = ((String) arguments[0]);
String value = ((String) arguments[1]);
fakeStringBundle.put(key, value);
return null;
}
}).when(bundle).putString(anyString(), anyString());
when(bundle.getString(anyString())).thenAnswer(new Answer() {
@Override
public String answer(InvocationOnMock invocation) throws Throwable {
Object[] arguments = invocation.getArguments();
String key = ((String) arguments[0]);
return fakeStringBundle.get(key);
}
});
//mock Boolean values in Bundle
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Object[] arguments = invocation.getArguments();
String key = ((String) arguments[0]);
Boolean value = ((boolean) arguments[1]);
fakeBooleanBundle.put(key, value);
return null;
}
}).when(bundle).putBoolean(anyString(), anyBoolean());
when(bundle.getBoolean(anyString())).thenAnswer(new Answer() {
@Override
public Boolean answer(InvocationOnMock invocation) throws Throwable {
Object[] arguments = invocation.getArguments();
String key = ((String) arguments[0]);
return fakeBooleanBundle.get(key);
}
});
//mock Integer values in Bundle
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Object[] arguments = invocation.getArguments();
String key = ((String) arguments[0]);
Integer value = ((int) arguments[1]);
fakeIntBundle.put(key, value);
return null;
}
}).when(bundle).putInt(anyString(), anyInt());
when(bundle.getInt(anyString())).thenAnswer(new Answer() {
@Override
public Integer answer(InvocationOnMock invocation) throws Throwable {
Object[] arguments = invocation.getArguments();
String key = ((String) arguments[0]);
return fakeIntBundle.get(key);
}
});
//mock Parcelable values in Bundle
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Object[] arguments = invocation.getArguments();
String key = ((String) arguments[0]);
Parcelable value = ((Parcelable) arguments[1]);
fakeParcelableBundle.put(key, value);
return null;
}
}).when(bundle).putParcelable(anyString(), any(Parcelable.class));
when(bundle.getParcelable(anyString())).thenAnswer(new Answer() {
@Override
public Parcelable answer(InvocationOnMock invocation) throws Throwable {
Object[] arguments = invocation.getArguments();
String key = ((String) arguments[0]);
return fakeParcelableBundle.get(key);
}
});
//mock get
when(bundle.get(anyString())).thenAnswer(new Answer