[Bookmark] Fix TCT test
authorKamil Nowac <k.nowac@samsung.com>
Wed, 14 Jan 2015 14:45:18 +0000 (15:45 +0100)
committerRafal Galka <r.galka@samsung.com>
Thu, 15 Jan 2015 09:42:01 +0000 (18:42 +0900)
Added 2 methods for checking the validity of values.

PASS RATE: 35 / 36

Change-Id: I0d67cdb3dbb810127773fc7a147147c7622cdd37
Signed-off-by: Kamil Nowac <k.nowac@samsung.com>
src/bookmark/bookmark_instance.cc
src/bookmark/bookmark_instance.h

index b4eeef5880548138a20fc9a980d690fcca9e0dbe..f25e200c0ce780d7083f3c1fcb92377368ad15a4 100644 (file)
@@ -51,7 +51,7 @@ BookmarkInstance::~BookmarkInstance() {
 bool BookmarkInstance::bookmark_foreach(
     Context& ctx, bp_bookmark_info_fmt& info) {
   int ids_count = 0;
-  int *ids = NULL;
+  intids = NULL;
   BookmarkObject item;
   if (bp_bookmark_adaptor_get_full_ids_p(&ids, &ids_count) < 0)
     return false;
@@ -70,6 +70,64 @@ bool BookmarkInstance::bookmark_foreach(
   return true;
 }
 
+bool BookmarkInstance::bookmark_url_exists(const char* url) {
+  int ids_count = 0;
+  int* ids = NULL;
+  char* compare_url = NULL;
+
+  if (bp_bookmark_adaptor_get_full_ids_p(&ids, &ids_count) < 0)
+    return true;
+  if (ids_count > 0) {
+    for (int i = 0; i < ids_count; i++) {
+      if (bp_bookmark_adaptor_get_url(ids[i], &compare_url) < 0) {
+        free(ids);
+        return true;
+      }
+      if (strcmp(url, compare_url) == 0) {
+        free(compare_url);
+        free(ids);
+        return true;
+      }
+    }
+  }
+  free(compare_url);
+  free(ids);
+  return false;
+}
+
+bool BookmarkInstance::bookmark_title_exists_in_parent(
+    const char* title, int parent) {
+  int ids_count = 0;
+  int compare_parent = -1;
+  int* ids = NULL;
+  char* compare_title = NULL;
+
+  if (bp_bookmark_adaptor_get_full_ids_p(&ids, &ids_count) < 0)
+    return true;
+  if (ids_count > 0) {
+    for (int i = 0; i < ids_count; i++) {
+      if (bp_bookmark_adaptor_get_title(ids[i], &compare_title) < 0) {
+        free(ids);
+        return true;
+      }
+      if (bp_bookmark_adaptor_get_parent_id(ids[i], &compare_parent) < 0) {
+        free(compare_title);
+        free(ids);
+        return true;
+      }
+      if (strcmp(title, compare_title) == 0
+        && (parent == compare_parent)) {
+        free(compare_title);
+        free(ids);
+        return true;
+      }
+    }
+  }
+  free(compare_title);
+  free(ids);
+  return false;
+}
+
 void BookmarkInstance::Bookmark_get(
     const picojson::value& arg, picojson::object& o) {
   Context ctx = {0};
@@ -111,6 +169,16 @@ void BookmarkInstance::Bookmark_add(
   data.parent = arg.get(kParentId).get<double>();
   data.type   = arg.get(kType).get<double>();
   data.url    = const_cast<char*>(arg.get(kUrl).to_str().c_str());
+
+  if (!data.type && bookmark_url_exists(data.url)) {
+    ReportError(o);
+    return;
+  }
+  if (data.type && bookmark_title_exists_in_parent(data.title, data.parent)) {
+    ReportError(o);
+    return;
+  }
+
   if (bp_bookmark_adaptor_create(&saved_id) < 0) {
     ReportError(o);
     return;
index 5326b445cff4661ee57517dfe5f91084a9d5ac0c..00472e4212502601d96bc5be942bde24b64f16b1 100644 (file)
@@ -32,6 +32,8 @@ class BookmarkInstance : public common::ParsedInstance {
 
  private:
   bool bookmark_foreach(Context& ctx, bp_bookmark_info_fmt& info);
+  bool bookmark_url_exists(const char* url);
+  bool bookmark_title_exists_in_parent(const char* title, int parent);
   void Bookmark_get(const picojson::value& arg, picojson::object& o);
   void Bookmark_add(const picojson::value& arg, picojson::object& o);
   void Bookmark_remove(const picojson::value& arg, picojson::object& o);