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_util.h"
18 #include "base/strings/stringprintf.h"
19 #include "net/base/net_errors.h"
20 #include "net/http/http_status_code.h"
21 #include "sync/internal_api/public/base/model_type.h"
22 #include "sync/protocol/sync.pb.h"
27 using syncer::ModelType
;
29 // The separator used when formatting IDs.
31 // We chose the underscore character because it doesn't conflict with the
32 // special characters used by base/base64.h's encoding, which is also used in
33 // the construction of some IDs.
34 const char kIdSeparator
[] = "_";
36 namespace fake_server
{
38 FakeServerEntity::~FakeServerEntity() { }
40 const std::string
& FakeServerEntity::GetId() const {
44 ModelType
FakeServerEntity::GetModelType() const {
48 int64
FakeServerEntity::GetVersion() const {
52 void FakeServerEntity::SetVersion(int64 version
) {
56 const std::string
& FakeServerEntity::GetName() const {
61 string
FakeServerEntity::CreateId(const ModelType
& model_type
,
62 const string
& inner_id
) {
63 int field_number
= GetSpecificsFieldNumberFromModelType(model_type
);
64 return base::StringPrintf("%d%s%s",
71 std::string
FakeServerEntity::GetTopLevelId(const ModelType
& model_type
) {
72 return FakeServerEntity::CreateId(
74 syncer::ModelTypeToRootTag(model_type
));
78 ModelType
FakeServerEntity::GetModelTypeFromId(const string
& id
) {
79 vector
<string
> tokens
;
80 size_t token_count
= Tokenize(id
, kIdSeparator
, &tokens
);
83 if (token_count
!= 2 || !base::StringToInt(tokens
[0], &field_number
)) {
84 return syncer::UNSPECIFIED
;
87 return syncer::GetModelTypeFromSpecificsFieldNumber(field_number
);
90 FakeServerEntity::FakeServerEntity(const string
& id
,
91 const ModelType
& model_type
,
94 : model_type_(model_type
),
99 void FakeServerEntity::SerializeBaseProtoFields(
100 sync_pb::SyncEntity
* sync_entity
) {
101 // FakeServerEntity fields
102 sync_entity
->set_id_string(id_
);
103 sync_entity
->set_version(version_
);
104 sync_entity
->set_name(name_
);
106 // Data via accessors
107 sync_entity
->set_deleted(IsDeleted());
108 sync_entity
->set_folder(IsFolder());
111 } // namespace fake_server