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";
69 private MockMvc mockMvc;
72 private AutomationCompositionElementHandler automationCompositionElementHandler;
75 private WebApplicationContext context;
78 void mockServiceClass() {
79 this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
83 void testgetConfig() throws Exception {
84 var requestBuilder = MockMvcRequestBuilders.get(CONFIG_URL).accept(MediaType.APPLICATION_JSON_VALUE);
86 doReturn(new SimConfig()).when(automationCompositionElementHandler).getConfig();
88 mockMvc.perform(requestBuilder).andExpect(status().isOk())
89 .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
90 .andExpect(jsonPath("$.primeTimerMs", is(100)));
94 void testsetConfig() throws Exception {
95 var requestBuilder = MockMvcRequestBuilders.put(CONFIG_URL).accept(MediaType.APPLICATION_JSON_VALUE)
96 .content(CODER.encode(new SimConfig())).contentType(MediaType.APPLICATION_JSON_VALUE);
98 mockMvc.perform(requestBuilder).andExpect(status().isOk());
102 void testgetAutomationCompositions() throws Exception {
103 var requestBuilder = MockMvcRequestBuilders.get(INSTANCE_URL).accept(MediaType.APPLICATION_JSON_VALUE);
105 var automationCompositions = new AutomationCompositions();
106 automationCompositions.getAutomationCompositionList().add(CommonTestData.getTestAutomationComposition());
108 doReturn(automationCompositions).when(automationCompositionElementHandler).getAutomationCompositions();
110 var result = mockMvc.perform(requestBuilder).andExpect(status().isOk())
111 .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)).andReturn();
112 var body = result.getResponse().getContentAsString();
113 var acsResult = CODER.decode(body, AutomationCompositions.class);
114 assertEquals(automationCompositions.getAutomationCompositionList().get(0).getInstanceId(),
115 acsResult.getAutomationCompositionList().get(0).getInstanceId());
119 void testgetDatas() throws Exception {
120 var internalDatas = new InternalDatas();
121 var internalData = new InternalData();
122 internalData.setAutomationCompositionId(UUID.randomUUID());
123 internalDatas.getList().add(internalData);
125 doReturn(internalDatas).when(automationCompositionElementHandler).getDataList();
127 var requestBuilder = MockMvcRequestBuilders.get(DATAS_URL).accept(MediaType.APPLICATION_JSON_VALUE);
128 var result = mockMvc.perform(requestBuilder).andExpect(status().isOk())
129 .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)).andReturn();
130 var body = result.getResponse().getContentAsString();
131 var acsResult = CODER.decode(body, InternalDatas.class);
132 assertEquals(internalData.getAutomationCompositionId(),
133 acsResult.getList().get(0).getAutomationCompositionId());
137 void testsetDatas() throws Exception {
138 var requestBuilder = MockMvcRequestBuilders.put(DATAS_URL).accept(MediaType.APPLICATION_JSON_VALUE)
139 .content(CODER.encode(new InternalData())).contentType(MediaType.APPLICATION_JSON_VALUE);
141 mockMvc.perform(requestBuilder).andExpect(status().isOk());