Improved helm-generator code to make it more testable and improved code coverage
[dcaegen2/platform.git] / mod2 / helm-generator / helmchartgenerator-core / src / test / java / org / onap / dcaegen2 / platform / helmchartgenerator / ChartMuseumDistributorTest.java
1 /*
2  * # ============LICENSE_START=======================================================
3  * # Copyright (c) 2021 AT&T Intellectual Property. All rights reserved.
4  * # ================================================================================
5  * # Licensed under the Apache License, Version 2.0 (the "License");
6  * # you may not use this file except in compliance with the License.
7  * # You may obtain a copy of the License at
8  * #
9  * #      http://www.apache.org/licenses/LICENSE-2.0
10  * #
11  * # Unless required by applicable law or agreed to in writing, software
12  * # distributed under the License is distributed on an "AS IS" BASIS,
13  * # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * # See the License for the specific language governing permissions and
15  * # limitations under the License.
16  * # ============LICENSE_END=========================================================
17  */
18
19 package org.onap.dcaegen2.platform.helmchartgenerator;
20
21 import okhttp3.mockwebserver.MockResponse;
22 import okhttp3.mockwebserver.MockWebServer;
23 import org.apache.commons.io.FileUtils;
24 import org.junit.jupiter.api.AfterAll;
25 import org.junit.jupiter.api.BeforeAll;
26 import org.junit.jupiter.api.BeforeEach;
27 import org.junit.jupiter.api.Test;
28 import org.onap.dcaegen2.platform.helmchartgenerator.distribution.ChartMuseumDistributor;
29
30 import java.io.File;
31 import java.io.IOException;
32
33 public class ChartMuseumDistributorTest {
34
35     ChartMuseumDistributor distributor;
36
37     public static MockWebServer mockChartMuseumServer;
38
39     private static File mockChartFile;
40
41     @BeforeAll
42     static void init() throws IOException {
43         mockChartMuseumServer = new MockWebServer();
44         mockChartMuseumServer.start();
45         mockChartFile = File.createTempFile("dcae-ves-collector-1.8.0","tgz");
46     }
47
48     @BeforeEach
49     void setUp(){
50         String baseUrl = String.format("http://localhost:%s",
51                 mockChartMuseumServer.getPort());
52         distributor = new ChartMuseumDistributor(baseUrl, "mockUsername", "mockPassword");
53     }
54
55     @Test
56     void distribute() throws Exception{
57         mockChartMuseumServer.enqueue(getMock201Response());
58         distributor.distribute(mockChartFile);
59     }
60
61     private MockResponse getMock201Response() {
62         return new MockResponse()
63                 .setResponseCode(201)
64                 .setBody("{\"saved\": true}");
65     }
66
67
68     @AfterAll
69     static void tearDown() throws IOException {
70         mockChartMuseumServer.shutdown();
71         FileUtils.deleteQuietly(mockChartFile);
72     }
73 }
74