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.hamcrest.Matchers.hasSize;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.when;
30
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.List;
34
35 import org.junit.jupiter.api.BeforeEach;
36 import org.junit.jupiter.api.Test;
37 import org.mockito.Mock;
38 import org.mockito.MockitoAnnotations;
39 import org.onap.aai.modelloader.config.ModelLoaderConfig;
40 import org.onap.aai.modelloader.entity.Artifact;
41 import org.onap.aai.modelloader.restclient.AaiRestClient;
42 import org.springframework.http.HttpStatus;
43 import org.springframework.http.ResponseEntity;
44
45 /**
46  * Test the Model Artifact Handler using Mocks
47  *
48  */
49 public class TestModelArtifactHandler {
50
51     @Mock
52     private ModelLoaderConfig config;
53
54     @Mock
55     private AaiRestClient aaiClient;
56
57     @BeforeEach
58     public void setupMocks() {
59         MockitoAnnotations.openMocks(this);
60     }
61
62     @Test
63     public void testEmptyLists() {
64         ModelArtifactHandler handler = new ModelArtifactHandler(config);
65         handler.pushArtifacts(Collections.emptyList(), "", Collections.emptyList(), aaiClient);
66         handler.rollback(Collections.emptyList(), "", aaiClient);
67         assertTrue(true);
68     }
69
70     @Test
71     public void testPushExistingModelsWithRollback() {
72         when(config.getAaiBaseUrl()).thenReturn("");
73         when(config.getAaiModelUrl(any())).thenReturn("");
74
75         ResponseEntity operationResult = mock(ResponseEntity.class);
76         when(aaiClient.getResource(any(), any(), any(), any())).thenReturn(operationResult);
77         when(operationResult.getStatusCode()).thenReturn(HttpStatus.OK);
78
79         List<Artifact> artifacts = new ArrayList<>();
80         Artifact artifact = new ModelArtifact();
81         artifacts.add(artifact);
82
83         ModelArtifactHandler handler = new ModelArtifactHandler(config);
84         boolean pushed = handler.pushArtifacts(artifacts, "", Collections.emptyList(), aaiClient);
85         assertTrue(pushed);
86         handler.rollback(artifacts, "", aaiClient);
87     }
88
89     @Test
90     public void testPushNewModelsWithRollback() {
91         when(config.getAaiBaseUrl()).thenReturn("");
92         when(config.getAaiModelUrl(any())).thenReturn("");
93         when(config.getAaiNamedQueryUrl(any())).thenReturn("");
94
95         ResponseEntity getResult = mock(ResponseEntity.class);
96         when(aaiClient.getResource(any(), any(), any(), any())).thenReturn(getResult);
97         when(getResult.getStatusCode()).thenReturn(HttpStatus.NOT_FOUND);
98
99         ResponseEntity putResult = mock(ResponseEntity.class);
100         when(aaiClient.putResource(any(), any(), any(), any(), any())).thenReturn(putResult);
101         when(putResult.getStatusCode()).thenReturn(HttpStatus.CREATED);
102
103         List<Artifact> artifacts = new ArrayList<>();
104         artifacts.add(new ModelArtifact());
105         NamedQueryArtifact namedQueryArtifact = new NamedQueryArtifact();
106         namedQueryArtifact.setNamedQueryUuid("fred");
107         namedQueryArtifact.setModelNamespace("http://org.onap.aai.inventory/v13");
108         artifacts.add(namedQueryArtifact);
109
110         List<Artifact> completedArtifacts = new ArrayList<>();
111         ModelArtifactHandler handler = new ModelArtifactHandler(config);
112         boolean pushed = handler.pushArtifacts(artifacts, "", completedArtifacts, aaiClient);
113         assertThat(pushed, is(true));
114         handler.rollback(artifacts, "", aaiClient);
115     }
116
117     @Test
118     public void testPushNewModelsBadRequest() {
119         when(config.getAaiBaseUrl()).thenReturn("");
120         when(config.getAaiModelUrl(any())).thenReturn("");
121         when(config.getAaiNamedQueryUrl(any())).thenReturn("");
122
123         ResponseEntity getResult = mock(ResponseEntity.class);
124         when(aaiClient.getResource(any(), any(), any(), any())).thenReturn(getResult);
125         when(getResult.getStatusCode()).thenReturn(HttpStatus.NOT_FOUND);
126
127         ResponseEntity putResult = mock(ResponseEntity.class);
128         when(aaiClient.putResource(any(), any(), any(), any(), any())).thenReturn(putResult);
129         when(putResult.getStatusCode()).thenReturn(HttpStatus.BAD_REQUEST);
130
131         checkRollback(Collections.singletonList(new ModelArtifact()));
132     }
133
134     @Test
135     public void testBadRequestResourceModelResult() {
136         when(config.getAaiBaseUrl()).thenReturn("");
137         when(config.getAaiModelUrl(any())).thenReturn("");
138
139         ResponseEntity operationResult = mock(ResponseEntity.class);
140         when(aaiClient.getResource(any(), any(), any(), any())).thenReturn(operationResult);
141         when(operationResult.getStatusCode()).thenReturn(HttpStatus.BAD_REQUEST);
142
143         checkRollback(Collections.singletonList(new ModelArtifact()));
144     }
145
146     private void checkRollback(List<Artifact> artifacts) {
147         ModelArtifactHandler handler = new ModelArtifactHandler(config);
148         boolean pushed = handler.pushArtifacts(artifacts, "", Collections.emptyList(), aaiClient);
149         assertThat(pushed, is(false));
150         handler.rollback(artifacts, "", aaiClient);
151     }
152 }
153