f62d6cd2dd8a524a841c043d80438d6d6ac93163
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2023-2024 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.acm.participant.sim.rest;
22
23 import static org.hamcrest.CoreMatchers.is;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.mockito.Mockito.doReturn;
26 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
27 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
28 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
29
30 import java.util.UUID;
31 import org.junit.jupiter.api.BeforeEach;
32 import org.junit.jupiter.api.Test;
33 import org.junit.jupiter.api.extension.ExtendWith;
34 import org.onap.policy.clamp.acm.participant.sim.comm.CommonTestData;
35 import org.onap.policy.clamp.acm.participant.sim.main.handler.SimulatorService;
36 import org.onap.policy.clamp.acm.participant.sim.model.InternalData;
37 import org.onap.policy.clamp.acm.participant.sim.model.InternalDatas;
38 import org.onap.policy.clamp.acm.participant.sim.model.SimConfig;
39 import org.onap.policy.clamp.acm.participant.sim.parameters.ParticipantSimParameters;
40 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
41 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositions;
42 import org.onap.policy.common.utils.coder.Coder;
43 import org.onap.policy.common.utils.coder.StandardCoder;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration;
46 import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
47 import org.springframework.boot.context.properties.EnableConfigurationProperties;
48 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
49 import org.springframework.boot.test.mock.mockito.MockBean;
50 import org.springframework.context.annotation.Import;
51 import org.springframework.http.MediaType;
52 import org.springframework.test.context.junit.jupiter.SpringExtension;
53 import org.springframework.test.web.servlet.MockMvc;
54 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
55 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
56 import org.springframework.web.context.WebApplicationContext;
57
58 @ExtendWith(SpringExtension.class)
59 @WebMvcTest(value = SimulatorController.class)
60 @Import({MetricsAutoConfiguration.class, CompositeMeterRegistryAutoConfiguration.class})
61 @EnableConfigurationProperties(value = ParticipantSimParameters.class)
62 class AcSimRestTest {
63
64     private static final Coder CODER = new StandardCoder();
65     private static final String CONFIG_URL = "/v2/parameters";
66     private static final String INSTANCE_URL = "/v2/instances";
67     private static final String DATAS_URL = "/v2/datas";
68     private static final String COMPOSITION_DATAS_URL = "/v2/compositiondatas";
69
70     @Autowired
71     private MockMvc mockMvc;
72
73     @MockBean
74     private SimulatorService simulatorService;
75
76     @Autowired
77     private WebApplicationContext context;
78
79     @BeforeEach
80     void mockServiceClass() {
81         this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
82     }
83
84     @Test
85     void testGetConfig() throws Exception {
86         var requestBuilder = MockMvcRequestBuilders.get(CONFIG_URL).accept(MediaType.APPLICATION_JSON_VALUE);
87
88         doReturn(new SimConfig()).when(simulatorService).getConfig();
89
90         mockMvc.perform(requestBuilder).andExpect(status().isOk())
91                 .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
92                 .andExpect(jsonPath("$.primeTimerMs", is(100)));
93     }
94
95     @Test
96     void testSetConfig() throws Exception {
97         var requestBuilder = MockMvcRequestBuilders.put(CONFIG_URL).accept(MediaType.APPLICATION_JSON_VALUE)
98                 .content(CODER.encode(new SimConfig())).contentType(MediaType.APPLICATION_JSON_VALUE);
99
100         mockMvc.perform(requestBuilder).andExpect(status().isOk());
101     }
102
103     @Test
104     void testGetAutomationCompositions() throws Exception {
105         var requestBuilder = MockMvcRequestBuilders.get(INSTANCE_URL).accept(MediaType.APPLICATION_JSON_VALUE);
106
107         var automationCompositions = new AutomationCompositions();
108         automationCompositions.getAutomationCompositionList().add(CommonTestData.getTestAutomationComposition());
109
110         doReturn(automationCompositions).when(simulatorService).getAutomationCompositions();
111
112         var result = mockMvc.perform(requestBuilder).andExpect(status().isOk())
113                 .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)).andReturn();
114         var body = result.getResponse().getContentAsString();
115         var acsResult = CODER.decode(body, AutomationCompositions.class);
116         assertEquals(automationCompositions.getAutomationCompositionList().get(0).getInstanceId(),
117                 acsResult.getAutomationCompositionList().get(0).getInstanceId());
118     }
119
120     @Test
121     void testGetAutomationComposition() throws Exception {
122         var automationComposition = CommonTestData.getTestAutomationComposition();
123
124         var requestBuilder = MockMvcRequestBuilders
125                 .get(INSTANCE_URL + "/" + automationComposition.getInstanceId())
126                 .accept(MediaType.APPLICATION_JSON_VALUE);
127
128         doReturn(automationComposition).when(simulatorService)
129                 .getAutomationComposition(automationComposition.getInstanceId());
130
131         var result = mockMvc.perform(requestBuilder).andExpect(status().isOk())
132                 .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)).andReturn();
133         var body = result.getResponse().getContentAsString();
134         var acsResult = CODER.decode(body, AutomationComposition.class);
135         assertEquals(automationComposition, acsResult);
136     }
137
138     @Test
139     void testGetDatas() throws Exception {
140         var internalDatas = new InternalDatas();
141         var internalData = new InternalData();
142         internalData.setAutomationCompositionId(UUID.randomUUID());
143         internalDatas.getList().add(internalData);
144
145         doReturn(internalDatas).when(simulatorService).getDataList();
146
147         var requestBuilder = MockMvcRequestBuilders.get(DATAS_URL).accept(MediaType.APPLICATION_JSON_VALUE);
148         var result = mockMvc.perform(requestBuilder).andExpect(status().isOk())
149                 .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)).andReturn();
150         var body = result.getResponse().getContentAsString();
151         var acsResult = CODER.decode(body, InternalDatas.class);
152         assertEquals(internalData.getAutomationCompositionId(),
153                 acsResult.getList().get(0).getAutomationCompositionId());
154     }
155
156     @Test
157     void testSetDatas() throws Exception {
158         var requestBuilder = MockMvcRequestBuilders.put(DATAS_URL).accept(MediaType.APPLICATION_JSON_VALUE)
159                 .content(CODER.encode(new InternalData())).contentType(MediaType.APPLICATION_JSON_VALUE);
160
161         mockMvc.perform(requestBuilder).andExpect(status().isOk());
162     }
163
164     @Test
165     void testGetCompositionDatas() throws Exception {
166         var internalDatas = new InternalDatas();
167         var internalData = new InternalData();
168         internalData.setCompositionId(UUID.randomUUID());
169         internalDatas.getList().add(internalData);
170
171         doReturn(internalDatas).when(simulatorService).getCompositionDataList();
172
173         var requestBuilder = MockMvcRequestBuilders.get(COMPOSITION_DATAS_URL).accept(MediaType.APPLICATION_JSON_VALUE);
174         var result = mockMvc.perform(requestBuilder).andExpect(status().isOk())
175                 .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)).andReturn();
176         var body = result.getResponse().getContentAsString();
177         var acsResult = CODER.decode(body, InternalDatas.class);
178         assertEquals(internalData.getCompositionId(), acsResult.getList().get(0).getCompositionId());
179     }
180
181     @Test
182     void testSetCompositionDatas() throws Exception {
183         var requestBuilder = MockMvcRequestBuilders.put(COMPOSITION_DATAS_URL).accept(MediaType.APPLICATION_JSON_VALUE)
184                 .content(CODER.encode(new InternalData())).contentType(MediaType.APPLICATION_JSON_VALUE);
185
186         mockMvc.perform(requestBuilder).andExpect(status().isOk());
187     }
188 }