nickwallen commented on a change in pull request #1346: METRON-2016: Parser aggregate groups should be persisted and available through REST URL: https://github.com/apache/metron/pull/1346#discussion_r262222587
########## File path: metron-interface/metron-rest/src/test/java/org/apache/metron/rest/controller/SensorParserGroupControllerIntegrationTest.java ########## @@ -0,0 +1,251 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.metron.rest.controller; + +import org.adrianwalker.multilinestring.Multiline; +import org.apache.commons.io.FileUtils; +import org.apache.metron.common.configuration.SensorParserConfig; +import org.apache.metron.common.configuration.SensorParserGroup; +import org.apache.metron.integration.utils.TestUtils; +import org.apache.metron.rest.MetronRestConstants; +import org.apache.metron.rest.service.GlobalConfigService; +import org.apache.metron.rest.service.SensorParserConfigService; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.core.env.Environment; +import org.springframework.http.MediaType; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +import java.io.File; +import java.io.IOException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.concurrent.atomic.AtomicInteger; + +import static org.apache.metron.integration.utils.TestUtils.assertEventually; +import static org.apache.metron.rest.MetronRestConstants.TEST_PROFILE; +import static org.hamcrest.Matchers.hasSize; +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic; +import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment.RANDOM_PORT) +@ActiveProfiles(TEST_PROFILE) +public class SensorParserGroupControllerIntegrationTest { + + /** + { + "name":"group1", + "description":"group1 description", + "sensors":["bro","snort"] + } + */ + @Multiline + public static String group1BroSnort; + + /** + { + "name":"group1", + "description":"group1 description", + "sensors":["bro","squid"] + } + */ + @Multiline + public static String group1BroSquid; + + /** + { + "name":"group2", + "description":"group2 description", + "sensors":["yaf","jsonMap"] + } + */ + @Multiline + public static String group2YafJsonMap; + + /** + { + "name":"errorGroup", + "description":"error description", + "sensors":["bro"] + } + */ + @Multiline + public static String errorGroup; + + @Autowired + private Environment environment; + + @Autowired + private GlobalConfigService globalConfigService; + + @Autowired + private SensorParserConfigService sensorParserConfigService; + + @Autowired + private WebApplicationContext wac; + + private MockMvc mockMvc; + + private String sensorParserGroupUrl = "/api/v1/sensor/parser/group"; + private String user = "user"; + private String password = "password"; + + @Before + public void setup() throws Exception { + this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).apply(springSecurity()).build(); + } + + @Test + public void testSecurity() throws Exception { + this.mockMvc.perform(post(sensorParserGroupUrl).with(csrf()).contentType(MediaType.parseMediaType("application/json;charset=UTF-8")).content(group1BroSnort)) + .andExpect(status().isUnauthorized()); + + this.mockMvc.perform(get(sensorParserGroupUrl + "/group1")) + .andExpect(status().isUnauthorized()); + + this.mockMvc.perform(get(sensorParserGroupUrl)) + .andExpect(status().isUnauthorized()); + + this.mockMvc.perform(delete(sensorParserGroupUrl + "/group1").with(csrf())) + .andExpect(status().isUnauthorized()); + } + + @Test + public void test() throws Exception { + this.globalConfigService.save(new HashMap<>()); + this.sensorParserConfigService.save("bro", new SensorParserConfig()); + this.sensorParserConfigService.save("snort", new SensorParserConfig()); + this.sensorParserConfigService.save("squid", new SensorParserConfig()); + this.sensorParserConfigService.save("yaf", new SensorParserConfig()); + this.sensorParserConfigService.save("jsonMap", new SensorParserConfig()); + Method[] method = SensorParserGroup.class.getMethods(); + final AtomicInteger numFields = new AtomicInteger(0); + for(Method m : method) { + if(m.getName().startsWith("set")) { + numFields.set(numFields.get() + 1); + } + } + this.mockMvc.perform(post(sensorParserGroupUrl).with(httpBasic(user, password)).with(csrf()).contentType(MediaType.parseMediaType("application/json;charset=UTF-8")).content(group1BroSnort)) Review comment: Rather that one big test method, can we separate the logic into separate test methods; some testing create, some testing delete, some testing edit, etc? If in the future I change something and break this, it helps me much more to see, for example, that `testCreate` failed and not the other tests. With just a single `test` method I have to dig through all this code to find what actually broke. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
