2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (c) 2020 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.onap.dcaegen2.platform.mod.web.service;
23 import org.onap.dcaegen2.platform.mod.model.specification.DeploymentType;
24 import org.onap.dcaegen2.platform.mod.model.microserviceinstance.MsInstance;
25 import org.onap.dcaegen2.platform.mod.model.restapi.SpecificationRequest;
26 import org.onap.dcaegen2.platform.mod.model.specification.Specification;
27 import org.onap.dcaegen2.platform.mod.model.specification.SpecificationStatus;
28 import org.onap.dcaegen2.platform.mod.objectmothers.MsInstanceObjectMother;
29 import org.onap.dcaegen2.platform.mod.web.service.microserviceinstance.MsInstanceService;
30 import org.onap.dcaegen2.platform.mod.web.service.specification.SpecificationGateway;
31 import org.onap.dcaegen2.platform.mod.web.service.specification.SpecificationServiceImpl;
32 import org.onap.dcaegen2.platform.mod.web.service.specification.SpecificationValidatorService;
33 import org.junit.jupiter.api.BeforeEach;
34 import org.junit.jupiter.api.Test;
35 import org.junit.jupiter.api.extension.ExtendWith;
36 import org.mockito.Mock;
37 import org.mockito.junit.jupiter.MockitoExtension;
39 import static org.assertj.core.api.Assertions.assertThat;
40 import static org.onap.dcaegen2.platform.mod.objectmothers.MsInstanceObjectMother.MS_INSTANCE_ID;
41 import static org.onap.dcaegen2.platform.mod.objectmothers.SpecificationObjectMother.getMockSpecification;
42 import static org.onap.dcaegen2.platform.mod.objectmothers.SpecificationObjectMother.getSpecificationRequest;
43 import static org.mockito.ArgumentMatchers.any;
44 import static org.mockito.Mockito.*;
46 @ExtendWith({MockitoExtension.class})
47 public class SpecificationServiceTest {
49 private SpecificationServiceImpl service;
52 private SpecificationGateway specRepo;
55 private MsInstanceService msInstanceService;
58 private SpecificationValidatorService validatorService;
62 service = new SpecificationServiceImpl();
63 service.setMsInstanceService(msInstanceService);
64 service.setSpecificationValidatorService(validatorService);
65 service.setSpecificationGateway(specRepo);
69 void createSpecificationTest() throws Exception {
71 SpecificationRequest request = getSpecificationRequest();
72 Specification specFromRepo = getMockSpecification(DeploymentType.K8S);
73 MsInstance msInstance = MsInstanceObjectMother.createMsInstance();
75 when(msInstanceService.getMsInstanceById(MS_INSTANCE_ID)).thenReturn(msInstance);
76 when(specRepo.save(any(Specification.class))).thenReturn(specFromRepo);
79 Specification spec = service.createSpecification(MS_INSTANCE_ID, request);
82 assertThatFieldsAreCorrect(request, spec);
83 verifyCalls(request, msInstance);
87 private void assertThatFieldsAreCorrect(SpecificationRequest request, Specification spec) {
88 assertThat(spec.getStatus()).isEqualTo(SpecificationStatus.ACTIVE);
89 assertThat(spec.getSpecContent()).isEqualTo(request.getSpecContent());
90 assertThat(spec.getPolicyJson()).isEqualTo(request.getPolicyJson());
91 assertThat(spec.getType()).isEqualTo(request.getType());
92 assertThat(spec.getMetadata().get("createdBy")).isEqualTo(request.getUser());
93 assertThat(spec.getMetadata().get("createdOn")).isNotNull();
94 assertThat(spec.getMsInstanceInfo()).isNotNull();
97 private void verifyCalls(SpecificationRequest request, MsInstance msInstance) {
98 verify(msInstanceService, times(1)).getMsInstanceById(MS_INSTANCE_ID);
99 verify(validatorService, times(1)).validateSpecForRelease(request, msInstance.getRelease());
100 verify(specRepo, times(2)).save(any(Specification.class));
101 verify(msInstanceService, times(1)).updateMsInstance(msInstance);