dav library test

Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>
This commit is contained in:
tobiasKaminsky 2021-08-02 07:42:40 +02:00 committed by Andy Scherzinger (Rebase PR Action)
parent 7efd323608
commit eb5b6a80d9
4 changed files with 152 additions and 106 deletions

View File

@ -21,12 +21,13 @@
package com.nextcloud.talk; package com.nextcloud.talk;
import android.content.Context; import android.content.Context;
import androidx.test.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import androidx.test.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
/** /**

View File

@ -0,0 +1,40 @@
package com.nextcloud.talk.utils
import at.bitfire.dav4jvm.HttpUtils
import org.apache.commons.lang3.time.DateUtils
import org.junit.Assert.assertEquals
import org.junit.Test
import java.util.Date
import java.util.Locale
class ShareUtilsIT {
@Test
fun date() {
assertEquals(1207778138000, parseDate2("Mon, 09 Apr 2008 23:55:38 GMT").time)
assertEquals(1207778138000, HttpUtils.parseDate("Mon, 09 Apr 2008 23:55:38 GMT")?.time)
}
private fun parseDate2(dateStr: String): Date {
return DateUtils.parseDate(
dateStr, Locale.US,
HttpUtils.httpDateFormatStr,
"EEE, dd MMM yyyy HH:mm:ss zzz", // RFC 822, updated by RFC 1123 with any TZ
"EEEE, dd-MMM-yy HH:mm:ss zzz", // RFC 850, obsoleted by RFC 1036 with any TZ.
"EEE MMM d HH:mm:ss yyyy", // ANSI C's asctime() format
// Alternative formats.
"EEE, dd-MMM-yyyy HH:mm:ss z",
"EEE, dd-MMM-yyyy HH-mm-ss z",
"EEE, dd MMM yy HH:mm:ss z",
"EEE dd-MMM-yyyy HH:mm:ss z",
"EEE dd MMM yyyy HH:mm:ss z",
"EEE dd-MMM-yyyy HH-mm-ss z",
"EEE dd-MMM-yy HH:mm:ss z",
"EEE dd MMM yy HH:mm:ss z",
"EEE,dd-MMM-yy HH:mm:ss z",
"EEE,dd-MMM-yyyy HH:mm:ss z",
"EEE, dd-MM-yyyy HH:mm:ss z",
/* RI bug 6641315 claims a cookie of this format was once served by www.yahoo.com */
"EEE MMM d yyyy HH:mm:ss z"
)
}
}

View File

@ -1,104 +0,0 @@
/*
* Nextcloud Talk application
*
* @author Mario Danic
* Copyright (C) 2017-2019 Mario Danic <mario@lovelyhq.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
package com.nextcloud.talk.utils;
import android.content.Context;
import android.content.res.Resources;
import android.text.TextUtils;
import com.nextcloud.talk.R;
import com.nextcloud.talk.models.database.UserEntity;
import com.nextcloud.talk.models.json.conversations.Conversation;
import com.nextcloud.talk.utils.database.user.UserUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
@RunWith(PowerMockRunner.class)
@PrepareForTest(TextUtils.class)
public class ShareUtilsTest {
@Mock
private Context context;
@Mock
private Resources resources;
@Mock
private UserUtils userUtils;
@Mock
private Conversation conversation;
@Mock
private UserEntity userEntity;
private final String baseUrl = "https://my.nextcloud.com";
private final String token = "2aotbrjr";
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mockStatic(TextUtils.class);
when(userUtils.getCurrentUser()).thenReturn(userEntity);
when(userEntity.getBaseUrl()).thenReturn(baseUrl);
when(conversation.getToken()).thenReturn(token);
when(context.getResources()).thenReturn(resources);
when(resources.getString(R.string.nc_share_text)).thenReturn("Join the conversation at %1$s/index.php/call/%2$s");
when(resources.getString(R.string.nc_share_text_pass)).thenReturn("\nPassword: %1$s");
}
@Test
public void getStringForIntent_noPasswordGiven_correctStringWithoutPasswordReturned() {
PowerMockito.when(TextUtils.isEmpty(anyString())).thenReturn(true);
String expectedResult = String.format("Join the conversation at %s/index.php/call/%s",
baseUrl, token);
assertEquals("Intent string was not as expected",
expectedResult, ShareUtils.getStringForIntent(context, "", userUtils, conversation));
}
@Test
public void getStringForIntent_passwordGiven_correctStringWithPasswordReturned() {
PowerMockito.when(TextUtils.isEmpty(anyString())).thenReturn(false);
String password = "superSecret";
String expectedResult = String.format("Join the conversation at %s/index.php/call/%s\nPassword: %s",
baseUrl, token, password);
assertEquals("Intent string was not as expected",
expectedResult, ShareUtils.getStringForIntent(context, password, userUtils, conversation));
}
}

View File

@ -0,0 +1,109 @@
/*
* Nextcloud Talk application
*
* @author Mario Danic
* Copyright (C) 2017-2019 Mario Danic <mario@lovelyhq.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
package com.nextcloud.talk.utils
import android.content.Context
import android.content.res.Resources
import android.text.TextUtils
import at.bitfire.dav4jvm.HttpUtils.parseDate
import com.nextcloud.talk.R
import com.nextcloud.talk.models.database.UserEntity
import com.nextcloud.talk.models.json.conversations.Conversation
import com.nextcloud.talk.utils.database.user.UserUtils
import junit.framework.Assert.assertEquals
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers
import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.MockitoAnnotations
import org.powermock.api.mockito.PowerMockito
import org.powermock.core.classloader.annotations.PrepareForTest
import org.powermock.modules.junit4.PowerMockRunner
import java.text.ParseException
@RunWith(PowerMockRunner::class)
@PrepareForTest(TextUtils::class)
class ShareUtilsTest {
@Mock
private val context: Context? = null
@Mock
private val resources: Resources? = null
@Mock
private val userUtils: UserUtils? = null
@Mock
private val conversation: Conversation? = null
@Mock
private val userEntity: UserEntity? = null
private val baseUrl = "https://my.nextcloud.com"
private val token = "2aotbrjr"
@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
PowerMockito.mockStatic(TextUtils::class.java)
Mockito.`when`(userUtils!!.currentUser).thenReturn(userEntity)
Mockito.`when`(userEntity!!.baseUrl).thenReturn(baseUrl)
Mockito.`when`(conversation!!.getToken()).thenReturn(token)
Mockito.`when`(context!!.resources).thenReturn(resources)
Mockito.`when`(resources!!.getString(R.string.nc_share_text))
.thenReturn("Join the conversation at %1\$s/index.php/call/%2\$s")
Mockito.`when`(resources.getString(R.string.nc_share_text_pass)).thenReturn("\nPassword: %1\$s")
}
@Test
fun stringForIntent_noPasswordGiven_correctStringWithoutPasswordReturned() {
PowerMockito.`when`(TextUtils.isEmpty(ArgumentMatchers.anyString())).thenReturn(true)
val expectedResult = String.format(
"Join the conversation at %s/index.php/call/%s",
baseUrl, token
)
Assert.assertEquals(
"Intent string was not as expected",
expectedResult, ShareUtils.getStringForIntent(context, "", userUtils, conversation)
)
}
@Test
fun stringForIntent_passwordGiven_correctStringWithPasswordReturned() {
PowerMockito.`when`(TextUtils.isEmpty(ArgumentMatchers.anyString())).thenReturn(false)
val password = "superSecret"
val expectedResult = String.format(
"Join the conversation at %s/index.php/call/%s\nPassword: %s",
baseUrl, token, password
)
Assert.assertEquals(
"Intent string was not as expected",
expectedResult, ShareUtils.getStringForIntent(context, password, userUtils, conversation)
)
}
@Test
@Throws(ParseException::class)
fun date() {
assertEquals(1207778138000, parseDate("Mon, 09 Apr 2008 23:55:38 GMT")?.time)
}
}