2 * Copyright © 2019 Bell Canada
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.ccsdk.cds.cdssdclistener.handler;
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertTrue;
20 import com.google.protobuf.ByteString;
21 import io.grpc.ManagedChannel;
22 import io.grpc.inprocess.InProcessChannelBuilder;
23 import io.grpc.inprocess.InProcessServerBuilder;
24 import io.grpc.stub.StreamObserver;
25 import io.grpc.testing.GrpcCleanupRule;
27 import java.io.IOException;
28 import java.nio.file.Paths;
29 import org.apache.commons.io.FileUtils;
30 import org.junit.Before;
31 import org.junit.Rule;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.onap.ccsdk.cds.cdssdclistener.client.CdsSdcListenerAuthClientInterceptor;
35 import org.onap.ccsdk.cds.controllerblueprints.common.api.Status;
36 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintManagementOutput;
37 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintManagementServiceGrpc.BluePrintManagementServiceImplBase;
38 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintUploadInput;
39 import org.onap.ccsdk.cds.controllerblueprints.management.api.FileChunk;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.boot.context.properties.EnableConfigurationProperties;
42 import org.springframework.boot.test.context.SpringBootTest;
43 import org.springframework.test.context.junit4.SpringRunner;
45 @RunWith(SpringRunner.class)
46 @EnableConfigurationProperties({BluePrintProcesssorHandler.class, CdsSdcListenerAuthClientInterceptor.class})
47 @SpringBootTest(classes = {BluePrintProcessorHandlerTest.class})
48 public class BluePrintProcessorHandlerTest {
51 private BluePrintProcesssorHandler bluePrintProcesssorHandler;
54 private CdsSdcListenerAuthClientInterceptor cdsSdcListenerAuthClientInterceptor;
57 public GrpcCleanupRule grpcCleanup = new GrpcCleanupRule();
59 private static final String CBA_ARCHIVE = "src/test/resources/testcba.zip";
60 private static final String SUCCESS_MSG = "Successfully uploaded CBA";
61 private static final int SUCCESS_CODE = 200;
62 private ManagedChannel channel;
65 public void setUp() throws IOException {
66 final BluePrintManagementServiceImplBase serviceImplBase = new BluePrintManagementServiceImplBase() {
68 public void uploadBlueprint(BluePrintUploadInput request,
69 StreamObserver<BluePrintManagementOutput> responseObserver) {
70 responseObserver.onNext(getBluePrintManagementOutput());
71 responseObserver.onCompleted();
75 // Generate server name.
76 String serverName = InProcessServerBuilder.generateName();
78 // Create a server, add service, start, and register.
80 InProcessServerBuilder.forName(serverName).addService(serviceImplBase).directExecutor().build().start());
82 // Create a client channel.
83 channel = grpcCleanup.register(InProcessChannelBuilder.forName(serverName).directExecutor().build());
87 public void testApplicationEndPointSucess() throws IOException {
89 BluePrintUploadInput request = generateRequest();
92 Status output = bluePrintProcesssorHandler.sendRequest(request, channel);
95 assertEquals(SUCCESS_CODE, output.getCode());
96 assertTrue(output.getMessage().contains(SUCCESS_MSG));
99 private BluePrintUploadInput generateRequest() throws IOException {
100 File file = Paths.get(CBA_ARCHIVE).toFile();
101 byte[] bytes = FileUtils.readFileToByteArray(file);
102 FileChunk fileChunk = FileChunk.newBuilder().setChunk(ByteString.copyFrom(bytes)).build();
104 return BluePrintUploadInput.newBuilder().setFileChunk(fileChunk).build();
107 private BluePrintManagementOutput getBluePrintManagementOutput() {
108 return BluePrintManagementOutput.newBuilder()
109 .setStatus(Status.newBuilder().setMessage(SUCCESS_MSG).setCode(200).build())