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