Skip to content

Commit fa21897

Browse files
Merge pull request #1721 from nextcloud/fix-abstract-it
Fix - AbstractIT
2 parents cee7b9f + 58bd46a commit fa21897

File tree

1 file changed

+64
-36
lines changed

1 file changed

+64
-36
lines changed

library/src/androidTest/java/com/owncloud/android/AbstractIT.java

Lines changed: 64 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
import static junit.framework.TestCase.assertTrue;
1212
import static org.junit.Assert.assertEquals;
13-
import static org.junit.Assert.fail;
1413
import static org.junit.Assume.assumeTrue;
1514

1615
import android.content.Context;
@@ -19,6 +18,7 @@
1918

2019
import androidx.test.platform.app.InstrumentationRegistry;
2120

21+
import com.nextcloud.android.lib.resources.files.ToggleFileLockRemoteOperation;
2222
import com.nextcloud.common.NextcloudClient;
2323
import com.owncloud.android.lib.common.OwnCloudBasicCredentials;
2424
import com.owncloud.android.lib.common.OwnCloudClient;
@@ -54,6 +54,7 @@
5454
import java.security.NoSuchAlgorithmException;
5555
import java.security.cert.CertificateException;
5656
import java.security.cert.X509Certificate;
57+
import java.util.concurrent.TimeUnit;
5758

5859
import okhttp3.Credentials;
5960

@@ -77,7 +78,8 @@ public abstract class AbstractIT {
7778
protected String baseFolderPath = "/test_for_build/";
7879

7980
public static final String ASSETS__TEXT_FILE_NAME = "textFile.txt";
80-
private static String LOCAL_TRUSTSTORE_FILENAME = "knownServers.bks";
81+
private static final String LOCAL_TRUSTSTORE_FILENAME = "knownServers.bks";
82+
private static final String TAG = "AbstractIT";
8183

8284
@BeforeClass
8385
public static void beforeAll() throws InterruptedException,
@@ -115,26 +117,26 @@ public static void beforeAll() throws InterruptedException,
115117
}
116118

117119
private static void waitForServer(OwnCloudClient client, Uri baseUrl) {
118-
// use http
119-
Uri httpUrl = Uri.parse(baseUrl.toString().replaceFirst("https", "http"));
120-
GetMethod get = new GetMethod(httpUrl + "/status.php");
120+
String statusUrl = baseUrl + "/status.php";
121+
GetMethod get;
122+
int maxRetries = 3;
121123

122-
try {
123-
int i = 0;
124-
while (client.executeMethod(get) != HttpStatus.SC_OK && i < 3) {
125-
System.out.println("wait…");
126-
Thread.sleep(60 * 1000);
127-
i++;
128-
}
124+
for (int i = 0; i < maxRetries; i++) {
125+
get = new GetMethod(statusUrl);
129126

130-
if (i == 3) {
131-
fail("Server not ready!");
132-
}
127+
try {
128+
if (client.executeMethod(get) == HttpStatus.SC_OK) {
129+
Log_OC.d(TAG, "Server is ready");
130+
return;
131+
}
133132

134-
} catch (IOException e) {
135-
e.printStackTrace();
136-
} catch (InterruptedException e) {
137-
e.printStackTrace();
133+
Log_OC.d(TAG, "Server not ready, retrying in 60 seconds...");
134+
TimeUnit.MINUTES.sleep(1);
135+
} catch (Exception e) {
136+
Log_OC.d(TAG, "Server not ready, failed: " + e);
137+
} finally {
138+
get.releaseConnection();
139+
}
138140
}
139141
}
140142

@@ -265,30 +267,56 @@ public void after() {
265267
}
266268

267269
private void removeOnClient(OwnCloudClient client) {
268-
RemoteOperationResult result = new ReadFolderRemoteOperation("/").execute(client);
270+
final var result = new ReadFolderRemoteOperation("/").execute(client);
269271
assertTrue(result.getLogMessage(context), result.isSuccess());
270272

271273
for (Object object : result.getData()) {
272-
RemoteFile remoteFile = (RemoteFile) object;
273-
274-
if (!"/".equals(remoteFile.getRemotePath()) &&
275-
remoteFile.getMountType() != WebdavEntry.MountType.GROUP) {
276-
if (remoteFile.isEncrypted()) {
277-
assertTrue(new ToggleEncryptionRemoteOperation(
278-
remoteFile.getLocalId(),
279-
remoteFile.getRemotePath(),
280-
false)
281-
.execute(client)
282-
.isSuccess());
283-
}
274+
if (!(object instanceof RemoteFile remoteFile)) {
275+
Log_OC.d(TAG, "Skipping removeOnClient: not instance of RemoteFile");
276+
continue;
277+
}
278+
279+
String remotePath = remoteFile.getRemotePath();
280+
281+
if ("/".equals(remotePath) || remoteFile.getMountType() == WebdavEntry.MountType.GROUP) {
282+
Log_OC.d(TAG, "Skipping removeOnClient: remote path is root path or mount type is group");
283+
continue;
284+
}
285+
286+
if (remoteFile.isEncrypted()) {
287+
assertTrue(toggleEncryptionRemoteFile(remoteFile));
288+
}
284289

285-
assertTrue("Failed to remove " + remoteFile.getRemotePath(),
286-
new RemoveFileRemoteOperation(remoteFile.getRemotePath()).execute(client).isSuccess());
290+
if (remoteFile.isLocked() && remotePath != null) {
291+
unlockRemoteFile(remotePath);
287292
}
293+
294+
boolean isRemoteFileRemoved = removeRemoteFile(remotePath);
295+
assertTrue("Failed to remove " + remotePath, isRemoteFileRemoved);
296+
}
297+
298+
boolean isKeyStoreDeleted = new File(context.getFilesDir(), LOCAL_TRUSTSTORE_FILENAME).delete();
299+
Log_OC.d(TAG, "KeyStore file deletion result: " + isKeyStoreDeleted);
300+
}
301+
302+
private boolean toggleEncryptionRemoteFile(RemoteFile remoteFile) {
303+
final var operation = new ToggleEncryptionRemoteOperation(remoteFile.getLocalId(), remoteFile.getRemotePath(), false);
304+
final var result = operation.execute(client);
305+
return result.isSuccess();
306+
}
307+
308+
private void unlockRemoteFile(String path) {
309+
final var operation = new ToggleFileLockRemoteOperation(false, path);
310+
final var result = operation.execute(nextcloudClient);
311+
if (result.isSuccess()) {
312+
Log_OC.d(TAG, "Locked file: " + path + " unlocked");
288313
}
314+
}
289315

290-
// delete keystore
291-
new File(context.getFilesDir(), LOCAL_TRUSTSTORE_FILENAME).delete();
316+
private boolean removeRemoteFile(String path) {
317+
final var operation = new RemoveFileRemoteOperation(path);
318+
final var result = operation.execute(client);
319+
return result.isSuccess();
292320
}
293321

294322
public static File getFile(String filename) throws IOException {

0 commit comments

Comments
 (0)