Merge "Reorder modifiers"
[so.git] / common / src / test / java / org / openecomp / mso / client / aai / AAITransactionalClientTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 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.openecomp.mso.client.aai;
22
23 import static org.junit.Assert.assertEquals;
24
25 import java.io.IOException;
26 import java.nio.file.Files;
27 import java.nio.file.Paths;
28 import java.util.Map;
29 import java.util.Optional;
30
31 import org.junit.Test;
32 import org.openecomp.mso.client.aai.entities.uri.AAIResourceUri;
33 import org.openecomp.mso.client.aai.entities.uri.AAIUriFactory;
34
35 import com.fasterxml.jackson.core.type.TypeReference;
36 import com.fasterxml.jackson.databind.ObjectMapper;
37 import com.fasterxml.jackson.databind.SerializationFeature;
38
39 public class AAITransactionalClientTest {
40
41         
42         private final static String AAI_JSON_FILE_LOCATION = "src/test/resources/__files/aai/bulkprocess/";
43         
44         @Test
45         public void run() throws IOException {
46                 
47                 
48                 AAIResourcesClient client = new AAIResourcesClient();
49                 AAIResourceUri uriA = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test1");
50                 AAIResourceUri uriB = AAIUriFactory.createResourceUri(AAIObjectType.PSERVER, "test2");
51                 AAIResourceUri uriC = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test3");
52                 AAIResourceUri uriD = AAIUriFactory.createResourceUri(AAIObjectType.PSERVER, "test4");
53                 AAIResourceUri uriE = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test5");
54                 AAIResourceUri uriF = AAIUriFactory.createResourceUri(AAIObjectType.PSERVER, "test6");
55                 
56                 AAIResourceUri uriAClone = uriA.clone();
57                 AAITransactionalClient transactions = client
58                                 .beginTransaction().connect(uriA, uriB).connect(uriC, uriD)
59                                 .beginNewTransaction().connect(uriE, uriF);
60                 ObjectMapper mapper = new AAICommonObjectMapperProvider().getMapper();
61                 mapper.enable(SerializationFeature.INDENT_OUTPUT);
62                 String serializedTransactions = mapper.writeValueAsString(transactions.getTransactions());
63                 Map<String, Object> map1 = mapper.readValue(serializedTransactions, new TypeReference<Map<String, Object>>(){});
64                 Map<String, Object> map2 = mapper.readValue(getJson("test-request.json"), new TypeReference<Map<String, Object>>(){});
65                 assertEquals("payloads are equal", map2, map1);
66                 assertEquals("uri not manipulated", uriAClone.build().toString(), uriA.build().toString());
67         }
68         
69         @Test
70         public void verifyResponse() throws IOException {
71                 AAIResourcesClient client = new AAIResourcesClient();
72                 AAITransactionalClient transactions = client
73                                 .beginTransaction();
74                 assertEquals("success status", Optional.empty(), transactions.locateErrorMessages(getJson("response-success.json")));
75                 assertEquals(transactions.locateErrorMessages(getJson("response-failure.json")).get(), "another error message\nmy great error");
76
77                 
78         }
79         
80         private String getJson(String filename) throws IOException {
81                  return new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + filename)));
82         }
83 }