Merge "Reorder modifiers"
[so.git] / mso-api-handlers / mso-requests-db / src / test / java / org / openecomp / mso / requestsdb / WatchdogComponentDistributionStatusDbTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 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.openecomp.mso.requestsdb;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.mockito.Matchers.any;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
27
28 import java.util.Arrays;
29
30 import org.hibernate.Query;
31 import org.hibernate.Session;
32 import org.hibernate.SessionFactory;
33 import org.junit.Before;
34 import org.junit.Rule;
35 import org.junit.Test;
36 import org.junit.rules.ExpectedException;
37 import org.mockito.Mock;
38 import org.mockito.MockitoAnnotations;
39 import org.openecomp.mso.db.AbstractSessionFactoryManager;
40
41
42 public class WatchdogComponentDistributionStatusDbTest {
43
44         @Mock
45         private AbstractSessionFactoryManager sessionFactoryRequest;
46         @Mock
47         private SessionFactory sessionFactory;
48         @Mock
49         private Session session;
50
51         @Rule
52         public ExpectedException thrown = ExpectedException.none();
53
54         @Before
55         public void setUp() {
56                 MockitoAnnotations.initMocks(this);
57                 when(sessionFactory.openSession()).thenReturn(session);
58                 when(sessionFactoryRequest.getSessionFactory()).thenReturn(sessionFactory);
59
60         }
61
62         @Test
63         public void insertFailure() {
64                 WatchdogComponentDistributionStatusDb wds = new WatchdogComponentDistributionStatusDb(this.sessionFactoryRequest);
65                 Query mockQuery = mock(Query.class);
66                 when(session.createQuery(any(String.class))).thenReturn(mockQuery);
67                 when(mockQuery.uniqueResult()).thenReturn(null);
68                 when(session.isOpen()).thenReturn(true);
69                 when(session.getTransaction()).thenThrow(Exception.class);
70                 thrown.expect(Exception.class);
71
72                 wds.insertWatchdogComponentDistributionStatus("myId", "myComponentName", "myStatus");
73         }
74
75
76         @Test
77         public void getWatchdogComponentNamesTest() {
78                 WatchdogComponentDistributionStatusDb wds = new WatchdogComponentDistributionStatusDb(this.sessionFactoryRequest);
79                 Query mockQuery = mock(Query.class);
80                 when(session.createQuery(any(String.class))).thenReturn(mockQuery);
81                 when(mockQuery.list()).thenReturn(Arrays.asList("myValue"));
82                 when(session.isOpen()).thenReturn(true);
83                 assertEquals("myValue", wds.getWatchdogComponentNames("myId").get(0));
84         }
85
86         @Test
87         public void getWatchdogComponentDistributionStatusTest() {
88                 WatchdogComponentDistributionStatusDb wds = new WatchdogComponentDistributionStatusDb(this.sessionFactoryRequest);
89                 Query mockQuery = mock(Query.class);
90                 when(session.createQuery(any(String.class))).thenReturn(mockQuery);
91                 when(mockQuery.list()).thenReturn(Arrays.asList("myValue"));
92                 when(session.isOpen()).thenReturn(true);
93                 assertEquals("myValue", wds.getWatchdogComponentDistributionStatus("myStatus").get(0));
94                 assertEquals("myValue", wds.getWatchdogComponentDistributionStatus("myId", "myName").get(0));
95         }
96         
97         @Test
98         public void testGetWatchdogDistributionIdNotFound() {
99                 WatchdogDistributionStatusDb wds = new WatchdogDistributionStatusDb(this.sessionFactoryRequest);
100                 Query mockQuery = mock(Query.class);
101                 when(session.createQuery(any(String.class))).thenReturn(mockQuery);
102                 when(mockQuery.uniqueResult()).thenReturn(null);
103                 when(session.isOpen()).thenReturn(true);
104                 assertEquals(null, wds.getWatchdogDistributionId("test"));
105         }
106
107         @Test
108         public void testGetWatchdogDistributionIdStatus() {
109                 WatchdogDistributionStatusDb wds = new WatchdogDistributionStatusDb(this.sessionFactoryRequest);
110                 Query mockQuery = mock(Query.class);
111                 when(session.createQuery(any(String.class))).thenReturn(mockQuery);
112                 when(mockQuery.uniqueResult()).thenReturn("myValue");
113                 when(session.isOpen()).thenReturn(true);
114                 assertEquals("myValue", wds.getWatchdogDistributionIdStatus("test"));
115         }
116
117         @Test
118         public void testGetWatchdogDistributionIdStatusNotFound() {
119                 WatchdogDistributionStatusDb wds = new WatchdogDistributionStatusDb(this.sessionFactoryRequest);
120                 Query mockQuery = mock(Query.class);
121                 when(session.createQuery(any(String.class))).thenReturn(mockQuery);
122                 when(mockQuery.uniqueResult()).thenReturn(null);
123                 when(session.isOpen()).thenReturn(true);
124                 assertEquals(null, wds.getWatchdogDistributionIdStatus("test"));
125         }
126
127 }