Unit test for ChampDataService.java
[aai/champ.git] / champ-service / src / test / java / org / onap / champ / service / ChampDataServiceTest.java
1 /**
2  * ============LICENSE_START==========================================
3  * org.onap.aai
4  * ===================================================================
5  * Copyright © 2019 IBM
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 package org.onap.champ.service;
21
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.onap.aai.champcore.ChampGraph;
26 import org.onap.aai.champcore.ChampTransaction;
27 import org.onap.aai.champcore.exceptions.ChampTransactionException;
28 import org.onap.aai.champcore.exceptions.ChampUnmarshallingException;
29 import org.onap.aai.champcore.model.ChampElement;
30 import org.onap.aai.champcore.model.ChampObject;
31 import org.onap.champ.exception.ChampServiceException;
32 import org.onap.champ.util.ChampProperties;
33 import org.powermock.core.classloader.annotations.PrepareForTest;
34 import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
35 import org.powermock.modules.junit4.PowerMockRunner;
36
37 import java.util.Optional;
38
39 import static org.junit.Assert.assertEquals;
40 import static org.mockito.Matchers.anyString;
41 import static org.mockito.Matchers.eq;
42 import static org.powermock.api.mockito.PowerMockito.*;
43
44
45 @RunWith(PowerMockRunner.class)
46 @SuppressStaticInitializationFor("org.onap.champ.util.ChampProperties")
47 @PrepareForTest({ChampProperties.class, ChampObject.class, ChampElement.class})
48 public class ChampDataServiceTest {
49
50     ChampDataService champDataService;
51     ChampUUIDService champUUIDService;
52     ChampGraph graphImpl;
53     ChampTransactionCache cache;
54
55     @Before
56     public void setUp() throws Exception {
57         mockStatic(ChampProperties.class);
58         when(ChampProperties.get(anyString())).thenReturn("");
59         champUUIDService = mock(ChampUUIDService.class);
60         graphImpl = mock(ChampGraph.class);
61         cache = mock(ChampTransactionCache.class);
62         champDataService = new ChampDataService(champUUIDService, graphImpl, cache);
63     }
64
65     @Test
66     public void getObject() throws ChampServiceException, ChampTransactionException, ChampUnmarshallingException {
67         ChampTransaction transaction = mock(ChampTransaction.class);
68         Optional<ChampObject> retrieved = Optional.of(mock(ChampObject.class));
69         ChampObject element = mock(ChampObject.class);
70
71         when(champUUIDService.getObjectbyUUID(anyString(), eq(transaction))).thenReturn(retrieved);
72         when(champUUIDService.populateUUIDKey(retrieved.get())).thenReturn(element);
73         assertEquals(element, champDataService.getObject("testId", Optional.of(transaction)));
74     }
75 }