25bbda0f41c691b773788da18c7b39ab5f51a379
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * TestSOActorServiceProvider
4  * ================================================================================
5  * Copyright (C) 2018 Ericsson. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (C) 2018 AT&T. All rights reserved.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.controlloop.actor.so;
24
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.fail;
29 import java.io.IOException;
30 import java.nio.charset.StandardCharsets;
31 import java.util.UUID;
32 import org.apache.commons.io.IOUtils;
33 import org.junit.Test;
34 import org.onap.policy.aai.AaiNqResponse;
35 import org.onap.policy.aai.AaiNqResponseWrapper;
36 import org.onap.policy.controlloop.ControlLoopOperation;
37 import org.onap.policy.controlloop.VirtualControlLoopEvent;
38 import org.onap.policy.controlloop.policy.Policy;
39 import org.onap.policy.so.SORequest;
40 import org.onap.policy.so.util.Serialization;
41
42 public class TestSOActorServiceProvider {
43
44     @Test
45     public void testConstructRequest() throws Exception {
46         VirtualControlLoopEvent onset = new VirtualControlLoopEvent();
47         final ControlLoopOperation operation = new ControlLoopOperation();
48         final AaiNqResponseWrapper aaiNqResp = loadAaiResponse(onset, "aai/AaiNqResponse-Full.json");
49
50         final UUID requestId = UUID.randomUUID();
51         onset.setRequestId(requestId);
52
53         Policy policy = new Policy();
54         policy.setActor("Dorothy");
55         policy.setRecipe("GoToOz");
56         assertNull(new SOActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
57
58         policy.setActor("SO");
59         assertNull(new SOActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
60
61         policy.setRecipe("VF Module Create");
62         SORequest request = new SOActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
63         assertNotNull(request);
64
65         assertEquals("my_module_3", request.getRequestDetails().getRequestInfo().getInstanceName());
66         assertEquals("policy", request.getRequestDetails().getRequestInfo().getRequestorId());
67         assertEquals("RegionOne", request.getRequestDetails().getCloudConfiguration().getLcpCloudRegionId());
68
69         // null response
70         assertNull(new SOActorServiceProvider().constructRequest(onset, operation, policy, null));
71
72         // response has no base VF module
73         assertNull(new SOActorServiceProvider().constructRequest(onset, operation, policy,
74                         loadAaiResponse(onset, "aai/AaiNqResponse-NoBase.json")));
75
76         // response has no non-base VF modules (other than the "dummy")
77         assertNull(new SOActorServiceProvider().constructRequest(onset, operation, policy,
78                         loadAaiResponse(onset, "aai/AaiNqResponse-NoNonBase.json")));
79     }
80
81     @Test
82     public void testSendRequest() {
83         try {
84             SOActorServiceProvider.sendRequest(UUID.randomUUID().toString(), null, null);
85         } catch (Exception e) {
86             fail("Test should not throw an exception");
87         }
88     }
89
90     @Test
91     public void testMethods() {
92         SOActorServiceProvider sp = new SOActorServiceProvider();
93
94         assertEquals("SO", sp.actor());
95         assertEquals(1, sp.recipes().size());
96         assertEquals("VF Module Create", sp.recipes().get(0));
97         assertEquals(0, sp.recipePayloads("VF Module Create").size());
98     }
99
100     /**
101      * Reads an AAI vserver named-query response from a file.
102      * 
103      * @param onset the ONSET event
104      * @param fileName name of the file containing the JSON response
105      * @return output from the AAI vserver named-query
106      * @throws IOException if the file cannot be read
107      */
108     private AaiNqResponseWrapper loadAaiResponse(VirtualControlLoopEvent onset, String fileName)
109                     throws IOException {
110         String resp = IOUtils.toString(getClass().getResource(fileName), StandardCharsets.UTF_8);
111         AaiNqResponse aaiNqResponse = Serialization.gsonPretty.fromJson(resp, AaiNqResponse.class);
112
113         return new AaiNqResponseWrapper(onset.getRequestId(), aaiNqResponse);
114     }
115 }