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