2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.mso.requestsdb;
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;
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;
39 public class WatchdogDistributionStatusDbTest {
42 private AbstractSessionFactoryManager sessionFactoryRequest;
44 private SessionFactory sessionFactory;
46 private Session session;
49 public ExpectedException thrown = ExpectedException.none();
53 MockitoAnnotations.initMocks(this);
54 when(sessionFactory.openSession()).thenReturn(session);
55 when(sessionFactoryRequest.getSessionFactory()).thenReturn(sessionFactory);
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);
69 wds.insertWatchdogDistributionId("myId");
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);
81 wds.updateWatchdogDistributionIdStatus("myId", "myStatus");
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"));
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"));
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"));
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"));