|
| 1 | +/* |
| 2 | + * Copyright (C) 2020 European Spallation Source ERIC. |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or |
| 5 | + * modify it under the terms of the GNU General Public License |
| 6 | + * as published by the Free Software Foundation; either version 2 |
| 7 | + * of the License, or (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program; if not, write to the Free Software |
| 16 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | + */ |
| 18 | +package org.phoebus.olog; |
| 19 | + |
| 20 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 21 | +import org.junit.jupiter.api.BeforeAll; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 24 | +import org.phoebus.olog.entity.Logbook; |
| 25 | +import org.phoebus.olog.entity.State; |
| 26 | +import org.phoebus.olog.entity.Tag; |
| 27 | +import org.springframework.beans.factory.annotation.Autowired; |
| 28 | +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; |
| 29 | +import org.springframework.http.HttpHeaders; |
| 30 | +import org.springframework.test.context.ActiveProfiles; |
| 31 | +import org.springframework.test.context.ContextConfiguration; |
| 32 | +import org.springframework.test.context.ContextHierarchy; |
| 33 | +import org.springframework.test.context.TestPropertySource; |
| 34 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 35 | +import org.springframework.test.web.servlet.MvcResult; |
| 36 | +import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; |
| 37 | + |
| 38 | +import java.util.ArrayList; |
| 39 | +import java.util.Arrays; |
| 40 | +import java.util.List; |
| 41 | +import java.util.Optional; |
| 42 | + |
| 43 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 44 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 45 | +import static org.mockito.Mockito.reset; |
| 46 | +import static org.mockito.Mockito.times; |
| 47 | +import static org.mockito.Mockito.verify; |
| 48 | +import static org.mockito.Mockito.when; |
| 49 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; |
| 50 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 51 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; |
| 52 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 53 | + |
| 54 | +/** |
| 55 | + * Tests {@link Logbook} resource endpoints. The authentication scheme used is the |
| 56 | + * hard coded user/userPass credentials. The {@link LogbookRepository} is mocked. |
| 57 | + */ |
| 58 | + |
| 59 | +@ExtendWith(SpringExtension.class) |
| 60 | +@ContextHierarchy({@ContextConfiguration(classes = {ResourcesTestConfig.class})}) |
| 61 | +@WebMvcTest(TagsResourceTest.class) |
| 62 | +@TestPropertySource(locations = "classpath:no_ldap_test_application.properties") |
| 63 | +@ActiveProfiles({"test"}) |
| 64 | +public class TagsResourceTest extends ResourcesTestBase { |
| 65 | + |
| 66 | + @Autowired |
| 67 | + private TagRepository tagRepository; |
| 68 | + |
| 69 | + private static Tag tag1; |
| 70 | + private static Tag tag2; |
| 71 | + |
| 72 | + |
| 73 | + @BeforeAll |
| 74 | + public static void init() { |
| 75 | + tag1 = new Tag("tag1", State.Active); |
| 76 | + tag2 = new Tag("tag2"); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void testFindAll() throws Exception { |
| 81 | + when(tagRepository.findAll()).thenReturn(Arrays.asList(tag1, tag2)); |
| 82 | + |
| 83 | + MockHttpServletRequestBuilder request = get("/" + OlogResourceDescriptors.TAG_RESOURCE_URI); |
| 84 | + MvcResult result = mockMvc.perform(request).andExpect(status().isOk()) |
| 85 | + .andReturn(); |
| 86 | + Iterable<Tag> tags = objectMapper.readValue(result.getResponse().getContentAsString(), |
| 87 | + new TypeReference<>() { |
| 88 | + }); |
| 89 | + assertEquals("tag1", tags.iterator().next().getName()); |
| 90 | + verify(tagRepository, times(1)).findAll(); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void testFindAllNoTags() throws Exception { |
| 95 | + when(tagRepository.findAll()).thenReturn(new ArrayList<>()); |
| 96 | + |
| 97 | + MockHttpServletRequestBuilder request = get("/" + OlogResourceDescriptors.TAG_RESOURCE_URI); |
| 98 | + MvcResult result = mockMvc.perform(request).andExpect(status().isOk()) |
| 99 | + .andReturn(); |
| 100 | + Iterable<Logbook> logbooks = objectMapper.readValue(result.getResponse().getContentAsString(), |
| 101 | + new TypeReference<>() { |
| 102 | + }); |
| 103 | + assertFalse(logbooks.iterator().hasNext()); |
| 104 | + verify(tagRepository, times(1)).findAll(); |
| 105 | + reset(tagRepository); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + void testFindTagByName() throws Exception { |
| 110 | + when(tagRepository.findById("tag1")).thenReturn(Optional.of(tag1)); |
| 111 | + |
| 112 | + MockHttpServletRequestBuilder request = get("/" + |
| 113 | + OlogResourceDescriptors.TAG_RESOURCE_URI + |
| 114 | + "/tag1"); |
| 115 | + MvcResult result = mockMvc.perform(request).andExpect(status().isOk()) |
| 116 | + .andReturn(); |
| 117 | + Tag tag = objectMapper.readValue(result.getResponse().getContentAsString(), |
| 118 | + Tag.class); |
| 119 | + assertEquals("tag1", tag.getName()); |
| 120 | + verify(tagRepository, times(1)).findById("tag1"); |
| 121 | + reset(tagRepository); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + void testCreateTagUnauthorized() throws Exception { |
| 126 | + MockHttpServletRequestBuilder request = put("/" + |
| 127 | + OlogResourceDescriptors.TAG_RESOURCE_URI + |
| 128 | + "/tag1") |
| 129 | + .content(objectMapper.writeValueAsString(tag1)) |
| 130 | + .contentType(JSON); |
| 131 | + mockMvc.perform(request).andExpect(status().isUnauthorized()); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + void testCreateTag() throws Exception { |
| 136 | + Tag tag = new Tag("tag"); |
| 137 | + when(tagRepository.save(tag)).thenReturn(tag); |
| 138 | + MockHttpServletRequestBuilder request = put("/" + |
| 139 | + OlogResourceDescriptors.TAG_RESOURCE_URI + |
| 140 | + "/tag") |
| 141 | + .header(HttpHeaders.AUTHORIZATION, AUTHORIZATION) |
| 142 | + .content(objectMapper.writeValueAsString(tag)) |
| 143 | + .contentType(JSON); |
| 144 | + mockMvc.perform(request).andExpect(status().isOk()).andReturn(); |
| 145 | + reset(tagRepository); |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + void testUpdateLogbooksUnauthorized() throws Exception { |
| 150 | + MockHttpServletRequestBuilder request = put("/" + |
| 151 | + OlogResourceDescriptors.LOGBOOK_RESOURCE_URI) |
| 152 | + .content(objectMapper.writeValueAsString(new ArrayList<>())) |
| 153 | + .contentType(JSON); |
| 154 | + mockMvc.perform(request).andExpect(status().isUnauthorized()); |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + void testUpdateTags() throws Exception { |
| 159 | + List<Tag> tags = Arrays.asList(tag1, tag2); |
| 160 | + when(tagRepository.saveAll(tags)).thenReturn(tags); |
| 161 | + MockHttpServletRequestBuilder request = put("/" + |
| 162 | + OlogResourceDescriptors.TAG_RESOURCE_URI) |
| 163 | + .header(HttpHeaders.AUTHORIZATION, AUTHORIZATION) |
| 164 | + .content(objectMapper.writeValueAsString(tags)) |
| 165 | + .contentType(JSON); |
| 166 | + MvcResult result = mockMvc.perform(request).andExpect(status().isOk()).andReturn(); |
| 167 | + tags = objectMapper.readValue(result.getResponse().getContentAsString(), |
| 168 | + new TypeReference<>() { |
| 169 | + }); |
| 170 | + assertEquals("tag1", tags.iterator().next().getName()); |
| 171 | + verify(tagRepository, times(1)).saveAll(tags); |
| 172 | + reset(tagRepository); |
| 173 | + } |
| 174 | + |
| 175 | + @Test |
| 176 | + void testDeleteUnauthorized() throws Exception { |
| 177 | + MockHttpServletRequestBuilder request = delete("/" + |
| 178 | + OlogResourceDescriptors.TAG_RESOURCE_URI + |
| 179 | + "/tag1"); |
| 180 | + mockMvc.perform(request).andExpect(status().isUnauthorized()); |
| 181 | + } |
| 182 | + |
| 183 | + @Test |
| 184 | + void testDelete() throws Exception { |
| 185 | + MockHttpServletRequestBuilder request = delete("/" + |
| 186 | + OlogResourceDescriptors.TAG_RESOURCE_URI + |
| 187 | + "/tag1") |
| 188 | + .header(HttpHeaders.AUTHORIZATION, AUTHORIZATION); |
| 189 | + mockMvc.perform(request).andExpect(status().isNotFound()); |
| 190 | + reset(tagRepository); |
| 191 | + } |
| 192 | +} |
0 commit comments