summaryrefslogtreecommitdiff
path: root/src/components/application_manager/test/app_launch/app_launch_data_json_test.cc
blob: 12a375685ee05d6e0e3ba5a63bc8b41674e68870 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
 * Copyright (c) 2017, Ford Motor Company
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following
 * disclaimer in the documentation and/or other materials provided with the
 * distribution.
 *
 * Neither the name of the Ford Motor Company nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "application_manager/app_launch/app_launch_data_json.h"
#include <algorithm>
#include <memory>
#include <sstream>
#include "application_manager/mock_app_launch_settings.h"
#include "application_manager/smart_object_keys.h"
#include "gtest/gtest.h"
#include "json/json.h"
#include "resumption/last_state_impl.h"
#include "smart_objects/smart_object.h"
#include "utils/date_time.h"
#include "utils/file_system.h"
#include "utils/macro.h"

namespace test {
namespace components {
namespace test_app_launch {

using ::testing::_;
using ::testing::NiceMock;
using ::testing::Return;

namespace am = application_manager;
using namespace Json;
using namespace file_system;

using namespace app_launch;

const std::string kAppStorageFolder = "app_storage_folder";
const std::string kAppStorageFile = "./app_info.dat";
const std::string kAppInfoStorage = "app_info_storage";

class AppLaunchDataJsonTest : public ::testing::Test {
 private:
  virtual void SetUp() {
    ::file_system::DeleteFile(kAppStorageFile);
    test_last_state_ = std::unique_ptr<resumption::LastState>(
        new resumption::LastStateImpl(kAppStorageFolder, kAppInfoStorage));
    ASSERT_TRUE(::file_system::CreateFile(kAppStorageFile));

    NiceMock<app_launch_test::MockAppLaunchSettings> mock_app_launch_settings_;
    ON_CALL(mock_app_launch_settings_, max_number_of_ios_device())
        .WillByDefault(Return(15u));

    res_json_.reset(
        new AppLaunchDataJson(mock_app_launch_settings_, *test_last_state_));
  }

 public:
  void TearDown() OVERRIDE {
    res_json_.get()->Clear();
  }

  static void SetUpTestCase() {}

  static void TearDownTestCase() {
    ::file_system::DeleteFile(kAppStorageFile);
    ::file_system::RemoveDirectory(kAppStorageFolder);
  }

  AppLaunchDataJson* res_json() {
    return res_json_.get();
  }

  void AddApplicationDataWithIncreaseTable(const ApplicationData& data);
  void AddApplicationDataWithoutIncreaseTable(const ApplicationData& data);
  date_time::TimeDuration GetApplicationData_EXPECT_TRUE(
      const ApplicationData& in_data, ApplicationData& out_data);
  void GetApplicationData_EXPECT_FALSE(const ApplicationData& in_data);
  std::string AddCounter(const std::string& inp, int32_t val);

  std::unique_ptr<resumption::LastState> test_last_state_;
  std::unique_ptr<AppLaunchDataJson> res_json_;
  void SetTimestamp(const ApplicationData& in_data,
                    date_time::TimeDuration& timestamp);
};

void AppLaunchDataJsonTest::AddApplicationDataWithIncreaseTable(
    const ApplicationData& data) {
  uint32_t sizeBeforeAdding = res_json()->GetCurentNumberOfAppData();

  EXPECT_TRUE(res_json()->AddApplicationData(data));

  uint32_t sizeAfterAdding = res_json()->GetCurentNumberOfAppData();

  EXPECT_EQ(sizeBeforeAdding + 1u, sizeAfterAdding);
}

void AppLaunchDataJsonTest::AddApplicationDataWithoutIncreaseTable(
    const ApplicationData& data) {
  uint32_t sizeBeforeAdding = res_json()->GetCurentNumberOfAppData();

  EXPECT_TRUE(res_json()->AddApplicationData(data));

  uint32_t sizeAfterAdding = res_json()->GetCurentNumberOfAppData();

  EXPECT_EQ(sizeBeforeAdding, sizeAfterAdding);
}

date_time::TimeDuration AppLaunchDataJsonTest::GetApplicationData_EXPECT_TRUE(
    const ApplicationData& in_data, ApplicationData& out_data) {
  uint32_t sizeBeforeGetting = res_json()->GetCurentNumberOfAppData();

  int32_t index = NotFound;
  Json::Value& json_data_list =
      res_json()->GetApplicationListAndIndex(in_data, index);
  EXPECT_FALSE(index == NotFound);

  uint32_t sizeAfterGetting = res_json()->GetCurentNumberOfAppData();

  EXPECT_EQ(sizeBeforeGetting, sizeAfterGetting);
  out_data.device_mac_ =
      json_data_list[index][am::strings::device_id].asString();
  out_data.mobile_app_id_ =
      json_data_list[index][am::strings::app_id].asString();
  out_data.bundle_id_ =
      json_data_list[index][am::strings::bundle_id].asString();
  // time stamp
  date_time::TimeDuration tmVal = date_time::seconds(
      json_data_list[index][am::strings::app_launch_last_session].asUInt64());

  return tmVal;
}

void AppLaunchDataJsonTest::GetApplicationData_EXPECT_FALSE(
    const ApplicationData& in_data) {
  uint32_t sizeBeforeGetting = res_json()->GetCurentNumberOfAppData();

  int32_t index = NotFound;
  res_json()->GetApplicationListAndIndex(in_data, index);
  EXPECT_TRUE(index == NotFound);

  uint32_t sizeAfterGetting = res_json()->GetCurentNumberOfAppData();

  EXPECT_EQ(sizeBeforeGetting, sizeAfterGetting);
}

void AppLaunchDataJsonTest::SetTimestamp(const ApplicationData& in_data,
                                         date_time::TimeDuration& timestamp) {
  uint32_t sizeBeforeGetting = res_json()->GetCurentNumberOfAppData();

  int32_t index = NotFound;
  Json::Value& json_data_list =
      res_json()->GetApplicationListAndIndex(in_data, index);
  EXPECT_FALSE(index == NotFound);

  uint32_t sizeAfterGetting = res_json()->GetCurentNumberOfAppData();

  EXPECT_FALSE(index == NotFound);

  EXPECT_EQ(sizeBeforeGetting, sizeAfterGetting);
  // time stamp
  json_data_list[index][am::strings::app_launch_last_session] =
      static_cast<Json::Value::UInt64>(date_time::getSecs(timestamp));
}

std::string AppLaunchDataJsonTest::AddCounter(const std::string& inp,
                                              int32_t val) {
  std::stringstream ss;
  ss << inp << val;
  return ss.str();
}

TEST_F(AppLaunchDataJsonTest, SaveAndGetData) {
  ApplicationData data("mobile_app_id", "bundle_id", "device_mac");
  AddApplicationDataWithIncreaseTable(data);
  ApplicationData recoveredData("", "", "");
  GetApplicationData_EXPECT_TRUE(data, recoveredData);
  EXPECT_TRUE(data == recoveredData);
}

TEST_F(AppLaunchDataJsonTest, NotAddEmptyAppData) {
  ApplicationData data1("", "bundle_id", "device_mac");
  ApplicationData data2("mobile_app_id", "", "device_mac");
  ApplicationData data3("mobile_app_id", "bundle_id", "");
  ApplicationData data4("", "", "");
  EXPECT_FALSE(res_json()->AddApplicationData(data1));
  EXPECT_FALSE(res_json()->AddApplicationData(data2));
  EXPECT_FALSE(res_json()->AddApplicationData(data3));
  EXPECT_FALSE(res_json()->AddApplicationData(data4));
  EXPECT_EQ(0u, res_json()->GetCurentNumberOfAppData());
}

TEST_F(AppLaunchDataJsonTest, SaveOneAndGetAnotherData) {
  ApplicationData data("mobile_app_id", "bundle_id", "device_mac");
  ApplicationData recoverData = data;
  AddApplicationDataWithIncreaseTable(data);
  recoverData.device_mac_ += "test";
  GetApplicationData_EXPECT_FALSE(recoverData);
}

TEST_F(AppLaunchDataJsonTest, RefreshTimestamp) {
  ApplicationData data("mobile_app_id", "bundle_id", "device_mac");
  AddApplicationDataWithIncreaseTable(data);
  ApplicationData recoveredData("", "", "");
  date_time::TimeDuration timestamp1 =
      GetApplicationData_EXPECT_TRUE(data, recoveredData);
  date_time::TimeDuration tm = date_time::TimeDurationZero();
  SetTimestamp(data, tm);
  date_time::TimeDuration timestamp2 =
      GetApplicationData_EXPECT_TRUE(data, recoveredData);
  EXPECT_NE(date_time::getSecs(timestamp1), date_time::getSecs(timestamp2));
  AddApplicationDataWithoutIncreaseTable(data);  // again insert the same
  date_time::TimeDuration timestamp3 =
      GetApplicationData_EXPECT_TRUE(data, recoveredData);
  EXPECT_TRUE(data == recoveredData);
  EXPECT_NE(date_time::getSecs(timestamp2), date_time::getSecs(timestamp3));
}

TEST_F(AppLaunchDataJsonTest, MaxCount) {
  const uint32_t max_ios_devs = res_json()->get_max_number_iOS_devs();
  for (uint32_t i = 0; i < max_ios_devs; i++) {
    ApplicationData data(AddCounter("mobile_app_id_", i),
                         AddCounter("bundle_id_", i),
                         "device_mac");
    AddApplicationDataWithIncreaseTable(data);
  }

  // insert new time stamp
  ApplicationData changedRecord("mobile_app_id_0", "bundle_id_0", "device_mac");
  date_time::TimeDuration tm = date_time::TimeDurationZero();
  SetTimestamp(changedRecord, tm);

  uint32_t size_max = res_json()->GetCurentNumberOfAppData();
  EXPECT_EQ(max_ios_devs, size_max);
  EXPECT_TRUE(res_json()->AddApplicationData(ApplicationData(
      "mobile_app_id_last", "bundle_id_last", "device_mac_last")));
  uint32_t size_after_max = res_json()->GetCurentNumberOfAppData();
  EXPECT_EQ(size_max, size_after_max);
  EXPECT_FALSE(res_json()->IsAppDataAlreadyExisted(changedRecord));
}

TEST_F(AppLaunchDataJsonTest, DeleteAllJsonDataTwice) {
  for (uint32_t i = 0; i < res_json()->get_max_number_iOS_devs(); i++) {
    ApplicationData data(AddCounter("mobile_app_id_", i),
                         AddCounter("bundle_id_", i),
                         "device_mac");
    AddApplicationDataWithIncreaseTable(data);
  }

  uint32_t full_size = res_json()->GetCurentNumberOfAppData();

  EXPECT_EQ(full_size, res_json()->get_max_number_iOS_devs());
  EXPECT_TRUE(res_json()->Clear());  // delete data
  EXPECT_EQ(0u, res_json()->GetCurentNumberOfAppData());
  EXPECT_TRUE(res_json()->Clear());  // second time delete data
  EXPECT_EQ(0u, res_json()->GetCurentNumberOfAppData());
}

namespace {
bool ApplicationDataComporator(const ApplicationDataPtr& left,
                               const ApplicationDataPtr& right) {
  return (left->device_mac_ < right->device_mac_ &&
          left->mobile_app_id_ < right->mobile_app_id_ &&
          left->bundle_id_ < right->bundle_id_);
}
}  // namespace

TEST_F(AppLaunchDataJsonTest, SelectMultipleData) {
  std::vector<ApplicationDataPtr> input_data1;
  std::vector<ApplicationDataPtr> input_data2;
  std::vector<ApplicationDataPtr> output_data1;
  std::vector<ApplicationDataPtr> output_data2;
  const std::string device_mac_1 = "device_mac_1";
  const std::string device_mac_2 = "device_mac_2";
  uint32_t half_part = res_json()->get_max_number_iOS_devs() / 2u;

  for (uint32_t i = 0; i < half_part; i++) {
    const std::string mobile_app_id = AddCounter("d1_mobile_app_id_", i);
    const std::string bundle_id = AddCounter("d1_bundle_id_", i);

    ApplicationDataPtr app_data = std::make_shared<ApplicationData>(
        mobile_app_id, bundle_id, device_mac_1);
    AddApplicationDataWithIncreaseTable(*app_data);
    input_data1.push_back(app_data);
  }

  for (uint32_t i = 0; i < half_part; i++) {
    const std::string mobile_app_id = AddCounter("d2_mobile_app_id_", i);
    const std::string bundle_id = AddCounter("d2_bundle_id_", i);

    ApplicationDataPtr app_data = std::make_shared<ApplicationData>(
        mobile_app_id, bundle_id, device_mac_2);
    AddApplicationDataWithIncreaseTable(*app_data);
    input_data2.push_back(app_data);
  }

  output_data1 = res_json()->GetApplicationDataByDevice(device_mac_1);
  output_data2 = res_json()->GetApplicationDataByDevice(device_mac_2);

  EXPECT_EQ(half_part, output_data1.size());
  EXPECT_EQ(half_part, output_data2.size());

  std::sort(
      output_data1.begin(), output_data1.end(), ApplicationDataComporator);
  std::sort(
      output_data2.begin(), output_data2.end(), ApplicationDataComporator);
  std::sort(input_data1.begin(), input_data1.end(), ApplicationDataComporator);
  std::sort(input_data2.begin(), input_data2.end(), ApplicationDataComporator);

  for (uint32_t i = 0; i < output_data1.size(); i++) {
    EXPECT_TRUE(*output_data1[i] == *input_data1[i]);
  }

  for (uint32_t i = 0; i < output_data2.size(); i++) {
    EXPECT_TRUE(*output_data2[i] == *input_data2[i]);
  }
}

}  // namespace test_app_launch
}  // namespace components
}  // namespace test