d0c48cc939f242e8cd77a47da35e511ad961a508
[appc.git] / appc-dispatcher / appc-license-manager / appc-license-manager-core / src / test / java / org / onap / appc / licmgr / impl / XmlToLicenseModelConverterTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2018 Nokia
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  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.appc.licmgr.impl;
23
24 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
25 import static org.mockito.BDDMockito.given;
26 import static org.mockito.BDDMockito.then;
27 import static org.mockito.Matchers.any;
28 import static org.mockito.Matchers.anyString;
29
30 import java.io.IOException;
31 import java.io.InputStream;
32 import javax.xml.stream.XMLInputFactory;
33 import javax.xml.stream.XMLStreamException;
34 import javax.xml.stream.XMLStreamReader;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mock;
38 import org.mockito.runners.MockitoJUnitRunner;
39
40
41 @RunWith(MockitoJUnitRunner.class)
42 public class XmlToLicenseModelConverterTest {
43
44     @Mock
45     private XMLStreamReader xmlStreamReader;
46
47     @Mock
48     private XMLInputFactory xmlInputFactory;
49
50     @Test
51     public void apply_shouldCloseXMLStreamReader_whenNoExceptionIsThrown()
52         throws XMLStreamException, IOException {
53
54         // GIVEN
55         XmlToLicenseModelConverter converter = new XmlToLicenseModelConverter(xmlInputFactory);
56         given(xmlInputFactory.createXMLStreamReader(any(InputStream.class))).willReturn(xmlStreamReader);
57
58         // WHEN
59         converter.convert((a, b) -> {
60         }, anyString(), null);
61
62         // THEN
63         then(xmlStreamReader).should().close();
64     }
65
66     @Test
67     public void apply_shouldCloseXMLStreamReader_whenExceptionIsThrown()
68         throws XMLStreamException {
69         // GIVEN
70         XmlToLicenseModelConverter converter = new XmlToLicenseModelConverter(xmlInputFactory);
71         given(xmlInputFactory.createXMLStreamReader(any(InputStream.class))).willReturn(xmlStreamReader);
72
73         // WHEN THEN
74         assertThatExceptionOfType(XMLStreamException.class)
75             .isThrownBy(() -> converter.convert((a, b) -> {
76                 throw new XMLStreamException();
77             }, anyString(), null));
78         then(xmlStreamReader).should().close();
79     }
80 }