Github user merrimanr commented on a diff in the pull request:

    https://github.com/apache/metron/pull/694#discussion_r135909951
  
    --- Diff: 
metron-interface/metron-rest/src/test/java/org/apache/metron/rest/controller/AlertsProfileControllerIntegrationTest.java
 ---
    @@ -0,0 +1,226 @@
    +/**
    + * 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 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;
    +
    +import org.adrianwalker.multilinestring.Multiline;
    +import org.apache.metron.rest.model.AlertsProfile;
    +import org.apache.metron.rest.service.AlertsProfileService;
    +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.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;
    +
    +@RunWith(SpringRunner.class)
    +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    +@ActiveProfiles(TEST_PROFILE)
    +public class AlertsProfileControllerIntegrationTest {
    +
    +  /**
    +   * { "tableColumns": ["user1_field"], "savedSearches": [ { "name": 
"user1 search 1",
    +   * "searchRequest": { "from": 0, "indices": ["bro"], "query": "*", 
"size": 5 } },
    +   * { "name": "user1 search 2", "searchRequest": { "from": 10, "indices": 
["snort"],
    +   * "query": "*", "size": 10 } } ]
    +   * }
    +   */
    +  @Multiline
    +  public static String user1ProfileJson;
    +
    +
    +  /**
    +   * { "tableColumns": ["user2_field"], "savedSearches": [ { "name": 
"user2 search 1",
    +   * "searchRequest": { "from": 0, "indices": ["bro", "snort"], "query": 
"ip_src_addr:192.168.1.1",
    +   * "size": 100 } } ] }
    +   */
    +  @Multiline
    +  public static String user2ProfileJson;
    +
    +  /**
    +   * { "tableColumns": ["user2_field"], "savedSearches": [] }
    +   */
    +  @Multiline
    +  public static String testJson;
    +
    +  /**
    +   * { "sampleData":"1467011157.401 415 127.0.0.1 TCP_MISS/200 337891 GET
    +   * http://www.aliexpress.com/af/shoes.html? - DIRECT/207.109.73.154 
text/html",
    +   * "patternLabel":"SQUID" }
    +   */
    +  @Multiline
    +  public static String missingStatementGrokValidationJson;
    +
    +  @Autowired
    +  private WebApplicationContext wac;
    +
    +  @Autowired
    +  private AlertsProfileService alertsProfileService;
    +
    +  private MockMvc mockMvc;
    +
    +  private String url = "/api/v1/alerts/profile";
    +  private String user1 = "user1";
    +  private String user2 = "user2";
    +  private String admin = "admin";
    +  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(get(url))
    +        .andExpect(status().isUnauthorized());
    +    this.mockMvc.perform(get(url + "/all"))
    +        .andExpect(status().isUnauthorized());
    +    this.mockMvc.perform(post(url).with(csrf())
    +        
.contentType(MediaType.parseMediaType("application/json;charset=UTF-8"))
    +        .content(user1ProfileJson))
    +        .andExpect(status().isUnauthorized());
    +    this.mockMvc.perform(get(url + "/all").with(httpBasic(user1, 
password)).with(csrf()))
    +        .andExpect(status().isForbidden());
    +    this.mockMvc.perform(delete(url + "/user1").with(httpBasic(user1, 
password)).with(csrf()))
    +        .andExpect(status().isForbidden());
    +  }
    +
    +  @Test
    +  public void test() throws Exception {
    +    for (AlertsProfile alertsProfile : alertsProfileService.findAll()) {
    +      alertsProfileService.delete(alertsProfile.getId());
    +    }
    +
    +    this.mockMvc.perform(get(url).with(httpBasic(user1, password)))
    +        .andExpect(status().isNotFound());
    +
    +    this.mockMvc.perform(get(url).with(httpBasic(user2, password)))
    +        .andExpect(status().isNotFound());
    +
    +    this.mockMvc.perform(get(url + "/all").with(httpBasic(admin, 
password)))
    +        .andExpect(status().isOk())
    +        .andExpect(
    +            
content().contentType(MediaType.parseMediaType("application/json;charset=UTF-8")))
    +        .andExpect(jsonPath("$.*", hasSize(0)));
    +
    +    this.mockMvc.perform(post(url).with(httpBasic(user1, 
password)).with(csrf())
    +        
.contentType(MediaType.parseMediaType("application/json;charset=UTF-8"))
    +        .content(user1ProfileJson))
    +        .andExpect(status().isCreated())
    +        .andExpect(content().json(user1ProfileJson));
    +
    +    this.mockMvc.perform(post(url).with(httpBasic(user1, 
password)).with(csrf())
    +        
.contentType(MediaType.parseMediaType("application/json;charset=UTF-8"))
    +        .content(user1ProfileJson))
    +        .andExpect(status().isOk())
    +        .andExpect(content().json(user1ProfileJson));
    +
    +    this.mockMvc.perform(get(url).with(httpBasic(user1, password)))
    +        .andExpect(status().isOk())
    +        .andExpect(
    +            
content().contentType(MediaType.parseMediaType("application/json;charset=UTF-8")))
    +        .andExpect(content().json(user1ProfileJson));
    +
    +    this.mockMvc.perform(get(url).with(httpBasic(user2, password)))
    +        .andExpect(status().isNotFound());
    +
    +    this.mockMvc.perform(get(url + "/all").with(httpBasic(admin, 
password)))
    +        .andExpect(status().isOk())
    +        .andExpect(
    +            
content().contentType(MediaType.parseMediaType("application/json;charset=UTF-8")))
    +        .andExpect(content().json("[" + user1ProfileJson + "]"));
    +
    +    this.mockMvc.perform(post(url).with(httpBasic(user2, 
password)).with(csrf())
    +        
.contentType(MediaType.parseMediaType("application/json;charset=UTF-8"))
    +        .content(user2ProfileJson))
    +        .andExpect(status().isCreated())
    +        .andExpect(content().json(user2ProfileJson));
    +
    +    this.mockMvc.perform(get(url).with(httpBasic(user1, password)))
    +        .andExpect(status().isOk())
    +        .andExpect(
    +            
content().contentType(MediaType.parseMediaType("application/json;charset=UTF-8")))
    +        .andExpect(content().json(user1ProfileJson));
    +
    +    this.mockMvc.perform(get(url).with(httpBasic(user2, password)))
    +        .andExpect(status().isOk())
    +        .andExpect(
    +            
content().contentType(MediaType.parseMediaType("application/json;charset=UTF-8")))
    +        .andExpect(content().json(user2ProfileJson));
    +
    +    this.mockMvc.perform(get(url + "/all").with(httpBasic(admin, 
password)))
    +        .andExpect(status().isOk())
    +        .andExpect(
    +            
content().contentType(MediaType.parseMediaType("application/json;charset=UTF-8")))
    +        .andExpect(content().json("[" + user1ProfileJson + "," + 
user2ProfileJson + "]"));
    +
    +    this.mockMvc.perform(delete(url + "/user1").with(httpBasic(admin, 
password)))
    +        .andExpect(status().isOk());
    +
    +    this.mockMvc.perform(delete(url + "/user1").with(httpBasic(admin, 
password)))
    +        .andExpect(status().isNotFound());
    +
    +    this.mockMvc.perform(get(url).with(httpBasic(user1, password)))
    +        .andExpect(status().isNotFound());
    +
    +    this.mockMvc.perform(get(url).with(httpBasic(user2, password)))
    +        .andExpect(status().isOk())
    +        .andExpect(
    +            
content().contentType(MediaType.parseMediaType("application/json;charset=UTF-8")))
    +        .andExpect(content().json(user2ProfileJson));
    +
    +    this.mockMvc.perform(get(url + "/all").with(httpBasic(admin, 
password)))
    +        .andExpect(status().isOk())
    +        .andExpect(
    +            
content().contentType(MediaType.parseMediaType("application/json;charset=UTF-8")))
    +        .andExpect(content().json("[" + user2ProfileJson + "]"));
    +
    +    this.mockMvc.perform(delete(url + "/user2").with(httpBasic(admin, 
password)))
    +        .andExpect(status().isOk());
    +
    +    this.mockMvc.perform(get(url).with(httpBasic(user1, password)))
    +        .andExpect(status().isNotFound());
    +
    +    this.mockMvc.perform(get(url).with(httpBasic(user2, password)))
    +        .andExpect(status().isNotFound());
    +
    +    this.mockMvc.perform(get(url + "/all").with(httpBasic(admin, 
password)))
    --- End diff --
    
    There is a technical issue here and that's annotating multiple methods with 
@Test.  This changes the lifecycle of the tests and makes it so that each @Test 
method has to do it's own setup and teardown.  Not a big deal with unit tests 
because functions are tested in isolation with mock objects but it's harder for 
integration tests.  This test is not mocking anything, the same in-memory 
components that our other integration tests use serve as the backend.  We 
definitely don't want to spin them up/down before each test.  In fact, the REST 
integration tests only spin them up/down once for all tests.
    
    If we were to organize the cases into different methods but NOT annotate 
them with @Test would that be ok?  I would rather document the state each test 
expects rather than recreating it.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to