Skip to content

Commit

Permalink
Completed tests for database working - now full functional
Browse files Browse the repository at this point in the history
  • Loading branch information
Javinator9889 committed May 26, 2018
1 parent 2cf759f commit 52de736
Show file tree
Hide file tree
Showing 12 changed files with 376 additions and 188 deletions.
@@ -0,0 +1,88 @@
package javinator9889.securepass;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.Arrays;
import java.util.Map;

import javinator9889.securepass.io.database.DatabaseManager;
import javinator9889.securepass.io.database.DatabaseOperations;

/**
* Created by Javinator9889 on 20/05/2018.
*/
@RunWith(AndroidJUnit4.class)
public class DatabaseTest {
private Context context = InstrumentationRegistry.getTargetContext();

private DatabaseOperations op;

@Before
public void initParams() {
DatabaseManager manager = DatabaseManager.newInstance(context, "1234");
this.op = DatabaseOperations.newInstance(manager);
}

@Test
public void insertIntoDB() {
long defaultCategoryId = op.registerDefaultCategory();
long newAccountId = op.registerNewAccount("cuenta", "password",
"icono", "descripción", defaultCategoryId);
long newCategoryId = op.registerNewCategory("cat2");
long newQRCodeId = op.registerQRCode(newAccountId, "qr1", "desc",
"1234");
long newSecurityCodeId = op.registerNewSecurityCodeSource("sec1");
long newFieldForSecCodeId = op.registerNewFieldForSecurityCodeSource("12345",
false,
newSecurityCodeId);
op.updateInformationForCategory("cat3", newCategoryId);
op.updateInformationForEntry("cuent", "pass", "icon",
"desc", newCategoryId, newAccountId);
op.updateInformationForQRCode(newAccountId, "qr1,2", "descq", "data",
newQRCodeId);
op.updateInformationForSecurityCode("secCode", newSecurityCodeId);
op.updateInformationForField("12321", true, newFieldForSecCodeId,
newSecurityCodeId);
printer();
op.registerNewAccount("cuenta", "password", "icono",
"descripción", defaultCategoryId);
op.registerNewCategory("cat2");
op.registerQRCode(newAccountId, "qr1", "desc",
"1234");
op.registerNewSecurityCodeSource("sec1");
op.registerNewFieldForSecurityCodeSource("12345",false,
newSecurityCodeId);
printer();
op.deleteCategory(newCategoryId);
op.deleteAccount(newAccountId);
op.deleteQRCode(newQRCodeId);
op.deleteSecurityCode(newSecurityCodeId);
op.deleteField(newFieldForSecCodeId);
printer();
}

private void printer() {
for (Map<String, Object> objectMap : op.getAllCategories())
System.out.println(objectMap);
for (Map<String, Object> objectMap : op.getAllEntries())
System.out.println(objectMap);
for (Map<String, Object> objectMap : op.getAllQRCodes())
System.out.println(objectMap);
for (Map<String, Object> objectMap : op.getAllSecurityCodes())
System.out.println(objectMap);
for (Map<String, Object> objectMap : op.getAllFields())
System.out.println(objectMap);
}

@After
public void closeConnection() {
op.finishConnection();
}
}
Expand Up @@ -81,17 +81,18 @@ public void storeDataInDB() {
entry.getAccountPassword(),
entry.getIcon(),
entry.getDescription(),
entry.getCategory());
entry.getCategory().getId());
for (QRCode qrCode : qrCodes)
operations.registerQRCode(
qrCode.getEntry(),
qrCode.getEntry().getId(),
qrCode.getName(),
qrCode.getDescription(),
qrCode.getQrData());
for (SecurityCode code : securityCodes)
operations.registerNewSecurityCodeSource(code);
operations.registerNewSecurityCodeSource(code.getAccountName());
for (Field field : fields)
operations.registerNewFieldForSecurityCodeSource(field);
operations.registerNewFieldForSecurityCodeSource(field.getCode(), field.isCodeUsed(),
field.getSecurityCodeID());
operations.finishConnection();
}

Expand Down
29 changes: 18 additions & 11 deletions APP/app/src/main/java/javinator9889/securepass/io/IOManager.java
Expand Up @@ -16,6 +16,8 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javinator9889.securepass.R;
import javinator9889.securepass.util.cipher.PasswordCipher;
Expand All @@ -28,29 +30,34 @@

public class IOManager {
private Context activityContext;
private InputStream sqlScriptInputFile;
private File filesCache;

private IOManager(@NonNull Context activityContext) {
this.activityContext = activityContext;
this.filesCache = activityContext.getCacheDir();
int sqlScript = R.raw.database_script;
this.sqlScriptInputFile = activityContext.getResources().openRawResource(sqlScript);
}

@NonNull
public static IOManager newInstance(Context activityContext) {
return new IOManager(activityContext);
}

public String loadSQLScript() throws IOException {
BufferedReader sqlStringsInFile = new BufferedReader(
new InputStreamReader(sqlScriptInputFile));
StringBuilder completeFileRead = new StringBuilder();
String currentLine;
while ((currentLine = sqlStringsInFile.readLine()) != null)
completeFileRead.append(currentLine).append("\n");
return completeFileRead.toString();
public List<String> loadSQLScript() throws IOException {
List<String> result = new ArrayList<>(5);
int[] sqlScripts = new int[]{R.raw.create_category, R.raw.create_entry, R.raw.create_qrcode,
R.raw.create_security_code, R.raw.create_field};
for (int sqlScript : sqlScripts) {
InputStream sqlScriptInputFile = this.activityContext.getResources()
.openRawResource(sqlScript);
StringBuilder builder = new StringBuilder();
BufferedReader sqlStringsInFile = new BufferedReader(
new InputStreamReader(sqlScriptInputFile));
String currentLine;
while ((currentLine = sqlStringsInFile.readLine()) != null)
builder.append(currentLine).append("\n");
result.add(builder.toString());
}
return result;
}

public void storePassword(@NonNull String userPassword) {
Expand Down
Expand Up @@ -8,6 +8,7 @@

import java.io.File;
import java.io.IOException;
import java.util.List;

import javinator9889.securepass.io.IOManager;
import javinator9889.securepass.util.resources.ISharedPreferencesManager;
Expand Down Expand Up @@ -56,13 +57,15 @@ public void run() {
databaseFile,
databasePassword,
null);
String databaseScript;
List<String> databaseScripts;
ISharedPreferencesManager preferencesManager = SharedPreferencesManager
.newInstance();
try {
if (!preferencesManager.isDatabaseInitialized()) {
databaseScript = IOManager.newInstance(databaseContext).loadSQLScript();
database.execSQL(databaseScript);
databaseScripts = IOManager.newInstance(databaseContext).loadSQLScript();
for (String script : databaseScripts) {
database.execSQL(script);
}
DatabaseManager.this.createDefaultCategory();
}
} catch (IOException e) {
Expand Down

0 comments on commit 52de736

Please sign in to comment.