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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.model.basicmodel.handling;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertTrue;
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;
36 @RunWith(MockitoJUnitRunner.class)
37 public class ApexModelWriterTest {
39 public void testModelWriter() throws IOException, ApexException {
40 ApexModelWriter<AxModel> modelWriter = new ApexModelWriter<AxModel>(AxModel.class);
42 modelWriter.setValidate(true);
43 assertTrue(modelWriter.isValidate());
45 ByteArrayOutputStream baos = new ByteArrayOutputStream();
47 AxModel model = new DummyApexBasicModelCreator().getModel();
49 modelWriter.write(model, baos);
51 modelWriter.setValidate(false);
52 modelWriter.write(model, baos);
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);
60 assertThatThrownBy(() -> modelWriter.write(null, baos))
61 .hasMessage("concept may not be null");
63 ByteArrayOutputStream nullBaos = null;
64 assertThatThrownBy(() -> modelWriter.write(model, nullBaos))
65 .hasMessage("concept stream may not be null");