1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "sync/test/fake_server/permanent_entity.h"
6
7#include <string>
8
9#include "base/basictypes.h"
10#include "base/logging.h"
11#include "sync/internal_api/public/base/model_type.h"
12#include "sync/protocol/sync.pb.h"
13#include "sync/test/fake_server/fake_server_entity.h"
14
15using std::string;
16
17using syncer::ModelType;
18
19// The parent tag for children of the root entity. Entities with this parent are
20// referred to as top level enities.
21static const char kRootParentTag[] = "0";
22
23namespace fake_server {
24
25PermanentEntity::~PermanentEntity() { }
26
27// static
28FakeServerEntity* PermanentEntity::Create(const ModelType& model_type,
29                                          const string& server_tag,
30                                          const string& name,
31                                          const string& parent_server_tag) {
32  CHECK(model_type != syncer::UNSPECIFIED) << "The entity's ModelType is "
33                                           << "invalid.";
34  CHECK(!server_tag.empty()) << "A PermanentEntity must have a server tag.";
35  CHECK(!name.empty()) << "The entity must have a non-empty name.";
36  CHECK(!parent_server_tag.empty()) << "A PermanentEntity must have a parent "
37                                    << "server tag.";
38  CHECK(parent_server_tag != kRootParentTag) << "Top-level entities should not "
39                                             << "be created with this factory.";
40
41  string id = FakeServerEntity::CreateId(model_type, server_tag);
42  string parent_id = FakeServerEntity::CreateId(model_type, parent_server_tag);
43  sync_pb::EntitySpecifics entity_specifics;
44  AddDefaultFieldValue(model_type, &entity_specifics);
45  return new PermanentEntity(id,
46                             model_type,
47                             name,
48                             parent_id,
49                             server_tag,
50                             entity_specifics);
51}
52
53// static
54FakeServerEntity* PermanentEntity::CreateTopLevel(const ModelType& model_type) {
55  CHECK(model_type != syncer::UNSPECIFIED) << "The entity's ModelType is "
56                                           << "invalid.";
57  string server_tag = syncer::ModelTypeToRootTag(model_type);
58  string name = syncer::ModelTypeToString(model_type);
59  string id = FakeServerEntity::CreateId(model_type, server_tag);
60  sync_pb::EntitySpecifics entity_specifics;
61  AddDefaultFieldValue(model_type, &entity_specifics);
62  return new PermanentEntity(id,
63                             model_type,
64                             name,
65                             kRootParentTag,
66                             server_tag,
67                             entity_specifics);
68}
69
70// static
71FakeServerEntity* PermanentEntity::CreateUpdatedNigoriEntity(
72    const sync_pb::SyncEntity& client_entity,
73    FakeServerEntity* current_server_entity) {
74  ModelType model_type = current_server_entity->GetModelType();
75  CHECK(model_type == syncer::NIGORI) << "This factory only supports NIGORI "
76                                      << "entities.";
77
78  return new PermanentEntity(current_server_entity->GetId(),
79                             model_type,
80                             current_server_entity->GetName(),
81                             current_server_entity->GetParentId(),
82                             syncer::ModelTypeToRootTag(model_type),
83                             client_entity.specifics());
84}
85
86PermanentEntity::PermanentEntity(const string& id,
87                                 const ModelType& model_type,
88                                 const string& name,
89                                 const string& parent_id,
90                                 const string& server_defined_unique_tag,
91                                 const sync_pb::EntitySpecifics& specifics)
92    : FakeServerEntity(id, model_type, 0, name),
93      server_defined_unique_tag_(server_defined_unique_tag),
94      parent_id_(parent_id),
95      specifics_(specifics) { }
96
97string PermanentEntity::GetParentId() const {
98  return parent_id_;
99}
100
101sync_pb::SyncEntity* PermanentEntity::SerializeAsProto() {
102  sync_pb::SyncEntity* sync_entity = new sync_pb::SyncEntity();
103  FakeServerEntity::SerializeBaseProtoFields(sync_entity);
104
105  sync_pb::EntitySpecifics* specifics = sync_entity->mutable_specifics();
106  specifics->CopyFrom(specifics_);
107
108  sync_entity->set_parent_id_string(parent_id_);
109  sync_entity->set_server_defined_unique_tag(server_defined_unique_tag_);
110
111  return sync_entity;
112}
113
114bool PermanentEntity::IsDeleted() const {
115  return false;
116}
117
118bool PermanentEntity::IsFolder() const {
119  return true;
120}
121
122}  // namespace fake_server
123