View Javadoc
1   /*
2    * This file is part of dependency-check-core.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   * Copyright (c) 2015 The OWASP Foundatio. All Rights Reserved.
17   */
18  package org.owasp.dependencycheck.data.composer;
19  
20  import org.junit.Before;
21  import org.junit.Test;
22  
23  import java.io.ByteArrayInputStream;
24  import java.io.InputStream;
25  import java.nio.charset.Charset;
26  
27  import static org.junit.Assert.*;
28  
29  /**
30   * Created by colezlaw on 9/5/15.
31   */
32  public class ComposerLockParserTest {
33  
34      private InputStream inputStream;
35  
36      @Before
37      public void setUp() {
38          inputStream = this.getClass().getClassLoader().getResourceAsStream("composer.lock");
39      }
40  
41      @Test
42      public void testValidComposerLock() {
43          ComposerLockParser clp = new ComposerLockParser(inputStream);
44          clp.process();
45          assertEquals(30, clp.getDependencies().size());
46          assertTrue(clp.getDependencies().contains(new ComposerDependency("symfony", "translation", "2.7.3")));
47      }
48  
49      @Test(expected = ComposerException.class)
50      public void testNotJSON() throws Exception {
51          String input = "NOT VALID JSON";
52          ComposerLockParser clp = new ComposerLockParser(new ByteArrayInputStream(input.getBytes(Charset.defaultCharset())));
53          clp.process();
54      }
55  
56      @Test(expected = ComposerException.class)
57      public void testNotComposer() throws Exception {
58          String input = "[\"ham\",\"eggs\"]";
59          ComposerLockParser clp = new ComposerLockParser(new ByteArrayInputStream(input.getBytes(Charset.defaultCharset())));
60          clp.process();
61      }
62  
63      @Test(expected = ComposerException.class)
64      public void testNotPackagesArray() throws Exception {
65          String input = "{\"packages\":\"eleventy\"}";
66          ComposerLockParser clp = new ComposerLockParser(new ByteArrayInputStream(input.getBytes(Charset.defaultCharset())));
67          clp.process();
68      }
69  }