063855976fd6f900ea17e2b84f914527a0d53346
[policy/apex-pdp.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020,2022 Nordix Foundation
5  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.model.basicmodel.handling;
24
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertTrue;
27
28 import java.io.ByteArrayOutputStream;
29 import java.io.IOException;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.runners.MockitoJUnitRunner;
33 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxModel;
35
36 @RunWith(MockitoJUnitRunner.class)
37 public class ApexModelWriterTest {
38     @Test
39     public void testModelWriter() throws IOException, ApexException {
40         ApexModelWriter<AxModel> modelWriter = new ApexModelWriter<AxModel>(AxModel.class);
41
42         modelWriter.setValidate(true);
43         assertTrue(modelWriter.isValidate());
44
45         ByteArrayOutputStream baos = new ByteArrayOutputStream();
46
47         AxModel model = new DummyApexBasicModelCreator().getModel();
48
49         modelWriter.write(model, baos);
50
51         modelWriter.setValidate(false);
52         modelWriter.write(model, baos);
53
54         modelWriter.setValidate(true);
55         model.getKeyInformation().getKeyInfoMap().clear();
56         assertThatThrownBy(() -> modelWriter.write(model, baos))
57             .hasMessageContaining("Apex concept (BasicModel:0.0.1) validation failed");
58         model.getKeyInformation().generateKeyInfo(model);
59
60         assertThatThrownBy(() -> modelWriter.write(null, baos))
61             .hasMessage("concept may not be null");
62
63         ByteArrayOutputStream nullBaos = null;
64         assertThatThrownBy(() -> modelWriter.write(model, nullBaos))
65             .hasMessage("concept stream may not be null");
66     }
67 }