46feebb131bf52bbb2ddb12361ed5c99d57b4da2
[aaf/authz.git] / misc / env / src / test / java / org / onap / aaf / misc / env / jaxb / JU_JAXBObjectifierTest.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.StringReader;
29
30 import javax.xml.bind.JAXBException;
31 import javax.xml.validation.Schema;
32
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.Mock;
36 import org.mockito.Mockito;
37 import org.onap.aaf.misc.env.APIException;
38 import org.onap.aaf.misc.env.Env;
39 import org.onap.aaf.misc.env.LogTarget;
40 import org.onap.aaf.misc.env.TimeTaken;
41
42 public class JU_JAXBObjectifierTest {
43
44         @Mock
45         JAXBumar jumar;
46         
47         @Mock
48         Schema schema;
49         
50         @Mock
51         Env env;
52         
53         TimeTaken tt,ttObjectify;
54         
55         LogTarget logT;
56         
57         @Before
58         public void setUp() {
59                 initMocks(this);
60                 tt=Mockito.mock(TimeTaken.class);
61         Mockito.doReturn(tt).when(env).start("JAXB Unmarshal", Env.XML);
62                 Mockito.doNothing().when(tt).done();
63                 logT = Mockito.mock(LogTarget.class);
64                 Mockito.doReturn(logT).when(env).debug();
65         }
66
67         @Test
68     public void testObjectify() {
69                 JAXBObjectifier<?> bdfObj = null;
70                 try {
71                         bdfObj = new JAXBObjectifier( schema, new Class[] {this.getClass()});
72                         bdfObj = new JAXBObjectifier(jumar);
73                         Mockito.doReturn(this.getClass()).when(jumar).unmarshal(logT, "test");
74                         bdfObj.objectify(env, "test");
75                 } catch (APIException e) {
76                         assertTrue(e.getMessage().contains("Test Exception"));
77                 } catch (JAXBException e) {
78                         // TODO Auto-generated catch block
79                         e.printStackTrace();
80                 } 
81         
82     }
83         
84         @Test
85     public void testObjectifyException() {
86                 JAXBObjectifier<?> bdfObj = null;
87                 try {
88                         bdfObj = new JAXBObjectifier(jumar);
89                         Mockito.doThrow(new JAXBException("Test Exception")).when(jumar).unmarshal(logT, "test");
90                         bdfObj.objectify(env, "test");
91                 } catch (APIException e) {
92                         assertTrue(e.getMessage().contains("Test Exception"));
93                 } catch (JAXBException e) {
94                         // TODO Auto-generated catch block
95                         e.printStackTrace();
96                 } 
97         
98     }
99         
100         @Test
101     public void testObjectifyRdr() {
102                 JAXBObjectifier<?> bdfObj = null;
103                 try {
104                         bdfObj = new JAXBObjectifier(new Class[] {this.getClass()});
105                         bdfObj = new JAXBObjectifier(jumar);
106                         Mockito.doReturn(this.getClass()).when(jumar).unmarshal(logT, Mockito.mock(StringReader.class));
107                         bdfObj.objectify(env, Mockito.mock(StringReader.class));
108                 } catch (APIException e) {
109                         assertTrue(e.getMessage().contains("Test Exception"));
110                 } catch (JAXBException e) {
111                         // TODO Auto-generated catch block
112                         e.printStackTrace();
113                 } 
114         
115     }
116         
117         @Test
118     public void testObjectifyRdrException() {
119                 JAXBObjectifier<?> bdfObj = null;
120                 try {
121                         bdfObj = new JAXBObjectifier(jumar);
122                         StringReader sr = Mockito.mock(StringReader.class);
123                         Mockito.doThrow(new JAXBException("Test Exception")).when(jumar).unmarshal(logT, sr);
124                         bdfObj.objectify(env, sr);
125                 } catch (APIException e) {
126                         assertTrue(e.getMessage().contains("Test Exception"));
127                 } catch (JAXBException e) {
128                         // TODO Auto-generated catch block
129                         e.printStackTrace();
130                 } 
131         
132     }
133         
134         @Test
135     public void testObjectifyIs() {
136                 JAXBObjectifier<?> bdfObj = null;
137                 try {
138                         bdfObj = new JAXBObjectifier(jumar);
139                         Mockito.doReturn(this.getClass()).when(jumar).unmarshal(logT, Mockito.mock(InputStream.class));
140                         bdfObj.objectify(env, Mockito.mock(InputStream.class));
141                 } catch (APIException e) {
142                         assertTrue(e.getMessage().contains("Test Exception"));
143                 } catch (JAXBException e) {
144                         // TODO Auto-generated catch block
145                         e.printStackTrace();
146                 } 
147         
148     }
149         
150         @Test
151     public void testObjectifyIsException() {
152                 JAXBObjectifier<?> bdfObj = null;
153                 try {
154                         bdfObj = new JAXBObjectifier(jumar);
155                         InputStream sr = Mockito.mock(InputStream.class);
156                         Mockito.doThrow(new JAXBException("Test Exception")).when(jumar).unmarshal(logT, sr);
157                         bdfObj.objectify(env, sr);
158                 } catch (APIException e) {
159                         assertTrue(e.getMessage().contains("Test Exception"));
160                 } catch (JAXBException e) {
161                         // TODO Auto-generated catch block
162                         e.printStackTrace();
163                 } 
164         
165     }
166         
167         @Test
168     public void testEmptyMethods() {
169                 JAXBObjectifier<?> bdfObj = null;
170                 try {
171                         bdfObj = new JAXBObjectifier(jumar);
172                         bdfObj.servicePrestart(env);
173                         bdfObj.threadPrestart(env);
174                         bdfObj.threadDestroy(env);
175                         bdfObj.serviceDestroy(env);
176                         bdfObj.refresh(env);
177                 } catch (APIException e) {
178                         assertTrue(e.getMessage().contains("Test Exception"));
179                 } 
180         
181     }
182         
183         @Test
184     public void testNewInstance() {
185                 JAXBObjectifier<?> bdfObj = null;
186                 try {
187                         bdfObj = new JAXBObjectifier(jumar);
188                         Object retVal = bdfObj.newInstance();
189                         Mockito.doThrow(new IllegalAccessException("Test Exception")).when(jumar).newInstance();
190                 
191                 } catch (IllegalAccessException e) {
192                         assertEquals("Test Exception", e.getLocalizedMessage());
193                 } catch (APIException e) {
194                         assertTrue(e.getMessage().contains("Test Exception"));
195                 } catch (InstantiationException e) {
196                         // TODO Auto-generated catch block
197                         e.printStackTrace();
198                 }
199     }
200         
201         @Test
202     public void testNewInstanceException() {
203                 JAXBObjectifier<?> bdfObj = null;
204                 try {
205                         bdfObj = new JAXBObjectifier(jumar);
206                         Mockito.doThrow(new IllegalAccessException("Test Exception")).when(jumar).newInstance();
207                         Object retVal = bdfObj.newInstance();
208                 } catch (IllegalAccessException e) {
209                         assertEquals("Test Exception", e.getLocalizedMessage());
210                 } catch (APIException e) {
211                         assertTrue(e.getMessage().contains("Test Exception"));
212                 } catch (InstantiationException e) {
213                         // TODO Auto-generated catch block
214                         e.printStackTrace();
215                 }
216         
217     }
218 }