Return List<Artifact> in ArtifactDownloadManager
[aai/model-loader.git] / src / test / java / org / onap / aai / modelloader / entity / model / TestModelArtifactHandler.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 European Software Marketing Ltd.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.modelloader.entity.model;
22
23 import static org.hamcrest.CoreMatchers.is;
24 import static org.hamcrest.MatcherAssert.assertThat;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
29
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.List;
33
34 import org.junit.jupiter.api.BeforeEach;
35 import org.junit.jupiter.api.Test;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38 import org.onap.aai.modelloader.config.ModelLoaderConfig;
39 import org.onap.aai.modelloader.entity.Artifact;
40 import org.onap.aai.modelloader.restclient.AaiRestClient;
41 import org.springframework.http.HttpStatus;
42 import org.springframework.http.ResponseEntity;
43
44 /**
45  * Test the Model Artifact Handler using Mocks
46  *
47  */
48 public class TestModelArtifactHandler {
49
50     @Mock
51     private ModelLoaderConfig config;
52
53     @Mock
54     private AaiRestClient aaiClient;
55
56     @BeforeEach
57     public void setupMocks() {
58         MockitoAnnotations.openMocks(this);
59     }
60
61     @Test
62     public void testEmptyLists() {
63         ModelArtifactHandler handler = new ModelArtifactHandler(config);
64         handler.pushArtifacts(Collections.emptyList(), "", Collections.emptyList(), aaiClient);
65         handler.rollback(Collections.emptyList(), "", aaiClient);
66         assertTrue(true);
67     }
68
69     @Test
70     public void testPushExistingModelsWithRollback() {
71         when(config.getAaiBaseUrl()).thenReturn("");
72         when(config.getAaiModelUrl(any())).thenReturn("");
73
74         ResponseEntity operationResult = mock(ResponseEntity.class);
75         when(aaiClient.getResource(any(), any(), any(), any())).thenReturn(operationResult);
76         when(operationResult.getStatusCode()).thenReturn(HttpStatus.OK);
77
78         List<Artifact> artifacts = new ArrayList<>();
79         Artifact artifact = new ModelArtifact();
80         artifacts.add(artifact);
81
82         ModelArtifactHandler handler = new ModelArtifactHandler(config);
83         boolean pushed = handler.pushArtifacts(artifacts, "", Collections.emptyList(), aaiClient);
84         assertTrue(pushed);
85         handler.rollback(artifacts, "", aaiClient);
86     }
87
88     @Test
89     public void testPushNewModelsWithRollback() {
90         when(config.getAaiBaseUrl()).thenReturn("");
91         when(config.getAaiModelUrl(any())).thenReturn("");
92         when(config.getAaiNamedQueryUrl(any())).thenReturn("");
93
94         ResponseEntity getResult = mock(ResponseEntity.class);
95         when(aaiClient.getResource(any(), any(), any(), any())).thenReturn(getResult);
96         when(getResult.getStatusCode()).thenReturn(HttpStatus.NOT_FOUND);
97
98         ResponseEntity putResult = mock(ResponseEntity.class);
99         when(aaiClient.putResource(any(), any(), any(), any(), any())).thenReturn(putResult);
100         when(putResult.getStatusCode()).thenReturn(HttpStatus.CREATED);
101
102         List<Artifact> artifacts = new ArrayList<>();
103         artifacts.add(new ModelArtifact());
104         NamedQueryArtifact namedQueryArtifact = new NamedQueryArtifact();
105         namedQueryArtifact.setNamedQueryUuid("fred");
106         namedQueryArtifact.setModelNamespace("http://org.onap.aai.inventory/v13");
107         artifacts.add(namedQueryArtifact);
108
109         List<Artifact> completedArtifacts = new ArrayList<>();
110         ModelArtifactHandler handler = new ModelArtifactHandler(config);
111         boolean pushed = handler.pushArtifacts(artifacts, "", completedArtifacts, aaiClient);
112         assertThat(pushed, is(true));
113         handler.rollback(artifacts, "", aaiClient);
114     }
115
116     @Test
117     public void testPushNewModelsBadRequest() {
118         when(config.getAaiBaseUrl()).thenReturn("");
119         when(config.getAaiModelUrl(any())).thenReturn("");
120         when(config.getAaiNamedQueryUrl(any())).thenReturn("");
121
122         ResponseEntity getResult = mock(ResponseEntity.class);
123         when(aaiClient.getResource(any(), any(), any(), any())).thenReturn(getResult);
124         when(getResult.getStatusCode()).thenReturn(HttpStatus.NOT_FOUND);
125
126         ResponseEntity putResult = mock(ResponseEntity.class);
127         when(aaiClient.putResource(any(), any(), any(), any(), any())).thenReturn(putResult);
128         when(putResult.getStatusCode()).thenReturn(HttpStatus.BAD_REQUEST);
129
130         checkRollback(Collections.singletonList(new ModelArtifact()));
131     }
132
133     @Test
134     public void testBadRequestResourceModelResult() {
135         when(config.getAaiBaseUrl()).thenReturn("");
136         when(config.getAaiModelUrl(any())).thenReturn("");
137
138         ResponseEntity operationResult = mock(ResponseEntity.class);
139         when(aaiClient.getResource(any(), any(), any(), any())).thenReturn(operationResult);
140         when(operationResult.getStatusCode()).thenReturn(HttpStatus.BAD_REQUEST);
141
142         checkRollback(Collections.singletonList(new ModelArtifact()));
143     }
144
145     private void checkRollback(List<Artifact> artifacts) {
146         ModelArtifactHandler handler = new ModelArtifactHandler(config);
147         boolean pushed = handler.pushArtifacts(artifacts, "", Collections.emptyList(), aaiClient);
148         assertThat(pushed, is(false));
149         handler.rollback(artifacts, "", aaiClient);
150     }
151 }
152