Skip to content

Instantly share code, notes, and snippets.

@koifish082
Last active October 17, 2018 08:54
Show Gist options
  • Save koifish082/9d4cd04a99f4d74f27290b7337d7b09c to your computer and use it in GitHub Desktop.
Save koifish082/9d4cd04a99f4d74f27290b7337d7b09c to your computer and use it in GitHub Desktop.
package jp.co.sample.presentation.navigation;
import android.net.Uri;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import jp.co.sample.BuildConfig;
import jp.co.sample.data.repositories.UserInfo;
public class WebViewUrlBuilder {
private final String URL_PARAM_VALUE_CATEGORY_ID_TOW = "2";
private final String URL_PARAM_VALUE_CATEGORY_ID_ONE = "1";
private final String URL_PARAM_CATEGORY_ID = "categoryID";
private final String URL_PARAM_USER_ID = "userID";
public String buildLendingWebViewUrl(String userId, UserInfo userInfo) {
String workID = userInfo.getStatus().isContracted() ?
URL_PARAM_VALUE_CATEGORY_ID_TWO :
URL_PARAM_VALUE_CATEGORY_ID_ONE;
String memberPageUrl = BuildConfig.MEMBER_PAGE_URL;
Uri.Builder builder = Uri.parse(memberPageUrl).buildUpon()
.appendQueryParameter(URL_PARAM_CATEGORY_ID, workID)
.appendQueryParameter(URL_PARAM_USERID, userID);
String requestUrl = builder.build().toString();
return BuildConfig.WEB_BASE_URL + "sign_in?return_to=" + getURLEncStr(requestUrl);
}
private String getURLEncStr(String str){
if(str == null) return null;
try {
return URLEncoder.encode(str , "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
}
package jp.co.sample.presentation.navigation;
import org.junit.Before;
import org.junit.Test;
import jp.co.sample.BuildConfig;
import jp.co.sample.data.entities.user.Status;
import jp.co.sample.data.repositories.UserInfo;
import timber.log.Timber;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class WebViewUrlBuilderTest {
private WebViewUrlBuilder mBuilder;
@Before
public void setUp() throws Exception {
mBuilder = new WebViewUrlBuilder();
}
@Test
public void buildLendingWebViewUrl() {
Status status = mock(Status.class);
UserInfo userInfo = new UserInfo();
userInfo.setStatus(status);
when(userInfo.getStatus().isContracted()).thenReturn(true);
String url = mBuilder.buildLendingWebViewUrl("A11111111", userInfo);
assertThat(url, is(BuildConfig.WEB_BASE_URL + "sign_in?return_to=" + BuildConfig.MEMBER_PAGE_URL + "?categoryID=2&userId=A11111111"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment