aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/dcae/inventory/exceptions/mappers/DBIExceptionMapperTests.java
blob: 21feaba5cfa3a01dec1c88b5982c4ede0572396a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*-
 * ============LICENSE_START=======================================================
 * dcae-inventory
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.dcae.inventory.exceptions.mappers;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import javax.ws.rs.core.Response;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.onap.dcae.inventory.daos.InventoryDAOManager;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.skife.jdbi.v2.exceptions.UnableToCreateStatementException;
import org.skife.jdbi.v2.exceptions.UnableToExecuteStatementException;
import org.skife.jdbi.v2.exceptions.UnableToObtainConnectionException;

import io.swagger.api.ApiResponseMessage;

/**
 * Created by mhwang on 3/8/17.
 */
@PrepareForTest({InventoryDAOManager.class})
@RunWith(PowerMockRunner.class)
public class DBIExceptionMapperTests {

    @Test
    public void testReinitializeSuccess() {
        // PowerMockito does bytecode magic to mock static methods and use final classes
        PowerMockito.mockStatic(InventoryDAOManager.class);
        InventoryDAOManager mockDaoManager = mock(InventoryDAOManager.class);
        when(InventoryDAOManager.getInstance()).thenReturn(mockDaoManager);

        RuntimeException fakeException = new RuntimeException("Spoofing database failure");

        // Test UnableToObtainConnectionExceptionMapper

        DBIExceptionMapper.UnableToObtainConnectionExceptionMapper mapperOne =
	    new DBIExceptionMapper.UnableToObtainConnectionExceptionMapper();
        Response responseOne = mapperOne.toResponse(new UnableToObtainConnectionException(fakeException));
        assert responseOne.getStatus() == 502;
        String messageOne = ((ApiResponseMessage) responseOne.getEntity()).getMessage();
        assert messageOne.contains("successfully reset");

        // Test UnableToCreateStatementExceptionMapper

        DBIExceptionMapper.UnableToCreateStatementExceptionMapper mapperTwo =
	    new DBIExceptionMapper.UnableToCreateStatementExceptionMapper();
        Response responseTwo = mapperTwo.toResponse(new UnableToCreateStatementException(fakeException));
        assert responseTwo.getStatus() == 502;
        String messageTwo = ((ApiResponseMessage) responseTwo.getEntity()).getMessage();
        assert messageTwo.contains("successfully reset");

        // Test UnableToExecuteStatementExceptionMapper

        DBIExceptionMapper.UnableToExecuteStatementExceptionMapper mapperThree =
	    new DBIExceptionMapper.UnableToExecuteStatementExceptionMapper();
        Response responseThree = mapperThree.toResponse(new UnableToExecuteStatementException(fakeException));
        assert responseThree.getStatus() == 502;
        String messageThree = ((ApiResponseMessage) responseThree.getEntity()).getMessage();
        assert messageThree.contains("successfully reset");
    }

    @Test
    public void testReinitializeFailed() {
        // PowerMockito does bytecode magic to mock static methods and use final classes
        PowerMockito.mockStatic(InventoryDAOManager.class);
        InventoryDAOManager mockDaoManager = mock(InventoryDAOManager.class);
        when(InventoryDAOManager.getInstance()).thenReturn(mockDaoManager);
        Mockito.doThrow(new RuntimeException("Spoof initialization failure")).when(mockDaoManager).initialize();

        RuntimeException fakeException = new RuntimeException("Spoofing database failure");

        // Test UnableToObtainConnectionExceptionMapper

        DBIExceptionMapper.UnableToObtainConnectionExceptionMapper mapperOne =
	    new DBIExceptionMapper.UnableToObtainConnectionExceptionMapper();
        Response responseOne = mapperOne.toResponse(new UnableToObtainConnectionException(fakeException));
        assert responseOne.getStatus() == 502;
        String messageOne = ((ApiResponseMessage) responseOne.getEntity()).getMessage();
        assert !messageOne.contains("successfully reset");

        // Test UnableToCreateStatementExceptionMapper

        DBIExceptionMapper.UnableToCreateStatementExceptionMapper mapperTwo =
	    new DBIExceptionMapper.UnableToCreateStatementExceptionMapper();
        Response responseTwo = mapperTwo.toResponse(new UnableToCreateStatementException(fakeException));
        assert responseTwo.getStatus() == 502;
        String messageTwo = ((ApiResponseMessage) responseTwo.getEntity()).getMessage();
        assert !messageTwo.contains("successfully reset");

        // Test UnableToExecuteStatementExceptionMapper

        DBIExceptionMapper.UnableToExecuteStatementExceptionMapper mapperThree =
	    new DBIExceptionMapper.UnableToExecuteStatementExceptionMapper();
        Response responseThree = mapperThree.toResponse(new UnableToExecuteStatementException(fakeException));
        assert responseThree.getStatus() == 502;
        String messageThree = ((ApiResponseMessage) responseThree.getEntity()).getMessage();
        assert !messageThree.contains("successfully reset");
    }

}