繁体   English   中英

如何测试在 wso2 集成器中使用连接器的 API

[英]How to test an API that uses a connector in a wso2 integrator

我正在尝试使用 Redis 连接器在 wso2 中测试我的简单 API。 GET 请求通过其端点发送到 API 银行以检索汇率并将数据解析到 Redis 缓存中。 我根据文档https://ei.docs.wso2.com/en///developing-unit-testsuitegator为 API 和 mockservice 编写了单元测试但是当我尝试运行这个测试时,我得到了以下错误:

[error] Artifact data reading failed 
    java.nio.file.NoSuchFileException: ..\NBU_EXCHANGE_DATA_INDEX_660_EI\NBU_EXCHANGE_DATA_INDEX_660_EIConnector\redis-connector-1.0.1.zip
    at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:79)
    at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
    at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
    at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:230)
    at java.nio.file.Files.newByteChannel(Files.java:361)
    at java.nio.file.Files.newByteChannel(Files.java:407)
    at java.nio.file.Files.readAllBytes(Files.java:3152)
    at org.wso2.synapse.unittest.SynapseTestCaseFileReader.processConnectorResourcesData(SynapseTestCaseFileReader.java:290)
    at org.wso2.synapse.unittest.SynapseTestCaseFileReader.processArtifactData(SynapseTestCaseFileReader.java:91)
    at org.wso2.synapse.unittest.UnitTestClient.executeTests(UnitTestClient.java:54)
    at org.wso2.synapse.unittest.UnitTestCasesMojo.testCaseRunner(UnitTestCasesMojo.java:141)
    at org.wso2.synapse.unittest.UnitTestCasesMojo.execute(UnitTestCasesMojo.java:79)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:957)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:289)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:193)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:347)
    [error] Error while creating a deployable message with test suites`

这是我的单元测试.xml:

<unit-test>
    <artifacts>
        <test-artifact>
            <artifact>/NBU_EXCHANGE_DATA_INDEX_660_EIConfigs/src/main/synapse-config/api/WRITE_EXCHANGE_TO_REDIS.xml</artifact>
        </test-artifact>
        <supportive-artifacts>
            <artifact>/NBU_EXCHANGE_DATA_INDEX_660_EI/NBU_EXCHANGE_DATA_INDEX_660_EIConfigs/src/main/synapse-config/sequences/WRITE_INDEX_EXCHANGE_TO_REDIS_XML.xml</artifact>
            <artifact>/NBU_EXCHANGE_DATA_INDEX_660_EI/NBU_EXCHANGE_DATA_INDEX_660_EIConfigs/src/main/synapse-config/endpoints/EXCHANGE_XML_FROM_SITE_NBU_EP.xml</artifact>
        </supportive-artifacts>
        <registry-resources/>
        <connector-resources>
            <connector-resource>/NBU_EXCHANGE_DATA_INDEX_660_EI/NBU_EXCHANGE_DATA_INDEX_660_EIConnector/redis-connector-1.0.1.zip</connector-resource>
        </connector-resources>
    </artifacts>
    <test-cases>
        <test-case name="GetXML_exchange">
            <input>
                <request-path>/nbu-exchange/xml</request-path>
                <request-method>GET</request-method>
                <request-protocol>http</request-protocol>
            </input>
            <assertions>
                <assertEquals>
                    <actual>$body</actual>
                    <expected><![CDATA[{
                       "Date": "2022-09-14",
                       "HTTP_SC": "200",
                       "XML_connect": "true",
                       "MAIN_XML_isUpdate": "false",
                       "TMP_XML_isUpdate": "false",
                       "NBU_XML_isUpdate": "false"
                    }]]></expected>
                    <message>not equals</message>
                </assertEquals>
                <assertNotNull>
                    <actual>$body</actual>
                    <message>not null</message>
                </assertNotNull>
            </assertions>
        </test-case>
    </test-cases>
    <mock-services>
        <mock-service>/NBU_EXCHANGE_DATA_INDEX_660_EI/NBU_EXCHANGE_DATA_INDEX_660_EIConfigs/test/resources/mock-services/ImitationNBU_EP_XML.xml</mock-service>
    </mock-services>
</unit-test>

这是我的模拟服务.xml:

<mock-service>
    <service-name>EXCHANGE_XML_FROM_SITE_NBU_EP</service-name>
    <port>9090</port>
    <context>/nbu-exchange</context>
    <resources>
        <resource>
            <sub-context>/xml</sub-context>
            <method>GET</method>
            <response>
                <status-code>200</status-code>
                <payload><![CDATA[<?xml version="1.0" encoding="utf-8"?>&#13;
                                  <exchange>&#13;
                                     <currency>&#13;
                                        <r030>36</r030>&#13;
                                        <txt>Австралійський долар</txt>&#13;
                                        <rate>25.2652</rate>&#13;
                                        <cc>AUD</cc>&#13;
                                        <exchangedate>14.09.2022</exchangedate>&#13;
                                     </currency>&#13;
                                    <currency>&#13;
                                        <r030>124</r030>&#13;
                                        <txt>Канадський долар</txt>&#13;
                                        <rate>28.1969</rate>&#13;
                                        <cc>CAD</cc>&#13;
                                        <exchangedate>14.09.2022</exchangedate>&#13;
                                        </currency>&#13;
                                   </exchange>]]></payload>
                <headers>
                    <header name="Content-Type" value="application/xml"/>
                </headers>
            </response>
        </resource>
    </resources>
   </mock-service>

这是我的 pom.xml 用于测试

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.example</groupId>
    <artifactId>NBU_EXCHANGE_DATA_INDEX_660_EI</artifactId>
    <version>1.0.0-SNAPSHOT</version>
  </parent>
  <groupId>com.example</groupId>
  <artifactId>NBU_EXCHANGE_DATA_INDEX_660_EIConfigs</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <packaging>pom</packaging>
  <name>NBU_EXCHANGE_DATA_INDEX_660_EIConfigs</name>
  <description>NBU_EXCHANGE_DATA_INDEX_660_EIConfigs</description>
  <properties>
   <CApp.type>bpel/workflow=zip,lib/registry/filter=jar,webapp/jaxws=war,lib/library/bundle=jar,service/dataservice=dbs,synapse/local-entry=xml,synapse/proxy-service=xml,carbon/application=car,registry/resource=zip,lib/dataservice/validator=jar,synapse/endpoint=xml,web/application=war,lib/carbon/ui=jar,service/axis2=aar,synapse/sequence=xml,synapse/configuration=xml,wso2/gadget=dar,lib/registry/handlers=jar,lib/synapse/mediator=jar,synapse/task=xml,synapse/api=xml,synapse/template=xml,synapse/message-store=xml,synapse/message-processors=xml,synapse/inbound-endpoint=xml,synapse/metadata=yaml</CApp.type>
    <maven.test.skip>false</maven.test.skip>
  </properties>
  <repositories>
    <repository>
      <releases>
        <enabled>true</enabled>
        <updatePolicy>daily</updatePolicy>
        <checksumPolicy>ignore</checksumPolicy>
      </releases>
      <id>wso2-nexus</id>
      <url>https://maven.wso2.org/nexus/content/groups/wso2-public/</url>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <releases>
        <enabled>true</enabled>
        <updatePolicy>daily</updatePolicy>
        <checksumPolicy>ignore</checksumPolicy>
      </releases>
      <id>wso2-nexus</id>
      <url>https://maven.wso2.org/nexus/content/groups/wso2-public/</url>
    </pluginRepository>
  </pluginRepositories>
  <build>
    <directory>target/capp</directory>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.4.0</version>
        <extensions>true</extensions>
        <executions>
          <execution>
            <id>package</id>
            <phase>package</phase>
            <goals>
              <goal>exec</goal>
            </goals>
            <configuration>
              <executable>mvn</executable>
              <workingDirectory>${project.build.directory}</workingDirectory>
              <arguments>
                <argument>clean</argument>
                <argument>package</argument>
                <argument>-Dmaven.test.skip=${maven.test.skip}</argument>
              </arguments>
            </configuration>
          </execution>
          <execution>
            <id>install</id>
            <phase>install</phase>
            <goals>
              <goal>exec</goal>
            </goals>
            <configuration>
              <executable>mvn</executable>
              <workingDirectory>${project.build.directory}</workingDirectory>
              <arguments>
                <argument>clean</argument>
                <argument>install</argument>
                <argument>-Dmaven.test.skip=${maven.test.skip}</argument>
              </arguments>
            </configuration>
          </execution>
          <execution>
            <id>deploy</id>
            <phase>deploy</phase>
            <goals>
              <goal>exec</goal>
            </goals>
            <configuration>
              <executable>mvn</executable>
              <workingDirectory>${project.build.directory}</workingDirectory>
              <arguments>
                <argument>deploy</argument>
                <argument>-Dmaven.test.skip=${maven.test.skip}</argument>
              </arguments>
            </configuration>
          </execution>
        </executions>
        <configuration />
      </plugin>
      <plugin>
        <artifactId>maven-eclipse-plugin</artifactId>
        <version>2.9</version>
        <configuration>
          <buildcommands />
          <projectnatures>
            <projectnature>org.wso2.developerstudio.eclipse.esb.project.nature</projectnature>
          </projectnatures>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.wso2.maven</groupId>
        <artifactId>synapse-unit-test-maven-plugin</artifactId>
        <version>5.2.38</version>
        <executions>
          <execution>
            <id>synapse-unit-test</id>
            <phase>test</phase>
            <goals>
              <goal>synapse-unit-test</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <server>
            <testServerType>${testServerType}</testServerType>
            <testServerHost>${testServerHost}</testServerHost>
            <testServerPort>${testServerPort}</testServerPort>
            <testServerPath>${testServerPath}</testServerPath>
          </server>
          <testCasesFilePath>${project.basedir}/test/${testFile}</testCasesFilePath>
          <mavenTestSkip>${maven.test.skip}</mavenTestSkip>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.wso2.maven</groupId>
        <artifactId>wso2-esb-sequence-plugin</artifactId>
        <version>2.1.0</version>
        <extensions>true</extensions>
        <executions>
          <execution>
            <id>sequence</id>
            <phase>process-resources</phase>
            <goals>
              <goal>pom-gen</goal>
            </goals>
            <configuration>
              <artifactLocation>.</artifactLocation>
              <typeList>${artifact.types}</typeList>
            </configuration>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>com.sun.activation</groupId>
            <artifactId>javax.activation</artifactId>
            <version>1.2.0</version>
          </dependency>
        </dependencies>
        <configuration />
      </plugin>
      <plugin>
        <groupId>org.wso2.maven</groupId>
        <artifactId>wso2-esb-endpoint-plugin</artifactId>
        <version>2.1.0</version>
        <extensions>true</extensions>
        <executions>
          <execution>
            <id>endpoint</id>
            <phase>process-resources</phase>
            <goals>
              <goal>pom-gen</goal>
            </goals>
            <configuration>
              <artifactLocation>.</artifactLocation>
              <typeList>${artifact.types}</typeList>
            </configuration>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>com.sun.activation</groupId>
            <artifactId>javax.activation</artifactId>
            <version>1.2.0</version>
          </dependency>
        </dependencies>
        <configuration />
      </plugin>
      <plugin>
        <groupId>org.wso2.maven</groupId>
        <artifactId>wso2-esb-api-plugin</artifactId>
        <version>2.1.0</version>
        <extensions>true</extensions>
        <executions>
          <execution>
            <id>api</id>
            <phase>process-resources</phase>
            <goals>
              <goal>pom-gen</goal>
            </goals>
            <configuration>
              <artifactLocation>.</artifactLocation>
              <typeList>${artifact.types}</typeList>
            </configuration>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>com.sun.activation</groupId>
            <artifactId>javax.activation</artifactId>
            <version>1.2.0</version>
          </dependency>
        </dependencies>
        <configuration />
      </plugin>
      <plugin>
        <groupId>org.wso2.maven</groupId>
        <artifactId>wso2-esb-metadata-plugin</artifactId>
        <version>5.2.34</version>
        <extensions>true</extensions>
        <executions>
          <execution>
            <id>metadata</id>
            <phase>process-resources</phase>
            <goals>
              <goal>pom-gen</goal>
            </goals>
            <configuration>
              <artifactLocation>.</artifactLocation>
              <typeList>${artifact.types}</typeList>
            </configuration>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>com.sun.activation</groupId>
            <artifactId>javax.activation</artifactId>
            <version>1.2.0</version>
          </dependency>
        </dependencies>
        <configuration />
      </plugin>
      <plugin>
        <groupId>org.wso2.maven</groupId>
        <artifactId>wso2-esb-task-plugin</artifactId>
        <version>2.1.0</version>
        <extensions>true</extensions>
        <executions>
          <execution>
            <id>task</id>
            <phase>process-resources</phase>
            <goals>
              <goal>pom-gen</goal>
            </goals>
            <configuration>
              <artifactLocation>.</artifactLocation>
              <typeList>${artifact.types}</typeList>
            </configuration>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>com.sun.activation</groupId>
            <artifactId>javax.activation</artifactId>
            <version>1.2.0</version>
          </dependency>
        </dependencies>
        <configuration />
      </plugin>
      <plugin>
        <groupId>org.wso2.maven</groupId>
        <artifactId>wso2-esb-proxy-plugin</artifactId>
        <version>2.1.0</version>
        <extensions>true</extensions>
        <executions>
          <execution>
            <id>proxy</id>
            <phase>process-resources</phase>
            <goals>
              <goal>pom-gen</goal>
            </goals>
            <configuration>
              <artifactLocation>.</artifactLocation>
              <typeList>${artifact.types}</typeList>
            </configuration>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>com.sun.activation</groupId>
            <artifactId>javax.activation</artifactId>
            <version>1.2.0</version>
          </dependency>
        </dependencies>
        <configuration />
      </plugin>
    </plugins>
  </build>
</project>

我究竟做错了什么?

如果您创建了连接器导出器项目,您应该能够在Supportive Artifacts Page中创建测试套件时将连接器添加为依赖项。

另一种选择是在运行测试套件之前单独部署连接器。 您可以创建一个 Common 项目来打包所有公共依赖项并将其部署到 MI 运行时,以便所有其他项目都可以引用它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM