All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.microcks.service.MessageService Maven / Gradle / Ivy

The newest version!
/*
 * Copyright The Microcks Authors.
 *
 * Licensed 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 io.github.microcks.service;

import io.github.microcks.domain.*;
import io.github.microcks.repository.EventMessageRepository;
import io.github.microcks.repository.RequestRepository;
import io.github.microcks.repository.ResponseRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * Service bean for common processing around messages (request and responses).
 * @author laurent
 */
@org.springframework.stereotype.Service
public class MessageService {

   /** A simple logger for diagnostic messages. */
   private static Logger log = LoggerFactory.getLogger(MessageService.class);

   @Autowired
   private RequestRepository requestRepository;

   @Autowired
   private ResponseRepository responseRepository;

   @Autowired
   private EventMessageRepository eventMessageRepository;


   /**
    * Retrieve unidirectional events corresponding to an Operation.
    * @param operationId The identifier of operation to get messages for.
    * @return A list of unidirectional event messages
    */
   public List getEventByOperation(String operationId) {
      // Retrieve event messages using operation identifier.
      List eventMessages = eventMessageRepository.findByOperationId(operationId);
      if (log.isDebugEnabled()) {
         log.debug("Found {} event(s) for operation {}", eventMessages.size(), operationId);
      }

      // Just wrap then into an UnidirectionalEvent exchange.
      List results = new ArrayList<>(eventMessages.size());
      for (EventMessage eventMessage : eventMessages) {
         results.add(new UnidirectionalEvent(eventMessage));
      }
      return results;
   }

   /**
    * Retrieve pairs of requests and responses corresponding to an Operation.
    * @param operationId The identifier of operation to get messages for.
    * @return A list of paired requests and responses
    */
   public List getRequestResponseByOperation(String operationId) {
      // Retrieve requests and responses using operation identifier.
      List requests = requestRepository.findByOperationId(operationId);
      List responses = responseRepository.findByOperationId(operationId);
      if (log.isDebugEnabled()) {
         log.debug("Found {} request(s) for operation {}", requests.size(), operationId);
         log.debug("Found {} response(s) for operation {}", responses.size(), operationId);
      }

      // Browse them to reassociate them.
      List results = associatePairs(requests, responses);
      if (log.isDebugEnabled()) {
         log.debug("Emitting {} request/response pair(s) as result", results.size());
      }
      return results;
   }

   /**
    * Retrieve unidirectional events corresponding to a TestCase.
    * @param testCaseId The identifier of test case to get messages for.
    * @return A list of unidirectional event messages
    */
   public List getEventByTestCase(String testCaseId) {
      // Retrieve events using testCase identifier.
      List eventMessages = eventMessageRepository.findByTestCaseId(testCaseId);
      if (log.isDebugEnabled()) {
         log.debug("Found {} event(s) for testCase {}", eventMessages.size(), testCaseId);
      }

      // Just wrap then into an UnidirectionalEvent exchange.
      List results = new ArrayList<>(eventMessages.size());
      for (EventMessage eventMessage : eventMessages) {
         results.add(new UnidirectionalEvent(eventMessage));
      }
      return results;
   }

   /**
    * Retrieved pairs of requests and responses corresponding to a TestCase.
    * @param testCaseId The identifier of test case to get messages for.
    * @return A list of paired requests and responses
    */
   public List getRequestResponseByTestCase(String testCaseId) {
      // Retrieve requests and responses using testCase identifier.
      List requests = requestRepository.findByTestCaseId(testCaseId);
      List responses = responseRepository.findByTestCaseId(testCaseId);
      if (log.isDebugEnabled()) {
         log.debug("Found {} request(s) for testCase {}", requests.size(), testCaseId);
         log.debug("Found {} response(s) for testCase {}", responses.size(), testCaseId);
      }

      // Browse them to reassociate them.
      List results = associatePairs(requests, responses);
      if (log.isDebugEnabled()) {
         log.debug("Emitting {} request/response pair(s) as result", results.size());
      }
      return results;
   }

   /** */
   private List associatePairs(List requests, List responses) {
      List results = new ArrayList();

      // Browse them to reassociate them.
      for (Request request : requests) {
         for (Response response : responses) {
            if (request.getResponseId() != null && request.getResponseId().equals(response.getId())) {
               // If found a matching response, build a pair.
               results.add(new RequestResponsePair(request, response));
               break;
            }
         }
      }
      return results;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy