Skip to content

Commit 15fd3bf

Browse files
committed
fixes
Signed-off-by: alperozturk <[email protected]>
1 parent fdb542b commit 15fd3bf

File tree

1 file changed

+65
-35
lines changed

1 file changed

+65
-35
lines changed

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

Lines changed: 65 additions & 35 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,58 @@ 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, "removeOnClient skipping 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, "removeOnClient skipping 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+
final var removeFileOperationErrorMessage = ("Failed to remove " + remotePath);
296+
assertTrue(removeFileOperationErrorMessage, isRemoteFileRemoved);
288297
}
289298

290299
// delete keystore
291-
new File(context.getFilesDir(), LOCAL_TRUSTSTORE_FILENAME).delete();
300+
boolean isKeyStoreDeleted = new File(context.getFilesDir(), LOCAL_TRUSTSTORE_FILENAME).delete();
301+
Log_OC.d(TAG,"KeyStore file deletion result: " + isKeyStoreDeleted);
302+
}
303+
304+
private boolean toggleEncryptionRemoteFile(RemoteFile remoteFile) {
305+
final var operation = new ToggleEncryptionRemoteOperation(remoteFile.getLocalId(), remoteFile.getRemotePath(), false);
306+
final var result = operation.execute(client);
307+
return result.isSuccess();
308+
}
309+
310+
private void unlockRemoteFile(String path) {
311+
final var operation = new ToggleFileLockRemoteOperation(false, path);
312+
final var result = operation.execute(nextcloudClient);
313+
if (result.isSuccess()) {
314+
Log_OC.d(TAG,"Locked file: " + path + " unlocked");
315+
}
316+
}
317+
318+
private boolean removeRemoteFile(String path) {
319+
final var operation = new RemoveFileRemoteOperation(path);
320+
final var result = operation.execute(client);
321+
return result.isSuccess();
292322
}
293323

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

0 commit comments

Comments
 (0)