Fix check for return value of EssenceType.
[libdcp.git] / test / read_change_write_test.cc
1 /*
2     Copyright (C) 2022 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     libdcp is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with libdcp.  If not, see <http://www.gnu.org/licenses/>.
18
19     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34
35 #include "cpl.h"
36 #include "dcp.h"
37 #include "reel.h"
38 #include "reel_mono_picture_asset.h"
39 #include "reel_sound_asset.h"
40 #include "test.h"
41 #include <boost/filesystem.hpp>
42 #include <boost/test/unit_test.hpp>
43
44
45 using std::make_shared;
46 using std::string;
47
48
49 /* Read a DCP, change a few things and write it back */
50 BOOST_AUTO_TEST_CASE(read_change_write_test)
51 {
52         boost::filesystem::path path = "build/test/read_change_write_test";
53         boost::filesystem::remove_all(path);
54         boost::filesystem::create_directories(path);
55         auto in_picture = simple_picture(path, "1");
56         auto in_sound = simple_sound(path, "1", {}, "de-DE");
57         auto in_reel_picture = make_shared<dcp::ReelMonoPictureAsset>(in_picture, 0);
58         auto in_reel_sound = make_shared<dcp::ReelSoundAsset>(in_sound, 0);
59         auto in_reel = make_shared<dcp::Reel>(in_reel_picture, in_reel_sound);
60         auto in_cpl = make_shared<dcp::CPL>("Input CPL", dcp::ContentKind::FEATURE, dcp::Standard::SMPTE);
61         in_cpl->add(in_reel);
62         dcp::DCP in_dcp(path);
63         in_dcp.add(in_cpl);
64         in_dcp.set_issuer("my great issuer");
65         in_dcp.set_creator("the creator");
66         in_dcp.write_xml();
67
68         dcp::DCP work_dcp(path);
69         work_dcp.read();
70         auto add_picture = simple_picture(path, "2");
71         auto add_sound = simple_sound(path, "2", {}, "de-DE");
72         auto add_reel_picture = make_shared<dcp::ReelMonoPictureAsset>(add_picture, 0);
73         auto add_reel_sound = make_shared<dcp::ReelSoundAsset>(add_sound, 0);
74         auto add_reel = make_shared<dcp::Reel>(add_reel_picture, add_reel_sound);
75         auto add_cpl = make_shared<dcp::CPL>("Added CPL", dcp::ContentKind::FEATURE, dcp::Standard::SMPTE);
76         add_cpl->add(add_reel);
77         work_dcp.add(add_cpl);
78         work_dcp.write_xml();
79
80         auto id_in_xml = [](cxml::Document const& doc, string id) {
81                 auto assets = doc.node_child("AssetList")->node_children("Asset");
82                 return std::find_if(assets.begin(), assets.end(), [id](cxml::ConstNodePtr asset) {
83                         return dcp::remove_urn_uuid(asset->string_child("Id")) == id;
84                 }) != assets.end();
85         };
86
87         cxml::Document check_pkl("PackingList");
88         check_pkl.read_file(find_file(path, "pkl_"));
89
90         BOOST_CHECK_EQUAL(check_pkl.string_child("Issuer"), "my great issuer");
91         BOOST_CHECK_EQUAL(check_pkl.string_child("Creator"), "the creator");
92
93         BOOST_CHECK(id_in_xml(check_pkl, in_picture->id()));
94         BOOST_CHECK(id_in_xml(check_pkl, in_sound->id()));
95         BOOST_CHECK(id_in_xml(check_pkl, in_cpl->id()));
96         BOOST_CHECK(id_in_xml(check_pkl, add_picture->id()));
97         BOOST_CHECK(id_in_xml(check_pkl, add_sound->id()));
98         BOOST_CHECK(id_in_xml(check_pkl, add_cpl->id()));
99
100         auto const pkl_id = dcp::remove_urn_uuid(check_pkl.string_child("Id"));
101
102         cxml::Document check_assetmap("AssetMap");
103         check_assetmap.read_file(path / "ASSETMAP.xml");
104
105         BOOST_CHECK_EQUAL(check_assetmap.string_child("Issuer"), "my great issuer");
106         BOOST_CHECK_EQUAL(check_assetmap.string_child("Creator"), "the creator");
107
108         BOOST_CHECK(id_in_xml(check_assetmap, pkl_id));
109
110         BOOST_CHECK(id_in_xml(check_assetmap, in_picture->id()));
111         BOOST_CHECK(id_in_xml(check_assetmap, in_sound->id()));
112         BOOST_CHECK(id_in_xml(check_assetmap, in_cpl->id()));
113         BOOST_CHECK(id_in_xml(check_assetmap, add_picture->id()));
114         BOOST_CHECK(id_in_xml(check_assetmap, add_sound->id()));
115         BOOST_CHECK(id_in_xml(check_assetmap, add_cpl->id()));
116 }
117