bcdabc86d387e0a7b8909fdbba9af5ddec937dc1
[policy/clamp.git] /
1 /*-
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
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.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;
56
57 @ExtendWith(SpringExtension.class)
58 @WebMvcTest(value = SimulatorController.class)
59 @Import({MetricsAutoConfiguration.class, CompositeMeterRegistryAutoConfiguration.class})
60 @EnableConfigurationProperties(value = ParticipantSimParameters.class)
61 class AcSimRestTest {
62
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
68     @Autowired
69     private MockMvc mockMvc;
70
71     @MockBean
72     private AutomationCompositionElementHandler automationCompositionElementHandler;
73
74     @Autowired
75     private WebApplicationContext context;
76
77     @BeforeEach
78     void mockServiceClass() {
79         this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
80     }
81
82     @Test
83     void testgetConfig() throws Exception {
84         var requestBuilder = MockMvcRequestBuilders.get(CONFIG_URL).accept(MediaType.APPLICATION_JSON_VALUE);
85
86         doReturn(new SimConfig()).when(automationCompositionElementHandler).getConfig();
87
88         mockMvc.perform(requestBuilder).andExpect(status().isOk())
89                 .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
90                 .andExpect(jsonPath("$.primeTimerMs", is(100)));
91     }
92
93     @Test
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);
97
98         mockMvc.perform(requestBuilder).andExpect(status().isOk());
99     }
100
101     @Test
102     void testgetAutomationCompositions() throws Exception {
103         var requestBuilder = MockMvcRequestBuilders.get(INSTANCE_URL).accept(MediaType.APPLICATION_JSON_VALUE);
104
105         var automationCompositions = new AutomationCompositions();
106         automationCompositions.getAutomationCompositionList().add(CommonTestData.getTestAutomationComposition());
107
108         doReturn(automationCompositions).when(automationCompositionElementHandler).getAutomationCompositions();
109
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());
116     }
117
118     @Test
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);
124
125         doReturn(internalDatas).when(automationCompositionElementHandler).getDataList();
126
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());
134     }
135
136     @Test
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);
140
141         mockMvc.perform(requestBuilder).andExpect(status().isOk());
142     }
143 }