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.
5 #include "sync/test/fake_server/fake_server_entity.h"
11 #include "base/basictypes.h"
12 #include "base/guid.h"
13 #include "base/logging.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/string_split.h"
18 #include "base/strings/string_util.h"
19 #include "base/strings/stringprintf.h"
20 #include "net/base/net_errors.h"
21 #include "net/http/http_status_code.h"
22 #include "sync/internal_api/public/base/model_type.h"
23 #include "sync/protocol/sync.pb.h"
28 using syncer::ModelType
;
30 // The separator used when formatting IDs.
32 // We chose the underscore character because it doesn't conflict with the
33 // special characters used by base/base64.h's encoding, which is also used in
34 // the construction of some IDs.
35 const char kIdSeparator
[] = "_";
37 namespace fake_server
{
39 FakeServerEntity::~FakeServerEntity() { }
41 const std::string
& FakeServerEntity::GetId() const {
45 ModelType
FakeServerEntity::GetModelType() const {
49 int64
FakeServerEntity::GetVersion() const {
53 void FakeServerEntity::SetVersion(int64 version
) {
57 const std::string
& FakeServerEntity::GetName() const {
61 void FakeServerEntity::SetName(std::string name
) {
65 void FakeServerEntity::SetSpecifics(
66 const sync_pb::EntitySpecifics
& updated_specifics
) {
67 specifics_
= updated_specifics
;
70 bool FakeServerEntity::IsDeleted() const {
74 bool FakeServerEntity::IsFolder() const {
78 bool FakeServerEntity::IsPermanent() const {
83 string
FakeServerEntity::CreateId(const ModelType
& model_type
,
84 const string
& inner_id
) {
85 int field_number
= GetSpecificsFieldNumberFromModelType(model_type
);
86 return base::StringPrintf("%d%s%s",
93 std::string
FakeServerEntity::GetTopLevelId(const ModelType
& model_type
) {
94 return FakeServerEntity::CreateId(
96 syncer::ModelTypeToRootTag(model_type
));
100 ModelType
FakeServerEntity::GetModelTypeFromId(const string
& id
) {
101 vector
<base::StringPiece
> tokens
= base::SplitStringPiece(
102 id
, kIdSeparator
, base::KEEP_WHITESPACE
, base::SPLIT_WANT_NONEMPTY
);
105 if (tokens
.size() != 2 || !base::StringToInt(tokens
[0], &field_number
)) {
106 return syncer::UNSPECIFIED
;
109 return syncer::GetModelTypeFromSpecificsFieldNumber(field_number
);
112 FakeServerEntity::FakeServerEntity(const string
& id
,
113 const ModelType
& model_type
,
117 model_type_(model_type
),
121 void FakeServerEntity::SerializeBaseProtoFields(
122 sync_pb::SyncEntity
* sync_entity
) const {
123 sync_pb::EntitySpecifics
* specifics
= sync_entity
->mutable_specifics();
124 specifics
->CopyFrom(specifics_
);
126 // FakeServerEntity fields
127 sync_entity
->set_id_string(id_
);
128 sync_entity
->set_version(version_
);
129 sync_entity
->set_name(name_
);
131 // Data via accessors
132 sync_entity
->set_deleted(IsDeleted());
133 sync_entity
->set_folder(IsFolder());
136 } // namespace fake_server