MabinGo commented on a change in pull request #8: SCB-1384 refact the code to 
optimize user experience
URL: https://github.com/apache/servicecomb-toolkit/pull/8#discussion_r305617486
 
 

 ##########
 File path: 
contractgen/src/test/java/org/apache/servicecomb/toolkit/contractgen/DefaultContractsGeneratorTest.java
 ##########
 @@ -0,0 +1,150 @@
+/*
+ * 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.servicecomb.toolkit.contractgen;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.BDDMockito.given;
+import static org.mockito.Mockito.mock;
+
+import java.io.File;
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.TimeoutException;
+
+import org.apache.maven.artifact.DependencyResolutionRequiredException;
+import org.apache.maven.plugin.testing.resources.TestResources;
+import org.apache.maven.project.MavenProject;
+import org.apache.servicecomb.provider.rest.common.RestSchema;
+import org.apache.servicecomb.toolkit.ContractsGenerator;
+import org.apache.servicecomb.toolkit.GeneratorFactory;
+import org.apache.servicecomb.toolkit.common.ClassMaker;
+import org.junit.Rule;
+import org.junit.Test;
+
+public class DefaultContractsGeneratorTest {
+
+  @Rule
+  public TestResources resources = new TestResources();
+
+  private static final String TEST_PROJECT = "demo";
+
+  @Test
+  public void testCanProcess() {
+    assertTrue(new DefaultContractsGenerator().canProcess("default"));
+    assertFalse(new DefaultContractsGenerator().canProcess("others"));
+  }
+
+  @Test
+  public void testConfigure() throws DependencyResolutionRequiredException {
+    Map<String, Object> config = new HashMap<>();
+
+    MavenProject project = new MavenProject();
+    config.put("classpathUrls", project.getRuntimeClasspathElements());
+    config.put("outputDir", "target");
+    config.put("contractfileType", "yaml");
+  }
+
+  @Test
+  public void testGenerate()
+      throws DependencyResolutionRequiredException, IOException, 
TimeoutException, InterruptedException {
+
+    File demoPath = this.resources.getBasedir(TEST_PROJECT);
+    ClassMaker.compile(demoPath.getCanonicalPath());
+    MavenProject project = mock(MavenProject.class);
+
+    List<String> runtimeUrlPath = new ArrayList<>();
+    runtimeUrlPath.add(demoPath + File.separator + "target/classes");
+
+    Map<String, Object> config = new HashMap<>();
+    given(project.getRuntimeClasspathElements()).willReturn(runtimeUrlPath);
+    config.put("classpathUrls", project.getRuntimeClasspathElements());
+    config.put("outputDir", "target");
+    config.put("contractfileType", "yaml");
+
+    DefaultContractsGenerator defaultContractsGenerator = new 
DefaultContractsGenerator();
+    defaultContractsGenerator.configure(config);
+    assertTrue(defaultContractsGenerator.generate());
+  }
+
+  @Test
+  public void testCheckConfig() throws NoSuchMethodException, 
InvocationTargetException, IllegalAccessException,
+      DependencyResolutionRequiredException {
+    DefaultContractsGenerator defaultContractsGenerator = new 
DefaultContractsGenerator();
+    Method method = 
defaultContractsGenerator.getClass().getDeclaredMethod("checkConfig", new 
Class[] {});
+    method.setAccessible(true);
+
+    defaultContractsGenerator.configure(null);
+    assertFalse((boolean) method.invoke(defaultContractsGenerator, new 
Object[] {}));
+
+    Map<String, Object> config = new HashMap<>();
+    config.put("classpathUrls", null);
+    defaultContractsGenerator.configure(config);
+    assertFalse((boolean) method.invoke(defaultContractsGenerator, new 
Object[] {}));
+
+    MavenProject project = new MavenProject();
+    config.put("classpathUrls", project.getRuntimeClasspathElements());
+    defaultContractsGenerator.configure(config);
+    assertTrue((boolean) method.invoke(defaultContractsGenerator, new Object[] 
{}));
+  }
+
+  @Test
+  public void testPrivateCanProcess() throws NoSuchMethodException, 
InvocationTargetException, IllegalAccessException {
+    DefaultContractsGenerator defaultContractsGenerator = new 
DefaultContractsGenerator();
+    Method method = 
defaultContractsGenerator.getClass().getDeclaredMethod("canProcess", new 
Class[] {Class.class});
+    method.setAccessible(true);
+
+    assertFalse((boolean) method.invoke(defaultContractsGenerator, new 
Object[] {null}));
+
+//    Class<Runnable> mockRunnableaClass = (Class<Runnable>)mock(Class.class);
 
 Review comment:
   done

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to