2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2023 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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.acm.participant.sim.rest;
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;
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.AutomationCompositionElementHandler;
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.AutomationCompositions;
41 import org.onap.policy.common.utils.coder.Coder;
42 import org.onap.policy.common.utils.coder.StandardCoder;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration;
45 import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
46 import org.springframework.boot.context.properties.EnableConfigurationProperties;
47 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
48 import org.springframework.boot.test.mock.mockito.MockBean;
49 import org.springframework.context.annotation.Import;
50 import org.springframework.http.MediaType;
51 import org.springframework.test.context.junit.jupiter.SpringExtension;
52 import org.springframework.test.web.servlet.MockMvc;
53 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
54 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
55 import org.springframework.web.context.WebApplicationContext;
57 @ExtendWith(SpringExtension.class)
58 @WebMvcTest(value = SimulatorController.class)
59 @Import({MetricsAutoConfiguration.class, CompositeMeterRegistryAutoConfiguration.class})
60 @EnableConfigurationProperties(value = ParticipantSimParameters.class)
63 private static final Coder CODER = new StandardCoder();
64 private static final String CONFIG_URL = "/v2/parameters";
65 private static final String INSTANCE_URL = "/v2/instances";
66 private static final String DATAS_URL = "/v2/datas";
67 private static final String COMPOSITION_DATAS_URL = "/v2/compositiondatas";
70 private MockMvc mockMvc;
73 private AutomationCompositionElementHandler automationCompositionElementHandler;
76 private WebApplicationContext context;
79 void mockServiceClass() {
80 this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
84 void testgetConfig() throws Exception {
85 var requestBuilder = MockMvcRequestBuilders.get(CONFIG_URL).accept(MediaType.APPLICATION_JSON_VALUE);
87 doReturn(new SimConfig()).when(automationCompositionElementHandler).getConfig();
89 mockMvc.perform(requestBuilder).andExpect(status().isOk())
90 .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
91 .andExpect(jsonPath("$.primeTimerMs", is(100)));
95 void testsetConfig() throws Exception {
96 var requestBuilder = MockMvcRequestBuilders.put(CONFIG_URL).accept(MediaType.APPLICATION_JSON_VALUE)
97 .content(CODER.encode(new SimConfig())).contentType(MediaType.APPLICATION_JSON_VALUE);
99 mockMvc.perform(requestBuilder).andExpect(status().isOk());
103 void testgetAutomationCompositions() throws Exception {
104 var requestBuilder = MockMvcRequestBuilders.get(INSTANCE_URL).accept(MediaType.APPLICATION_JSON_VALUE);
106 var automationCompositions = new AutomationCompositions();
107 automationCompositions.getAutomationCompositionList().add(CommonTestData.getTestAutomationComposition());
109 doReturn(automationCompositions).when(automationCompositionElementHandler).getAutomationCompositions();
111 var result = mockMvc.perform(requestBuilder).andExpect(status().isOk())
112 .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)).andReturn();
113 var body = result.getResponse().getContentAsString();
114 var acsResult = CODER.decode(body, AutomationCompositions.class);
115 assertEquals(automationCompositions.getAutomationCompositionList().get(0).getInstanceId(),
116 acsResult.getAutomationCompositionList().get(0).getInstanceId());
120 void testgetDatas() throws Exception {
121 var internalDatas = new InternalDatas();
122 var internalData = new InternalData();
123 internalData.setAutomationCompositionId(UUID.randomUUID());
124 internalDatas.getList().add(internalData);
126 doReturn(internalDatas).when(automationCompositionElementHandler).getDataList();
128 var requestBuilder = MockMvcRequestBuilders.get(DATAS_URL).accept(MediaType.APPLICATION_JSON_VALUE);
129 var result = mockMvc.perform(requestBuilder).andExpect(status().isOk())
130 .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)).andReturn();
131 var body = result.getResponse().getContentAsString();
132 var acsResult = CODER.decode(body, InternalDatas.class);
133 assertEquals(internalData.getAutomationCompositionId(),
134 acsResult.getList().get(0).getAutomationCompositionId());
138 void testsetDatas() throws Exception {
139 var requestBuilder = MockMvcRequestBuilders.put(DATAS_URL).accept(MediaType.APPLICATION_JSON_VALUE)
140 .content(CODER.encode(new InternalData())).contentType(MediaType.APPLICATION_JSON_VALUE);
142 mockMvc.perform(requestBuilder).andExpect(status().isOk());
146 void testgetCompositionDatas() throws Exception {
147 var internalDatas = new InternalDatas();
148 var internalData = new InternalData();
149 internalData.setCompositionId(UUID.randomUUID());
150 internalDatas.getList().add(internalData);
152 doReturn(internalDatas).when(automationCompositionElementHandler).getCompositionDataList();
154 var requestBuilder = MockMvcRequestBuilders.get(COMPOSITION_DATAS_URL).accept(MediaType.APPLICATION_JSON_VALUE);
155 var result = mockMvc.perform(requestBuilder).andExpect(status().isOk())
156 .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)).andReturn();
157 var body = result.getResponse().getContentAsString();
158 var acsResult = CODER.decode(body, InternalDatas.class);
159 assertEquals(internalData.getCompositionId(), acsResult.getList().get(0).getCompositionId());
163 void testsetCompositionDatas() throws Exception {
164 var requestBuilder = MockMvcRequestBuilders.put(COMPOSITION_DATAS_URL).accept(MediaType.APPLICATION_JSON_VALUE)
165 .content(CODER.encode(new InternalData())).contentType(MediaType.APPLICATION_JSON_VALUE);
167 mockMvc.perform(requestBuilder).andExpect(status().isOk());