Add reason for rApp instance deploy/undeploy failure
[nonrtric/plt/rappmanager.git] / rapp-manager-sme / src / test / java / com / oransc / rappmanager / sme / service / SmeLifecycleManagerTest.java
1 /*-
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2023 Nordix Foundation. All rights reserved.
4  * ===============================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END========================================================================
17  */
18
19 package com.oransc.rappmanager.sme.service;
20
21 import static org.junit.jupiter.api.Assertions.assertFalse;
22 import static org.junit.jupiter.api.Assertions.assertTrue;
23 import static org.mockito.Mockito.doNothing;
24 import static org.mockito.Mockito.times;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
27
28 import com.oransc.rappmanager.sme.provider.data.APIProviderEnrolmentDetails;
29 import org.junit.jupiter.api.Test;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.boot.test.context.SpringBootTest;
32 import org.springframework.boot.test.mock.mockito.MockBean;
33 import org.springframework.boot.test.mock.mockito.SpyBean;
34
35 @SpringBootTest(classes = SmeLifecycleManager.class)
36 class SmeLifecycleManagerTest {
37
38     @Autowired
39     @MockBean
40     SmeDeployer smeDeployer;
41
42     @Autowired
43     @SpyBean
44     SmeLifecycleManager smeLifecycleManager;
45
46     @Test
47     void testStartWithSuccess() {
48         when(smeDeployer.createAMF()).thenReturn(new APIProviderEnrolmentDetails());
49         smeLifecycleManager.start();
50         assertTrue(smeLifecycleManager.isRunning());
51     }
52
53     @Test
54     void testStartWithFailure() {
55         when(smeDeployer.createAMF()).thenReturn(null);
56         smeLifecycleManager.start();
57         assertFalse(smeLifecycleManager.isRunning());
58     }
59
60     @Test
61     void testStopWithSuccess() {
62         doNothing().when(smeDeployer).deleteAMF();
63         when(smeDeployer.createAMF()).thenReturn(new APIProviderEnrolmentDetails());
64         smeLifecycleManager.start();
65         assertTrue(smeLifecycleManager.isRunning());
66         smeLifecycleManager.stop();
67         verify(smeDeployer, times(1)).deleteAMF();
68         assertFalse(smeLifecycleManager.isRunning());
69     }
70
71     @Test
72     void testStopWithoutStart() {
73         smeLifecycleManager.stop();
74         verify(smeDeployer, times(0)).deleteAMF();
75         assertFalse(smeLifecycleManager.isRunning());
76     }
77 }