Sonar Fixes, Formatting
[aaf/authz.git] / misc / env / src / test / java / org / onap / aaf / misc / env / jaxb / JU_JAXBStringifierTest.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 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  * ============LICENSE_END====================================================
19  *
20  */
21
22 package org.onap.aaf.misc.env.jaxb;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.MockitoAnnotations.initMocks;
27
28 import java.io.InputStream;
29 import java.io.OutputStream;
30 import java.io.StringWriter;
31
32 import javax.xml.bind.JAXBException;
33 import javax.xml.namespace.QName;
34
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.mockito.Mock;
38 import org.mockito.Mockito;
39 import org.onap.aaf.misc.env.APIException;
40 import org.onap.aaf.misc.env.Env;
41 import org.onap.aaf.misc.env.LogTarget;
42 import org.onap.aaf.misc.env.TimeTaken;
43
44 public class JU_JAXBStringifierTest {
45
46     @Mock
47     JAXBmar jumar;
48
49     @Mock
50     QName qname;
51
52     @Mock
53     Env env;
54
55     TimeTaken tt,ttstringify;
56
57     LogTarget logT;
58
59     @Before
60     public void setUp() {
61         initMocks(this);
62         tt=Mockito.mock(TimeTaken.class);
63         Mockito.doReturn(tt).when(env).start("JAXB Marshal", Env.XML);
64         Mockito.doNothing().when(tt).done();
65         logT = Mockito.mock(LogTarget.class);
66         Mockito.doReturn(logT).when(env).debug();
67     }
68
69     @Test
70     public void teststringify() {
71         JAXBStringifier<JAXBmar> bdfObj = null;
72         try {
73             bdfObj = new JAXBStringifier<JAXBmar>( qname, new Class[] {this.getClass()});
74             bdfObj = new JAXBStringifier<JAXBmar>(jumar);
75             Mockito.doReturn(this.getClass()).when(jumar).marshal(logT, jumar, Mockito.mock(StringWriter.class), true);
76             bdfObj.stringify(env, jumar, true);
77         } catch (APIException e) {
78             assertTrue(e.getMessage().contains("Test Exception"));
79         } catch (JAXBException e) {
80             // TODO Auto-generated catch block
81             e.printStackTrace();
82         }
83
84     }
85
86     @Test
87     public void teststringifyWriter() {
88         JAXBStringifier<JAXBmar> bdfObj = null;
89         try {
90             bdfObj = new JAXBStringifier<JAXBmar>(new Class[] {this.getClass()});
91             bdfObj = new JAXBStringifier<JAXBmar>(jumar);
92             Mockito.doReturn(this.getClass()).when(jumar).marshal(logT, jumar, Mockito.mock(StringWriter.class), true);
93             bdfObj.stringify(env, jumar, Mockito.mock(StringWriter.class), true);
94         } catch (APIException e) {
95             assertTrue(e.getMessage().contains("Test Exception"));
96         } catch (JAXBException e) {
97             // TODO Auto-generated catch block
98             e.printStackTrace();
99         }
100
101     }
102
103     @Test
104     public void teststringifyWriterException() {
105         JAXBStringifier<JAXBmar> bdfObj = null;
106         try {
107             bdfObj = new JAXBStringifier<JAXBmar>(jumar);
108             StringWriter sr = Mockito.mock(StringWriter.class);
109             Mockito.doThrow(new JAXBException("Test Exception")).when(jumar).marshal(logT, jumar, sr, true);
110             bdfObj.stringify(env, jumar, sr, true);
111         } catch (APIException e) {
112             assertTrue(e.getMessage().contains("Test Exception"));
113         } catch (JAXBException e) {
114             // TODO Auto-generated catch block
115             e.printStackTrace();
116         }
117
118     }
119
120     @Test
121     public void teststringifyOs() {
122         JAXBStringifier<JAXBmar> bdfObj = null;
123         try {
124             bdfObj = new JAXBStringifier<JAXBmar>(jumar);
125             Mockito.doReturn(this.getClass()).when(jumar).marshal(logT, jumar, Mockito.mock(OutputStream.class), true);
126             bdfObj.stringify(env, jumar, Mockito.mock(OutputStream.class), true);
127         } catch (APIException e) {
128             assertTrue(e.getMessage().contains("Test Exception"));
129         } catch (JAXBException e) {
130             // TODO Auto-generated catch block
131             e.printStackTrace();
132         }
133
134     }
135
136     @Test
137     public void teststringifyOsException() {
138         JAXBStringifier<JAXBmar> bdfObj = null;
139         try {
140             bdfObj = new JAXBStringifier<JAXBmar>(jumar);
141             OutputStream os = Mockito.mock(OutputStream.class);
142             Mockito.doThrow(new JAXBException("Test Exception")).when(jumar).marshal(logT, jumar, os, true);
143             bdfObj.stringify(env, jumar, os, true);
144         } catch (APIException e) {
145             assertTrue(e.getMessage().contains("Test Exception"));
146         } catch (JAXBException e) {
147             // TODO Auto-generated catch block
148             e.printStackTrace();
149         }
150
151     }
152
153     @Test
154     public void testEmptyMethods() {
155         JAXBStringifier<JAXBmar> bdfObj = null;
156         try {
157             bdfObj = new JAXBStringifier<JAXBmar>(jumar);
158             bdfObj.servicePrestart(env);
159             bdfObj.threadPrestart(env);
160             bdfObj.threadDestroy(env);
161             bdfObj.serviceDestroy(env);
162             bdfObj.refresh(env);
163         } catch (APIException e) {
164             assertTrue(e.getMessage().contains("Test Exception"));
165         }
166
167     }
168
169     @Test
170     public void testPretty() {
171         JAXBStringifier<JAXBmar> bdfObj = null;
172         bdfObj = new JAXBStringifier<JAXBmar>(jumar);
173         Mockito.doReturn(jumar).when(jumar).pretty(true);
174         Object retVal = bdfObj.pretty(true);
175         assertTrue(retVal instanceof JAXBStringifier);
176     }
177
178     @Test
179     public void testNewInstanceException() {
180         JAXBStringifier<JAXBmar> bdfObj = null;
181         bdfObj = new JAXBStringifier<JAXBmar>(jumar);
182         Mockito.doReturn(jumar).when(jumar).asFragment(true);
183         Object retVal = bdfObj.asFragment(true);
184         assertTrue(retVal instanceof JAXBStringifier);
185
186     }
187 }