|
| 1 | +package net.es.oscars.cuke; |
| 2 | + |
| 3 | +import static net.es.oscars.app.util.PrettyPrinter.prettyLog; |
| 4 | + |
| 5 | +import java.io.InputStream; |
| 6 | +import java.nio.charset.Charset; |
| 7 | +import java.time.Instant; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.Map; |
| 10 | + |
| 11 | +import org.apache.commons.text.StringSubstitutor; |
| 12 | +import org.junit.experimental.categories.Category; |
| 13 | +import org.springframework.beans.factory.annotation.Autowired; |
| 14 | +import org.springframework.boot.test.context.SpringBootTest; |
| 15 | +import org.springframework.boot.test.web.client.TestRestTemplate; |
| 16 | +import org.springframework.core.io.ClassPathResource; |
| 17 | +import org.springframework.http.HttpEntity; |
| 18 | +import org.springframework.http.HttpHeaders; |
| 19 | +import org.springframework.http.HttpMethod; |
| 20 | +import org.springframework.http.ResponseEntity; |
| 21 | +import org.springframework.util.StreamUtils; |
| 22 | + |
| 23 | +import org.springframework.http.HttpStatus; |
| 24 | +import org.springframework.http.MediaType; |
| 25 | + |
| 26 | +import io.cucumber.java.Before; |
| 27 | +import io.cucumber.java.en.Given; |
| 28 | +import io.cucumber.java.en.Then; |
| 29 | +import io.cucumber.java.en.When; |
| 30 | +import lombok.extern.slf4j.Slf4j; |
| 31 | +import net.es.oscars.app.Startup; |
| 32 | +import net.es.oscars.ctg.UnitTests; |
| 33 | +import net.es.oscars.nsi.svc.NsiAsyncQueue; |
| 34 | +import net.es.oscars.nsi.svc.NsiConnectionEventService; |
| 35 | +import net.es.oscars.nsi.svc.NsiService; |
| 36 | +import net.es.oscars.resv.svc.ConnService; |
| 37 | +import net.es.oscars.soap.NsiProvider; |
| 38 | +import net.es.oscars.topo.TopoService; |
| 39 | +import net.es.oscars.topo.beans.Topology; |
| 40 | +import net.es.oscars.topo.pop.TopoPopulator; |
| 41 | +import net.es.oscars.topo.svc.TopologyStore; |
| 42 | + |
| 43 | +@Slf4j |
| 44 | +@Category({UnitTests.class}) |
| 45 | +@SpringBootTest( |
| 46 | + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, |
| 47 | + classes = { |
| 48 | + TopoService.class, |
| 49 | + NsiService.class, |
| 50 | + NsiAsyncQueue.class, |
| 51 | + NsiConnectionEventService.class, |
| 52 | + |
| 53 | + NsiProvider.class, |
| 54 | + } |
| 55 | +) |
| 56 | +public class NsiProviderSteps extends CucumberSteps { |
| 57 | + @Autowired |
| 58 | + private CucumberWorld world; |
| 59 | + |
| 60 | + @Autowired |
| 61 | + private TestRestTemplate restTemplate; |
| 62 | + |
| 63 | + @Autowired |
| 64 | + private Startup startup; |
| 65 | + |
| 66 | + @Autowired |
| 67 | + private TopologyStore topoService; |
| 68 | + |
| 69 | + @Autowired |
| 70 | + private TopoPopulator topoPopulator; |
| 71 | + |
| 72 | + @Autowired |
| 73 | + private ConnService connService; |
| 74 | + |
| 75 | + @Autowired |
| 76 | + private NsiService nsiService; |
| 77 | + |
| 78 | + @Autowired |
| 79 | + private NsiAsyncQueue queue; |
| 80 | + |
| 81 | + private ResponseEntity<String> response; |
| 82 | + |
| 83 | + @Before("@NsiProviderSteps") |
| 84 | + public void before() throws Exception { |
| 85 | + |
| 86 | + try { |
| 87 | + startup.setInStartup(false); |
| 88 | + queue.queue.clear(); |
| 89 | + topoService.clear(); |
| 90 | + nsiService.setErrorCount(0); |
| 91 | + loadTopology(); |
| 92 | + releaseAllConnections(); |
| 93 | + } catch (Exception e) { |
| 94 | + world.add(e); |
| 95 | + log.error("NsiProviderSteps Error - {}", e); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + @Given("The NSI connection is queued for asynchronous reservation while not including a projectId") |
| 100 | + public void the_nsi_connection_is_queued_for_asynchronous_reservation_while_not_including_a_project_id() throws Throwable { |
| 101 | + try { |
| 102 | + queueNsiConnection(); |
| 103 | + } catch (Exception e) { |
| 104 | + world.add(e); |
| 105 | + log.error("NsiProviderSteps Error - {}", e); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + @Given("The NSI connection is queued for asynchronous reservation while including a projectId") |
| 110 | + public void the_NSI_connection_is_queued_for_asynchronous_reservation_while_including_a_projectId() { |
| 111 | + try { |
| 112 | + queueNsiConnection(true, "ABCD-1234-EFGH-5678"); |
| 113 | + } catch (Exception e) { |
| 114 | + world.add(e); |
| 115 | + log.error("NsiProviderSteps Error - {}", e); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + @Given("The NSI connection is queued for asynchronous reservation while including a blank projectId") |
| 120 | + public void the_NSI_connection_is_queued_for_asynchronous_reservation_while_including_a_blank_projectId() { |
| 121 | + try { |
| 122 | + queueNsiConnection(true, ""); |
| 123 | + } catch (Exception e) { |
| 124 | + world.add(e); |
| 125 | + log.error("NsiProviderSteps Error - {}", e); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + @When("The NSI queue size is {}") |
| 130 | + public void the_NSI_queue_size_is(int expectedSize) { |
| 131 | + try { |
| 132 | + assert queue.queue != null && !queue.queue.isEmpty(); |
| 133 | + log.info("NSI QUEUE size: {}", queue.queue.size()); |
| 134 | + assert queue.queue.size() == expectedSize; |
| 135 | + } catch (Exception e) { |
| 136 | + world.add(e); |
| 137 | + log.error("NsiProviderSteps Error - {}", e); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + @When("The NSI reserve is requested") |
| 142 | + public void the_NSI_reserve_is_requested() { |
| 143 | + try { |
| 144 | + queue.processQueue(); |
| 145 | + } catch (Exception e) { |
| 146 | + world.add(e); |
| 147 | + log.error("NsiProviderSteps Error - {}", e); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + @Then("The NSI connection does not have a projectId") |
| 152 | + public void the_NSI_connection_does_not_have_a_projectId() { |
| 153 | + try { |
| 154 | + queue.queue.forEach(element -> { |
| 155 | + prettyLog(element); |
| 156 | + }); |
| 157 | + } catch (Exception e) { |
| 158 | + world.add(e); |
| 159 | + log.error("NsiProviderSteps Error - {}", e); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + @Then("The NSI connection is put on hold") |
| 164 | + public void the_NSI_connection_is_put_on_hold() { |
| 165 | + try { |
| 166 | + assert connService.getHeld().size() == 1; |
| 167 | + } catch (Exception e) { |
| 168 | + world.add(e); |
| 169 | + log.error("NsiProviderSteps Error - {}", e); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + @Then("The NSI connection has a projectId") |
| 174 | + public void the_NSI_connection_has_a_projectId() { |
| 175 | + try { |
| 176 | + connService.getHeld().forEach((id, conn) -> { |
| 177 | + log.info("Connection {} has projectId {}", id, conn.getProjectId()); |
| 178 | + assert conn.getProjectId() != null; |
| 179 | + }); |
| 180 | + } catch (Exception e) { |
| 181 | + world.add(e); |
| 182 | + log.error("NsiProviderSteps Error - {}", e); |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + @Then("The NSI provider encountered {int} errors") |
| 187 | + public void the_NSI_provider_encountered_errors(int errorCount) { |
| 188 | + assert nsiService.getErrorCount() == errorCount; |
| 189 | + assert world.getExceptions().size() == errorCount; |
| 190 | + } |
| 191 | + |
| 192 | + private void loadTopology() throws Exception { |
| 193 | + String topoPath = "topo/esnet.json"; |
| 194 | + Topology t = topoPopulator.loadTopology(topoPath); |
| 195 | + if (t == null) throw new Exception(topoPath + " is not a topology"); |
| 196 | + topoService.replaceTopology(t); |
| 197 | + } |
| 198 | + |
| 199 | + private void queueNsiConnection() throws Exception { |
| 200 | + queueNsiConnection(false, null); |
| 201 | + } |
| 202 | + |
| 203 | + private void queueNsiConnection(boolean withProjectId, String projectIdValue) throws Exception { |
| 204 | + HttpMethod method = HttpMethod.POST; |
| 205 | + String xmlDataTemplate = "http/nsi.reserve.template.xml"; |
| 206 | + Map<String, String> valuesMap = new HashMap<>(); |
| 207 | + Instant now = Instant.now(); |
| 208 | + |
| 209 | + log.info("NOW time used is {}", now.toString()); |
| 210 | + |
| 211 | + Instant startTime = now; |
| 212 | + Instant endTime = now.plusSeconds(60 * 20); |
| 213 | + |
| 214 | + log.info("START time will be {}", startTime.toString()); |
| 215 | + log.info("END time will be {}", endTime.toString()); |
| 216 | + |
| 217 | + valuesMap.put("startTime", startTime.toString() ); |
| 218 | + valuesMap.put("endTime", endTime.toString() ); |
| 219 | + |
| 220 | + if (withProjectId) { |
| 221 | + xmlDataTemplate = "http/nsi.reserve.template.xml"; |
| 222 | + valuesMap.put("projectId", projectIdValue); |
| 223 | + } else { |
| 224 | + xmlDataTemplate = "http/nsi.reserve.no-project-id.template.xml"; |
| 225 | + } |
| 226 | + |
| 227 | + String url = "/services/provider?ServiceName=NsiProviderService&PortName=NsiProviderPort&PortTypeName=ConnectionProviderPort"; |
| 228 | + InputStream bodyInputStream = new ClassPathResource(xmlDataTemplate).getInputStream(); |
| 229 | + String payload = StreamUtils.copyToString(bodyInputStream, Charset.defaultCharset()); |
| 230 | + |
| 231 | + StringSubstitutor sub = new StringSubstitutor(valuesMap); |
| 232 | + |
| 233 | + payload = sub.replace(payload); |
| 234 | + |
| 235 | + response = new ResponseEntity<>("", HttpStatus.NOT_FOUND); |
| 236 | + |
| 237 | + HttpHeaders headers = new HttpHeaders(); |
| 238 | + headers.setContentType(MediaType.APPLICATION_XML); |
| 239 | + HttpEntity<String> entity = new HttpEntity<>(payload, headers); |
| 240 | + |
| 241 | + response = restTemplate.exchange(url, method, entity, String.class); |
| 242 | + |
| 243 | + assert response.getStatusCode() == HttpStatus.OK; |
| 244 | + } |
| 245 | + |
| 246 | + public void releaseAllConnections() { |
| 247 | + connService.getHeld().clear(); |
| 248 | + } |
| 249 | +} |
0 commit comments