BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
jsonitemconverter.test.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // qt-mvvm: Model-view-view-model framework for large GUI applications
4 //
5 //! @file mvvm/tests/testmodel/jsonitemconverter.test.cpp
6 //! @brief Implements class CLASS?
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2020
11 //! @authors Gennady Pospelov et al, Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
15 #include "folderbasedtest.h"
16 #include "google_test.h"
20 #include "mvvm/model/sessionitem.h"
26 #include "test_utils.h"
27 #include <QJsonArray>
28 #include <QJsonDocument>
29 #include <QJsonObject>
30 
31 using namespace ModelView;
32 
33 //! Checks JsonItem class and its ability to convert SessionItems to json and back.
34 
36 public:
37  class TestItem : public CompoundItem {
38  public:
39  TestItem() : CompoundItem("TestItem")
40  {
41  setToolTip("compound");
42  addProperty("Thickness", 42)->setToolTip("thickness");
43  }
44  };
45 
46  class TestModel : public SessionModel {
47  public:
48  TestModel() : SessionModel("TestModel")
49  {
50  auto catalogue = std::make_unique<ModelView::ItemCatalogue>();
51  catalogue->registerItem<TestItem>();
52  setItemCatalogue(std::move(catalogue));
53  }
54  };
55 
57  : FolderBasedTest("test_JsonItemConverter"), m_model(std::make_unique<TestModel>())
58  {
59  }
61 
62  std::unique_ptr<JsonItemConverter> createConverter()
63  {
64  ConverterContext context{m_model->factory(), ConverterMode::clone};
65  return std::make_unique<JsonItemConverter>(context);
66  }
67 
68 private:
69  std::unique_ptr<SessionModel> m_model;
70 };
71 
73 
74 //! PropertyItem to json object.
75 
76 TEST_F(JsonItemConverterTest, propertyItemToJson)
77 {
78  auto converter = createConverter();
79 
80  PropertyItem item;
81  auto object = converter->to_json(&item);
82 
83  // this object represents SessionItem
84  JsonItemFormatAssistant assistant;
85  EXPECT_TRUE(assistant.isSessionItem(object));
86 }
87 
88 //! PropertyItem to json object and back.
89 
90 TEST_F(JsonItemConverterTest, propertyItemToJsonAndBack)
91 {
92  auto converter = createConverter();
93 
94  PropertyItem item;
95  item.setToolTip("abc");
96  auto object = converter->to_json(&item);
97 
98  auto reco = converter->from_json(object);
99 
100  EXPECT_EQ(reco->modelType(), item.modelType());
101  EXPECT_EQ(reco->displayName(), item.displayName());
102  EXPECT_EQ(reco->identifier(), item.identifier());
103  EXPECT_EQ(reco->toolTip(), std::string("abc"));
104 }
105 
106 //! PropertyItem to json file and back.
107 
108 TEST_F(JsonItemConverterTest, propertyItemToFileAndBack)
109 {
110  auto converter = createConverter();
111 
112  PropertyItem item;
113  auto object = converter->to_json(&item);
114 
115  // saving object to file
116  auto fileName = TestUtils::TestFileName(testDir(), "propertyItemToFileAndBack.json");
117  TestUtils::SaveJson(object, fileName);
118 
119  auto document = TestUtils::LoadJson(fileName);
120  auto reco = converter->from_json(document.object());
121 
122  EXPECT_EQ(reco->parent(), nullptr);
123  EXPECT_EQ(reco->modelType(), item.modelType());
124  EXPECT_EQ(reco->displayName(), item.displayName());
125  EXPECT_EQ(reco->identifier(), item.identifier());
126 }
127 
128 //! Parent and child to json object.
129 
130 TEST_F(JsonItemConverterTest, parentAndChildToJsonAndBack)
131 {
132  auto converter = createConverter();
133  const std::string model_type(Constants::BaseType);
134 
135  auto parent = std::make_unique<SessionItem>(model_type);
136  parent->setDisplayName("parent_name");
137  parent->registerTag(TagInfo::universalTag("defaultTag"), /*set_as_default*/ true);
138  auto child = new SessionItem(model_type);
139  child->setDisplayName("child_name");
140  parent->insertItem(child, TagRow::append());
141 
142  // converting to json
143  auto object = converter->to_json(parent.get());
144  JsonItemFormatAssistant assistant;
145  EXPECT_TRUE(assistant.isSessionItem(object));
146 
147  // converting json back to item
148  auto reco_parent = converter->from_json(object);
149 
150  // checking parent reconstruction
151  EXPECT_EQ(reco_parent->childrenCount(), 1);
152  EXPECT_EQ(reco_parent->modelType(), model_type);
153  EXPECT_EQ(reco_parent->displayName(), "parent_name");
154  EXPECT_EQ(reco_parent->identifier(), parent->identifier());
155  EXPECT_EQ(reco_parent->itemTags()->defaultTag(), "defaultTag");
156  EXPECT_EQ(reco_parent->model(), nullptr);
157 
158  // checking child reconstruction
159  auto reco_child = reco_parent->getItem("defaultTag");
160  EXPECT_EQ(reco_child->parent(), reco_parent.get());
161  EXPECT_EQ(reco_child->childrenCount(), 0);
162  EXPECT_EQ(reco_child->modelType(), model_type);
163  EXPECT_EQ(reco_child->displayName(), "child_name");
164  EXPECT_EQ(reco_child->identifier(), child->identifier());
165  EXPECT_EQ(reco_child->itemTags()->defaultTag(), "");
166 }
167 
168 //! Parent and child to json file and back.
169 
170 TEST_F(JsonItemConverterTest, parentAndChildToFileAndBack)
171 {
172  auto converter = createConverter();
173  const std::string model_type(Constants::BaseType);
174 
175  auto parent = std::make_unique<SessionItem>(model_type);
176  parent->setDisplayName("parent_name");
177  parent->registerTag(TagInfo::universalTag("defaultTag"), /*set_as_default*/ true);
178  auto child = new SessionItem(model_type);
179  child->setDisplayName("child_name");
180  parent->insertItem(child, TagRow::append());
181 
182  // converting to json
183  auto object = converter->to_json(parent.get());
184  JsonItemFormatAssistant assistant;
185  EXPECT_TRUE(assistant.isSessionItem(object));
186 
187  // saving object to file
188  auto fileName = TestUtils::TestFileName(testDir(), "parentAndChildToFileAndBack.json");
189  TestUtils::SaveJson(object, fileName);
190 
191  // converting document back to item
192  auto document = TestUtils::LoadJson(fileName);
193  auto reco_parent = converter->from_json(document.object());
194 
195  // checking parent reconstruction
196  EXPECT_EQ(reco_parent->childrenCount(), 1);
197  EXPECT_EQ(reco_parent->modelType(), model_type);
198  EXPECT_EQ(reco_parent->displayName(), "parent_name");
199  EXPECT_EQ(reco_parent->identifier(), parent->identifier());
200  EXPECT_EQ(reco_parent->itemTags()->defaultTag(), "defaultTag");
201  EXPECT_EQ(reco_parent->model(), nullptr);
202 
203  // checking child reconstruction
204  auto reco_child = reco_parent->getItem("defaultTag");
205  EXPECT_EQ(reco_child->parent(), reco_parent.get());
206  EXPECT_EQ(reco_child->childrenCount(), 0);
207  EXPECT_EQ(reco_child->modelType(), model_type);
208  EXPECT_EQ(reco_child->displayName(), "child_name");
209  EXPECT_EQ(reco_child->identifier(), child->identifier());
210  EXPECT_EQ(reco_child->itemTags()->defaultTag(), "");
211 }
212 
213 //! TestItem to json file and back.
214 
215 TEST_F(JsonItemConverterTest, testItemToFileAndBack)
216 {
217  auto converter = createConverter();
218 
219  TestItem item;
220  auto object = converter->to_json(&item);
221 
222  // saving object to file
223  auto fileName = TestUtils::TestFileName(testDir(), "testItemToFileAndBack.json");
224  TestUtils::SaveJson(object, fileName);
225 
226  auto document = TestUtils::LoadJson(fileName);
227  auto reco = converter->from_json(document.object());
228 
229  EXPECT_EQ(reco->parent(), nullptr);
230  EXPECT_EQ(reco->modelType(), item.modelType());
231  EXPECT_EQ(reco->displayName(), item.displayName());
232  EXPECT_EQ(reco->identifier(), item.identifier());
233 
234  EXPECT_EQ(reco->toolTip(), "compound");
235  // tooltip was preserved after the serialization
236  EXPECT_EQ(reco->getItem("Thickness")->toolTip(), "thickness");
237 }
Convenience class which creates a directory on disk for test content.
Checks JsonItem class and its ability to convert SessionItems to json and back.
std::unique_ptr< JsonItemConverter > createConverter()
std::unique_ptr< SessionModel > m_model
Complex item holding mixed SessionItem types (single properties and other CompountItems).
Definition: compounditem.h:28
Utility class to determine, whether given JSON object can represent various parts of SessionModel.
bool isSessionItem(const QJsonObject &json) const
Returns true if given json object represents SessionItem.
Item to carry concrete editable entity (e.g.
Definition: propertyitem.h:27
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
std::string identifier() const
Returns unique identifier.
Definition: sessionitem.cpp:87
SessionItem * setToolTip(const std::string &tooltip)
Sets item tooltip (fluent interface).
virtual std::string displayName() const
Returns display name.
Definition: sessionitem.cpp:94
model_type modelType() const
Returns item's model type.
Definition: sessionitem.cpp:80
Main class to hold hierarchy of SessionItem objects.
Definition: sessionmodel.h:37
static TagInfo universalTag(std::string name, std::vector< std::string > modelTypes={})
Constructs universal tag intended for unlimited amount of various items.
Definition: taginfo.cpp:34
static TagRow append(const std::string &tag_name={})
Returns TagRow corresponding to the append to tag_name.
Definition: tagrow.cpp:36
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
TEST_F(JsonItemConverterTest, propertyItemToJson)
PropertyItem to json object.
Defines class CLASS?
const model_type BaseType
Definition: mvvm_types.h:45
materialitems.h Collection of materials to populate MaterialModel.
@ clone
full deep copying with item identifiers preserved
std::string model_type
Definition: types.h:23
std::string TestFileName(const std::string &test_sub_dir, const std::string &file_name)
Returns full path to the file in test directory.
Definition: test_utils.cpp:52
void SaveJson(const QJsonObject &object, const std::string &fileName)
Definition: test_utils.cpp:58
QJsonDocument LoadJson(const std::string &fileName)
Definition: test_utils.cpp:81
std::unique_ptr< IUnitConverter > createConverter(const ISimulation &simulation)
Definition: filesystem.h:81
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Collection of input paramters for SessionItemConverter.
Defines class CLASS?