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 java.util.Arrays;
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;
42 public class WatchdogComponentDistributionStatusDbTest {
45 private AbstractSessionFactoryManager sessionFactoryRequest;
47 private SessionFactory sessionFactory;
49 private Session session;
52 public ExpectedException thrown = ExpectedException.none();
56 MockitoAnnotations.initMocks(this);
57 when(sessionFactory.openSession()).thenReturn(session);
58 when(sessionFactoryRequest.getSessionFactory()).thenReturn(sessionFactory);
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);
72 wds.insertWatchdogComponentDistributionStatus("myId", "myComponentName", "myStatus");
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));
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));
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"));
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"));
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"));