Merge "adding catalog-service for mod2"
[dcaegen2/platform.git] / mod2 / catalog-service / src / test / java / org / onap / dcaegen2 / platform / mod / web / service / SpecificationServiceTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  org.onap.dcae
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
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
21 package org.onap.dcaegen2.platform.mod.web.service;
22
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;
38
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.*;
45
46 @ExtendWith({MockitoExtension.class})
47 public class SpecificationServiceTest {
48
49     private SpecificationServiceImpl service;
50
51     @Mock
52     private SpecificationGateway specRepo;
53
54     @Mock
55     private MsInstanceService msInstanceService;
56
57     @Mock
58     private SpecificationValidatorService validatorService;
59
60     @BeforeEach
61     void setUp() {
62         service = new SpecificationServiceImpl();
63         service.setMsInstanceService(msInstanceService);
64         service.setSpecificationValidatorService(validatorService);
65         service.setSpecificationGateway(specRepo);
66     }
67
68     @Test
69     void createSpecificationTest() throws Exception {
70         //given
71         SpecificationRequest request = getSpecificationRequest();
72         Specification specFromRepo = getMockSpecification(DeploymentType.K8S);
73         MsInstance msInstance = MsInstanceObjectMother.createMsInstance();
74
75         when(msInstanceService.getMsInstanceById(MS_INSTANCE_ID)).thenReturn(msInstance);
76         when(specRepo.save(any(Specification.class))).thenReturn(specFromRepo);
77
78         //when
79         Specification spec = service.createSpecification(MS_INSTANCE_ID, request);
80
81         //then
82         assertThatFieldsAreCorrect(request, spec);
83         verifyCalls(request, msInstance);
84
85     }
86
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();
95     }
96
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);
102     }
103 }