Implant vid-app-common org.onap.vid.job (main and test)
[vid.git] / vid-app-common / src / test / java / org / onap / vid / services / AuditServiceImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2018 - 2019 Nokia. 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 package org.onap.vid.services;
21
22 import org.mockito.InjectMocks;
23 import org.mockito.Mock;
24 import org.mockito.MockitoAnnotations;
25 import org.onap.vid.dal.AsyncInstantiationRepository;
26 import org.onap.vid.model.JobAuditStatus;
27 import org.onap.vid.mso.RestMsoImplementation;
28 import org.onap.vid.mso.RestObject;
29 import org.onap.vid.mso.rest.AsyncRequestStatus;
30 import org.onap.vid.mso.rest.AsyncRequestStatusList;
31 import org.onap.vid.testUtils.TestUtils;
32 import org.testng.annotations.BeforeMethod;
33 import org.testng.annotations.Test;
34
35 import java.util.List;
36
37 import static org.hamcrest.MatcherAssert.assertThat;
38 import static org.hamcrest.Matchers.equalTo;
39 import static org.mockito.ArgumentMatchers.eq;
40 import static org.mockito.Mockito.when;
41
42 public class AuditServiceImplTest {
43
44   @Mock
45   private RestMsoImplementation restMso;
46   @Mock
47   private AsyncInstantiationRepository asyncInstantiationRepository;
48
49   @InjectMocks
50   private AuditServiceImpl auditService;
51
52   @BeforeMethod
53   public void setUp() {
54     restMso = null;
55     asyncInstantiationRepository = null;
56     auditService = null;
57     MockitoAnnotations.initMocks(this);
58   }
59
60   @Test
61   public void testGetRequestsIdsByServiceIdAndRequestTypeAndScope() throws Exception {
62
63     String instanceId = "d40c8a82-cc04-45e5-a0f6-0c9394c8f8d2";
64     //the request id in multipleOrchestrationRequestsServiceInstance.json
65     String expectedRequestId = "fab854bf-e53c-415e-b3cc-b6fcce8414b2";
66     String msoBasePath = "/someMsoPath/v2019?";
67
68     AsyncRequestStatusList asyncRequestStatusList = TestUtils.readJsonResourceFileAsObject(
69         "/responses/mso/multipleOrchestrationRequestsServiceInstance.json",
70         AsyncRequestStatusList.class);
71     RestObject<AsyncRequestStatusList> msoResponse = new RestObject<>();
72     msoResponse.set(asyncRequestStatusList);
73     msoResponse.setStatusCode(200);
74     when(restMso.GetForObject(eq(msoBasePath + "filter=serviceInstanceId:EQUALS:" + instanceId),
75         eq(AsyncRequestStatusList.class)))
76         .thenReturn(msoResponse);
77     TestUtils.testWithSystemProperty("mso.restapi.get.orc.reqs", msoBasePath, () -> {
78       List<AsyncRequestStatus.Request> result = auditService
79           .retrieveRequestsFromMsoByServiceIdAndRequestTypeAndScope(instanceId, "createInstance", "service");
80       assertThat(result.size(), equalTo(1));
81       assertThat(result.get(0).requestId, equalTo(expectedRequestId));
82       assertThat(result.get(0).startTime, equalTo("Mon, 04 Mar 2019 20:47:15 GMT"));
83     });
84   }
85
86   @Test
87   public void nextOrdinalAfter_givenNull_returnZero() {
88     assertThat(
89         auditService.nextOrdinalAfter(null),
90         equalTo(0)
91     );
92   }
93
94   @Test
95   public void nextOrdinalAfter_givenX_returnXplus1() {
96     final int x = 6;
97     final JobAuditStatus jobAuditStatus = new JobAuditStatus();
98     jobAuditStatus.setOrdinal(x);
99
100     assertThat(
101         auditService.nextOrdinalAfter(jobAuditStatus),
102         equalTo(x + 1)
103     );
104   }
105
106 }