d75d859d8a39e50f794329a576c1bf8bb79a96ee
[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.util.HashMap;
30 import java.util.Map;
31 import java.util.UUID;
32 import org.junit.AfterClass;
33 import org.junit.Test;
34 import org.onap.policy.aai.AaiNqInstanceFilters;
35 import org.onap.policy.aai.AaiNqRequest;
36 import org.onap.policy.aai.AaiNqResponse;
37 import org.onap.policy.aai.AaiNqResponseWrapper;
38 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
39 import org.onap.policy.controlloop.ControlLoopOperation;
40 import org.onap.policy.controlloop.VirtualControlLoopEvent;
41 import org.onap.policy.controlloop.policy.Policy;
42 import org.onap.policy.simulators.AaiSimulatorJaxRs;
43 import org.onap.policy.so.SORequest;
44 import org.onap.policy.so.util.Serialization;
45
46 public class TestSOActorServiceProvider {
47
48     /**
49      * Tear down after test class.
50      */
51     @AfterClass
52     public static void tearDownSimulator() {
53         HttpServletServer.factory.destroy();
54     }
55
56     @Test
57     public void testConstructRequest() {
58         VirtualControlLoopEvent onset = new VirtualControlLoopEvent();
59         final ControlLoopOperation operation = new ControlLoopOperation();
60         final AaiNqResponseWrapper aaiNqResp = getNqVserverFromAai(onset);
61
62         final UUID requestId = UUID.randomUUID();
63         onset.setRequestId(requestId);
64
65         Policy policy = new Policy();
66         policy.setActor("Dorothy");
67         policy.setRecipe("GoToOz");
68         assertNull(new SOActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
69
70         policy.setActor("SO");
71         assertNull(new SOActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
72
73         policy.setRecipe("VF Module Create");
74         SORequest request = new SOActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
75         assertNotNull(request);
76
77         assertEquals("policy", request.getRequestDetails().getRequestInfo().getRequestorId());
78         assertEquals("RegionOne", request.getRequestDetails().getCloudConfiguration().getLcpCloudRegionId());
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      * Queries the AAI simulator directly (i.e., bypassing the REST API) to get the
102      * vserver named-query response.
103      * 
104      * @param onset the ONSET event
105      * @return output from the AAI vserver named-query
106      */
107     private AaiNqResponseWrapper getNqVserverFromAai(VirtualControlLoopEvent onset) {
108         AaiNqRequest aaiNqRequest = new AaiNqRequest();
109         final AaiNqInstanceFilters aaiNqInstanceFilter = new AaiNqInstanceFilters();
110
111         Map<String, Map<String, String>> aaiNqInstanceFilterMap = new HashMap<>();
112         Map<String, String> aaiNqInstanceFilterMapItem = new HashMap<>();
113         aaiNqInstanceFilterMapItem.put("vserver-name", "my-vserver-name");
114         aaiNqInstanceFilterMap.put("vserver", aaiNqInstanceFilterMapItem);
115         aaiNqInstanceFilter.getInstanceFilter().add(aaiNqInstanceFilterMap);
116         aaiNqRequest.setInstanceFilters(aaiNqInstanceFilter);
117
118         String req = Serialization.gsonPretty.toJson(aaiNqRequest);
119         String resp = new AaiSimulatorJaxRs().aaiPostQuery(req);
120         AaiNqResponse aaiNqResponse = Serialization.gsonPretty.fromJson(resp, AaiNqResponse.class);
121
122         return new AaiNqResponseWrapper(onset.getRequestId(), aaiNqResponse);
123     }
124 }